3 types: Hierarchical Inheritance

Hierarchical Inheritance is part of 3 types inheritance. This inheritance works on chain structure code like ( Manager-> Department1 Manager, Manager-> Department2 Manager).

 

Basic structure of hierarchical inheritance is.

A             Super class

|

——————————————–

|                                                              |

B             Sub class                              C             Sub Class

 

In this figure, we can see B class is sub class of A class and C class is sub class on A Class.  In this case B and C classes can access A class variables and method.

 

In this blog, we also introduce parameter constructor with hierarchical inheritance. For parameter constructor, we have to use super keyword.

 

class A

{

A(int x,int y)       // two Parameter

{

System.out.println(“Sum : ” + (x+y));

}

}

class B extends A

{

B()          // 0 Parameter

{

super(10,5);       // call to A(x,y) in super class -> must

System.out.println(“Welcome in B Class”);

}

}

class C extends A

{

C(int x,int y,int z)              // Three Parameter

{

super(x,12);       // call to A(x,y)

System.out.println(“Mul : ” + (x*y*z));

}

}

class inh3

{

public static void main(String args[])

{

// Create B Class object

B b1 = new B(); // zero parameter

// Create C Class object

C c1 = new C(7,6,9);        // call in to C Class

}

}

 

Output:

Sum = 15

Welcome in B Class

Sum = 19

Mul = 378

 

About Programs.

In this blog programs, B class call B() default constructor, but B class extends with A class, so B() uses super(int,int) for pass the value in A(int,int) constructor.

C class call C(int,int ,int) parameter constructor, but C class extends with A class, so C(int,int,int) uses super(int,int) for pass the value in A(int,int) constructor.

 

Video available on YouTube.


[sbscrbr_form]

If you have any query on blog example, you can directly comment us or subscribe on website


Earn Money Online

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

214total visits,2visits today


mayank

Hello, I am Maddy a Java blogger, trainer and developer

Related articles

Leave a Reply

EmailEmail
PrintPrint