Interface Implementation
Method Overriding
Multiple Interfaces
Programming Concepts
Object-Oriented Programming

Implementing two interfaces in a class with same method. Which interface method is overridden?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

In object-oriented programming, particularly in languages like Java, interfaces are used to define a contract or a guideline that classes can implement. An interface can be thought of as a collection of abstract methods which any class implementing the interface must override. Challenges arise when a class needs to implement two or more interfaces that contain methods with the same name. This situation leads us to discuss how such conflicts are resolved and what it means for the class implementing these interfaces.

Understanding Interface Collision

When two interfaces contain methods with the same signature (name plus parameters), and a single class chooses to implement both interfaces, the class faces the method collision issue. Given the nature of interfaces, both methods essentially declare the same contract, but there could be ambiguity about what action to take if the expectations of each interface are different. Let's look closely at how this issue is resolved in practice.

Resolution in Implementation

When a class implements two interfaces that both declare the same method, it only needs to provide a single implementation of that method. From a technical perspective, the class is considered to be overriding the method for both interfaces simultaneously. There is no distinction between one interface's method or the other - you effectively satisfy the requirements of both interfaces with a single overriding method.

Here's an example for clarity:

java
1interface FirstInterface {
2    void performAction();
3}
4
5interface SecondInterface {
6    void performAction();
7}
8
9class ImplementingClass implements FirstInterface, SecondInterface {
10    public void performAction() {
11        // Implementation goes here.
12        System.out.println("Performing action executing single method for both interfaces.");
13    }
14}

In the above scenario, ImplementingClass implements both FirstInterface and SecondInterface. Both interfaces define a method named performAction, yet ImplementingClass needs only one implementation for performAction to satisfy both.

Technical Nuance in Java: The default Methods in Interfaces

With the introduction of default methods in Java 8, interfaces can provide an actual implementation that can be inherited by classes that implement the interface. Now, let's imagine both interfaces have default implementations:

java
1interface FirstInterface {
2    default void performAction() {
3        System.out.println("Action from FirstInterface");
4    }
5}
6
7interface SecondInterface {
8    default void performAction() {
9        System.out.println("Action from SecondInterface");
10    }
11}

When a class implements both these interfaces, it results in a compilation error unless the class provides its own implementation for performAction. This is because Java cannot resolve on its own which default method to inherit, given the conflict.

Summary Table

FeatureDescription
Single Interface Method CollisionClass must override all method occurrences from different interfaces.
Java Default MethodsAmbiguity must be resolved by the class implementing the interfaces.
Method Name ClashHandled by class implementing a single method.
ImplementationOne implementation can satisfy all interfacing requirements for same method.

Further Considerations

Design Implications

When designing interfaces and classes, especially in a large project or a collaborative environment, careful consideration is required to manage potential method name clashes in interfaces. A good strategy is to clearly document the expected behaviour of methods if they are meant to be included in multiple interfaces.

Handling Complexity

Tools like interface segregation and ensuring low coupling can reduce the issues of method collision in interfaces. Making interfaces more specialized and avoiding large, generic interfaces can also help.

Practical Applications

In real-world applications, you might find common method names across interfaces quite frequently. Implementing careful design and respecting principles like SOLID can help manage these effectively.

Understanding and managing method collisions is crucial for implementing clean and effective object-oriented designs and helps in creating systems that are scalable, robust, and maintainable.


Course illustration
Course illustration

All Rights Reserved.