Relation between inheritance and Constructors | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Relation between inheritance and Constructors

can anyone explain me the statement that Constructors are not inherited by subclasses because they are not member methods however constructor of Superclass is called when subclass is "instantiated "

3rd Jan 2018, 5:47 AM
Shalini Jha
Shalini Jha - avatar
1 Answer
+ 12
Constructor is responsible to setup the class when it was created/instantiated and therefore it's belong to itself. Let's take my favourite Pokemon as an example: ⚡ Raichu extends Pikachu ⚡ and assume that the Pikachu default constructor initialize the gender in random. It's natural to say that in order to create Raichu we don't need to know how Pikachu was created which lies in the Pikachu's constructor itself and thus it's not inherited. Furthermore if we are creating a Raichu object, the Pikachu object would need to be created first which will in turn call it constructor. (It evolves!) 😉
3rd Jan 2018, 6:52 AM
Zephyr Koo
Zephyr Koo - avatar