Browse Tutorials
Multiple Inheritance in Java

Multiple Inheritance in Java

28 Mar 2024
Beginner
255 Views
10 min read
Learn via Video Course & by Doing Hands-on Labs

Java Programming For Beginners Free Course

The Multiple Inheritance in Java: An Overview

Inheritance is one of the powerful Object-Oriented Programming concepts in Java. In which a class can inherit attributes and behaviors from superclasses. This allows systematic designing and structuring of classes, enabling access to properties of different methods or classes. But In this Java Tutorial, we are going to discuss specifically Multiple Inheritance in Java, which will include why multiple inheritance in Java is not supported and how to achieve multiple inheritances in Java. We will also explore multiple inheritance in Java with its example. So, Let's first discuss a little bit about "What is multiple inheritance in Java?".

What is Multiple Inheritance?

  • Multiple inheritance is the capability of creating a single class with characteristics of multiple superclasses.
  • In popular object-oriented programming languages such as C++, we can achieve multiple inheritance very easily
  • But in the case of Java, It doesn’t provide support for multiple inheritances in classes.
  • Java doesn’t support multiple inheritances because it can lead to a diamond problem
  • We will talk about the diamond problem in multiple inheritance later in this article.
  • Before that, we will see how other types of inheritance support Java.

Types of Inheritance:

There are more 4 types of Inheritance in Java except Multiple inheritance

  1. Single inheritance.
  2. Multi-level inheritance.
  3. Hierarchical Inheritance.
  4. Hybrid Inheritance.

1. Single Inheritance:

  • As the heading shows, just one class is subject to this kind of inheritance.
  • The parent shares its characteristics with just one child class.
  • The attributes in this sort of inheritance are only shares from one parent class.
  • In this inheritance Code, reusability and the implementation of new features are made easier.
  • The following shows how single inheritance can be achieved:

2. Multi-level inheritance.

  • In Multi-Level Inheritance, a class extends to another class that is already extended from another class.
  • For example, if A daughter extends the feature of the mother and the mother extends from the grandmother, then this scenario is known to follow Multi-level Inheritance.
  • As the name shows, numerous base classes are involved in multi-level inheritance.
  • The newly derived class from the parent class becomes the base class for another newly derived class.
  • The inherited features in multilevel inheritance in Java likewise come from several base classes.
  • The following shows how single inheritance can be achieved:

Multi-level inheritance.

3. Hierarchical Inheritance.

  • Hierarchical inheritance in Java is the sort of inheritance when many subclasses derive from a single class.
  • In short, It is a mixture of various inheritance types called hierarchical inheritance.
  • Because so many classes are descended from a single superclass,
  • Since It is different from multilevel inheritance.
  • This process makes dynamic polymorphism.

4. Hybrid Inheritance.

  • Hybrid Inheritance in Java is a combination of all inheritances.
  • Single and hierarchical inheritances or multiple inheritance.
  • For example, if we have class Son and class Daughter that extend class Mother and then there is another class Grand Mother that extends class Mother, then this type of Inheritance is known as Hybrid Inheritance.
  • Why? Because we observe that there are two kinds of inheritance here- Hierarchical and Single Inheritance.
  • In short, A hybrid inheritance combines more than two inheritance types, such as multiple and single.

Hybrid Inheritance

Why is Multiple Inheritance Not Supported in Java?

  • Java doesn’t support multiple inheritances in classes because it can create a diamond problem.
  • To understand the diamond problem easily, let’s assume that multiple inheritances can be supported in Java.
  • In that case, we will imagine a class hierarchy like the below image.

Multiple Inheritance

Suppose we have two classes Mother and Uncle which have the same method feature(). If multiple inheritance is possible then the Grandchild class can inherit data members (properties) and methods (behavior) of both Mother and Uncle classes. Now, the Grandchild class has two feature() methods inherited from Mother and Uncle. The problem occurs in a method call, when the feature() method is called with the Grandchild class object which method will be called, will it be of Mother or Uncle? This is an ambiguty problem because of which multiple inheritance is not supported in Java.

