java – Why add super () to the constructor of the child class if the compiler does it automatically?

Question:

Why add a super() call to the constructor of a child class if the compiler does it automatically?

I mean, in any case, the constructor of the parent class will be executed first and only then the child class, why then write it manually?

Answer:

Because in super() you can pass arguments, in the event that you need to execute the constructor with the arguments of the parent class. And what type of arguments you pass there depends on which constructor will be executed (if there are several of them).

Scroll to Top