Please consider the following code.
class Base{
Base() {
print();
}
void print() {
System.out.println("Base");
}
}
class Child extends Base{
int i = 4;
public static void main(String[] args){
Base base = new Child();
base.print();
}
void print() {
System.out.println(i);
}
}
The program will print 0,4.
What I understand by this is, the method to be executed will be selected depending on the class of the actual object so in this case is Child
. So when Base
's constructor is called print method of Child
is called so this will print 0,4.
Please tell if I understood this correctly?
If yes, I have one more question, while Base class constructor is running how come JVM can call Child
's method since Child
's object is not created?