Abstract Classes and Interfaces 17 Sep20 Abstract methods
Abstract Classes and Interfaces 17 -Sep-20
Abstract methods n You can declare an object without defining it: Person p; n Similarly, you can declare a method without defining it: n n 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 n n Any class containing an abstract method is an abstract class You must declare the class with the keyword abstract: abstract class My. Class {. . . } n An abstract class is incomplete n n It has “missing” method bodies You cannot instantiate (create a new instance of) an abstract class 3
Abstract classes II n You can extend (subclass) an abstract class n n n 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 n This prevents the class from being instantiated 4
Why have abstract classes? n n 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” n n 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 n n n 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
A problem n n class Shape {. . . } class Star extends Shape { void draw() {. . . } class Crescent extends Shape { void draw() {. . . } Shape some. Shape = new Star(); n n This is legal, because a Star is a Shape some. Shape. draw(); n n This is a syntax error, because some Shape might not have a draw() method Remember: A class knows its superclass, but not its subclasses 7
A solution n n abstract class Shape { abstract void draw(); } class Star extends Shape { void draw() {. . . } class Crescent extends Shape { void draw() {. . . } Shape some. Shape = new Star(); n n n This is legal, because a Star is a Shape However, Shape some. Shape = new Shape(); is no longer legal some. Shape. draw(); n This is legal, because every actual instance must have a draw() method 8
Interfaces n An interface declares (describes) methods but does not supply bodies for them interface Action. Listener{ public void action. Performed(Action. Event e); } interface Key. Listener { public void key. Pressed(Key. Event e); public void key. Released(Key. Event e); public void key. Typed(Key. Event e); } n All the methods are implicitly public and abstract n n You cannot instantiate an interface n n 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) 9
Designing interfaces n n 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: n n 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 10
Implementing an interface I n n n 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 Mouse. Adapter implements Mouse. Listener, Mouse. Wheel. Listener, Mouse. Motion. Listener{ … } 11
Implementing an interface II n n When you say a class implements an interface, you are promising to define all the methods that were declared in the interface Example: class My. Key. Listener implements Key. Listener { public void key. Pressed(Key. Event e) {. . . }; public void key. Released(Key. Event e) {. . . }; public void key. Typed(Key. Event e) {. . . }; } n The “. . . ” indicates actual code that you must supply n Now you can create a new My. Key. Listener 12
Partially implementing an Interface n It is possible to define some but not all of the methods defined in an interface: abstract class My. Key. Listener implements Key. Listener { public void key. Typed(Key. Event e) {. . . }; } n n n 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 (to add methods): n interface Funky. Key. Listener extends Key. Listener {. . . } 13
What are interfaces for? n Reason 1: A class can only extend one other class, but it can implement multiple interfaces n n n This lets the class fill multiple “roles” In writing Applets, it is common to have one class implement several different listeners Example: class My. Applet extends Applet implements Action. Listener, Key. Listener {. . . } n Reason 2: You can write methods that work for more than one kind of class 14
How to use interfaces n n You can write methods that work with more than one class interface Rule. Set { boolean is. Legal(Move m, Board b); void make. Move(Move m); } n n n class Checkers. Rules implements Rule. Set { // one implementation public boolean is. Legal(Move m, Board b) {. . . } public void make. Move(Move m) {. . . } } class Chess. Rules implements Rule. Set {. . . } // another implementation class Lines. Of. Action. Rules implements Rule. Set {. . . } // and another Rule. Set rules. Of. This. Game = new Chess. Rules(); n n Every class that implements Rule. Set must have these methods This assignment is legal because a rules. Of. This. Game object is a Rule. Set object if (rules. Of. This. Game. is. Legal(m, b)) { make. Move(m); } n This statement is legal because, whatever kind of Rule. Set object rules. Of. This. Game is, it must have is. Legal and make. Move methods 15
instanceof n n 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 16
Interfaces, again n n When you implement an interface, you promise to define all the functions it declares There can be a lot of methods interface Key. Listener { public void key. Pressed(Key. Event e); public void key. Released(Key. Event e); public void key. Typed(Key. Event e); } n What if you only care about a couple of these methods? 17
Adapter classes n n Solution: use an adapter class An adapter class implements an interface and provides empty method bodies class Key. Adapter implements Key. Listener { public void key. Pressed(Key. Event e) { }; public void key. Released(Key. Event e) { }; public void key. Typed(Key. Event e) { }; } n You can override only the methods you care about n This isn’t elegant, but it does work n Java provides a number of adapter classes 18
Vocabulary n n n abstract method—a method which is declared but not defined (it has no method body) abstract class—a class which either (1) contains abstract methods, or (2) has been declared abstract instantiate—to create an instance (object) of a class interface—similar to a class, but contains only abstract methods (and possibly constants) adapter class—a class that implements an interface but has only empty method bodies 19
Some Extra Vocabulary n n Final methods—a method that cannot be overridden (all private or static methods are implicitly final). Final classes—a class that cannot be extended. Dynamic or late binding— methods to be executed are determined in run time (e. g. , due to polymorphism. ) Static binding— A final method’s declaration can never change, so all subclasses use the same method implementation, and calls to final methods are resolved at compile time—this is known as static binding. 20
The End 21
- Slides: 21