2

There is an interface1 having method1, variable x and interface2 having method1, variable x. Why does it shows up error in Line 1 and not in Line 2?

interface interface1{
    public int x =10;
    public void method1();
}
interface interface2{
    public int x =11;
    public void method1();
}

public class Test implements interface1, interface2{

    int y = x; // Line 1
    @Override
    public void method1() {  //Line 2
    }

}
1
  • Give the interfaces and methods sensible names and this problem will likely vanish. Can you show a 'real world' example of this problem? Commented Jan 12, 2013 at 6:47

4 Answers 4

3

'x' is ambiguous because there are two of them in scope, one from each interface. 'method1()' by contrast is not, because by the rules of Java the definition in Test satisfies the requirement to provide an implementation of it as defined in both interfaces.

Sign up to request clarification or add additional context in comments.

Comments

2
interface interface1{
  public static final int x =10;
  public void method1();
}

interface interface2{
  public static final int x =11;
  public void method1();
}

public class Test implements interface1, interface2{

  int y = interface1.x; // Line 1 or int y = interface2.x;
  @Override
    public void method1() {  //Line 2
  }

}

This is the correct way.

2 Comments

The compiler actually will implicitly change the types. The real answer is: Don't do that.
The only change here that is relevant is adding the 'interface' qualifier. Adding 'static final' accomplishes precisely nothing: it would be more to the point to remove 'public'.
0

Because your assignment is ambiguous. You have to specify if you want interface1.x or interface2.x.

For example:

public class Test implements interface1, interface2{

  int y = interface1.x; // Line 1
  @Override
    public void method1() {  //Line 2
    }

}

3 Comments

Test.java:12: error: reference to x is ambiguous, both variable x in interface1 and variable x in interface2 match int y = x; // Line 1
@BrianRoach it's not completely incorrect. What are you talking about?
NM, I read the original post wrong; dyslexia kicking in. That said, this is still a horrible thing to do.
-1

in the final variables interaface1.x, interface2.x; if we take x of interface1 the object referred might be different than the second interface.

But In method1() if we take any interface the body of method will be same, as interface1.method1(), is same as interface2.mathod1(). But in the other hand with x. interface1.x is not same as interface2.x

2 Comments

It has nothing to do with the values. Down vote. Your edit doesn't improve things.
The problem is that there are two x's in scope. It is as simple as that.

Your Answer

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.