Here is the code i have three interfaces
interface i1{
int x=1;
}
interface i2{
int x=2;
}
interface i3{
int x=3;
}
class A implements i1,i2,i3{
system.out.println(x); // It shows Field is ambgous
}
How to answer this or how to overcome this problem.
6 Answers 6
How to answer this or how to overcome this problem.
Don't use fields in interfaces, or if you must use them, and they must have the same names, just fully qualify them:
System.out.println(i3.x);
Note that with import static, the "brevity" reason for importing interfaces containing constants is removed - interfaces should really only be implemented for genuine behavioural reasons. See Effective Java 2nd edition for more advice on this front.
Comments
Though this question has been answered by many, here is some detailed analysis:
interface I1 {
int x = 1; // Implicitly public, static, and final.
}
interface I2 {
int x = 2; // Implicitly public, static, and final.
}
interface I3{
int x = 3; // Implicitly public, static, and final.
}
class A implements I1, I2, I3{
static {
System.out.println(I1.x);
}
}
The integer x is implicitly public, static, and final per section 9.3. Field (Constant) Declarations of the Java Language Specification.
Since x can't have three different values at the same time, attempting to print x fails because of this ambiguity. You must use the fully qualified name, e.g. I1.x.
On a side note, your code has many issues, e.g. not following Java Naming Convention, using system instead of System, not using a static block or method for the print statement, etc.
For the practical implementation of this, please use the javap tool which is available out of the box in JDK.
Usage: javap I1
1 Comment
x's had the same value, the same compilation error would occur.This is because same field is defined in all interfaces hence it doesn't know which field to use among these 3 .
- You will have to specify interface name.
- Code should be in some init block or method.
- its
System.out.printlnand notsystem
Also your names for types should start with capital leters
interface I1{
int x=1;
}
interface I2{
int x=2;
}
interface I3{
int x=3;
}
class A implements I1,I2,I3{
static{
System.out.println(I1.x);
}
}
Comments
In all the of the 3 interface variable x is there,so it shows ambiguous. Compiler confuses to print which x from i1 or i2 or i3
Comments
Interfaces are designed to demonstrate a class structure. If you must declare fields in multiple interfaces and use all those interfaces in one class, you should explicitly declare which interface you're using for that variable.
System.out.println(i3.x);
You can also use the extends keyword in your interfaces to decrease ambiguity by setting up inheritance.
interface i1{int x=1;}
interface i2 extends i1 {...}
interface i3 extends i2 {...}
class A implements i3{...}
Class A will have to implement all functions declared in i1,i2 and i3.
Comments
Don't do this. This is called Constant Interface Antipattern. Or just use the fully qualified names.
i3.x.