Abstract Classes and Interfaces Jan 16 2019 Abstract methods You 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 missing A method that has been declared but not defined is an abstract method 2 Abstract classes I Any class containing an abstract method is an abstract class You must declare the class with the keyword abstract abstract class MyClass An abstract class is incomplete It has missing method bodies You cannot instantiate create a new instance of an abstract class 3 Abstract classes II You can extend subclass an abstract class If the subclass defines all the inherited abstract methods it is complete and can be instantiated If the subclass does not define all the inherited abstract methods it too must be abstract You can declare a class to be abstract even if it does not contain any abstract methods This prevents the class from being instantiated 4 Why 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 ones If Shape is abstract you can t create a new Shape You can create a new Oval a new Rectangle etc Abstract classes are good for defining a general category containing specific concrete classes 5 An example abstract class public abstract class Animal abstract int eat abstract void breathe This class cannot be instantiated Any non abstract subclass of Animal must provide the eat and breathe methods 6 Why have abstract methods Suppose you have a class Shape that isn t abstract Now suppose you have a variable Shape figure where figure contains some subclass object such as a Star Shape should not have a draw method Each subclass of Shape should have a draw method It is a syntax error to say figure draw because the Java compiler can t tell in advance what will be in the figure variable Solution Give Shape an abstract method draw Now the class Shape is abstract so it can t be instantiated The figure variable cannot contain a generic Shape because it is impossible to create one Any object such as a Star object that is a kind of Shape will have the draw method The Java compiler can depend on figure draw being a legal call and does not give a syntax error 7 Interfaces An 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 abstract You cannot instantiate an interface You can add these qualifiers if you like but why bother An interface is like a very abstract class none of its methods are defined An interface may also contain constants final variables 8 Designing interfaces Most of the time you will use Sun supplied Java interfaces Sometimes you will want to design your own You would write an interface if you want classes of various types to all have a certain set of capabilities For 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 methods 9 Implementing an interface I You extend a class but you implement an interface A class can only extend subclass one other class but it can implement as many interfaces as you like Example class MyListener implements KeyListener ActionListener 10 Implementing an interface II When you say a class implements an interface you are promising to define all the methods that were declared in the interface Example class MyKeyListener implements KeyListener public void keyPressed KeyEvent e public void keyReleased KeyEvent e public void keyTyped KeyEvent e Now you can create a new MyKeyListener 11 Partially implementing an Interface It 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 class You must label it as such with the keyword abstract You can even extend an interface interface FunkyKeyListener extends KeyListener 12 What are interfaces for Reason 1 A class can only extend one other class but it can implement multiple interfaces This lets the class fill multiple roles In writing Applets it is common to have one class implement several different listeners Example class MyApplet extends Applet implements ActionListener KeyListener Reason 2 You can write methods that work for more than one kind of class 13 How to use interfaces You can write methods that work with more than one class interface RuleSet boolean isLegal Move m Board b void makeMove Move m class CheckersRules implements RuleSet one implementation public boolean isLegal Move m Board b public void makeMove Move m class ChessRules implements RuleSet another implementation class LinesOfActionRules implements RuleSet and another RuleSet rulesOfThisGame new ChessRules Every class that implements RuleSet must have these methods This assignment is legal because a rulesOfThisGame object is a RuleSet object if rulesOfThisGame isLegal m b makeMove m This method is legal because whatever kind of RuleSet object rulesOfThisGame is it must have isLegal and makeMove methods 14 instanceof instanceof is a keyword that tells you whether a variable is a member of a class or interface For example if class Dog extends Animal implements Pet Animal fido new Dog then the following are all true fido instanceof Dog fido instanceof Animal fido instanceof Pet instanceof is seldom used When you find yourself wanting to use instanceof think about whether the method you are writing should be moved to the individual subclasses 15 Interfaces again When you implement an interface you promise to define all the functions it declares There can be a lot of methods interface KeyListener public void keyPressed KeyEvent e public void keyReleased KeyEvent e public void keyTyped KeyEvent e What if you only care about a couple of these methods 16 Adapter classes Solution use an adapter class An adapter class implements an interface and provides empty method bodies class KeyAdapter implements KeyListener public void keyPressed KeyEvent e public void
View Full Document