Here is the code:
public class MyClass implements Inreface1, Inreface2 {
public MyClass() {
System.out.println("name is :: " + name);
}
public static void main(String[] args) {
new MyClass();
}
}
//Interface1
public interface Inreface1 {
public String name="Name";
}
//Interface2
public interface Inreface2 {
public String name="Name";
}
Here is the error it causes:
The field name is ambiguous
What is the problem? What is ambiguous?
Your class is implementing two interfaces, and on both of them, the variable
nameis defined. Thus, when you callnamein your class, Java is not able to determine if the variable refers toInterface1.nameorInterface.name.That’s the problem in your code…