Table of Contents
Multiple Inheritance is a feature of an object-oriented concept, where a class can inherit properties of more than one parent class. The problem occurs when there exist methods with the same signature in both the superclasses and subclass. On calling the method, the compiler cannot determine which class method to be called and even on calling which class method gets the priority.
To know more about Java, Download Entri App!
The idea behind inheritance in Java is that you can create new classes
that are built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of the parent class. Moreover, you can add new methods and fields in your current class also.
Terms used in Inheritance
- Class: A class is a group of objects which have common properties. It is a template or blueprint from which objects are created.
- Sub Class/Child Class: Subclass is a class which inherits the other class. It is also called a derived class, extended class, or child class.
- Super Class/Parent Class: Superclass is the class from where a subclass inherits the features. It is also called a base class or a parent class.
- Reusability: As the name specifies, reusability is a mechanism which facilitates you to reuse the fields and methods of the existing class when you create a new class. You can use the same fields and methods already defined in the previous class.
Learn Coding in your Language! Enroll Here!
Types of inheritance in java
1: What is the default value of a boolean in Java?
On the basis of class, there can be three types of inheritance in java: single, multilevel and hierarchical.
In java programming, multiple and hybrid inheritance is supported through interface only. We will learn about interfaces later.
When one class inherits multiple classes, it is known as multiple inheritance. For Example:
Single Inheritance
When a class inherits another class, it is known as a single inheritance. In the example given below, Dog class inherits the Animal class, so there is the single inheritance.
File: TestInheritance.java
- classAnimal{
- void eat(){System.out.println(“eating…”);}
- }
- class Dog extends Animal{
- voidbark(){System.out.println(“barking…”);}
- }
- classTestInheritance{
- public static void main(String args[]){
- Dog d=newDog();
- d.bark();
- eat();
- }}
Output:
barking…eating…
Multilevel Inheritance
When there is a chain of inheritance, it is known as multilevel inheritance. As you can see in the example given below, BabyDog class inherits the Dog class which again inherits the Animal class, so there is a multilevel inheritance.
File: TestInheritance2.java
- classAnimal{
- void eat(){System.out.println(“eating…”);}
- }
- class Dog extends Animal{
- voidbark(){System.out.println(“barking…”);}
- }
- classBabyDog extends Dog{
- void weep(){System.out.println(“weeping…”);}
- }
- class TestInheritance2{
- publicstatic void main(String args[]){
- BabyDog d=new BabyDog();
- weep();
- d.bark();
- eat();
- }}
Output:
weeping…barking…eating…
Hierarchical Inheritance
When two or more classes inherits a single class, it is known as hierarchical inheritance. In the example given below, Dog and Cat classes inherits the Animal class, so there is hierarchical inheritance.
File: TestInheritance3.java
- classAnimal{
- void eat(){System.out.println(“eating…”);}
- }
- class Dog extends Animal{
- voidbark(){System.out.println(“barking…”);}
- }
- classCat extends Animal{
- void meow(){System.out.println(“meowing…”);}
- }
- class TestInheritance3{
- publicstatic void main(String args[]){
- Cat c=new Cat();
- meow();
- c.eat();
- //c.bark();//C.T.Error
- }}
Output:
meowing…eating…
Learn Coding in your Language! Enroll Here!
Multiple Inheritance
Java does not support multiple inheritance. Multiple inheritance means a class derived from more than one direct super class. This increases complexities and ambiguity in the relationship among classes. The problem is clearly visible if we consider what happens in function overriding. Suppose there are two classes, A and B, each defining a function called func(). Now, let’s say we define another class, C, which inherits both from A and B (multiple inheritance), but let’s say this class does not override the function called func().
Now, if we do something like the following:
C c = new C();c.func();
Can we determine which member function func() is invoked? Is it function defined by class A or class B? As we can see, the class C inherits this function doubly from both A and B. This creates ambiguity and the compiler is in a fix to resolve the issue. There is solution to this problem, but it’s extremely complex; therefore, Java decided to stay away from the cause of the problem by not allowing multiple inheritance.
Multiple Inheritance with Interfaces
Although Java does not allow multiple inheritance, it allows a multiple implementation of interfaces. So, in a way the idea is still there. Now, what is an interface?
An interface describes a set of methods but does not provide any concrete implementation for all methods. We can implement the interface with the help of a class which provides the concrete implementation of the method. In this way, a class can implement more than one interface and therefore can provide a concrete implementation of methods derived from one or more interfaces. The class that implements one or more interfaces forms a is-a relationship with the interface type. This also means that objects instantiated from the class are guaranteed to provide the functionality declared by the interface. Any subclass derived from this class also provides the same functionality.
Interfaces are especially useful for providing a common functionality to a number of possibly unrelated classes. Therefore, the object of classes that implement same interface can respond to method calls described in all of the interfaces.
From Java 8, interfaces support default methods with its full implementation. As we know, a class can implement more than one interface; therefore, if multiple interfaces contain a default method with the same method signature, the implemented class should specify which particular method to use or override.
Example
package com.mano.examples;
public interface Interface1 {
default void func(){
System.out.println(“from interface 1”);
}}
package com.mano.examples;public interface Interface2 {
default void func(){
System.out.println(“from interface 2”);
}}
package com.mano.examples;public class MyClass implements Interface1, Interface2 {
public void func(){ // Invoking Interface1 func() method
Interface1.super.func(); // Invoking Interface2 func() method Interface2.super.func();
}
public static void main(String[] args){
MyClass c=new MyClass(); c.func();
}
Why multiple inheritance is not supported in java?
To reduce the complexity and simplify the language, multiple inheritance is not supported in java.
Consider a scenario where A, B, and C are three classes. The C class inherits A and B classes. If A and B classes have the same method and you call it from child class object, there will be ambiguity to call the method of A or B class.
Since compile-time errors are better than runtime errors, Java renders compile-time error if you inherit 2 classes. So whether you have same method or different, there will be compile time error.
- classA{
- void msg(){System.out.println(“Hello”);}
- }
- class B{
- voidmsg(){System.out.println(“Welcome”);}
- }
- classC extends A,B{//suppose if it were
- public static void main(String args[]){
- C obj=new C();
- msg();//Now which msg() method would be invoked?
- }
- }
Output:
Compile Time Error
Learn Coding in your Language! Enroll Here!
One of the classic problems with multiple inheritance is called the diamond problem. This can be tackled with the inheritance mechanism called virtual inheritance. But, experience says Java has not lost much by disallowing multiple inheritance altogether. In fact, the Java compiler has gotten rid of them by providing a complex solution to a problem that we can easily do away with. Still, there are OOP languages, such as C++, which support it.
Why is it important to choose Entri?
- Excellent online platform for all the Competitive Exams.
- Provides updated materials created by the Entri Experts.
- Entri provides a best platform with full- length mock tests including previous year question papers.
- You can download the app for free and join the required classes.
- Entri wishes you all the best for your examinations and future endeavours.
“YOU DON’T HAVE TO BE GREAT TO START, BUT YOU HAVE TO START TO BE GREAT.”