Java OCAJP7: having the same field inherited from both a superclass and an implemented interface

What happens if a class extends a base class that has an instance variable and at the same time it implements an interface in which a member with the same name is defined?
Nothing happens if we don’t try to access to that variable:

interface MyInterface {
    public static int x = 0;
}

class SuperClass {

    protected int x = 10;

}

public class VarInheritanceTest extends SuperClass implements MyInterface{

    public static void main(String[] args) {

        System.out.println("OK!");
    }
}
OK!

But when we try to access the variable problems can occur. If we try to access the variable directly by its name we get a compilation error that reports the ambiguity of the variable access we are attempting:

interface MyInterface {
    public static int x = 0;
}

class SuperClass {

    protected int x = 10;
}

public class VarInheritanceTest extends SuperClass implements MyInterface{

    public static void main(String[] args) {

        System.out.println("Value: " + x);
    }
}
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	The field x is ambiguous

	at net.davismol.ocpjp7.VarInheritanceTest.main(VarInheritanceTest.java:18)

If we want to access the “x” value defined in the interface we must explicitly indicate it:

interface MyInterface {
    public static int x = 0;
}

class SuperClass {

    protected int x = 10;

}

public class VarInheritanceTest extends SuperClass implements MyInterface{

    public static void main(String[] args) {

        System.out.println("Value: " + MyInterface.x);

    }
}

In this case we get the following result:

Value: 0

The variable defined in the SuperClass class in this case is not static so, in order to access to it, it’s necessary to create an instance of the class. Also by creating a class instance however direct access via the variable name remains ambiguous.

interface MyInterface {
    public static int x = 0;
}

class SuperClass {

    protected int x = 10;

}

public class VarInheritanceTest extends SuperClass implements MyInterface{

    public static void main(String[] args) {

        VarInheritanceTest vit = new VarInheritanceTest();
        System.out.println("Value: " + vit.x);

    }
}

To indicate which of the two values we want to access its necessary to explicitly cast the object to the base class or to the interface. In the following two examples we see how, depending on the explicit cast applied, the relevant “x” field value is printed out.

interface MyInterface {
    public static int x = 0;
}

class SuperClass {

    protected int x = 10;

}

public class VarInheritanceTest extends SuperClass implements MyInterface{

    public static void main(String[] args) {

        VarInheritanceTest vit = new VarInheritanceTest();
        System.out.println("Value: " + ((SuperClass)vit).x);

    }
}
Value: 10
interface MyInterface {
    public static int x = 0;
}

class SuperClass {

    protected int x = 10;

}

public class VarInheritanceTest extends SuperClass implements MyInterface{

    public static void main(String[] args) {

        VarInheritanceTest vit = new VarInheritanceTest();
        System.out.println("Value: " + ((MyInterface)vit).x);
    }
}
Value: 0
This entry was posted in $1$s. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *