1

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.

asked Sep 23, 2013 at 10:18
1
  • 1
    How do you expect the compiler to make a choice between the 3 different variables with the same name? Prefix your variable name with the name of the interface. : i3.x. Commented Sep 23, 2013 at 10:21

6 Answers 6

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.

answered Sep 23, 2013 at 10:20
Sign up to request clarification or add additional context in comments.

Comments

3

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

Arvind Kumar Avinash
81.2k10 gold badges99 silver badges144 bronze badges
answered Sep 23, 2013 at 11:23

1 Comment

Note: even if all three x's had the same value, the same compilation error would occur.
1

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.println and not system

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);
}
}
answered Sep 23, 2013 at 10:20

Comments

0

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

answered Sep 23, 2013 at 10:20

Comments

0

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.

answered Sep 23, 2013 at 10:32

Comments

0

Don't do this. This is called Constant Interface Antipattern. Or just use the fully qualified names.

answered Sep 23, 2013 at 11:04

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.