Skip to content

Multiple Interfaces

In Java, a class can implement multiple interfaces, enabling it to inherit the behavior of multiple types. This is Java’s way of supporting multiple inheritance, as Java does not allow a class to extend more than one class directly.


Key Concepts of Multiple Interfaces

  1. Class Implements Multiple Interfaces:
    1. A class can implement multiple interfaces by separating their names with commas in the implements clause.
  2. No Conflicts in Implementation:
    1. If two interfaces have methods with the same name and signature, the class implementing them must provide a single implementation for that method.
  3. Default Methods:
    1. If two interfaces have conflicting default methods, the implementing class must override and provide its own implementation.

Syntax

interface InterfaceA {

    void methodA();

}

interface InterfaceB {

    void methodB();

}

class ClassName implements InterfaceA, InterfaceB {

    public void methodA() {

        System.out.println(“Implementing methodA”);

    }

    public void methodB() {

        System.out.println(“Implementing methodB”);

    }

}


Example: Class Implementing Multiple Interfaces

interface Printable {

    void print();

}

interface Showable {

    void show();

}

class Document implements Printable, Showable {

    public void print() {

        System.out.println(“Printing document…”);

    }

    public void show() {

        System.out.println(“Showing document…”);

    }

}

public class Main {

    public static void main(String[] args) {

        Document doc = new Document();

        doc.print();

        doc.show();

    }

}

Output:

Printing document…

Showing document…


Handling Conflicts in Default Methods

When two interfaces have default methods with the same name and signature, the implementing class must resolve the conflict by overriding the method.

Example: Conflict in Default Methods

interface A {

    default void show() {

        System.out.println(“Default method in Interface A”);

    }

}

interface B {

    default void show() {

        System.out.println(“Default method in Interface B”);

    }

}

class TestClass implements A, B {

    // Resolving the conflict

    public void show() {

        System.out.println(“Overriding conflicting method in TestClass”);

    }

}

public class Main {

    public static void main(String[] args) {

        TestClass obj = new TestClass();

        obj.show();

    }

}

Output:

Overriding conflicting method in TestClass


Using Multiple Interfaces in Real-World Applications

Example: Combining Behaviors

interface Animal {

    void eat();

}

interface Pet {

    void play();

}

class Dog implements Animal, Pet {

    public void eat() {

        System.out.println(“Dog is eating”);

    }

    public void play() {

        System.out.println(“Dog is playing”);

    }

}

public class Main {

    public static void main(String[] args) {

        Dog dog = new Dog();

        dog.eat();

        dog.play();

    }

}

Output:

Dog is eating

Dog is playing


Advantages of Multiple Interfaces

  1. Flexibility:
    1. A class can combine behaviors from multiple sources.
  2. Decoupling:
    1. Promotes modular and reusable code by separating concerns.
  3. Polymorphism:
    1. Allows a class to be referred to as multiple types.

Comparison to Multiple Inheritance in Classes

FeatureMultiple Inheritance in ClassesMultiple Interfaces in Java
Method ImplementationInherits concrete methods from parent classes.Requires implementing all methods in interfaces.
Conflict ResolutionAmbiguity in inheritance can occur.Explicit implementation resolves conflicts.
UsageNot supported directly in Java.Supported using implements keyword.

Best Practices

  1. Avoid Overloading with Too Many Interfaces:
    1. Implement only necessary interfaces to keep the class manageable.
  2. Handle Conflicts Properly:
    1. Use overriding to resolve conflicts in default methods.
  3. Use Interface Segregation:
    1. Split large interfaces into smaller, more focused ones (Interface Segregation Principle).

Conclusion

Implementing multiple interfaces in Java allows for flexible and modular design, enabling a class to inherit multiple behaviors. It avoids the pitfalls of multiple inheritance in classes while ensuring that the implementing class adheres to the contracts defined by the interfaces. This feature is essential for achieving polymorphism and loose coupling in object-oriented programming.