Naming Conflicts Interface

Naming Conflicts Interface

                                    Naming Conflicts Interface 


Method Naming conflicts

case 1-: If two interfaces contain the same signature and same return type then in the implementation class we have to provide an implementation for only one method.

eg-:

interface A{
    public void m();
}
interface B{
   public void m();
}

class Test implements A,B{
   public void m(){
        System.out.print("hello");
   }
}

case 2-: If two interfaces contain a method with same name but different argument types then in the implementation class we have to provide an implementation for both methods and these methods access overloaded methods.

eg-:
interface A{
    public void m();
}
interface B{
   public void m(int x);
}

class Test implements A,B{
   public void m(){
        System.out.print("hello");
   }
   public void m(int x){
      System.out.print(x);
   }
}



case 3-: If two interfaces contains a method same signature but a different return type then it is impossible to implement both interfaces simultaneously (if the return type is not covariant).

eg-:

interface left{
   public int m();
}
interface right{
   public void m();
}

we can't write any Java class which implements both interfaces simultaneously.

Oues-: Is a Java class can implement any no. of interfaces simultaneously?
Ans -: yew except for a particular case if two interfaces contain a method with the same signature but different return type then it is impossible to implement both interfaces simultaneously.



Variable Naming Conflict


Two interfaces can contain a variable with the same name and there may be a chance of variable naming conflicts but we can solve this problem by using interface names.

eg-:

interface A{
   int x=10;
}
interface B{
   int x=11;
}
class Test implements A,B{
   public static void main(String... args){
        System.out.print(x); // compile time error-:reference to x  is ambiguity
        System.out.print(A.x);  // 10
        System.out.print(B.x);  // 11
  }
}