In the inheritance why the base class constructor get the call first why not the derived one??
Objects are always destroyed in reverse order of their creation. The reason for reverse order is, an object created later may use the previously created object.
So if in the initialization process of child class there may be the use of parent class members. This is why the constructor of the base class is called first to initialize all the inherited members.
The answer is that in a class hierarchy, constructors are called in order of derivation, from superclass to subclass. Further, since super( ) must be the first statement executed in a subclass' constructor, this order is the same whether or not super( ) is used.
Order of execution of constructor in Single inheritance In single level inheritance, the constructor of the base class is executed first.
To make sure the public or protected members of the base-class are properly initialized before they are used in the derived class.
To be precise, the derived-class constructor is run first with an implicit call to the base-class constructor inserted as the first statement in the body of the derived-class constructor by the compiler (assuming default no-arg constructors).
The derived class is created by extending the base class. It should be ensured that the base class members are properly initialized, before it can be extended in a derived class. Also, members initialized in derived class should not be overridden by base class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With