Multiple Inheritance in Java – How to Implement It?

Updated on August 14, 2024

Article Outline

Inheritance, a fundamental concept in Object-Oriented Programming (OOP), is the process of inheriting all the properties from another class or superclass. Multiple Inheritance, where we inherit properties from multiple classes, has practical applications in Java programming, making it a concept worth mastering. While Java does not support multiple Inheritance, we can achieve it by using interfaces (instead of classes).

What is Multiple Inheritance?

Multiple inheritance is the process where our subclass inherits more than one superclass. Unlike some other programming languages, like C++, Java does not support multiple inheritance of classes. This means Java cannot inherit multiple superclasses. Java has interfaces.

Multiple Inheritance

*Image
Get curriculum highlights, career paths, industry insights and accelerate your technology journey.
Download brochure

Example of Multiple Inheritance in Java

There must be a reason why Java does not support multiple inheritance. Before talking about why. Let’s implement Inheritance in Java.

 

  • The below program has three classes: A, B, and C
  • There is one method in classes A and B, which is addition().
  • Class c inherits both superclasses using the extends keyword in java
  • Main class, we created an object of class C with the name obj itself.

 

Program 

class A{ void addition(int a, int b){ System.out.println("Output = "+(a+b)) ; } }  class B { void addition(int a, int b){ System.out.println("Output = "+(a+b)) ; } }  class C extends A,B{  } public class Example { public  static void main(String args[]){ C ob = new C() ; ob.addition(20, 30);  } }

Output

Example.java:15: error: '{' expected class C extends A, B{ ^ 1 error

This example will give you an error. Because the compiler is confused about whether to call the addition() from class A or class B

Java interfaces

Java interfaces are used to achieve abstraction, which is one of the principles in Object-Oriented Programming. The Java interface method does not have a Java body. It is also used to implement Multiple Interfaces in the Java language. In other words, interfaces are the methods and variables. They cannot have a method definition or body.

 

Syntax of Java Interfaces.

interface { // declare constant fields // declare methods that abstract // by default. }

Syntax of implementing multiple interfaces

class MyClass implements Interface1, Interface2, Interface3 { // class body }

Why is Multiple Inheritance Not Supported in Java?

In Java, Multiple Inheritance is not supported by default due to several ambiguity issues. Let’s find out “why this happened with Java”.

 

  • Diamond Problem: One of the well-known issues in Multiple Inheritance is the diamond problem. This problem occurs when a class inherits from two classes that have a common method. If both parent classes have the same method with the same name and the same parameters. Then the java interpreter can’t determine which method to call in the child class.

 

  • Complexity: Multiple Inheritance creates more complex problems and hierarchies, making it harder to understand and maintain code. Java focuses on keeping the language simple and easy to learn, so it does not include features like multiple inheritances that can increase complexity.

 

  • Ambiguity: Allowing multiple inheritance can lead to ambiguity in method resolution. If a subclass inherits methods from two superclasses with the same parameter but different implementations, it is unclear if implementation occurs. This ambiguity can cause errors and make code hard to reason about.

An Alternative to Multiple Inheritance in Java

So, we know that Java programming language does not support Multiple Inheritance in Java. Let’s find an Alternative Approach to this approach.

 

We can use an interface in Java to implement Multiple Inheritance. A Java Interface is a blueprint of a class that contains abstract methods.

 

The following program demonstrates the Multiple Inheritance in Java using the interfaces.

 

  • In the code below, we have declared two methods, which is addition().
  • The interfaces First and Second define both methods, addition() and addition2().

 

Program

interface A { public abstract void addition(int a, int b); } interface B { public abstract void addition2(int x, int y); } class C implements A,B { public void addition(int a, int b) {  System.out.println("Addition = "+(a+b)) ; } public void addition2(int a,int b) { System.out.println("Addition = "+(a+b)  ); } } public class Main { public static void main(String[] args) { C obj = new C(); obj.addition(30,40); obj.addition2(50,70); } }

Output

Addition = 70 Addition = 120

By seeing the previous program, we can say that in the Java programming language. We can implement two or more interfaces in the same subclass or child class. Which clearly supports It as an alternative approach to multiple Inheritance in  Java language.

 

Let’s create another example for clarity where two interfaces contain the same method with the same parameters.

 

The following program has two interfaces, A and interface B, along with an implementation of class C and Main class and interface A and interface B contain the same method, which is addition()

 

Program

interface A { public void addition(int num1); } interface B { public void addition(int num1); } class C implements A, B { public void addition(int num1) { System.out.println("Output = "+num1); } } public class Main { public static void main(String[] args) { C obj = new C(); obj.addition(125); } }

Output

Output = 125

Conclusion

Java does not directly support multiple Inheritance of classes. However, Java developers can achieve the same thing using the Java language’s interfaces. This allows for achieving some level of code reuse and polymorphism without introducing complexity in the source code. This approach avoids conflicts and keeps code organised and manageable.

FAQs
Java avoids multiple inheritances to avoid the diamond problem, where conflict arises when you implement multiple superclasses in a single subclass. This helps the developer maintain code quality and simplicity.
Using the Java interfaces, we can achieve multiple inheritance in Java. A class can inherit multiple behaviours from each interface, effectively achieving multiple inheritance.
Multiple inheritance can lead to complex problems, hierarchies, code ambiguity, and maintenance issues. That’s why Java avoids multiple inheritance of classes, which helps prevent these problems and encourages clearer, more manageable code.
Multiple inheritance means that a subclass can inherit from two or more superclasses. However, in Java, we can only inherit one class at a time using the extends keyword.
Java supports four types of inheritance: single, multilevel, hierarchical, and hybrid. However, to avoid complexity and simplify design, Java does not support multiple inheritance with classes. However, we can achieve multiple inheritance in Java using interfaces.

Updated on August 14, 2024

Link
left dot patternright dot pattern

Programs tailored for your success

Popular

Management

Data Science

Finance

Technology

Future Tech

Upskill with expert articles

View all
Hero Vired logo
Hero Vired is a leading LearnTech company dedicated to offering cutting-edge programs in collaboration with top-tier global institutions. As part of the esteemed Hero Group, we are committed to revolutionizing the skill development landscape in India. Our programs, delivered by industry experts, are designed to empower professionals and students with the skills they need to thrive in today’s competitive job market.
Blogs
Reviews
Events
In the News
About Us
Contact us
Learning Hub
18003093939     ·     hello@herovired.com     ·    Whatsapp
Privacy policy and Terms of use

|

Sitemap

© 2024 Hero Vired. All rights reserved