InterfaceAbstractClass,interfaces,final keyword,.pptx

ArunPatrick2 7 views 19 slides Jul 02, 2024
Slide 1
Slide 1 of 19
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19

About This Presentation

about java


Slide Content

Interface and Abstract Class

9- 2 Interfaces An interface is a contract between its producer and client The general format of an interface definition: public interface InterfaceName { //( Method headers ...) } All methods specified by an interface are public by default. A class (as a producer) can implement one or more interfaces.

Interface in UML and Java <<Interface>> IUSBMemory s ave() Public interface IUSBMemory { void save(); }

9- 4 Producer of an Interface If a class implements an interface, it uses the implements keyword in the class header . <<Interface>> IUSBMemory s ave() USBMemory s ave() public class USBMemory implements IUSBMemory { // more fields are declared void save() {…}; // more methods are implemented }

Client of an Interface Class Computer uses any class implementing IUSBMemory to save a file <<Interface>> IUSBMemory s ave() USBMemory s ave() Computer editAfile () usb 1 public class Computer { private IUSBMemory usb ; // more fields are here. void setUsb ( IUSBMemory m) { usb = m; } public void editAfile () { usb.save (); } }

Now run your program Remember a class is only a blue-print. To run it, you need to “buy” computer and usb memory objects. public class Test { public static void main(String[] args ) { Computer myComputer = new Computer(); IUSBMemory myusb1 = new USBMemory (); myComputer.setUsb (myusb1); myComputer.editAfile (); } }

New Product Comes A new product implementing IUSBMemory called phone is available. <<Interface>> IUSBMemory s ave() Phone s ave() Computer editAfile () usb 1 public class Computer { private IUSBMemory usb ; // more fields are here. void setUsb ( IUSBMemory m) { usb = m; } public void editAfile () { usb.save (); } } Your Computer Class still works. No need to change!!!

Don’t forget to buy a phone!!! Remember a class is a blue-print. Don’t forget to buy a phone and connect to your computer. public class Test { public static void main(String[] args ) { Computer myComputer = new Computer(); IUSBMemory myusb1 = new USBMemory (); myComputer.setUsb (myusb1); myComputer.editAfile (); Phone phone = new Phone(); myComputer.setUsb (phone); myComputer.editAfile (); } } public class Computer { private IUSBMemory usb ; ….. void setUsb ( IUSBMemory m) { usb = m; } ……….. }

9- 9 Fields in Interfaces An interface can contain field declarations: all fields in an interface are treated as final and static . Because they automatically become final , you must provide an initialization value. public interface Doable { int FIELD1 = 1, FIELD2 = 2; (Method headers...) } In this interface, FIELD1 and FIELD2 are final static int variables. Any class that implements this interface has access to these variables.

Problem of Interface IUSBMemory has many features other than save a file, such as set brand, get brand, set speed and get speed etc. When a producer implements IUSBMemory , it should provide its implementations. But if many producers implement IUSBMemory but share same implementation for some of methods such as set brand and get brand etc. Only difference is how to save, i.e. the implementation of save()?

Abstract Class We declare AbsUSBMemory as an abstract class p ublic abstract class AbsUSBMemory { private String brand; public void setBrand (String b) {brand = b;} public String getBrand () { return brand;} // more fields are here public abstract void save(); } <<abstract>> AbsUSBMemory - brand:String v oid setBrand (String b) String getBrand () <<abstract>> void save()

Your Computer Class Class computer can use the all the features defined in an abstract class. public class Computer { private AbsUSBMemory usb ; // more fields are here. void setUsb ( AbsUSBMemory m) { usb = m; } public void editAfile () { usb.save (); } } <<Abstract>> AbsUSBMemory s ave() Computer editAfile () usb 1

Still Need to wait.. No product based on AbsUSBMemory is available yet. Method save() is not implemented yet… p ublic class USBMemory extends AbsUSBMemory { public void save() { // Your implementation // is here } } <<abstract>> AbsUSBMemory - brand:String v oid setBrand (String b) String getBrand () <<abstract>> void save() USBMemory s ave()

Now you can buy your products You can buy your computer with a usb memory based on the features provided by AbsUSBMemory public class Test { public static void main(String[] args ) { Computer myComputer = new Computer(); AbsUSBMemory myusb1 = new USBMemory (); myComputer.setUsb (myusb1); myComputer.editAfile (); } }

Similarly, New Product Available A new product called phone extends AbsUSBMemory and is available. <Abstract>> AbsUSBMemory s ave() Phone s ave() Computer editAfile () usb 1 public class Computer { private AbsUSBMemory usb ; // more fields are here. void setUsb ( AbsUSBMemory m) { usb = m; } public void editAfile () { usb.save (); } } Your Computer Class still works. No need to change!!!

Now you can buy your phone You can buy your computer with a usb memory based on the features provided by AbsUSBMemory public class Test { public static void main(String[] args ) { Computer myComputer = new Computer(); Phone myusb1 = new Phone(); myComputer.setUsb (myusb1); myComputer.editAfile (); } }

9- 17 Abstract Classes An abstract class cannot be instantiated, but other classes are derived from it. An Abstract class serves as a superclass for other classes. The abstract class represents the generic or abstract form of all the classes that are derived from it. A class becomes abstract when you place the abstract key word in the class definition. public abstract class ClassName

9- 18 Abstract Methods An abstract method has no body and must be overridden in a subclass. An abstract method is a method that appears in a superclass, but expects to be overridden in a subclass. An abstract method has only a header and no body. AccessSpecifier abstract ReturnType MethodName(ParameterList); Example: Student.java , CompSciStudent.java , CompSciStudentDemo.java

9- 19 Abstract Methods Notice that the key word abstract appears in the header, and that the header ends with a semicolon. public abstract void setValue(int value); Any class that contains an abstract method is automatically abstract. If a subclass fails to override an abstract method, a compiler error will result. Abstract methods are used to ensure that a subclass implements the method.
Tags