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.
- Method Naming Conflict
- 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 {
public void show();
}
|
Java
public interface Interface2 {
public void show();
}
|
Java
public class Case1 implements Interface1, Interface2 {
public void show()
{
System.out.println( "Geeks For Geeks" );
}
public static void main(String[] args)
{
Case1 obj = new Case1();
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 {
public void show();
}
|
Java
package geeks;
public interface Interface2 {
public void show(String s);
}
|
Java
public class Case2 implements Interface1, Interface2 {
@Override public void show(String s)
{
System.out.println(s);
}
@Override public void show()
{
System.out.println( "Geeks For Geeks" );
}
public static void main(String[] args)
{
Case2 obj = new Case2();
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 {
public void show();
}
|
Java
public interface Interface2 {
public String show();
}
|
Java
public class Case3_1 implements Interface1 {
@Override public void show()
{
System.out.println( "Geeks for Geeks" );
}
public static void main(String[] args)
{
Case3_1 obj = new Case3_1();
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
public class Case3_2 implements Interface2 {
@Override public String show() { return "GFG" ; }
public static void main(String[] args)
{
Case3_2 obj = new Case3_2();
String res = obj.show();
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
public class VariableConflict
implements Interface1, Interface2 {
public static void main(String[] args)
{
VariableConflict obj = new VariableConflict();
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.
Last Updated :
18 Feb, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...