Search code examples
javaobjectinheritance

Java object-oriented understanding problem


This is my first class

public class DigitalDevice {
    public int abc = 1;
}

And this is my second class

public class SmartPhone extends DigitalDevice {

    SmartPhone() {
        abc = 2;
        super.abc=3;
        DigitalDevice.abc=4;
        
    }
}

In the SmartPhone class, when I recall abc, DigitalDevice's abc is accessible and changeable, and when I recall super.abc, also DigitalDevice's abc is accessible and changeable, but when I recall DigitalDevice.abc, DigitalDevice's abc is not accessible and changeable and the IDE gives and error and asks for making abc static in the DigitalDevice class.

Is this true that in SmartPhone class abc is super.abc is DigitalDevice.abc?


Solution

  • No, it is not true. abc and super.abc is the same, as well as this.abc (not used in your code). But DigitalDevice.abc is an attempt to call a static field named abc in the class DigitalDevice. There is no such static field in that class and your IDE is rightfully telling you that. Check When to use static/non-static variable in JAVA on what the difference between static and non-static is.