Interface naming conflicts

Interface naming conflicts
1. Method naming conflicts

Case 1

If two interfaces contain a method with same signature and same return type in the implementation class we can provide implementation for only one method.

interface Left {
   public void m1();
}

interface Right {
   public void m1();
}

class Test implements Left, Right {
   public void m1() {
   }
}

Case 2

If two interfaces contain a method with same signature but different arguments then in the implementation class we can provide implementation for both methods and these methods are considered as overloaded methods.

E.g

interface Left {
   public void m1();
}

interface Right {
   public void m1(int i);
}

class Test implements Left, Right {
   public void m1() {
   }

   public void m1(int i) {
   }
}

Case 3

If two interfaces contains a method with same signature but different return types. Then it is impossible to implement both interfaces at a time.

interface Left {
   public void m1();
}

interface Right {
   public int m1();
}

We cannot write any java class which implements both interfaces simultaneously.

2. Variable naming conflicts
interface Left {
   int x = 888;
}

interface Right {
   int x = 999;
}

class Test implements Left, Right {
   public static void main(String args[]) {
      System.out.println(x);
   }
}
C.E: Reference to x is ambiguous

There may be a chance of 2 interfaces contains available with same name and may rise variable naming conflicts but we can resolve these naming conflicts using interface names

System.out.println(Left.x);
System.out.println(Right.x);
Interface naming conflicts
Scroll to top