Abstract classes and Interfaces Abstract classes Example n
Abstract classes and Interfaces
Abstract classes
Example n Notice that Face and Rect have common code n n Idea: declare an Abstract. Acting. Shape class to contain this common code n n then, Person and Dancing. Circle can just extend Abstract. Acting. Shape But note: how does Abstract. Acting. Shape define act() and draw()? n n i. e. , fields x, y, state; and methods set. XY(), get. X(), and get. Y() no default definition Solution: use an abstract class 11/30/2004 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 3
Abstract class n Same as class n n but it is possible to omit method bodies syntax: n abstract before class abstract before and method signatures w/o bodies n semicolon at the end of the method signatures n n Rules n n n 11/30/2004 may declare variables of abstract classes instantiation not possible (new will not work) subclasses must override incomplete methods (unless the subclass is abstract too) Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 4
Why Abstract Classes? n n Why not just a plain class with empty method bodies? Answer: Cleaner, Safer Code n n Don’t allow programmers to call methods that aren’t really defined yet for the super class Remind programmers to define the abstract methods in subclasses n n 11/30/2004 with ordinary classes, if you forget, you inherit the empty body no error with abstract classes, compiler will complain if a subclass does not define a body for the abstract methods Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 5
Interfaces
Interfaces (review) n n 11/30/2004 Interfaces allow you to say that something has certain methods, not necessarily what these methods do. In general, interfaces allow a separation of the interface (i. e. , what methods can you call) from the implementation (i. e. , how are these methods implemented) Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 7
Using Interfaces n Interface variables can be declared n n Interfaces cannot be instantiated n n n e. g. , Acting. Shape s = new Acting. Shape() won’t work! (because Acting. Shape doesn’t define its methods) Interface variables may point to objects of classes that implement the interface n n e. g. , Acting. Shape s; e. g. , s = new Smiley(); (assuming class Smiley implements Acting. Shape) Polymorphism works n 11/30/2004 e. g. , s. flip() will call Smiley’s flip() method Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 8
Using Interfaces (2) n Interfaces are “inherited” n n e. g. , if Parent. Smiling. Shape implements Acting. Shape, then Sponge. Bob implicitly implements Acting. Shape as well, since Sponge. Bob extends Parent. Smiling. Shape extends and implements can be combined n syntax: public class A extends B implements C n n inherited methods “count” towards implementing an interface n n e. g. , Dancing. Square 2 extends Square implements Acting. Shape e. g. , see Square and Dancing. Square 2 example A class can implement multiple interfaces n n 11/30/2004 e. g. , public class My. Applet implements Action. Listener, Mouse. Motion. Listener (Note: in contrast, a class cannot extend multiple classes. ) Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 9
Using Interfaces (3) n Interfaces can extend other interfaces n n n e. g. , see code on right note: we say an interface “extends” other interface, not “implements” it because an interface does not implement anything classes that implement an interface also implicitly implement its ancestors n 11/30/2004 Shape s = new Person(); is legal public interface Shape { public void set. XY( int x, int y ); public int get. X(); public int get. Y(); public void draw( Graphics g ); } public interface Acting. Object { public void act(); } public interface Acting. Shape extends Shape, Acting. Object { // nothing here since // method sigs are inherited } public class Person implements Acting. Shape {…} Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 10
Interfaces vs. Abstract Classes n n Note: you can write an abstract class whose methods are all abstract Functionally, it’s like an interface n n i. e. , no method bodies What’s the difference? n interfaces allow multiple inheritance n n abstract classes don’t n 11/30/2004 i. e. , you can implement more than one interface i. e. , you can only extend one class Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 11
Example public abstract class Acting. Shape { public abstract void set. XY( int x, int y ); public abstract int get. X(); public abstract int get. Y(); public abstract void draw( Graphics g ); public abstract void act(); } n n Acting. Shape can be an abstract class instead of an interface Person extends Acting. Shape, Dancing. Circle extends Acting. Shape It works. Code for the applet is same as with interface version BUT, now, you cannot say class Dancing. Square 2 extends Square extends Acting. Shape 11/30/2004 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 12
Single and multiple inheritance n A class can only extend one superclass n n A class can implement multiple interfaces n 11/30/2004 Why? Because inheriting parts from two different classes can be confusing C++ allows multiple inheritance, but many programmers had bad experience with it. What if there are parts of the same name? Do we create 2 copies, or do we share? This is OK because interfaces do not have fields or method bodies. So, nothing to share no confusion. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 13
A better use of abstract classes public abstract class Abstract. Acting. Shape implements Acting. Shape { protected int x, y; protected int state; public void set. XY( int x, int y ) { this. x = x; this. y = y; } public int get. X() { return x; } public int get. Y() { return y; } public abstract void draw( Graphics g ); public abstract void act(); } 11/30/2004 n n Use it to make a “template” for classes that want to implement Acting. Shape Provide a partial implementation n fields x, y, state methods set. XY, get. X, get. Y, Person extends Abstract. Acting. Shape, Dancing. Circle extends Abstract. Acting. Shape n they don’t need to write get. X, etc. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 14
Interfaces vs. Abstract Classes n n n Use interfaces when … Use abstract classes … you have classes that have n to make templates with common methods, partial implementations but do not share code, n and, when you don’t need or multiple inheritance your classes already n e. g. , template for extend a superclass that implementing an interface you can’t change n e. g, Abstract. Acting. Shape or n might need to implement several different interfaces 11/30/2004 n implements Acting. Shape Note: people don’t have to use Abstract. Acting. Shape, but you give them a convenient option Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 15
- Slides: 15