Multi level hierarchy

2,277 views
1,996 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,277
On SlideShare
0
From Embeds
0
Number of Embeds
24
Actions
Shares
0
Downloads
28
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Multi level hierarchy

  1. 1. Multi level hierarchy http://improvejava.blogspot.in 1
  2. 2. ObjectivesOn completion of this period, you would beable to know • Multi level inheritance hierarchy • To create multi level inheritance hierarchy http://improvejava.blogspot.in 2
  3. 3. RecapSuper 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. 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. 5. Multi Level Inheritance Hierarchy contd..• With the above description each subclassinherits all the properties found in all of its superclass• In the above diagram caseC inherits all aspects of B and A http://improvejava.blogspot.in 5
  6. 6. Multi Level Inheritance Hierarchy contd..A derived class with multilevel base classes is declared asfollows class A{ -------- --------}class B extends A // first level{ ------ ------}class C extends B // second level{ ------- --------- http://improvejava.blogspot.in 6}
  7. 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. 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. 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 BoxWeightFig. 24.4. Example on Multi level Hierarchy http://improvejava.blogspot.in 9
  10. 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. 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. 12. Program To Create Multi Level Hierarchy contd..// 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 createdBox(double len){ width = height = depth = len;} http://improvejava.blogspot.in 12
  13. 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. 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. 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. 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. 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. 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. 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. 20. Output of programVolume of shipment1 is 3000.0Weight of shipment1 is 10.0Shipping cost : $3.41Volume of shipment2 is 24.0Weight of shipment2 is 0.76Shipping cost : $1.28 http://improvejava.blogspot.in 20
  21. 21. Summary• In this class we have discussed • Multi level hierarchy • Creating multi level hierarchy http://improvejava.blogspot.in 21
  22. 22. Quiz1. A B CThe above figure depictsa) Single Inheritanceb) Hierarchical inheritancec) Multilevel inheritance http://improvejava.blogspot.in 22
  23. 23. Frequently Asked Questions1. What is multi level hierarchy ?2. Explain how to create a multilevel hierarchy with an example program http://improvejava.blogspot.in 23

×