106.
Yes, a class can implement two interfaces. Even if both interfaces
have
a method with the same name, this does not result in an
ambiguity as the implementation provided in the class is used.
The following code demonstrates this:
public interface MyInterface1 {
public void doSomething();
}
public interface MyInterface2 {
public void doSomething();
}
public class MyClass implements MyInterface1,
MyInterface2{
@Override
public void doSomething() {
// code here
}
}
MyClass obj = new MyClass();
obj.doSomething(); //Line 1
The above code specifies two interfaces MyInterface1,
MyInterface2. Both interfaces
have a method called
doSomething. The code also specifies a class MyClass that
implements both the interfaces.
It needs to provide an
implementation for the doSomething method. When Line 1 is
executed, the doSomething() method from MyClass is
executed.
Do'stlaringiz bilan baham: