3 types : Multilevel inheritance

Multilevel inheritance is part of 3 types inheritance. This inheritance works on chain structure code like Manager-> Department Manager -> Worker etc.

 

Basic structure of multilevel inheritance is.

A             Super class

|

B             Sub class | Super Class

|

C             Sub Class

In this figure, we can see B class is sub class of A class and super class of C class. Here C class can access A and B Class Data Members (variables and methods), B Class can access A Class variables and methods.

 

In this blog, we also introduce default constructor with inheritance.

 

class A

{

                A()

                {

                                System.out.println(“Welcome in A”); // 1st line

                }

}

class B extends A

{

                B()          // call to A() constructor

                {

 

                                System.out.println(“Welcome in B”);      //  2nd line

                }

}

class C extends B

{

                C()          // call to B() constructor

                {

                                System.out.println(“Welcome in C”);      // 3rd line

                }

}

class inh2

{

                public static void main(String args[])

                {

                                // we just create C class object | automatically call to super class B and A

                                C obj = new C();                // call to C() constructor

                }

}

Output:

Welcome in A

Welcome in B

Welcome in C

Use of inheritance

We have to just create C class object and this object manage all class methods and variables.

 

Video available on YouTube.


 

Earn Money Online

Share and Enjoy

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

211total visits,4visits today


mayank

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

Related articles

1 Comment

  1. October 14, 2016 - 12:31 am

    This is a topic which is close to my heart… Thank you!
    Exactly where are your contact details though?

Leave a Reply

Your email address will not be published. Required fields are marked *

Your comments (*)

Name (*)

Your full name please.

Email address (*)

Used for gravatar.

Website

Link back if you want.

Enter Captcha Here :*

Reload Image

EmailEmail
PrintPrint