Loading...

What is multilevel inheritance in Java?

What is multilevel inheritance in Java?

In this article, we are going to discuss what is inheritance, the types of inheritance, and finally what multiple inheritance is and how is it different from the other type of inheritance in Java.

What is Inheritance?

Inheritance is the ability of one class to inherit the properties or capabilities of another class.
Now, what exactly is a class? Class is simply a representation or template to build a specific object type. It is a basic concept of Object-Oriented Programming which revolve around real-life entities. In Java, a class determines how an object will behave and what the object will contain. An object is an instance of a class, it is nothing but a self-contained component that consists of methods and properties to make a particular type of data useful. For example- color name, table name, etc.

Say class X has inherited the properties of class Y, then class X is called the child class, derived class, or subclass, and class Y is called the parent class, base class, or superclass. The object of class X can now access the methods and fields of class Y, it can also override them. It is because of the inheritance only that we can reuse our code whenever possible and reduce redundancy. In order to inherit another class, you have to use extends keyword.

You can think of inheritance when a child inherits the traits of his/her parents. It is one of the most important concepts of Object Oriented Programming (OOPs).

Types of Inheritance

We will now discuss the various type of inheritance and which one is supported and not supported in Java. We categorize them on the basis of class.

Single inheritance

The most basic type of Inheritance is single inheritance where one class inherits the properties of another class.
Think of Animal class as an example where Dog class inherits the properties of Animal class. Here, only single inheritance is happening, one class is inheriting from only one another class. Dog class extends Animal class.
Let’s take class B which inherits class A.

class A{ ... ... ... } class B extends A{ ... ... ... }
Code language: Java (java)

As you can observe, since class B extends class A, the object of class B can now access the methods defined in class A. This is the power of inheritance.

Multiple Inheritance

When you try to inherit from numerous classes, then this is called multiple inheritance. In this case, we can have multiple superclasses.
For example, Bat derives from superclasses- Mammal and Winged Animal.

Derived class has features of Base1 and Base2.
It is not supported in Java.

Why multiple inheritance is not supported in Java?

Multiple Inheritance is supported in C++ but not in Java, why? It’s because Java doesn’t allow ambiguity. But how does multiple inheritance causes ambiguity? Say you have a class C that extends class A and B, and both class A and B have a common method called run(), if the object of class C calls the method run(), then Java compiler will get confused about which method to inherit from which class and this causes ambiguity.

How to achieve Multiple Inheritance in Java?

Fortunately, we have a way to achieve multiple inheritance in Java, via interfaces.

Interface is a blueprint of a class that consists of static constants and abstract methods. An abstract method is a method declared with abstract keyword and does not have any implementation. Only abstract methods are allowed in the Java interface, not the method body. Abstraction and multiple inheritance are achieved through interfaces in Java.

The following code is not allowed in Java –

public class A extends B, C{}
Code language: Java (java)

However, we can make a class extend one or more interfaces, hence helping us in achieving multiple inheritance.

// interface example interface Ajay { public void walk(); // interface method (does not have a body) public void run(); // interface method (does not have a body) }
Code language: Java (java)

Just like in inheritance, when you want to inherit an interface in an interface, you need to use extends keyword. To inherit an interface in a class, you need to use implements keyword.

class extends another class, class implements an interface, and an interface extends another interface.

//interface 1 interface StudentA { //interfaces method are only declared not defined public void walk(); } //interface 2 interface StudentB { //any number of methods can be declared in the interface public void walk(); public void run(); } //multiple inheritance achieved class StudentC implements StudentA,StudentB { //overidden methods public void walk() { System.out.println("StudentA is walking "); } public void run() { System.out.println("StudentB is running "); } public static void main (String args[]) { StudentC object = new StudentC(); object.walk(); object.run(); } }
Code language: Java (java)
#Output- StudentA is walking StudentB is running
Code language: Java (java)

We have achieved multiple inheritance via interface. We declared methods in interfaces called StudentA and StudentB. While implementing these interfaces in class StudentC, we have overridden these methods. Overriding means the method declared, is being defined again with the same name, return type, and parameters. As you can observe, we have re-implemented walk() and run() methods in our child class, StudentC. When the object of the child class will call these methods, there will be no ambiguity and we will get our desired output.

Multilevel Inheritance

When you derive from a class that is already deriving from another class, that means, say you have a class C that derives from class B that derives from class A, this scenario is called multilevel inheritance.

class A { ... .. ... }; class B extends A { ... .. ... }; class C extends B { ... ... ... };
Code language: Java (java)

For example- Son inherits from his Father who inherits from his own Father (Son’s grandfather), so there becomes three classes, class Son extending class Father and class Father extending class Grandfather.

Hierarchical Inheritance

This type of inheritance occurs when numerous subclasses derive from a single class. All the features common in subclasses are included in the superclass.

class A is inherited by class D, class C, and class B.

class A { ... .. ... }; class B extends A { ... .. ... }; class C extends A { ... ... ... }; class D extends A{ ... ... ... };
Code language: Java (java)

For example- Dog, Cat, Horse derive from Animal class.

Hybrid Inheritance

In hybrid inheritance, a combination of single and multiple inheritance is observed, and as we’ve already read, multiple inheritance is not allowed in Java.

class B and class C extends class A, and class D extends both class B and C.

How multilevel inheritance is different from other types in Java?

Multilevel Inheritance is not the same as multiple inheritance, as the former has one derived class inheriting from another derived class and the latter has one or more base classes for one subclass. So, here in multilevel inheritance, we have a derived class acting like a parent to another class.

Conclusion

Through this article, we discussed inheritance, its type, and multilevel in Java

I hope you have understood the concept!
Thanks for reading!

Sharing is caring

Did you like what Vineeta Tiwari wrote? Thank them for their work by sharing it on social media.

0/10000

No comments so far