JAVA Inheritance - 30
JAVA Inheritance - 30
Reuse Specialization and Extension Inheritance Polymorphism and Dynamic Binding Inheritance and Methods
Reuse
When you need a new class you can:
The easiest for the programmer! A very typical reuse, called composition reuse! Requires more knowledge than composition reuse. Today's main topic.
2
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 is 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.
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
Extension of C1
"IS-A" Relationship
n n n
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
Specialization, Example
Shape draw() resize() Circle draw() resize() Line draw() resize() Rectangle draw() resize() Square draw() resize() Line Should the extension be overlapping?
5
Shape Circle
Rectangle Square
OOP: Inheritance and Polymorphism, Part 1
Class Extension
In 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., Module-2 and PL/SQL. Extension is important in the context of reuse. 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.
Intension
The intension of an extended class C4 is a superset of the
intension of C3.
Inheritance
Inheritance is a way to derive a new class from an existing
class.
Specializing an ADT. Extend an existing class. Often both specialization and extension takes place when a class inherits from an existing class.
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. Maintenance of C3 properties must be done two places
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.
9
Pure Composition
Extension
Inheritance in Java
class Subclass extends Superclass { <class body>; }
11
Inheritance Example
public class Vehicle { protected String make; protected String model; public Vehicle() { make = ""; model = ""; } public String toString() { return "Make: " + make + " Model: " + model; } } public class Car extends Vehicle { private double price; public Car() { super (); price = 0.0; } public String toString() { return "Make: " + make + " Model: " + model + " Price: " + price; } public double getPrice(){ return price; } }
OOP: Inheritance and Polymorphism, Part 1 12
13
14
1
2.
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.
16
protected, Revisited
It must be possible to for a subclass to access properties in a
superclass.
n n
private will not do, it is to restrictive public will not do, it is to generous
Properties can be made "more public". Properties cannot be made "more private".
17
Class Hierarchies
Class hierarchy: a set of classes related by inheritance. Possibilities with inheritance
n n n
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 C A D
18
19
20
Polymorphism
Polymorphism: The ability of a variable or argument to refer at
run-time to instances of various classes.
Shape s = new Shape(); Circle c = new Circle(); Line l = new Line(); Rectangle r = new Rectangle(); s = l; l = s; // is this legal?
Why Polymorphism?
Separate interface from implementation. Allows programmers to isolate type specific details from the Can change types (and add new types) with this propagates to
existing code. main part of the code. Code is simpler to write and to read.
22
Dynamic Binding
Dynamic binding is not possible without polymorphism.
class A { void doSomething(){ ... } } class B extends A { void doSomething (){ ... } }
("Circle");
("Line"); { ("Rectangle");
public static void main (String args[]){ Shape[] s = new Shape[3]; s[0] = new Circle(); s[1] = new Line(); s[2] = new Rectangle(); for (int i = 0; i < s.length; i++){ s[i].draw(); // prints Circle, Line, Rectangle } }
OOP: Inheritance and Polymorphism, Part 1 24
Method Redefinition
Redefinition: A method/variable in a subclass has the same as a
method/variable in the superclass.
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.
25
Compile time constant final static int PI = 3.14 Run-time constant final int RAND = (int) Math.random * 10 double foo (final int i)
Prevents overwriting in a subclass Private methods are implicitly final Cannot inherit from the class
Final class
n
26
S sMethod() T tMethod()
28
Method Combination
Different method combination
It is programmatically controlled
n n n
Method doStuff on A controls the activation of doStuff on B Method doStuff on B controls the activation of doStuff on A Imperative method combination
doStuff on A should not activate doStuff on B, and vice versa Declarative method combination
29
Summary
Designing good reusable classes is hard! Reuse
n
Use composition when ever possible more flexible and easier to understand. Specialization and extension can be combined.
30