Why multiple inheritance is not allowed in Java
In this article, we are going to have an introduction to inheritance, its types, and why exactly multiple inheritance is not allowed in Java.
What is inheritance?
Inheritance is the ability of one class to inherit the properties of another class. Take one real-world scenario, you have inherited your parent’s characteristics, this is the most basic understanding of the term inheritance. Just like you have inherited from your parents, your parents also have inherited from their parents, and here is where the type of inheritance
comes into play.
Inheritance is one of the most important concepts of Object Oriented Programming (OOPs). Suppose class X inherits from class Y, then class X becomes the child class and class Y becomes the parent class.
There are other names for child class like subclass or derived class, and for class Y(parent class), we have superclass or base class.
By inheriting, we mean, inheriting the properties i.e. object of the child class can now access the methods and fields of the parent class and also it can override them.
Types of inheritance
Single Inheritance
When one class inherits from another class, then this is called ‘single inheritance’. For eg- Dog
class inherits from Animal
class. So, here you can observe only one-way inheritance is happening, from Animal
class to Dog
. This is the most basic type of inheritance.
Multiple Inheritance
When you as a class try to inherit from various other classes then this is called as multiple inheritance
. For eg- Bat
class comes under Mammal
as well as Winged Animal
class.
In this class A will inherit from class B and class C. But this isn’t supported in Java. (We’ll learn about the reason later on in this article)
Multilevel Inheritance
When class A derives from class B that derives from class C, then this is called multilevel inheritance. In this type of inheritance, you try to derive from a class that is already derived from another class.
Hierarchical Inheritance
In this type of inheritance, we see numerous subclasses deriving from a single class. So, they all have common features related to the superclass.
Hybrid Inheritance
This type of Inheritance is a combination of single
and multiple
inheritance. Since multiple inheritance
is not allowed in Java, hybrid inheritance
is also not allowed in Java (we’ll get to know why in the later section).
Why is multiple inheritance not allowed in Java?
In C++, multiple inheritance
is allowed, but not in Java, because multiple inheritance
results in ambiguity and Java doesn’t allow ambiguity. Now you must be wondering, how exactly multiple inheritance causes ambiguity. Say, for example, you have classes cricket
, baseball
, and both have a method called hit()
. You made one another class ball
that inherits both cricket
and baseball
. Now if you create one object of the class ball()
and call the method hit()
, then the Java compiler will get confused about which method to inherit from which class and this causes ambiguity. So to avoid this confusion, this type of inheritance is not allowed in Java.
How to achieve Multiple Inheritance in Java?
Though direct multiple inheritance
is not allowed in Java, they’ve provided us with a way to achieve it, which is via interfaces
.Interface
is a blueprint of a class that consists of static constants and abstract methods. A method that is declared with an abstract keyword and does not have any implementation is known as an abstract method. No method body is allowed in an interface, only abstract methods are allowed. Interfaces help us achieve abstraction and multiple inheritance.
This is not allowed in Java –
public class Bat extends Mammal, WingedAnimal{}
Code language: Java (java)
We can do something like this, where we can make a class extend one or more interfaces, hence helping us achieve multiple inheritance.
// interface example
interface Vijay {
public void walk(); // interface method (does not have a body)
public void run(); // interface method (does not have a body)
}
Code language: Java (java)
You need to use implements
keyword, if you want to inherit an interface in a class. When you want to inherit interface
in an interface, use extends
keyword.
So the key points are –
- class
extends
another class - class
implements
an interface - an interface
extends
another interface
//interface 1
interface Student1
{
//interfaces method are only declared not defined
public void walk();
}
//interface 2
interface Student2
{
//any number of methods can be declared in the interface
public void walk();
public void run();
}
//multiple inheritance achieved
class Student3 implements Student1,Student2
{
//overidden methods
public void walk()
{
System.out.println("Student1 is walking ");
}
public void run()
{
System.out.println("Student2 is running ");
}
public static void main (String args[])
{
Student3 object = new Student3();
object.walk();
object.run();
}
}
Code language: Java (java)
#Output-
Student1 is walking
Student2 is running
Code language: Java (java)
We saw how to achieve multiple inheritance via interfaces
. We declared methods in interfaces called Student1
and Student2
. These interfaces were overridden in class Student3
, during the implementation. Overriding means, we define an already declared method with the same name, return type, and parameters. We have re-implemented walk()
and run()
methods in our child class, Student3. When these methods will be called by the object of the child, no ambiguity will be there and we’ll get our desired output.
Conclusion
Through this article, we discussed inheritance, what are its types, why multiple inheritance
is not allowed in Java, and how to achieve it through interfaces
.
I hope you have understood the concept!
Thanks for reading!
Sharing is caring
Did you like what Vineeta Tiwari wrote? Thank them for their work by sharing it on social media.
No comments so far
Curious about this topic? Continue your journey with these coding courses: