SlideShare una empresa de Scribd logo
1 de 23
Descargar para leer sin conexión
Multi level hierarchy




                        http://improvejava.blogspot.in   1
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
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
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
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
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
}
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Summary

• In this class we have discussed
  • Multi level hierarchy
  • Creating multi level hierarchy




                   http://improvejava.blogspot.in   21
Quiz
1.   A

     B

     C
The above figure depicts
a) Single Inheritance
b) Hierarchical inheritance
c) Multilevel inheritance
                  http://improvejava.blogspot.in   22
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

Más contenido relacionado

Destacado

Some Basic Concepts of Object Oriented Methodology
Some Basic Concepts of Object Oriented MethodologySome Basic Concepts of Object Oriented Methodology
Some Basic Concepts of Object Oriented MethodologyManoj Kumar
 
Friend functions
Friend functions Friend functions
Friend functions Megha Singh
 
Object oriented methodology & unified modeling language
Object oriented methodology & unified modeling languageObject oriented methodology & unified modeling language
Object oriented methodology & unified modeling languageIsmail El Gayar
 
Introduction to oops concepts
Introduction to oops conceptsIntroduction to oops concepts
Introduction to oops conceptsNilesh Dalvi
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member FunctionsMuhammad Hammad Waseem
 
Multiple Inheritance For C++
Multiple Inheritance For C++Multiple Inheritance For C++
Multiple Inheritance For C++elliando dias
 
Principles and advantages of oop ppt
Principles and advantages of oop pptPrinciples and advantages of oop ppt
Principles and advantages of oop pptdaxesh chauhan
 
Java exception handling
Java exception handlingJava exception handling
Java exception handlingBHUVIJAYAVELU
 
Object-oriented Programming-with C#
Object-oriented Programming-with C#Object-oriented Programming-with C#
Object-oriented Programming-with C#Doncho Minkov
 
Templates in C++
Templates in C++Templates in C++
Templates in C++Tech_MX
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++Laxman Puri
 
file handling c++
file handling c++file handling c++
file handling c++Guddu Spy
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).pptAlok Kumar
 

Destacado (20)

Some Basic Concepts of Object Oriented Methodology
Some Basic Concepts of Object Oriented MethodologySome Basic Concepts of Object Oriented Methodology
Some Basic Concepts of Object Oriented Methodology
 
Friend functions
Friend functions Friend functions
Friend functions
 
Object oriented methodology & unified modeling language
Object oriented methodology & unified modeling languageObject oriented methodology & unified modeling language
Object oriented methodology & unified modeling language
 
Container ship
Container ship Container ship
Container ship
 
Introduction to oops concepts
Introduction to oops conceptsIntroduction to oops concepts
Introduction to oops concepts
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
 
Inheritance
InheritanceInheritance
Inheritance
 
Templates
TemplatesTemplates
Templates
 
Inheritance
InheritanceInheritance
Inheritance
 
Multiple Inheritance For C++
Multiple Inheritance For C++Multiple Inheritance For C++
Multiple Inheritance For C++
 
Principles and advantages of oop ppt
Principles and advantages of oop pptPrinciples and advantages of oop ppt
Principles and advantages of oop ppt
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
Object-oriented Programming-with C#
Object-oriented Programming-with C#Object-oriented Programming-with C#
Object-oriented Programming-with C#
 
Constructors & destructors
Constructors & destructorsConstructors & destructors
Constructors & destructors
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
 
Exception handling
Exception handlingException handling
Exception handling
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
file handling c++
file handling c++file handling c++
file handling c++
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).ppt
 
Object Oriented Design
Object Oriented DesignObject Oriented Design
Object Oriented Design
 

Similar a Multi level hierarchy

Super keyword.23
Super keyword.23Super keyword.23
Super keyword.23myrajendra
 
6.INHERITANCE.ppt(MB).ppt .
6.INHERITANCE.ppt(MB).ppt                    .6.INHERITANCE.ppt(MB).ppt                    .
6.INHERITANCE.ppt(MB).ppt .happycocoman
 
Assessment IIEssay IIdentify and describe three of the new a.docx
Assessment IIEssay IIdentify and describe three of the new a.docxAssessment IIEssay IIdentify and describe three of the new a.docx
Assessment IIEssay IIdentify and describe three of the new a.docxfestockton
 
Chapter 04 inheritance
Chapter 04 inheritanceChapter 04 inheritance
Chapter 04 inheritanceNurhanna Aziz
 
Basics of inheritance .22
Basics of inheritance .22Basics of inheritance .22
Basics of inheritance .22myrajendra
 
Mpl 9 oop
Mpl 9 oopMpl 9 oop
Mpl 9 oopAHHAAH
 
Inheritance (with classifications)
Inheritance (with classifications)Inheritance (with classifications)
Inheritance (with classifications)Redwan Islam
 
