If we are creatinga child that extends a parent, and both of them have a common (=overridden) method named CommonMethod,
this is what I think should be the sequence (shown in the code below):
1) JVM starts by starting the instantiation of Child
2) Since Child extends Parent, instantaition of Child is temporarily suspended
3) JVM starts the instantiation of Parent
4) Constructor of Parent is called
5) Parent's Constructor calls the method CommonMethod. This method exists in both Child and Parent
6) Here is the crux. I think that Parent's CommonMethod should be called. Execution however showed that Child's method was called instead!
7) And THEN, JVM instantiates (rather, completes the instantiation process of) Child
BUT: When I executed the below code , this is what happened:
(A) Parent constructor did get called first - as you would expect
(B) CommonMethod called used the "semi-existent" Child object's CommonMethod, not Parent's !
(C) AND THEN the constructor for the child object was called
Can someone please point out the error in my concepts?
public class Tester
{
public static void main(
String[] args)
{
Child c = new Child() ;
}
}
class Parent
{
public Parent() { CommonMethod() ; }
public void CommonMethod() { System.out.println ("I am the Parent") ; }
}
class Child extends Parent
{
public Child() { CommonMethod() ; }
public void CommonMethod() { System.out.println ("I am the Child") ; }
}
So I expected that the output would be:
1) I am the Parent
2) I am the Child
The actual output was
1) I am the Child
2) I am the Child
Need to understand the 'real sequence of events', I guess