+ 1
Related to construct calling in case of widening in classes.
Why does the constructor of subclass get called when we promote its object by making it refer from a superclass' reference ? https://code.sololearn.com/c2VvNF9PNFL9/?ref=app
1 Réponse
+ 6
When you create an instance of a subclass, the super class's constructor is called before anything else is done in the constructor of the subclass.
The reason is the keyword 'extends'.
Because a subclass extends a super class, the subclass can do anything the super class PLUS something more.
So before it can do something more, an object of the super class must be instantiated, that contains the basics for that 'something more'.