Interface naming conflicts
In this tutorial, we are going to discuss interface naming conflicts in java. Interface naming conflicts can occur in Java when two or more interfaces within a single application or library share the same name but are defined in different packages or contexts.
This can lead to ambiguity and compilation errors, as Java requires unique names for interfaces within the same package or scope.
![Interface naming conflicts](https://waytoeasylearn.com/storage/2021/11/Java-interface.png)
1. Method naming conflicts
Case 1
If two interfaces contain a method with the same signature and the same return type in the implementation class, we can implement only one method.
interface Left {
public void m1();
}
interface Right {
public void m1();
}
class Test implements Left, Right {
public void m1() {
// Implementation
}
}
Case 2
Suppose two interfaces contain a method with the same signature but different arguments. In that case, we can provide an implementation for both methods, and these methods are considered overloaded methods.
E.g
interface Left {
public void m1();
}
interface Right {
public void m1(int i);
}
class Test implements Left, Right {
public void m1() {
// Implementation
}
public void m1(int i) {
// Implementation
}
}
Case 3
Suppose two interfaces contain a method with the 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 containing variable with the same name and may raise variable naming conflicts, but we can resolve these naming conflicts using interface names.
System.out.println(Left.x);
System.out.println(Right.x);
That’s all about Interface naming conflicts in Java. If you have any queries or feedback, please write us email at contact@waytoeasylearn.com. Enjoy learning, Enjoy Java.!