+ 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

13th Sep 2017, 10:21 AM
Divya Vishwakarma
Divya Vishwakarma - avatar
1 Answer
+ 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'.
14th Sep 2017, 8:24 PM
Tashi N
Tashi N - avatar