Junior — Middle
How to implement two interfaces with methods having the same signatures in one class?
sobes.tech AI
Answer from AI
In Java, a class can implement multiple interfaces, even if they contain methods with the same signatures. In this case, it is sufficient to implement one method that satisfies both interfaces. For example:
interface InterfaceA {
void doSomething();
}
interface InterfaceB {
void doSomething();
}
public class MyClass implements InterfaceA, InterfaceB {
@Override
public void doSomething() {
System.out.println("Implementation of the method for both interfaces");
}
}
If the interfaces contain methods with the same name but different default implementations, then the class must explicitly override this method and choose the desired implementation.