2. Objectives
On completion of this period, you would be
able to know
• Multi level inheritance hierarchy
• To create multi level inheritance hierarchy
http://improvejava.blogspot.in 2
3. Recap
Super key word
• When ever a subclass needs to refer to it’s
immediate upper class it can do so by using
super keyword
http://improvejava.blogspot.in 3
4. Multi Level Inheritance Hierarchy
One can build hierarchies that contain as many layers of
inheritances as one like
Given three classes A,B,C
C can be sub class of B, which is a sub class of A
A A : super class to B
B B : sub class of A, super class of C
C C : subclass of B and A
Fig. 24.1. Multi level hierarchy
http://improvejava.blogspot.in 4
5. Multi Level Inheritance Hierarchy contd..
• With the above description each subclass
inherits all the properties found in all of its super
class
• In the above diagram case
C inherits all aspects of B and A
http://improvejava.blogspot.in 5
6. Multi Level Inheritance Hierarchy contd..
A derived class with multilevel base classes is declared as
follows
class A
{ --------
--------
}
class B extends A // first level
{ ------
------
}
class C extends B // second level
{ -------
---------
http://improvejava.blogspot.in 6
}
7. Discussion
• Can you give any real life example for multi level
inheritance hierarchy
• Employee – Manager – Clerk – WagedEmployee
• Manager – AccountsDepartment
• Manager – IT Department
Employee
• Manger - HRD
Manger Clerk WagedEmployee
Accounts Department IT Department HRD
Fig. 24.2. Real life example fro multi level hierarchy
http://improvejava.blogspot.in 7
8. Discussion contd..
• Can you give any software example for multi level
inheritance hierarchy
• Shape – Rectangle – Circle – Triangle
• Rectangle – Square - RoundedRectangle
Shape
Rectangle Circle Traingle
RoundedRectang
Square
le
Fig. 24.3. Software example fro multi level hierarchy
http://improvejava.blogspot.in 8
9. Program To Create Multi Level Hierarchy
• Consider the following example
• Add a class Shipment to the class BoxWeight
which is a sub class of Box
Box Box : Super class
BoxWeight BoxWeight : Subclass to Box class
super to shipment
Shipment Shipment : Newly added sub class to BoxWeight
Fig. 24.4. Example on Multi level Hierarchy
http://improvejava.blogspot.in 9
10. Program To Create Multi Level Hierarchy contd..
class Box{
private double width;
private double height;
private double depth;
// construct clone of one object
Box(Box ob) { // pass object to constructor
width = ob.width;
height = ob.height;
depth = ob.depth;
}
http://improvejava.blogspot.in 10
11. Program To Create Multi Level Hierarchy contd..
//Constructor used when all dimensions specified
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
// constructor used when no dimensions specified
Box(){
http://improvejava.blogspot.in 11
12. Program To Create Multi Level Hierarchy contd..
// constructor used when no dimensions specified
Box(){
width = -1; // use -1 to indicate an uninitialized box
height = -1;
depth = -1;
}
// constructor used when a cube is created
Box(double len){
width = height = depth = len;
}
http://improvejava.blogspot.in 12
13. Program To Create Multi Level Hierarchy contd..
// Compute and return volume
double volume() {
return width * height * depth;
}
}
// BoxWeight now fully implements all constructors.
class BoxWeight extends Box {
double weight; // weight box
// construct clone of an object
BoxWeight( BoxWeight ob) { // pass object to constructor
http://improvejava.blogspot.in 13
14. Program To Create Multi Level Hierarchy contd..
super(ob);
weight = ob.weight;
}
// constructor when all parameters are apecified
BoxWeight ( double w, double h, double d, double m) {
super(w,h,d); //call super class constructor
weight = m;
}
// default constructor
BoxWeight() {
Super();
weight = -1;
}
http://improvejava.blogspot.in 14
15. Program To Create Multi Level Hierarchy contd..
// constructor used when cube is created
BoxWeight( double len, double m) {
super(len);
weight = m;
}
}
http://improvejava.blogspot.in 15
16. Program To Create Multi Level Hierarchy contd..
// Add shipping costs
class Shipment extends BoxWeight {
double cost;
// construct clone of an object.
Shipment(Shipment ob) { // pass object to
constructor
super(ob);
cost = ob.cost;
}
http://improvejava.blogspot.in 16
17. Program To Create Multi Level Hierarchy contd..
// constructor when all parameters are specified
Shipment(double w, double h, double d, double m, double c) {
super( w,h,d,m); //call superclass constructor
cost = c;
}
//default constructor
Shipment() {
super();
cost = -1;
}
//constructor used when cube is created
Shipment( double len, double m, double c) {
super(len,m);
} }
http://improvejava.blogspot.in 17
18. Program To Create Multi Level Hierarchy contd..
class DemoShipment {
public static void main( String args[]) {
double vol;
Shipment shipment1 = new Shipment(10,20,15,10,3.41);
Shipment shipment2 = new Shipment(2,3,4,0.76,1.28);
vol = shipment1.volume();
System.out.println( “Volume of shipment1 is ” + vol);
System.out.println( “weight of shipment1 is”
+ shipment1.weight);
System.out.println( “shipping cost : $” +shipment1.cost);
System.out.println();
http://improvejava.blogspot.in 18
19. Program To Create Multi Level Hierarchy contd..
vol = shipment2.volume();
System.out.println( “Volume of shipment2 is ” + vol);
System.out.println( “weight of shipment2 is”
+ shipment2.weight);
System.out.println( “shipping cost : $” +shipment2.cost);
System.out.println();
}
}
http://improvejava.blogspot.in 19
20. Output of program
Volume of shipment1 is 3000.0
Weight of shipment1 is 10.0
Shipping cost : $3.41
Volume of shipment2 is 24.0
Weight of shipment2 is 0.76
Shipping cost : $1.28
http://improvejava.blogspot.in 20
21. Summary
• In this class we have discussed
• Multi level hierarchy
• Creating multi level hierarchy
http://improvejava.blogspot.in 21
22. Quiz
1. A
B
C
The above figure depicts
a) Single Inheritance
b) Hierarchical inheritance
c) Multilevel inheritance
http://improvejava.blogspot.in 22
23. Frequently Asked Questions
1. What is multi level hierarchy ?
2. Explain how to create a multilevel hierarchy
with an example program
http://improvejava.blogspot.in 23