Chapter 10 ! the More Concrete "Subclass" Extends the More Abstract "Superclass" and Inherits the Functionality of the Superclass
Total Page:16
File Type:pdf, Size:1020Kb
Inheritance ! Two things which are different at one level are similar or equivalent at a more abstract level. ! Upper classes are more abstract; lower classes are more concrete. Chapter 10 ! The more concrete "subclass" extends the more abstract "superclass" and inherits the functionality of the superclass. ! The abstract class generalizes or abstracts the concrete class. 2 Inheritance … Inheritance … ! Recall that abstraction can be realized with an interface. ! An interface generalizes a class or less abstract interface. ! We can also define the abstraction relation between classes. ! The more concrete interface extends the more abstract interface. ! One class can generalize (or abstract) another class. ! Weapon extends Movable ! The more concrete class extends the more abstract class. ! The more concrete class implements the more abstract interface. ! extends ! InteractivePlayer implements Player ClosedFigure Figure ! 3 Circle extends ClosedFigure 4 Inheritance … Inheritance … ! The more concrete class extends the more abstract class. ! The more abstract class generalizes the more concrete class. ! Circle extends ClosedFigure ! ClosedFigure generalizes Circle ! Circle is-a ClosedFigure ! ClosedFigure is an abstraction of Circle ! Circle is a subtype of ClosedFigure ! ClosedFigure is a supertype of Circle ! ! Circle is a descendant of ClosedFigure 5 ClosedFigure is an ancestor of Circle 6 Inheritance … Inheritance … ! Class extension defines a subtype. ! As with interfaces, class extension creates a subtype relation between the types defined by the two classes. ! Expression can be cast up or down the class hierarchy. ! Given the following variable declarations: Figure top; Circle bottom; ! The following are legal (though not necessarily safe) expressions: ! Class extension is a transitive relation. (ClosedFigure) top // downward cast ! Circle extends Figure, etc. (Circle) top // downward cast ! Figure generalizes Circle, etc. (ClosedFigure) bottom // upward cast ! This is not legal: 7 (Rectangle)X bottom 8 Inheritance … Inheritance … ! There is one significant difference ! The Object class is at the top (or root) of the hierarchy, and has no between extending or implementing parent. an interface, and extending a class. ! Every other class is a subclass of Object. ! A parent class is specified with an extends clause in the class ! A class can implement, and an heading. interface can extend, any number ! The definition of Circle looks like: of interfaces. public class Circle extends ClosedFigure { ! A class can have any number of … “interface parents”. } ! Every class (except the Object ! Omitting the extends clause in a class definition: class) extends exactly one other public class Figure { class. … ! Single inheritance } is equivalent to: public class Figure extends Object { 9 … 10 } Extension and inheritance Inheritance … ! An interface defines a set of features that are guaranteed to be ! If a class implements an interface, provided by an implementing class. the implements clause follows the ! Since the interface contains no implementation, the implementing extends clause. class is required to implement the features. ! The definition of Poison and ! This situation is somewhat different with a class. Potion look like: ! As with an interface, the generalizing class defines a set of features that are guaranteed to be provided by an extending class. ! But the generalizing class already has implementations for its public class Poison extends Potion implements Weapon { features. … ! The extending class inherits the public features of the generalizing } class. public class Potion implements Movable { ! The extending class does not need to independently implement these inherited features. … } 11 Extension and inheritance … Extension and inheritance … ! Class extension: ! the mechanism provided by the language for implementing public class ClosedFigure { abstraction. ! Inheritance: private Location location; ! a mechanism by which a subclass automatically possesses all of the non-private features of its superclass. public Location location () { return this.location; } public void moveTo (Location newLocation) { this.location = newLocation; } } Extension and inheritance … Extension and inheritance … public class Circle extends ClosedFigure { ! A subclass does not “inherit” private features of its superclass. … ! The variable location is private to ClosedFigure. public int radius (){ ! It cannot be directly accessed from . … Circle } ! Illegal within a method of Circle: } this.locationX = null; ! location can be indirectly accessed from Circle. ! Legal within a method of : Circle circle = new Circle(10); Circle Location loc = circle.location(); this.moveTo(null); int distance = circle.radius(); Subtyping and inheritance Subtyping and inheritance … ! The type Circle is a subtype of ClosedFigure. ! Type conformance rules are static. ! The rules for type conformance are the same, regardless of whether ! Thus we are not permitted to write: the type is defined by a class or an interface. distance = enclosure.radius(); ! Thus a Circle expression can be written wherever a X ClosedFigure value is needed: even if we are sure that enclosure will in fact reference a Circle Circle ring = new Circle(10); when the statement is executed. ClosedFigure enclosure; ! A ClosedFigure is not necessarily a Circle. enclosure = ring; ! A ClosedFigure does not have a radius() method. ! This is OK since Circle is a subtype of ClosedFigure. ! We can, of course, cast from the more abstract type to the more concrete: if (enclosure instanceof Circle) { distance =((Circle)enclosure).radius(); } DrJava Example Constructors and subclasses ! Let’s look at class extension in more detail in DrJava ! Constructors are not inherited. ! The constructor is responsible for initializing components defined in a superclass as well as those expressly defined in the class itself. ! The first thing that a constructor must do is invoke the constructor of its parent (super) class. ! A constructor can also invoke another constructor of the same class: this(arguments); ! A superclass constructor can be called: super(arguments); ! Constructor invocations via this() and super() can only appear as the first statement of another constructor. 19 20 Constructors and subclasses … Constructors and subclasses … public class Circle extends ClosedFigure { public class ColoredCircle extends Circle { private Color color; private int radius; public ColoredCircle (int radius, Color color) { super(radius); // invoke super 1-arg constructor public Circle (int radius) { this.color = color; super(); // invoke super no-arg constructor } this.radius = radius; public ColoredCircle (Color color) { } // implicitly calls no-arg super constructor this.color = color; public Circle () { } … this(1); // invoke above constructor } } ! Note that for this to be valid, Circle must have a 1-arg and a no-arg … 21 constructor. 22 } Constructors and subclasses … Constructors and subclasses … ! If no constructor is called for a superclass, and no constructor of the ! If a class definition doesn’t contain any constructor definitions then a same class is called, the constructor is assumed to begin with the default constructor requiring no arguments is automatically created. invocation of the superclass constructor super(); ! The default constructor is equivalent to: public ColoredCircle (Color color) { // implicitly calls no-arg super constructor public Class () { this.color = color; super(); } } ! This is equivalent to: public ColoredCircle (Color color) { super(); this.color = color; } 23 24 DrJava Example Method overloading ! Let’s look at constructors in more detail in DrJava ! A class can contain distinct methods with the same name as long as invocations of these methods can be distinguished by the compiler. ! Overloading ! Each overloaded method must differ from each other in the number and/or type of its parameters. public int report (int x) public int report (Object obj) public void report (int x, int y) ! These are completely separate, independent methods. ! The number and types of arguments determine which method is invoked. ! report(2) invokes the first, report(“2”) invokes the second, and report(2,3) invokes the third 25 26 Method overloading … Method overloading … ! Note that a class cannot contain two methods with the same name ! Overload resolution is static. and the same number of parameters, even if the methods have different return types. ! The method to be executed is determined by the compiler based on the static types of the argument expressions. ! Because Java supports coercion (implicit type conversion) and subtyping, it is not always possible to differentiate methods that ! It does not depend on the actual run-time argument values. differ only by their return type. ! Suppose a class Reporter has two methods named report defined as follows: ! A class cannot contain the following method definitions (even though one is a query returning an Object and the other is a command): public void report (Object obj) { public void report (int x) System.out.println(“The argument is an object.”); public Object report (int i) } X public void report (Circle circle) { System.out.println(“The argument is a circle.”); } 27 28 Method overloading … Method overloading … ! Assume is a subclass of and consider the following Circle Figure ! Which method is invoked for each of these calls? definitions and initializations: Circle circle = new Circle(1); reporter.report(circle); public void report (Object obj) { Figure