Multi level hierarchy
-
Upload
myrajendra -
Category
Documents
-
view
2.491 -
download
0
Transcript of Multi level hierarchy
http://improvejava.blogspot.in 1
Multi level hierarchy
http://improvejava.blogspot.in 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 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 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 : super class to B
B : sub class of A, super class of C
C : subclass of B and A
A
B
C
Fig. 24.1. Multi level hierarchy
http://improvejava.blogspot.in 5
• 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
Multi Level Inheritance Hierarchy contd..
http://improvejava.blogspot.in 6
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 { ------- ---------}
Multi Level Inheritance Hierarchy contd..
http://improvejava.blogspot.in 7
Discussion
• Can you give any real life example for multi level inheritance hierarchy
• Employee – Manager – Clerk – WagedEmployee• Manager – AccountsDepartment• Manager – IT Department• Manger - HRD
Employee
Manger Clerk WagedEmployee
Accounts Department IT Department HRD
Fig. 24.2. Real life example fro multi level hierarchy
http://improvejava.blogspot.in 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
Square RoundedRectangle
Fig. 24.3. Software example fro multi level hierarchy
http://improvejava.blogspot.in 9
• Consider the following example
• Add a class Shipment to the class BoxWeight which is a sub
class of Box
Program To Create Multi Level Hierarchy
Box
BoxWeight
Shipment
Fig. 24.4. Example on Multi level Hierarchy
Box : Super class
BoxWeight : Subclass to Box class
super to shipment
Shipment : Newly added sub class to BoxWeight
http://improvejava.blogspot.in 10
class Box{ private double width; private double height; private double depth;// construct clone of one objectBox(Box ob) { // pass object to constructor width = ob.width; height = ob.height; depth = ob.depth; }
Program To Create Multi Level Hierarchy contd..
http://improvejava.blogspot.in 11
//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(){
Program To Create Multi Level Hierarchy contd..
http://improvejava.blogspot.in 12
// constructor used when no dimensions specifiedBox(){
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;
}
Program To Create Multi Level Hierarchy contd..
http://improvejava.blogspot.in 13
// 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 objectBoxWeight( BoxWeight ob) { // pass object to constructor
Program To Create Multi Level Hierarchy contd..
http://improvejava.blogspot.in 14
super(ob); weight = ob.weight; }// constructor when all parameters are apecifiedBoxWeight ( double w, double h, double d, double m) {
super(w,h,d); //call super class constructor weight = m;}
// default constructor BoxWeight() {Super(); weight = -1;}
Program To Create Multi Level Hierarchy contd..
http://improvejava.blogspot.in 15
// constructor used when cube is created
BoxWeight( double len, double m) {
super(len); weight = m; }}
Program To Create Multi Level Hierarchy contd..
http://improvejava.blogspot.in 16
// 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;}
Program To Create Multi Level Hierarchy contd..
http://improvejava.blogspot.in 17
// constructor when all parameters are specifiedShipment(double w, double h, double d, double m, double c) {super( w,h,d,m); //call superclass constructor cost = c; }//default constructorShipment() { super(); cost = -1; }//constructor used when cube is createdShipment( double len, double m, double c) { super(len,m);} }
Program To Create Multi Level Hierarchy contd..
http://improvejava.blogspot.in 18
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();
Program To Create Multi Level Hierarchy contd..
http://improvejava.blogspot.in 19
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();}
}
Program To Create Multi Level Hierarchy contd..
http://improvejava.blogspot.in 20
Output of program
Volume of shipment1 is 3000.0Weight of shipment1 is 10.0Shipping cost : $3.41
Volume of shipment2 is 24.0Weight of shipment2 is 0.76Shipping cost : $1.28
http://improvejava.blogspot.in 21
Summary
• In this class we have discussed• Multi level hierarchy• Creating multi level hierarchy
http://improvejava.blogspot.in 22
1.
The above figure depicts
a) Single Inheritance
b) Hierarchical inheritance
c) Multilevel inheritance
A
B
C
Quiz
http://improvejava.blogspot.in 23
Frequently Asked Questions
1. What is multi level hierarchy ?
2. Explain how to create a multilevel hierarchy with an example program