Professional Documents
Culture Documents
Iinheritance_Polymorphism-review
Iinheritance_Polymorphism-review
• The term inheritance refers to the fact that one class can
inherit part or all of its structure and behavior from another
class.
– Structure : fields or attributes.
– Behavior : methods.
•Class B has:
•Its fields + fields of class A
•Its methods + methods of class A
class B extends A {
3
Inheritance and class hierarchy
4
Inheritance and class hierarchy
• In the diagram, classes B, C, and D
are sub classes.
class.
Object class
• Object class is the super-class of all the classes.
class A { ... }
is equivalent to:
class A extends Object { ... }
Design with Inheritance in Mind
• When designing a collection of new but related
classes, pay attention to what are the common
behaviors and characteristics. This process is known
as factorization.
– Try to create a coherent base class that provides this
commonality.
– From the base class, subclasses can be defined to provide
the special behaviors and characteristics.
– Your current work effort should be reduced as common
behaviors are implemented just once.
– Future work efforts should also be minimized as new
features can be added by extending the existing classes.
7
Inheritance - Example
8
Inheritance - Example
9
Inheritance - Example
class Vehicle {
protected int registrationNumber;
protected String owner;
public void transferOwnership(String newOwner) {
owner = newOwner;
}
…..
}
• protected modifier:
A subclass can access the superclass s
protected members.
13
Extending The Point Class (cont.)
y-axis z-axis
Point (x,y,z) in
3-dimensional
space
14
x-axis
Extending The Point Class (cont.)
15
Extending The Point Class (cont.)
16
It s All Inherited in Java
Class Object provides basic versions of several methods including: toString(), equals(), and
clone(). It also has methods finalize(), getClass(), hashCode(), notify(), notifyAll(), and wait().
These are all inherited by the subclasses of Object.
17
The ThreeDPoint Class
import java.awt.*;
18
//getZ(): z-coordinate accessor
public int getZ(){
return z; }
//setZ(): z-coordinate mutator
public void setZ(double z){
this.z = z; }
private double z;
21
Constructors in a Subclass
24
Constructors in a Subclass (cont.)
• The specific ThreeDPoint constructor initializes the
object under construction to the given x-, y-, and z-
coordinate values.
super(a,b);
public ThreeDPoint() {
//implicit super() invocation
}
26
Method toString() of Class ThreeDPoint
return getClass() + [ + a + , + b + , + ,
+ c + ] ;
}
27
Method getClass()
• Displays:
class ThreeDPoint;
28
Method toString() of Class ThreeDPoint
29
Method translate() of Class ThreeDPoint
Class ThreeDPoint[2,2,2]
Class ThreeDPoint[1,2,3]
32
• Why bother with inheritance at all? Why not just
modify the existing class to include the new features?
For example, why not give the Point class a z-
coordinate value?
33
Polymorphism
Student student1;
student1 = new Undergraduate(); // polymorphic assignment
student1 = new Graduate(); // polymorphic assignment
s = new Undergraduate();
s.m(); //undergraduate s m() is executed
s= new Graduate();
s.m(); //graduate s m() is executed
36
s.m(5); Compilation error ??
Polymorphic Method Invocation
Dynamic binding
//f(): facilitator
public void f(){
System.out.println ( U is final can't be extended );
}
}
40
Finality (cont.)
• Consider class V which contains a method f() which is
declared as final. Therefore, method f() cannot be
overridden if V is extended.
public class V {
//V(): default constructor
public V(){
//no body necessary
}
//f(): facilitator