So, I was experimenting with some basic encapsulation in Java when I found that the way the members and functions are accessed from inside the extended class is different. I mean to say this:
public class A {
int i = 10;
public void print() {
System.out.println(" inside A ");
}
}
public class B extends A{
int i = 20;
public void print() {
System.out.println(" in B");
}
public static void main(String args[]) {
A a = new B();
a.print();
System.out.println(a.i);
}
}
In the above case, for an object of the current class (the one that extends something super), method of the current class is invoked while member of the super class is accessed. So, what's the reason for this? Or, more generically, what's exactly happening when I say
A a = new B()
I mean, what's happening at the memory level?