recently I went through the inheritance concept.
As we all know, in inheritance, superclass objects are created/initialized prior to subclass objects. So if we create an object of subclass, it will contain all the superclass information.
But I got stuck at one point.
Do the superclass and the subclass methods are present on separate call-stack?
If it is so, is there any specific reason for same?
If it is not so, why they don’t appear on same call-stack?
E.g.
// Superclass
class A {
void play1( ) {
// ....
}
}
// Subclass
class B extends A {
void play2( ) {
//.....
}
}
Then does the above 2 methods i.e play1( ) and play2( ) appear on separate call stack?
Thanks.
There is not necessarily any relation between a call stack and inheritance. In fact frequently there isn’t. If a superclass method is overridden in a child then it is the child’s method that is called – the superclass method is not executed at all, unless there is code to explicitly do that in the child method. The superclass method will not appear at all in the call stack – unless it is called explicitly by the child method.
A.play2() appears on the call stack only during “second call”. B.play1() appears on call stack only during “first call”. A.play1() never appears on the call stack and is never executed.