
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to Implement Multiple Inheritance
In this article, we will understand how to implement multiple inheritance. Unlike other programming languages, such as C++, Java does not support multiple inheritance through classes. This means that a class cannot inherit from more than one class. But why? Let's understand first what multiple inheritance is and why it is so.
Multiple Inheritance
Multiple Inheritance is one of the features of object-oriented programming, where a class (child class) can inherit properties of more than one class (parent class) using the extends keyword. But Java does not support this to avoid complexity and ambiguity. It can also create a diamond problem.
Let's understand this, such as when two parent classes have methods with the same signature, on calling the method, the compiler cannot determine which class method to call and which class method should be prioritized. It creates ambiguity. This is the reason Java does not support multiple inheritance through classes.
Example
Now, understand the above learning through code -
import java.io.*; // First Parent Class class Parent1 { void fun() { System.out.println("Parent1"); } } // Second Parent Class class Parent2 { void fun() { System.out.println("Parent2"); } } // Inheriting Properties of // Parent1 and Parent2 class Childclass extends Parent1, Parent2 { // main method public static void main(String args[]) { // Creating instance of Test Childclass c = new Childclass(); Childclass.fun(); } }
On compiling, the above program gives you the following output.
ERROR! /tmp/Fawwr7VW64/Main.java:16: error: '{' expected class Childclass extends Parent1, Parent2 ^ 1 error ERROR! error: compilation failed
But we can still achieve multiple inheritance by using the interfaces. Interfaces implement the class, not inherit. Now, let's understand how the interface helps in achieving multiple inheritance.
Example
In the program below, we are achieving multiple inheritance using the abstract method in the interface -
interface Parent1 { void mother(); } interface Parent2 { void father(); } class Family implements Parent1, Parent2 { public void mother() { System.out.println("Mothers inherit"); } public void father() { System.out.println("Fathers inherit"); } } public class Demo { public static void main(String args[]) { Family a = new Family(); a.mother(); a.father(); } }
On compiling, the above program gives you the following output.
Mothers inherit Fathers inherit
Java 8 supports default methods. By using the default method implementation, a class can implement two or more interfaces. Both interfaces will have default methods with the same signature. Also, in Java, a class can extend one class and implement multiple interfaces to achieve multiple inheritance.
Using Default Methods
While we are using the interface, the function we are creating has to be a default method/function. So, while calling the function, we can prioritize which function should be called first using the super keyword, even after the function name is the same. The function should be overridden.
Example
Now, understand multiple Inheritance through default methods -
// Interface 1 interface Parent1 { default void show() { System.out.println("Default Parent1"); } } // Interface 2 interface Parent2 { default void show() { System.out.println("Default Parent2"); } } class Childclass implements Parent1, Parent2 { // Overriding default show method @Override public void show() { Parent1.super.show(); Parent2.super.show(); } // Declared new Method public void showOfParent1() { Parent1.super.show(); } // Declared new Method public void showOfParent2() { Parent2.super.show(); } // main Method public static void main(String args[]) { // Instance of Class Childclass d = new Childclass(); // Using show Method d.show(); // Executing the Methods System.out.println("Now Executing showOfParent1()" + " showOfParent2()"); d.showOfParent1(); d.showOfParent2(); } }
On compiling, the above program gives you the following output.
Default Parent1 Default Parent2 Now Executing showOfParent1() showOfParent2() Default Parent1 Default Parent2