Multiple Inheritance is a feature in object-oriented programming where a class can inherit properties and methods from more than one parent class. However, Java does not support multiple inheritance of classes. Instead, it allows a class to implement multiple interfaces, which is known as multiple interface inheritance. This enables a class to inherit abstract methods from multiple interfaces, promoting code reusability and flexibility in designing class hierarchies. In this article, we will discuss Multiple Inheritance in Java, which will include the reason why multiple inheritance in java is not supported and how to deal with this problem.
What is Multiple Inheritance in Java?
Java doesn't allow a class to inherit from more than one class directly, preventing multiple inheritance to avoid complexity and ambiguity. However, a Java class can still gain functionality from multiple sources by implementing multiple interfaces, effectively bypassing this restriction and promoting a cleaner, more organized approach to inheriting features and behaviours.
In our example of Inheritance above, we showed that a kid inherits attributes from only their mother, but that doesn't happen in reality. A child inherits qualities from both parents, as shown below.
Considering the parents as the base classes and the kid as the derived class, we can see that a single derived class inherits attributes from multiple base classes. This is known as multiple inheritance in Java.
Let's consider, we are building a multimedia application. It involves different types of media objects(images, videos, etc.). We want these media objects to have both display and play features. We also want to keep their implementations separate. Let us try to implement these:
Java
Java
// Interface for displaying interface DisplayInterface { void display(); }
// Interface for playing interface PlayInterface { void play(); }
// Class that implements DisplayInterface class Image implements DisplayInterface { @Override public void display() { System.out.println("This function is for displaying the image"); } }
// Class that implements DisplayInterface and PlayInterface class Video implements DisplayInterface, PlayInterface { @Override public void display() { System.out.println("This function is for displaying the video"); }
@Override public void play() { System.out.println("This function is for playing the video"); } }
class Main { public static void main(String[] args) { Image img = new Image(); Video vid = new Video();
img.display(); vid.display(); vid.play(); } }
You can also try this code with Online Java Compiler
This function is for displaying the image
This function is for displaying the video
This function is for Playing the video
Why is Multiple Inheritance Not Supported in Java?
The main reason for not allowing multiple inheritances in Java is the ambiguity around the diamond problem.
Consider a class NinjaA that contains a Coding() method. Classes NinjaB and NinjaC were derived from class NinjaA and each had a Coding() implementation. Class NinjaD is now derived from classes NinjaB and NinjaC using multiple inheritances. If we refer to simply Coding(), the compiler would not be able to determine which Coding() it should call.
Because of the inheritance scenario's structure, which resembles a four-edged diamond, this problem is also known as the "Diamond Problem."
The second and more convincing justification is that having multiple inheritances complicates the design and causes issues with casting, constructor chaining, etc. You can refer to the diagram below to understand it better.
Different Methods to Implement Multiple Inheritance in Java
As we already know, Multiple Inheritance is not supported in Java because of the ambiguity caused by methods or attributes with the same name. However, we can solve this problem by using interfaces instead of classes.
An Interface is the blueprint of a class that implements abstraction by using abstract methods. Declaring interfaces is easy and can be done by simply using the ‘interface’ keyword.
Now you may be wondering how using an interface solves the problem we were facing before.
To understand the reason, let us consider our previous example, but we’ll use an interface instead of a class this time.
Here, the text( ) methods from both the interfaces A and B are called separately, thereby eliminating the confusion about which class’s method to call.
Another method in which we can use interfaces for Multiple Inheritance is by using abstract methods. This method is shown below.
Java
Java
// Interface 1 interface A {
// Abstract method public abstract void text1(); }
// Interface 2 interface B {
// Abstract method public abstract void text2(); }
// Interface is implemented class C implements A, B {
// Abstract methods are overridden public void text1() { System.out.println("Hello"); }
public void text2() { System.out.println("What's your name?"); } }
class Main { public static void main(String[] args) {
//Create a C object C obj = new C(); obj.text1(); obj.text2(); } }
You can also try this code with Online Java Compiler
Here, two abstract methods text1() and text2() are declared in the interfaces, which are overridden in the derived class C.
If you've made it this far, we're confident that you have a firm grasp on Multiple Inheritance in Java. Before we wrap up this blog, let's look at some frequently asked Java questions.
An Alternative to Multiple Inheritance in Java
When a class can derive from numerous parent classes, this is known as multiple inheritances, and it can complicate and create disputes within the inheritance hierarchy. Java does not provide multiple class inheritance, unlike other programming languages like C++. Java promotes interfaces, which specify a set of methods a class implementing the interface must follow. Classes may define and execute behavior using interfaces without defining the specifics of the implementation. A class can inherit numerous contracts and uses polymorphism by implementing interfaces.
Java
Java
interface InterfaceX { void action1(); }
interface InterfaceY { void action2(); }
class MyClass implements InterfaceX, InterfaceY { public void action1() { System.out.println("Executing action1 command..."); }
public void action2() { System.out.println("Executing action2 command..."); } }
public class Main { public static void main(String[] args) {
//Create an instance of MyClass MyClass myObj = new MyClass();
//Call the action1 method myObj.action1();
//Call the action2 method myObj.action2(); } }
You can also try this code with Online Java Compiler
A class can inherit features and actions from multiple interfaces in Java programming. Since Java doesn't offer multiple inheritances options among classes, like some programming languages, a single class is limited to extending another class. On the other hand, it allows multiple inheritance through interfaces. A variety of inheritance through interface promotes a more adaptive and modular approach to design. It promotes code reuse and polymorphism by enabling a class to be a component of many object hierarchies.
In our example of Inheritance below, two interfaces and three classes implement those interfaces. The names of the interfaces are 'playing' and 'studying,' and the classes are 'Teachers,' 'Parents,' and 'Student.' The interface 'playing' and 'studying' consists of a single method, 'play()' and 'study().' When the 'Teachers' class implements the playing interface, it indicates implementing the 'play()' method. When a Teachers() object is created, the 'play()' method is called, printing the corresponding statements.
Frequently Asked Questions
Can we achieve multiple inheritance in Java?
Java does not support multiple inheritance with classes but allows it through interfaces using the implements keyword.
How to do multi-level inheritance in Java?
Multi-level inheritance in Java is achieved by having a class extend another class, which itself extends another class, forming a chain.
How can we achieve inheritance in Java?
Inheritance in Java is achieved using the extends keyword, where one class inherits properties and methods from another class.
Conclusion
In this article, we talked about Multiple Inheritance in Java. We learned that Java does not support multiple inheritance of classes but allows multiple interface inheritance. This design choice helps avoid ambiguity and complications associated with multiple class inheritance while still providing flexibility through interfaces. Multiple interface inheritance promotes code reusability and allows classes to inherit abstract methods from multiple sources.
We hope this blog increases your knowledge regarding Multiple Inheritance in Java. We recommend you check out some other interesting topics such as: