Abstract Classes and InterfacesAbstract methodsAbstract classes IAbstract classes IIWhy have abstract classes?An example abstract classWhy have abstract methods?A problemA solutionInterfacesDesigning interfacesImplementing an interface IImplementing an interface IIPartially implementing an InterfaceWhat are interfaces for?How to use interfacesinstanceofInterfaces, againAdapter classesVocabularyThe EndJan 14, 2019Abstract Classes and Interfaces2Abstract methodsYou can declare an object without defining it: Person p;Similarly, you can declare a method without defining it: public abstract void draw(int size);Notice that the body of the method is missingA method that has been declared but not defined is an abstract method3Abstract classes IAny class containing an abstract method is an abstract classYou must declare the class with the keyword abstract: abstract class MyClass {...}An abstract class is incompleteIt has “missing” method bodiesYou cannot instantiate (create a new instance of) an abstract class4Abstract classes IIYou can extend (subclass) an abstract classIf the subclass defines all the inherited abstract methods, it is “complete” and can be instantiatedIf the subclass does not define all the inherited abstract methods, it too must be abstractYou can declare a class to be abstract even if it does not contain any abstract methodsThis prevents the class from being instantiated5Why have abstract classes?Suppose you wanted to create a class Shape, with subclasses Oval, Rectangle, Triangle, Hexagon, etc.You don’t want to allow creation of a “Shape”Only particular shapes make sense, not generic onesIf Shape is abstract, you can’t create a new ShapeYou can create a new Oval, a new Rectangle, etc.Abstract classes are good for defining a general category containing specific, “concrete” classes6An example abstract classpublic abstract class Animal { abstract int eat(); abstract void breathe();}This class cannot be instantiatedAny non-abstract subclass of Animal must provide the eat() and breathe() methods7Why have abstract methods?Suppose you have a class Shape that isn’t abstractShape should not have a draw() methodEach subclass of Shape should have a draw() methodNow suppose you have a variable Shape figure; where figure contains some subclass object (such as a Star)It is a syntax error to say figure.draw(), because the Java compiler can’t tell in advance what kind of value will be in the figure variableSolution: Give Shape an abstract method draw()Now the class Shape is abstract, so it can’t be instantiatedThe figure variable cannot contain a (generic) Shape, because it is impossible to create oneAny object (such as a Star object) that is a (kind of) Shape will have the draw() methodThe Java compiler can depend on figure.draw() being a legal call and does not give a syntax error8A problemclass Shape { ... }class Star extends Shape { void draw() { ... } ...}class Crescent extends Shape { void draw() { ... } ...}Shape someShape = new Star();This is legal, because a Star is a ShapesomeShape.draw();This is a syntax error, because some Shape might not have a draw() methodRemember: A class knows its superclass, but not its subclasses9A solutionabstract class Shape { void draw();}class Star extends Shape { void draw() { ... } ...}class Crescent extends Shape { void draw() { ... } ...}Shape someShape = new Star();This is legal, because a Star is a ShapeHowever, Shape someShape = new Shape(); is no longer legalsomeShape.draw();This is legal, because every actual instance must have a draw() method10InterfacesAn interface declares (describes) methods but does not supply bodies for them interface KeyListener { public void keyPressed(KeyEvent e); public void keyReleased(KeyEvent e); public void keyTyped(KeyEvent e);}All the methods are implicitly public and abstractYou can add these qualifiers if you like, but why bother?You cannot instantiate an interfaceAn interface is like a very abstract class—none of its methods are definedAn interface may also contain constants (final variables)11Designing interfacesMost of the time, you will use Sun-supplied Java interfacesSometimes you will want to design your ownYou would write an interface if you want classes of various types to all have a certain set of capabilitiesFor example, if you want to be able to create animated displays of objects in a class, you might define an interface as:public interface Animatable { install(Panel p); display();}Now you can write code that will display any Animatable class in a Panel of your choice, simply by calling these methods12Implementing an interface IYou extend a class, but you implement an interfaceA class can only extend (subclass) one other class, but it can implement as many interfaces as you likeExample: class MyListener implements KeyListener, ActionListener { … }13Implementing an interface IIWhen you say a class implements an interface, you are promising to define all the methods that were declared in the interfaceExample: class MyKeyListener implements KeyListener { public void keyPressed(KeyEvent e) {...}; public void keyReleased(KeyEvent e) {...}; public void keyTyped(KeyEvent e) {...};}The “...” indicates actual code that you must supplyNow you can create a new MyKeyListener14Partially implementing an InterfaceIt is possible to define some but not all of the methods defined in an interface: abstract class MyKeyListener implements KeyListener { public void keyTyped(KeyEvent e) {...};}Since this class does not supply all the methods it has promised, it is an abstract classYou must label it as such with the keyword abstractYou can even extend an interface (to add methods):interface FunkyKeyListener extends KeyListener { ... }15What are interfaces for?Reason 1: A class can only extend one other class, but it can implement multiple interfacesThis lets the class fill multiple “roles”In writing Applets, it is common to have one class implement several different listenersExample: class MyApplet extends Applet implements ActionListener, KeyListener { ... }Reason 2: You can write methods that work for more than one kind of class16How to use interfacesYou can write methods
View Full Document