I have confusion in object life cycle. If parent class reference pointing to the child class object and method is overridden. For example
class Parent {
public void display(){
System.out.println("i am from Parent");
}
}
class Child extends Parent {
public void method1() { }
public void display(){
System.out.println("i am from Child class");
}
}
Now if I create the in main class
Parent p = new Child();
Is my understanding object life cycle is proper or not? If not please guide me.
new
Child()
constructor get calledby
super()
method parent class constructor(default constructor) get callednow according to object life cycle first constructor get called and concrete method that is
display()
method from parent class get stored in heap.after Child class constructor finish the execution and
method1()
anddisplay()
method get stored in heap memory.
Now if I call p.display()
its going to execute child class display()
method, but if you see both parent and child class display()
method stored in heap memory.
If this behavior proper then what is the use of storing the display()
method of parent in heap.