Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Multiple Inheritance Program in Java

Last Updated on January 9, 2023 by Prepbytes

In this article, we will learn what is multiple inheritance, why multiple inheritance program in java is not supported, and how we can achieve multiple inheritance in java through an interface. We will also take the relevant example of multiple inheritance in java.

What is multiple inheritance?

Multiple inheritance is one of the important features of Object Oriented Programming (OOP). When a single-child class tries to inherit the properties of the multiple parent class it is called multiple inheritance.

In the above image, we can see that the subclass ‘class3’ tries to inherit properties of both the parent classes ‘class1’ and ‘class2’. The problem arises when both parent classes have the same methods then the compiler gets confused about which method to call.

Important terms used in multiple inheritance:

  • Parent Class: A parent class is a class from which another class (Child Class) acquires all the properties of the parent class. It is also called a Super class or Base class.

  • Child Class: A child class is a class that acquires all the properties from its parent class. It is also called as Sub class or extended class.

In the above image, we can see that we have one parent class named College with a property name and a child class named Branch with property total seats. Let’s write an inheritance program in java using the above example.

Why Multiple Inheritance program in java is not supported?

Multiple inheritance program in java is not possible to reduce the complexity and make the language more simple. Let’s understand multiple inheritance program in java is not supported using a couple of examples.

Example 1:

In the above image, we can see that we have two parent classes “Class A” and “Class B” and both have the same method test now if we extend both the parent class in the same child class then the question arises that which test method to call in child class. Let’s understand further why this inheritance program in java is not possible.

// multiple inheritance program in java
class Car{
    public void test(){
        System.out.println("Testing a car");
    }
}
class Bike{
    public void test(){
        System.out.println("Testing a bike");
    }
}

class PrepBytes extends Car,Bike{
    public static void main(String[] args) {
        PrepBytes pb= new PrepBytes();
        pb.test();
    }
}

Output:

Main.java:13: error: '{' expected
class PrepBytes extends Car,Bike{
                           ^
1 error

In the above program, we have created two parent classes Car and Bike and both have the same method test. When we extend both the classes in a single child class Prepbytes problem rise when we create an object of the child class PrepBytes and we call the method test compiler to get confused to call the test method of which parent class.

Example 2:

In the above image, we have one superclass ‘Class A’ which is extended by two subclasses ‘Class B’ and ‘Class C’. Another class ‘Class D’ tries to inherit two classes ‘Class B’ and ‘Class C’. Let’s try to write multiple inheritance program in java for the above example.

// multiple inheritance program in java
class Vehicle{
    public void test(){
        System.out.println("Testing a vehicle");
    }
}

class Car extends Vehicle{
    public void test(){
        System.out.println("Testing a car");
    }
}
class Bike extends Vehicle{
    public void test(){
        System.out.println("Testing a bike");
    }
}

class PrepBytes extends Car,Bike{
    public static void main(String[] args) {
        PrepBytes pb= new PrepBytes();
        pb.test();
    }
}

Output:

Main.java:19: error: '{' expected
class PrepBytes extends Car,Bike{
                           ^
1 error

In the above program, first, we have created a parent class Vehicle with a method test. we have created two classes Car and Bike which inherit the parent class Vehicle and both have the same method test. When we extend both the classes in a single child class Prepbytes problem rise when we create an object of the child class PrepBytes and we call the method test compiler to get confused to call the test method of which parent class.

How to achieve multiple inheritance in java using the interface and default method:

We can achieve multiple inheritance in java using the interface and default method after it was introduced in java 8. After this feature, a class can inherit two or more than two parent classes with the same methods. We can also override the superclass method in the subclass. Let’s see how to achieve multiple inheritance in java using a couple of examples.

Example 1:

// multiple inheritance program in java

interface Car{
    default void test(){
        System.out.println("Testing a car");
    }
}
interface Bike{
    default void test(){
        System.out.println("Testing a bike");
    }
}

class PrepBytes implements Car,Bike{
    @Override
    public void test(){
        Car.super.test();
        Bike.super.test();
    }
    
    public static void main(String[] args) {
        PrepBytes pb= new PrepBytes();
        pb.test();
    }
}

Output:

Testing a car
Testing a bike

In the above example, first, we created two interfaces Car and Bike with the same default method test. In the child class Prepbyte, we have overridden the test method and inside it, we called both methods using the super keyword. In the output, we can see that the methods of both interfaces are called in the sub-class.

Example 2:

// multiple inheritance program in java

interface Vehicle{
    default void test(){
        System.out.println("Testing a vehicle");
    }
}
interface Car extends Vehicle{
}
interface Bike extends Vehicle{
}

class PrepBytes implements Car,Bike{
    
    public static void main(String[] args) {
        PrepBytes pb= new PrepBytes();
        pb.test();
    }
}

Output:

Testing a vehicle

Conclusion
In the above example, first, we have created an interface Vehicle with the method test. After that, we extended an interface Vehicle into two interfaces a Car and a Bike. In the child class Prepbyte, we have created an object of class Prepbytes and called the method test. In the output, we can see that the method of Vehicle interface is called in the sub-class.

Other Java Programs

Java Program to Add Two Numbers
Java Program to Check Prime Number
Java Program to Check Whether a Number is a Palindrome or Not
Java Program to Find the Factorial of a Number
Java Program to Reverse a Number
Java Program to search an element in a Linked List
Program to convert ArrayList to LinkedList in Java
Java Program to Reverse a linked list
Java Program to search an element in a Linked List
Anagram Program in Java
Inheritance Program in Java
Even Odd Program in Java
Hello World Program in Java
If else Program in Java
Binary Search Program in Java
Linear Search Program in Java
Menu Driven Program in Java
Package Program in Java
Leap Year Program in Java
Array Programs in Java
Linked List Program in Java
String Programs in Java
Star Program in Java
Number Pattern Program in Java
For Loop Program In Java
Pattern Program in Java
String Palindrome Program in Java
Thread Program in JAVA
Java Scanner Program
While Loop Program in Java
Bubble Sort Program in Java
Fibonacci Series Program in Java
Calculator Program in Java
Fizzbuzz Program in Java
Matrix Program in Java
Stack Program in Java
Recursion Program in Java

Leave a Reply

Your email address will not be published. Required fields are marked *