| by Arround The Web | No comments

How to Implement Multiple Inheritance in Java?

Multiple inheritance is the concept of object-oriented programming and is a type/branch of inheritance. It works similarly to traditional inheritance such as it inherits the methods, and traits of the parent class from which it is inherited. Multiple inheritance specifically inherits the methods, properties, or functions from multiple parent classes or interfaces at once. It reduces the effort, line of code, and a lot more for the programmer.

This article demonstrates the following below-stated content:

How to Implement Multiple Inheritance in Java?

Multiple inheritances are mostly utilized by the programmer while the creation of large applications or projects. But all object-oriented programming languages like C++ and Java do not support multiple inheritance. The reason is the cause of ambiguity which is specifically named the “Diamond” problem.

Follow the below demonstration to get detailed knowledge about the Diamond problem and how to implement multiple inheritance without causing this problem.

What is the Diamond Problem?

The “Diamond” problem is the ambiguity the compiler faces during the compilation phase to find which function/method needs to be executed first. Let us consider an example of implementing multiple inheritance:

In this example, more than one class invokes the same method that is available in the “parent” class. And the compiler gets confused about which class invokes the desired function first and which invokes it after that. This causes deadlock and the program does not get compiled successfully as shown below:

import java.io.*;
class ParentClass {
 void testFun() {
  System.out.println("ParentClass");
 }
}
class FirstChild extends ParentClass {
 void testFun() {
  System.out.println("FirstChild");
 }
}
class SecondChild extends ParentClass {
 void testFun() {
  System.out.println("SecondChild");
 }
}
class useCase extends FirstChild, SecondChild{
 public static void main(String args[]) {
  useCase uc = new useCase();
  uc.testFun();
 }
}

 
Explanation of the above code:

    • First, create a parent class named “ParentClass” and inside it initialize a function named “testFun()” which is displaying dummy text.
    • Next, create a child class that is inherited with the parent class using the “extends” keyword. And override the “testFun()” function which is created in the parent class.
    • Then, create another child class that also inherits the parent class and overrides the “testFun()” function.
    • After that, create a new class named “useCase” which inherits the functionality of both child classes using the “extends” keyword. And invoke the “main()” method.
    • In the end, create an object of the “useCase” class and call the “testFun()” function.
    • As the “testFun()” function is available in both child classes, the compiler does not know from which child class the specified function needs to be called first. It causes the ambiguity named the diamond problem.

After the end of the compilation:


The output displays that the diamond problem occurs while the creation of multiple inheritance.

How to Handle Diamond Problem and Implement Multiple Inheritance in Java?

The use of “interface” helps in implementing multiple inheritance without causing the diamond problem. The interfaces are utilized due to their nature of accepting/extending more than one interface at a time.

Visit the below code for a better understanding:

interface FirstChild {
  public void testFun();
}
class SecondChild  {
 public void response(String str) {
  System.out.println(str + " can also be used as SecondChild Data.");
 }
}
class useCase extends SecondChild implements FirstChild {
 String useCase = "Interface";
 public void testFun() {
  System.out.println(useCase + " can be used as FirstChild Data.");
 }
 public static void main(String[] args) {
  useCase uc = new useCase();
  uc.testFun();
  uc.response(uc.useCase);
 }
}

 
Explanation of the above code:

    • First, implement the “interface” named “FirstChild” and declare the “testFun()” function inside it.
    • Next, create a class named “SecondChild” which has a function named “response()” that takes a parameter having a type of “String” and prints it on the console.
    • Then, create a new class named “useCase” which inherits the class and interface already created using “extends” and “implements” keywords, respectively.
    • After that, create an object of the “useCase()” class and invoke both the “testFun()” and “response()” functions in the “main()” method.

After the end of the compilation:


The output shows that multiple inheritance has been implemented.

Conclusion

Multiple inheritance is a type/branch of inheritance as it can utilize the methods and functions of the parent class. The “diamond” problem is the only issue due to which it is not accepted by OOP programming languages like C++, Java, etc. This problem can be resolved by the usage of interfaces for the implementation of multiple inheritance. This article has demonstrated the procedure to implement multiple inheritance in Java.

Share Button

Source: linuxhint.com