Download presentation
Presentation is loading. Please wait.
Published byClaire O’Connor’ Modified over 9 years ago
1
Abstract classes and Interfaces
2
Abstract classes
3
9/3/2015 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: Abstract Class Slide 3 Example Notice that Face and Rect have common code i.e., fields x, y, state; and methods setXY(), getX(), and getY() Idea: declare an AbstractActingShape class to contain this common code then, Person and DancingCircle can just extend AbstractActingShape But note: how does AbstractActingShape define act() and draw()? no default definition Solution: use an abstract class
4
9/3/2015 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: Abstract Class Slide 4 Abstract class Same as class but it is possible to omit method bodies syntax: abstract before class abstract before and method signatures w/o bodies semicolon at the end of the method signatures Rules may declare variables of abstract classes instantiation not possible (new will not work) subclasses must override incomplete methods (unless the subclass is abstract too)
5
9/3/2015 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: Abstract Class Slide 5 Why Abstract Classes? Why not just a plain class with empty method bodies? Answer: Cleaner, Safer Code 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 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
6
Interfaces
7
9/3/2015 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: Abstract Class Slide 7 Interfaces (review) 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)
8
9/3/2015 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: Abstract Class Slide 8 Using Interfaces Interface variables can be declared e.g., ActingShape s ; Interfaces cannot be instantiated e.g., ActingShape s = new ActingShape() won’t work! (because ActingShape doesn’t define its methods) Interface variables may point to objects of classes that implement the interface e.g., s = new Smiley(); (assuming class Smiley implements ActingShape) Polymorphism works e.g., s.flip() will call Smiley’s flip() method
9
9/3/2015 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: Abstract Class Slide 9 Using Interfaces (2) Interfaces are “inherited” e.g., if ParentSmilingShape implements ActingShape, then SpongeBob implicitly implements ActingShape as well, since SpongeBob extends ParentSmilingShape extends and implements can be combined syntax: public class A extends B implements C e.g., DancingSquare2 extends Square implements ActingShape inherited methods “count” towards implementing an interface e.g., see Square and DancingSquare2 example A class can implement multiple interfaces e.g., public class MyApplet implements ActionListener, MouseListener, MouseMotionListener (Note: in contrast, a class cannot extend multiple classes.)
10
9/3/2015 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: Abstract Class Slide 10 Using Interfaces (3) Interfaces can extend other interfaces 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 Shape s = new Person(); is legal public interface Shape { public void setXY( int x, int y ); public int getX(); public int getY(); public void draw( Graphics g ); } public interface ActingObject { public void act(); } public interface ActingShape extends Shape, ActingObject { // nothing here since // method sigs are inherited } public class Person implements ActingShape {…}
11
9/3/2015 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: Abstract Class Slide 11 Interfaces vs. Abstract Classes Note: you can write an abstract class whose methods are all abstract Functionally, it’s like an interface i.e., no method bodies What’s the difference? interfaces allow multiple inheritance i.e., you can implement more than one interface abstract classes don’t i.e., you can only extend one class
12
9/3/2015 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: Abstract Class Slide 12 Example ActingShape can be an abstract class instead of an interface Person extends ActingShape, DancingCircle extends ActingShape It works. Code for the applet is same as with interface version BUT, now, you cannot say class DancingSquare2 extends Square extends ActingShape public abstract class ActingShape { public abstract void setXY( int x, int y ); public abstract int getX(); public abstract int getY(); public abstract void draw( Graphics g ); public abstract void act(); }
13
9/3/2015 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: Abstract Class Slide 13 Single and multiple inheritance A class can only extend one superclass 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? A class can implement multiple interfaces This is OK because interfaces do not have fields or method bodies. So, nothing to share no confusion.
14
9/3/2015 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: Abstract Class Slide 14 A better use of abstract classes Use it to make a “template” for classes that want to implement ActingShape Provide a partial implementation fields x, y, state methods setXY, getX, getY, Person extends AbstractActingShape, DancingCircle extends AbstractActingShape they don’t need to write getX, etc. public abstract class AbstractActingShape implements ActingShape { protected int x, y; protected int state; public void setXY( int x, int y ) { this.x = x; this.y = y; } public int getX() { return x; } public int getY() { return y; } public abstract void draw( Graphics g ); public abstract void act(); }
15
9/3/2015 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: Abstract Class Slide 15 Interfaces vs. Abstract Classes Use interfaces when … you have classes that have common methods, but do not share code, or your classes already extend a superclass that you can’t change or might need to implement several different interfaces Use abstract classes … to make templates with partial implementations and, when you don’t need multiple inheritance e.g., template for implementing an interface e.g, AbstractActingShape implements ActingShape Note: people don’t have to use AbstractActingShape, but you give them a convenient option
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.