Why doesn't it say ambiguous reference

public interface IContext
{
public double originX = 0;
}

public interface IShape extends IContext
{
public double originX = 10;
}

public class Draw implements IShape
{

public Draw()
{
double originX = this.originX;
}
}

Annu,

First, all members in an interface are implicitly static & final. Another thing to note is that static variables can be accessed in a class instance using the "this" keyword i.e. static variables are available to instances of a class, it is not possible the other way around.

Now when the jvm comes to the constructor of class Draw, here's the steps that it takes to determine the value of this.originX

  • Look in the current class for the referenced variable
  • Since it cannot find it, look in the parent classes or interfaces
  • In our case we only have one parent interface, so the variable(& its value) from the parent interface is used.

Do note that, In the context of a class instance originX in interface IShape has shadowed originX from interface IContext.

Now were we to change the implementation of class Draw as follows

public class IDraw implements IShape, IContext
Let's see what the jvm does
  • Look in the current class for the referenced variable
  • Since it cannot find it, look in the parent classes or interfaces
  • In this case both of the direct super interfaces define a member of the same name, the compiler doesn't know which one is more appropriate & you'll get an error at compile time stating that the reference to originX is ambiguous.

HTH
Ashish Hareet
iRix Technologies.