Should I always call the parent class constructor in the child class?

0

I created two classes a parent class that has a constructor where I have to pass some arguments, and a daughter class that inherits this parent class that I created, but in the child class I did not do the constructor and did not use the

parent::__construct();

But when instantiating the child class of the error, if I do not pass the arguments in the header of it, and when I pass the arguments it works normal and I can use all methods of the parent class.

So I came to the conclusion that it is not necessary to call the constructor of the parent class within the child class, unless the child class has its own constructor, so I would have to pass its arguments and parent class arguments through parent :: __ construct (); .

Is my conclusion correct?

    
asked by anonymous 18.06.2018 / 17:03

1 answer

1

That's right, if you are extending the behavior of the father you must pass the parameters required by it, in case you do not need new behaviors in the constructor it is not necessary to call the parent constructor. Calling the parent constructor indicates that you will override the object's construction, either to add, remove, or change the behavior of how the object is constructed.

    
18.06.2018 / 17:14