Multilevel inheritance in Java and how to implement it


Java provides multilevel inheritance which increases the code reduction and re-usability feature. learn how to implement this.

Java inheritance provide a very excellent power of code reduction and re usability however Java took off multiple inheritance functionality, well to be more correct direct multiple inheritance. However it provide the multiple inheritance through the use of Interfaces. But this article is not about the Interfaces but about multilevel inheritance provided by Java Language.

When do we need multilevel Inheritance?
We need it when there is certain properties of a class which could be applicable for one class or even its sub class. There is no limitations to the hierarchy, to which we can extend a class. But it is usually suggested to keep it as short as possible. but this this way the use of super may be little more confusing but you need not to worry. Look at following program and I'll explain how it works.


class AA {
    public int a = 100;
    public void methodA(){
        System.out.println("methodA in class A");
    }
}

class BB extends AA{
    public int b = 200;
    public void methodA(){
        System.out.println("methodA in class B");
    }
    
    public void methodB(){
        System.out.println("methodB in class B");
    }
}

class CC extends BB{
    public int c = 300;
    public void methodA(){
        System.out.println("methodA in class C");       
    }
    
    public void methodC(){
        super.methodA();
        System.out.println("methodC in class C");
    }
    
    public void showVars(){
        System.out.println("var a from claas A: "+a);
        System.out.println("var b from claas B: "+b);
        System.out.println("var c from claas C: "+c);
    }
}

class run{
    public static void main(String[] args){
        CC cc = new CC();
        BB bb = new BB();
        AA aa = new AA();
        aa.methodA();
        bb.methodA();
        cc.methodA();
        cc.showVars();
        cc.methodC();
    }
}

the output of this program is as follows:

methodA in class A
methodA in class B
methodA in class C
var a from claas A: 100
var b from claas B: 200
var c from claas C: 300
methodA in class B
methodC in class C

Now, if you have taken a good look at the program let me explain it to you. The class AA is a base class and it has two one property that is variable "a" and method methodA(). The purpose for declaring this class is to create a demo base class.

Second, class BB extends AA and hence all its methods and public properties. But If you noticed I have also created a methodA in this even after my declaration of the methodA in class AA. This is known as method Overriding.

Please note that method overloading and overriding is a different concept. Overloading is a function usually performed in one class only. However the overriding is a concept of Inheritance. Same process I have repeated with the Class CC.

Now if you'll see in the class CC that to show the level limit of super keyword I have called methodA() with super keyword from methodC. And in the result it will invoke the method from its predecessor class. And when you call the showVars() from class CC this will show all the variables in class CC which are actually inherited from class AA and BB also.

So, we saw that Java provides multiple level Inheritance and another powerful and flexible feature in the form of method overriding.