13
SepWhat is Multiple Inheritance in Java with Example
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?".
> Top Java Interview Questions And Answers > Java Multithreading Interview Questions |
What is Multiple Inheritance?
- Multiple inheritanceis 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 laterin this article.
- Before that, we will see how other types of inheritance support Java.
Types of Inheritance in Java:
There are more 4 types of Inheritance in Java except Multiple inheritance
- Single inheritance in Java.
- Multi-level inheritance in Java.
- Hierarchical Inheritance in Java.
- Hybrid Inheritance in Java.
1. Single Inheritance in Java:
- 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 Java.
- 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:
3. Hierarchical Inheritance in Java.
- 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 in Java.
- 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.
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.
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.