Sudharsan Ashwin wrote:
In this line B b = new B(); ,
Memory will be allocated in heap for print() method and that will be referenced by " b" .
There is no memory allocated for methods during object creation. Classes and instructions for their methods are loaded whenever the class is referenced for the first time.
When you say "B b = new B()", if this is the first time B is referenced, then Classloader loads B. Also, it loads the classes referenced by B.
i.e., As Class B extends A, and it references Class A, class A is loaded at the same time B is loaded.
when you do "A a = b", you can access all methods declared in Class A.
If you have overridden any of class A method, in class B, then it will invoke overridden method in class B.