Multi level Inheritance Java

Multi level Inheritance Java

In the earlier, Bird-Ostrich inheritance program, Ostrich extends only one class, Bird, because Ostrich wants to walk and the method is there in Bird’s class.

Now Ostrich also wants to eat but the eat() method is there in another class Animal. Now, it is required to Ostrich to extend two classes Bird and Animal. Observe the following pseudo code.

As you can observe in the above code, after extends two classes exist, Bird and Animal. This is not permitted by Java. If you write multiple classes after extends, it comes under multiple inheritance and Java does not support. That is, Java permits to extend only one class. But Ostrich wants both. How to solve the problem?

Very simple. As Java permits to extend only one class, we write multiple classes that extends only one to the other like a ladder.

You can see in the above code, one-to-one ladder like Ostrich extends Bird and Bird extends Animal (and infact, this ladder can go any extent).

Let us rewrite the earlier Bird-Ostrich code where we add one more class Animal.

Multi level Inheritance Java

Ostrich extends Bird and Bird extends Animal, it is one-to-one relation. This type of relation is known as "multilevel inheritance". Other inheritance is multiple inheritance.

Leave a Reply

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