Why do we need to insert same parameters of superclass constructor() in the super() method within subclass constructor() | Sololearn: Learn to code for FREE!

0

Why do we need to insert same parameters of superclass constructor() in the super() method within subclass constructor()

8/14/2022 12:26:23 PM

Ahamed Shifak

3 Answers

New Answer

+1

Ahamed Shifak because the super keyword is used to call the constructor of its parent class to access the parent's properties and methods. So if parent class constructor have parameters then you have to pass with super.

+1

Ahamed Shifak It is not necessary that subclass constructor and parent class constructor should have same parameters. Also it is not necessary super should have an argument. If you make parent class constructor with parameters then you have pass argument with super. You can write super without argument also, constructor will be call but you may get undefined because you are not providing argument https://code.sololearn.com/W6sNy1wg1zmn/?ref=app

0

But why do we code it in both the constructor parantheses of subclass and super method parantheses