Thursday, December 11, 2025
HomeLanguagesJavaHow to Implement Multiple Inheritance by Using Interfaces in Java?

How to Implement Multiple Inheritance by Using Interfaces in Java?

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 methods with the same signature exist 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.

 

Interface is just like a class, which contains only abstract methods. In this article, we will discuss How to Implement Multiple Inheritance by Using Interfaces in Java.

Syntax:

Class super
{
---
----
}
class sub1 Extends super
{
----
----
}
class sub2 Extend sub1
{
-----
-----
}

Implementation

Multiple inheritances can be achieved through the use of interfaces. Interfaces are similar to classes in that they define a set of methods that can be implemented by classes. Here’s how to implement multiple inheritance using interfaces in Java.

Step 1: Define the interfaces

interface Interface1 {
    void method1();
}

interface Interface2 {
    void method2();
}

Step 2: Implement the interfaces in the class

public class MyClass implements Interface1, Interface2 {
    public void method1() {
        // implementation of method1
    }

    public void method2() {
        // implementation of method2
    }
}

Step 3: Create an object of the class and call the interface methods

MyClass obj = new MyClass();
obj.method1();
obj.method2();

Example:

Java




// Declare the interfaces
interface Walkable {
    void walk();
}
  
interface Swimmable {
    void swim();
}
  
// Implement the interfaces in a class
class Duck implements Walkable, Swimmable {
    public void walk()
    {
        System.out.println("Duck is walking.");
    }
  
    public void swim()
    {
        System.out.println("Duck is swimming.");
    }
}
  
// Use the class to call the methods from the interfaces
class Main {
    public static void main(String[] args)
    {
        Duck duck = new Duck();
        duck.walk();
        duck.swim();
    }
}


Output:

Duck is walking.
Duck is swimming.
RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32440 POSTS0 COMMENTS
Milvus
105 POSTS0 COMMENTS
Nango Kala
6811 POSTS0 COMMENTS
Nicole Veronica
11950 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12023 POSTS0 COMMENTS
Shaida Kate Naidoo
6942 POSTS0 COMMENTS
Ted Musemwa
7193 POSTS0 COMMENTS
Thapelo Manthata
6889 POSTS0 COMMENTS
Umr Jansen
6880 POSTS0 COMMENTS