1

I have abstract class and interface like this

abstract class ParentClass
{
    int VALUE;
    public abstract void display();
    public void display2()
    {
        System.out.println("this is abstract class method");
    }
}

interface parentInterface
{
    int VALUE=88;
    abstract void display();
    void display2();
}

the child class extends and implements the above like following

class ChildClass extends ParentClass implements parentInterface
{

    ChildClass()
    {
        super.VALUE=0;
        //VALUE=0;     //<=will give ambiguous property
    }
    @Override
    public void display() 
    {
        System.out.println("Current Class-"+this.getClass());
        System.out.println("Integer value-"+super.VALUE);
    }
    public void display2()
    {
        //to call the method of abstract class 
        //call by using super.display2();
        System.out.println("this is implemented method");
    }
}

So, my question is How can i access interface VALUE variable in ChildClass ??

Akhil Jain
  • 13,872
  • 15
  • 57
  • 93

2 Answers2

3

You can access the VALUE from interface using parentInterface.VALUE as variables in interfaces are public static final by default.

And the abstract class's VALUE can be accessed using this.VALUE as it is a member variable.

Community
  • 1
  • 1
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
2

variables in interface are implicitly public static final.

static - because Interface cannot have any instance.

final - the value assigned to the interface variable is a true constant that cannot be re-assigned by program code.

Interface variables can be accessed using <Interface>.VALUE whereas the variables from the parent class are inherited and hence can be accessed using this.VALUE.

if any subclass class is implementing an interface which has instance members and if both subclass and interface are in the same package then that static members can be accessed from the child class without even using the Interface name.

Thats why you are getting the ambiguous error. Please put Interface in some other package and then it should not show such an error else you will have to access it like super.VALUE

Rahul
  • 15,979
  • 4
  • 42
  • 63