Tuesday, November 19, 2024
Google search engine
HomeLanguagesJavaInterface Naming Conflicts in Java

Interface Naming Conflicts in Java

Interfaces in Java consist of abstract methods (which do not contain a body) and variables (which are public static final). Implementation of the methods of the interface is defined in classes that implement that interface. It helps Java to achieve abstraction. 

Naming Conflicts occur when a class implements two interfaces that have methods and variables with the same name.

Interface Naming Conflicts

As Interface consists of variables and methods, So two types of naming conflicts can happen.

  1. Method Naming Conflict
  2. Variable Naming Conflict

1. Method Naming Conflict

This method naming conflicts can happen in various cases. 

Case-1: When we have two interfaces with the same method name, same signature, same return type, the implementation class can implement any one of them, and we can’t say which one of these implemented.

Code

Java




public interface Interface1 {
 
    // create a method
    public void show();
}


Java




public interface Interface2 {
 
    // create a method
    public void show();
}


Java




public class Case1 implements Interface1, Interface2 {
    // implement the methods of interface
    public void show()
    {
        System.out.println("Geeks For Geeks");
    }
    public static void main(String[] args)
    {
        // create object
        Case1 obj = new Case1();
        // using object call the implemented method
        obj.show();
    }
}


Output 

Geeks For Geeks

Explanation: Here in the below code, we can’t confirm which show a method of 2 interfaces gets executed. No error will be thrown.

Case-2: When two interfaces consist of methods with the same name but different signatures, then in the implementation class, we need to implement both the methods and methods get executed based on the kind of method we called. 

Code 

Java




public interface Interface1 {
 
    // create a method
    public void show();
}


Java




package geeks;
 
public interface Interface2 {
    // define an abstract method with parameters
    public void show(String s);
}


Java




public class Case2 implements Interface1, Interface2 {
    // implement method of Interface2
    @Override public void show(String s)
    {
        System.out.println(s);
    }
    // implement method of Interface1
    @Override public void show()
    {
        System.out.println("Geeks For Geeks");
    }
 
    public static void main(String[] args)
    {
 
        // create object
        Case2 obj = new Case2();
        // calling methods
        obj.show();
        obj.show("GFG");
    }
}


Output 

Geeks For Geeks
GFG

Explanation: In this case, the execution of these methods can be differentiated based on the signature, and return type is ignored in this case.

Case-3: When two interfaces contain a method with the same name, same signature but different return types, in this case, both interfaces can’t be implemented in the same class. Separate classes need to be created to implement each interface of that type.

Code 

Java




public interface Interface1 {
 
    // create a method
    public void show();
}


Java




public interface Interface2 {
    // create method with same name & signature but
    // different return type
    public String show();
}


Java




// implement only 1 interface
public class Case3_1 implements Interface1 {
    // override method of that interface
    @Override public void show()
    {
        System.out.println("Geeks for Geeks");
    }
 
    public static void main(String[] args)
    {
        // create object
        Case3_1 obj = new Case3_1();
        // calling method
        obj.show();
    }
}


Output 

Geeks for Geeks

Explanation: In this case, we can’t implement both interfaces in the same class because an error can be thrown due to ambiguity between them. So we need to create a different class to implement Interface2.

Other Examples: 

Java




// implement only 1 interface
public class Case3_2 implements Interface2 {
    // override the method of interface
    @Override public String show() { return "GFG"; }
 
    public static void main(String[] args)
    {
        // object creation
        Case3_2 obj = new Case3_2();
        // calling method
        String res = obj.show();
        // printing returned result
        System.out.println(res);
    }
}


Output 

GFG

If we try to implement both interfaces in the same class then, it will throw an error.

Code (Throws error)

Java




public class Case3_1_2 implements Interface1, Interface2 {
 
    public void show()
    {
        System.out.println("Geeks for Geeks");
    }
    @Override public String show()
    {
        String s = "GFG" return s;
    }
 
    public static void main(String[] args)
    {
        Case3_1_2 obj = new Case3_1_2();
        obj.show();
    }
}


Output 

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    The return type is incompatible with Interface2.show()
    Duplicate method show() in type Case3_1_2
  • As we implemented methods having the same signature and names with a different return type of both the interfaces in the same class throws an error.
  • It is impossible to implement these in the same class.

Now we will move into another type of interface naming conflicts

2. Variable Naming Conflict

When two interface consists of the variable with the same name, then the class which implements those interfaces can’t identify which variable to access and throws an error. Hence, to resolve that, access the variable using the interface name as the reference.

Code

Java




public interface Interface1 {
    String s = "Geeks for Geeks";
}


Java




public interface Interface2 {
    String s = "GFG";
}


Java




// implement the interfaces
public class VariableConflict
    implements Interface1, Interface2 {
    public static void main(String[] args)
    {
        // create object
        VariableConflict obj = new VariableConflict();
       
        // if we print the data in variable with out
        // specifying reference of interface it throws error
        // System.out.println(s);  error
        System.out.println(Interface1.s);
        System.out.println(Interface2.s);
    }
}


Output

Geeks for Geeks
GFG

Explanation: Here, if we don’t use of reference of interface name while accessing strings s it throws an ambiguous reference error. So using the Interface name as a reference prevents ambiguity while accessing. 

RELATED ARTICLES

Most Popular

Recent Comments