Interface and Abstract Class Interfaces An interface is
![Interface and Abstract Class Interface and Abstract Class](https://slidetodoc.com/presentation_image_h/e7d71e8e563825684f36285f432ba02d/image-1.jpg)
Interface and Abstract Class
![Interfaces • An interface is a contract between its producer and client • The Interfaces • An interface is a contract between its producer and client • The](http://slidetodoc.com/presentation_image_h/e7d71e8e563825684f36285f432ba02d/image-2.jpg)
Interfaces • An interface is a contract between its producer and client • The general format of an interface definition: public interface Interface. Name { //(Method headers. . . ) } • All methods specified by an interface are public by default. • A class (as a producer) can implement one or more interfaces. 9 -2
![Interface in UML and Java <<Interface>> IUSBMemory save() Public interface IUSBMemory { void save(); Interface in UML and Java <<Interface>> IUSBMemory save() Public interface IUSBMemory { void save();](http://slidetodoc.com/presentation_image_h/e7d71e8e563825684f36285f432ba02d/image-3.jpg)
Interface in UML and Java <<Interface>> IUSBMemory save() Public interface IUSBMemory { void save(); }
![Producer of an Interface • If a class implements an interface, it uses the Producer of an Interface • If a class implements an interface, it uses the](http://slidetodoc.com/presentation_image_h/e7d71e8e563825684f36285f432ba02d/image-4.jpg)
Producer of an Interface • If a class implements an interface, it uses the implements keyword in the class header. <<Interface>> IUSBMemory save() 9 -4 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 Client of an Interface • Class Computer uses any class implementing IUSBMemory to save](http://slidetodoc.com/presentation_image_h/e7d71e8e563825684f36285f432ba02d/image-5.jpg)
Client of an Interface • Class Computer uses any class implementing IUSBMemory to save a file <<Interface>> IUSBMemory save() usb 1 Computer edit. Afile() public class Computer { private IUSBMemory usb; // more fields are here. void set. Usb(IUSBMemory m) { usb = m; } public void edit. Afile() { usb. save(); } }
![Now run your program • Remember a class is only a blue-print. To run Now run your program • Remember a class is only a blue-print. To run](http://slidetodoc.com/presentation_image_h/e7d71e8e563825684f36285f432ba02d/image-6.jpg)
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 my. Computer = new Computer(); IUSBMemory myusb 1 = new USBMemory(); my. Computer. set. Usb(myusb 1); my. Computer. edit. Afile(); } }
![New Product Comes • A new product implementing IUSBMemory called phone is available. <<Interface>> New Product Comes • A new product implementing IUSBMemory called phone is available. <<Interface>>](http://slidetodoc.com/presentation_image_h/e7d71e8e563825684f36285f432ba02d/image-7.jpg)
New Product Comes • A new product implementing IUSBMemory called phone is available. <<Interface>> IUSBMemory save() Phone save() usb 1 Computer edit. Afile() public class Computer { private IUSBMemory usb; // more fields are here. void set. Usb(IUSBMemory m) { usb = m; } public void edit. Afile() { 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 Don’t forget to buy a phone!!! • Remember a class is a blue-print. Don’t](http://slidetodoc.com/presentation_image_h/e7d71e8e563825684f36285f432ba02d/image-8.jpg)
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 my. Computer = new Computer(); IUSBMemory myusb 1 = new USBMemory(); my. Computer. set. Usb(myusb 1); my. Computer. edit. Afile(); Phone phone = new Phone(); my. Computer. set. Usb(phone); my. Computer. edit. Afile(); } } public class Computer { private IUSBMemory usb; …. . void set. Usb(IUSBMemory m) { usb = m; } ………. . }
![Fields in Interfaces • An interface can contain field declarations: – all fields in Fields in Interfaces • An interface can contain field declarations: – all fields in](http://slidetodoc.com/presentation_image_h/e7d71e8e563825684f36285f432ba02d/image-9.jpg)
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 FIELD 1 = 1, FIELD 2 = 2; (Method headers. . . ) } • In this interface, FIELD 1 and FIELD 2 are final static int variables. • Any class that implements this interface has access to these variables. 9 -9
![Problem of Interface • IUSBMemory has many features other than save a file, such Problem of Interface • IUSBMemory has many features other than save a file, such](http://slidetodoc.com/presentation_image_h/e7d71e8e563825684f36285f432ba02d/image-10.jpg)
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 get brand etc. Only difference is how to save, i. e. the implementation of save()?
![Abstract Class • We declare Abs. USBMemory as an abstract class public abstract class Abstract Class • We declare Abs. USBMemory as an abstract class public abstract class](http://slidetodoc.com/presentation_image_h/e7d71e8e563825684f36285f432ba02d/image-11.jpg)
Abstract Class • We declare Abs. USBMemory as an abstract class public abstract class Abs. USBMemory { private String brand; public void set. Brand(String b) {brand = b; } public String get. Brand() { return brand; } // more fields are here public abstract void save(); } <<abstract>> Abs. USBMemory -brand: String void set. Brand(String b) String get. Brand() <<abstract>> void save()
![Your Computer Class • Class computer can use the all the features defined in Your Computer Class • Class computer can use the all the features defined in](http://slidetodoc.com/presentation_image_h/e7d71e8e563825684f36285f432ba02d/image-12.jpg)
Your Computer Class • Class computer can use the all the features defined in an abstract class. <<Abstract>> Abs. USBMemory save() usb 1 public class Computer { Computer private Abs. USBMemory usb; // more fields are here. edit. Afile() void set. Usb(Abs. USBMemory m) { usb = m; } public void edit. Afile() { usb. save(); } }
![Still Need to wait. . • No product based on Abs. USBMemory is available Still Need to wait. . • No product based on Abs. USBMemory is available](http://slidetodoc.com/presentation_image_h/e7d71e8e563825684f36285f432ba02d/image-13.jpg)
Still Need to wait. . • No product based on Abs. USBMemory is available yet. Method save() is not implemented yet… <<abstract>> Abs. USBMemory -brand: String void set. Brand(String b) String get. Brand() <<abstract>> void save() USBMemory save() public class USBMemory extends Abs. USBMemory { public void save() { // Your implementation // is here } }
![Now you can buy your products • You can buy your computer with a Now you can buy your products • You can buy your computer with a](http://slidetodoc.com/presentation_image_h/e7d71e8e563825684f36285f432ba02d/image-14.jpg)
Now you can buy your products • You can buy your computer with a usb memory based on the features provided by Abs. USBMemory public class Test { public static void main(String[] args) { Computer my. Computer = new Computer(); Abs. USBMemory myusb 1 = new USBMemory(); my. Computer. set. Usb(myusb 1); my. Computer. edit. Afile(); } }
![Similarly, New Product Available • A new product called phone extends Abs. USBMemory and Similarly, New Product Available • A new product called phone extends Abs. USBMemory and](http://slidetodoc.com/presentation_image_h/e7d71e8e563825684f36285f432ba02d/image-15.jpg)
Similarly, New Product Available • A new product called phone extends Abs. USBMemory and is available. <Abstract>> Abs. USBMemory save() Phone save() usb 1 Computer edit. Afile() public class Computer { private Abs. USBMemory usb; // more fields are here. void set. Usb(Abs. USBMemory m) { usb = m; } public void edit. Afile() { usb. save(); } } Your Computer Class still works. No need to change!!!
![Now you can buy your phone • You can buy your computer with a Now you can buy your phone • You can buy your computer with a](http://slidetodoc.com/presentation_image_h/e7d71e8e563825684f36285f432ba02d/image-16.jpg)
Now you can buy your phone • You can buy your computer with a usb memory based on the features provided by Abs. USBMemory public class Test { public static void main(String[] args) { Computer my. Computer = new Computer(); Phone myusb 1 = new Phone(); my. Computer. set. Usb(myusb 1); my. Computer. edit. Afile(); } }
![Abstract Classes • An abstract class cannot be instantiated, but other classes are derived Abstract Classes • An abstract class cannot be instantiated, but other classes are derived](http://slidetodoc.com/presentation_image_h/e7d71e8e563825684f36285f432ba02d/image-17.jpg)
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 Class. Name 9 -17
![Abstract Methods • An abstract method has no body and must be overridden in Abstract Methods • An abstract method has no body and must be overridden in](http://slidetodoc.com/presentation_image_h/e7d71e8e563825684f36285f432ba02d/image-18.jpg)
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. Access. Specifier abstract Return. Type Method. Name(Parameter. List); • Example: – Student. java, Comp. Sci. Student. Demo. java 9 -18
![Abstract Methods • Notice that the key word abstract appears in the header, and Abstract Methods • Notice that the key word abstract appears in the header, and](http://slidetodoc.com/presentation_image_h/e7d71e8e563825684f36285f432ba02d/image-19.jpg)
Abstract Methods • Notice that the key word abstract appears in the header, and that the header ends with a semicolon. public abstract void set. Value(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. 9 -19
- Slides: 19