Inheritance: Pattern
Inheritance: Pattern
Reuse Extension and intension Class specialization and class extension Inheritance The protected keyword revisited Inheritance and methods Method redefinition An widely used inheritance example the composite design pattern Finally, the final keyword
OOP: Inheritance
The easiest for the programmer! A very typical reuse, called composition reuse! Requires more knowledge than composition reuse. Today's main topic.
OOP: Inheritance
Class Specialization
In specialization a class is considered an Abstract Data Type
(ADT). The ADT is defined as a set of coherent values on which a set of operations are defined.
The instances of C2 are a subset of the instances of C1. Operations defined of C1 are also defined on C2. Operations defined on C1 can be redefined in C2. C1 C2
OOP: Inheritance
Extension
The extension of a specialized class C2 is a subset of the
extension of the general class C1.
Extension of C1 Extension of C2
is-a Relationship
A C2 object is a C1 object (but not vice-versa). There is an is-a relationship between C1 and C2. We will later discuss a has-a relationship
OOP: Inheritance
Class Extension
In class extension a class is considered a module. A module is a syntactical frame where a number of variables
and method are defined, found in, e.g., Modula-2 and PL/SQL. Class extension is important in the context of reuse. Class extension makes it possible for several modules to share code, i.e., avoid to have to copy code between modules.
In C4 new properties (variables and methods) are added. The properties of C3 are also properties of C4. C3 C4
OOP: Inheritance
Intension
The intension of an extended class C4 is a superset of the
intension of C3.
Intension of C4 Intension of C3
OOP: Inheritance
Inheritance
Inheritance is a way to derive a new class from an existing
class.
Specializing an ADT (i.e., class specialization). Extending an existing class (i.e., class extension). Often both class specialization and class extension takes place when a class inherits from an existing class.
OOP: Inheritance
C3 C3 C4 C4
Maintenance of C3 properties must be done two places Languages, e.g., Ada, Modula2, PL/SQL OOP: Inheritance
Class C4 is created by copying C3. There are C3 and C4 instances. Instance of C4 have all C3 properties. C3 and C4 are totally separated.
Class C4 inherits from C3. There are C3 and C4 instances. Instance of C4 have all C3 properties. C3 and C4 are closely related. Maintenance of C3 properties must be done in one place. Languages, C++, C#, Java, Smalltalk
9
Pure Composition
Class extension
OOP: Inheritance
Inheritance in Java
class Subclass extends Superclass { // <class body> }
OOP: Inheritance
11
Inheritance Example
public class Vehicle { Vehicle private String make; private String model; toString() public Vehicle() { make = ""; model = ""; } getMake() public String toString() { return "Make: " + make + " Model: " + model;getModel() } public String getMake(){ return make; } Car public String getModel() { return model; } } getPrice() public class Car extends Vehicle { private double price; public Car() { super(); // called implicitly can be left outs price = 0.0; } public String toString() { // method overwrites return "Make: " + getMake() + " Model: " + getModel() + " Price: " + price; } public double getPrice(){ return price; } }
OOP: Inheritance 12
OOP: Inheritance
13
from Shape is instantiated as a single object, with properties from the three classes Square, Rectangle, and Shape.
14
OOP: Inheritance
What about private fields in the superclass? Default superclass constructor called if exists
public class Vehicle{ private String make, model; public Vehicle(String ma, String mo) { make = ma; model = mo; } } public class Car extends Vehicle{ private double price; public Car() { // System.out.println("Start"); // not allowed super(, ); // must be called price = 0.0; } } OOP: Inheritance
15
OOP: Inheritance
16
OOP: Inheritance
3
2
3.
C4
4.
The properties of C3 that clients can use. The properties of C3 that C4 can use. The properties of C4 that clients can use. The properties of C4 that subclasses of C4 can use.
= interface
OOP: Inheritance 18
protected, Revisited
It must be possible for a subclass to access properties in a
superclass.
private will not do, it is to restrictive public will not do, it is to generous
Which is more restrictive protected or package access? Change access modifiers when inheriting
Properties can be made more public. Properties cannot be made more private.
19
OOP: Inheritance
protected, Revisited
public
Client
protected private
OOP: Inheritance
20
protected, Example
public class Vehicle1 { Vehicle protected String make; protected String model; toString() public Vehicle1() { make = ""; model = ""; } getMake() public String toString() { return "Make: " + make + " Model: " + model;getModel() } public String getMake(){ return make;} Car public String getModel() { return model;} } getPrice() public class Car1 extends Vehicle1 { private double price; public Car1() { price = 0.0; } public String toString() { return "Make: " + make + " Model: " + model + " Price: " + price; } public double getPrice(){ return price; } }
OOP: Inheritance 21
Cycles in the inheritance hierarchy is not allowed. Inheritance from multiple superclass may be allowed. Inheritance from the same superclass more than once may be allowed. A B C D A B C B D A C B A C
22
OOP: Inheritance
23
Method/Variable Redefinition
Redefinition: A method/variable in a subclass has the same as a
method/variable in the superclass. Redefinition should change the implementation of a method, not its semantics. Redefinition in Java class B inherits from class A if
Method: Both versions of the method is available in instances of B. Can be accessed in B via super. Variable: Both versions of the variable is available in instances of B. Can be accessed in B via super.
method redefinition does not change the semantics of the method. In the programming language Eiffel assertions (preand post conditions) and invariants are inherited. [Meyer pp. 228].
24
OOP: Inheritance
Upcasting
Treat a subclass as its superclass
Vehicle toString() getMake() getModel() Car getPrice()
// example Car c = new Car(); Vehicle v; v = c; // upcast v.toString(); // okay v.getMake(); // okay //v.getPrice(); // not okay
lecture) Should be obvious that a method/field cannot be made more private in a subclass when redefining method/field.
OOP: Inheritance
Upcast
Garden house
walls door
window
floor
use Component print() add() remove() components Single print() List print() add() remove() for all components c c.print()
OOP: Inheritance
OOP: Inheritance
28
29
The main objective! Can call dummy add/remove methods on these instances
OOP: Inheritance
30
Compile time constant (very useful) final static double PI = 3.14 Run-time constant (useful) final int RAND = (int) Math.random * 10 double foo (final int i)
Prevents overwriting in a subclass (use this very carefully) Private methods are implicitly final Cannot inherit from the class
Summary
Reuse
Use composition when ever possible more flexible and easier to understand than inheritance. Specialization and extension can be combined.
Java supports single inheritance, all have Object as superclass Designing good reusable classes is (very) hard!
while(!goodDesign()){ reiterateTheDesign(); }
OOP: Inheritance
32