I have a subclass object. Can I access a hidden variable of the superclass without using the super keyword. ?? Actually, I found a technique.. Its working but I don't understand the conceptual reason behind it.
class A {
public int a = 5;
private int c = 6;
void superclass() {
System.out.println("Super class" + " " + "value of a is " + a);
System.out.println("Super class" + " " + "value of c is " + c);
}
}
class B extends A {
int b = 7;
int a = 8;
void subclass() {
System.out.println("Sub class" + " " + "value of b is " + b);
System.out.println("Sub class" + " " + "value of a is " + a);
}
}
class Demo {
public static void main(String args[]) {
A a1 = new A();
B b1 = new B();
b1.superclass();
}
}
In the above code, if b1
is a object of class B.I have called a superclass method named superclass()
; Now the output is a=5
. But my argument is why can't it be a=8
? a=5
is hidden and to access it, we have to use super keyword. But here without super key word I am getting a=5
. How can it be possible?