Monday, 12 February 2018

Implementing two interfaces in a class with same method.

Two Interfaces With Same Method

Here is an example that shows a class implementing multiple interfaces with the same method.

If a type implements two interfaces, and each interface define a method that has identical signature, then in effect there is only one method, and they are not distinguishable.

interface I1 {
  abstract void method();
}
 
interface I2 {
  abstract void method();
}
 
public class MultipleInterfaces implements I1, I2 {
 
  @Override
  public void method() {
    System.out.println("hello world");
  }
 
  public static void main(String[] a) {
    MultipleInterfaces mi = new MultipleInterfaces();
    I1 i1 = new MultipleInterfaces();
    I2 i2 = new MultipleInterfaces();
    i1.method();
    i2.method();
  }
 
}
Output : $ java MultipleInterfaces
hello world
hello world

No comments:

Post a Comment