Example:


import java.util.*;
class Mother{
      public void feature(){
            System.out.println("Feature method inside Mother.");
      }
}
 
class Uncle{
      public void feature(){
            System.out.println("Feature method inside Uncle.");
      }
}
 
//let multiple inheritance be possible.
public class GrandChild extends Mother, Uncle{
      public static void main(String args[]){
             GrandChild obj = new GrandChild();
            //Ambiguity problem in method call which class display() method will be called.
            obj.feature();
      }
}

Output:

Mother.java:15: error: '{' expected
public class GrandChild extends Mother, Uncle{
                                      ^
1 error
error: compilation failed

Now the question is what is the solution to this diamond problem? And how to achieve multiple inheritance in Java. Yes, we can achieve multiple inheritance in Java by using Interfaces. Now let's see how the interface works in multiple inheritance with demonstration in the Java Compiler.

Example of Multiple Inheritance in Java using Interface

  • Multiple Inheritance is a feature of object-oriented programming
  • Where a child class can inherit properties of more than one parent class.
  • The problem occurs when methods with the same signature or feature exist in both the parent classes.
  • On calling the method, the compiler cannot determine which parent class method to call and even on calling which class method gets the priority.
  • In that case, we use the interface in multiple inheritances.

interface Parent1 {
   void mother();
}
interface Parent2 {
   void father();
}
class Family implements Parent1, Parent2 {
   public void mother() {
      System.out.println("Mothers Feature");
   }
   public void father() {
      System.out.println("Fathers Feature");
   }
}
public class Demo {
   public static void main(String args[]) {
      Family a = new Family();
      a.mother();
      a.father();
   }
}

Output:

Mothers Feature
Fathers Feature

Syntax of implementing Multiple interfaces:

Syntax:


Class Grand_Parent
{
---
----
}
class Parent Extends Grand_Parent
{
----
----
}
class Child Extend Parent
{
-----
-----
}

An Alternative to Multiple Inheritance in Java

  • The alternative to multiple inheritance is known as an interface.
  • Java does not support multiple inheritance of classes, meaning a class cannot inherit from more than one class.
  • So thatInstead, Java uses interfaces to achieve a similar effect.
  • An interface in Java is a collection of abstract methods, which are implemented by classes that implement the interface.
  • Likewise, abstract classes and interfaces cannot be used to create objects
  • Interface methods do not have a body, Because the body is provided by the "implement" class.
  • On implementation of an interface, we must override all of its methods
  • Interface methods are by default stays abstract and public
  • Interface attributes are by default public, static, and final
  • An interface cannot contain a constructor that's why it cannot be used to create objects.
Conclusion
We have seen, In C++, Python, and other languages Multiple inheritance is supported. But not by Java. In Java, The diamond problem, which happens in multiple inheritance, is one example of such a problem. We explored thatWe can achieve multiple inheritance in Java through the concept of interfaces.I hope you like this article. Also, consider the Java Programming Course.

FAQs

Q1. What is the multiple inheritance in Java?

A subclass can inherit from two or more superclasses is nothing but multiple inheritance. C++ allows multiple inheritance, but Java does not support multiple inheritance. 

Q2. Why multiple inheritance is not in Java?

The primary reason is to avoid complexity and ambiguity in the inheritance hierarchy, especially problems like the diamond issue

Q3. What is meant by multiple inheritance?

Multiple inheritance means that a subclass can inherit from two or more superclasses

Q4. What is a real-time example of multiple inheritance in Java?

A class that derives from both a “Vehicle” class and a “Electricity-Powered” class to generate a new class called “Electric Vehicle”.

Q5. Where is multiple inheritance used?

When a subclass needs to combine multiple contracts and inherit some, or all, of the implementation of those contracts
Share Article
Batches Schedule
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 8th time in a row (2016-2023). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Accept cookies & close this