2CPP07 - Inheritance
2CPP07 - Inheritance2CPP07 - Inheritance
2CPP07 - InheritanceMichael Heron
 
Chapter26 inheritance-ii
Chapter26 inheritance-iiChapter26 inheritance-ii
Chapter26 inheritance-iiDeepak Singh
 
Developing Modern Java Web Applications with Java EE 7 and AngularJS
Developing Modern Java Web Applications with Java EE 7 and AngularJSDeveloping Modern Java Web Applications with Java EE 7 and AngularJS
Developing Modern Java Web Applications with Java EE 7 and AngularJSShekhar Gulati
 
Multiple inheritance
Multiple inheritanceMultiple inheritance
Multiple inheritancezindadili
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++Vishal Patil
 
Week 3-4 Programming AssignmentCore Learning Outcomes addressed in.docx
Week 3-4 Programming AssignmentCore Learning Outcomes addressed in.docxWeek 3-4 Programming AssignmentCore Learning Outcomes addressed in.docx
Week 3-4 Programming AssignmentCore Learning Outcomes addressed in.docxco4spmeley
 

Similar a Multi level hierarchy (20)

Super keyword.23
Super keyword.23Super keyword.23
Super keyword.23
 
6.INHERITANCE.ppt(MB).ppt .
6.INHERITANCE.ppt(MB).ppt                    .6.INHERITANCE.ppt(MB).ppt                    .
6.INHERITANCE.ppt(MB).ppt .
 
Assessment IIEssay IIdentify and describe three of the new a.docx
Assessment IIEssay IIdentify and describe three of the new a.docxAssessment IIEssay IIdentify and describe three of the new a.docx
Assessment IIEssay IIdentify and describe three of the new a.docx
 
7_-_Inheritance
7_-_Inheritance7_-_Inheritance
7_-_Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Chapter 04 inheritance
Chapter 04 inheritanceChapter 04 inheritance
Chapter 04 inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Basics of inheritance .22
Basics of inheritance .22Basics of inheritance .22
Basics of inheritance .22
 
Java misc1
Java misc1Java misc1
Java misc1
 
Ganesh groups
Ganesh groupsGanesh groups
Ganesh groups
 
Mpl 9 oop
Mpl 9 oopMpl 9 oop
Mpl 9 oop
 
Inheritance (with classifications)
Inheritance (with classifications)Inheritance (with classifications)
Inheritance (with classifications)
 
VR Workshop #2
VR Workshop #2VR Workshop #2
VR Workshop #2
 
2CPP07 - Inheritance
2CPP07 - Inheritance2CPP07 - Inheritance
2CPP07 - Inheritance
 
Chapter26 inheritance-ii
Chapter26 inheritance-iiChapter26 inheritance-ii
Chapter26 inheritance-ii
 
Developing Modern Java Web Applications with Java EE 7 and AngularJS
Developing Modern Java Web Applications with Java EE 7 and AngularJSDeveloping Modern Java Web Applications with Java EE 7 and AngularJS
Developing Modern Java Web Applications with Java EE 7 and AngularJS
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 
Multiple inheritance
Multiple inheritanceMultiple inheritance
Multiple inheritance
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Week 3-4 Programming AssignmentCore Learning Outcomes addressed in.docx
Week 3-4 Programming AssignmentCore Learning Outcomes addressed in.docxWeek 3-4 Programming AssignmentCore Learning Outcomes addressed in.docx
Week 3-4 Programming AssignmentCore Learning Outcomes addressed in.docx
 

Más de myrajendra (20)

Fundamentals
FundamentalsFundamentals
Fundamentals
 
Data type
Data typeData type
Data type
 
Hibernate example1
Hibernate example1Hibernate example1
Hibernate example1
 
Jdbc workflow
Jdbc workflowJdbc workflow
Jdbc workflow
 
2 jdbc drivers
2 jdbc drivers2 jdbc drivers
2 jdbc drivers
 
3 jdbc api
3 jdbc api3 jdbc api
3 jdbc api
 
4 jdbc step1
4 jdbc step14 jdbc step1
4 jdbc step1
 
Dao example
Dao exampleDao example
Dao example
 
Sessionex1
Sessionex1Sessionex1
Sessionex1
 
Internal
InternalInternal
Internal
 
3. elements
3. elements3. elements
3. elements
 
2. attributes
2. attributes2. attributes
2. attributes
 
1 introduction to html
1 introduction to html1 introduction to html
1 introduction to html
 
Headings
HeadingsHeadings
Headings
 
Forms
FormsForms
Forms
 
Css
CssCss
Css
 
Views
ViewsViews
Views
 
Views
ViewsViews
Views
 
Views
ViewsViews
Views
 
Starting jdbc
Starting jdbcStarting jdbc
Starting jdbc
 

Multi level hierarchy

  • 1. Multi level hierarchy http://improvejava.blogspot.in 1
  • 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