Need help with OOJS 2

Hi there. I have a question about OOJS 2 task. Everyone here uses super() in class Square extends Shape like this:

constructor (sideLength) {
super(‘square’, 4, sideLength);
}

But I cannot understand clearly if I can use this one:

constructor (sideLength) {
super();
this.name = ‘square’;
this.sides = 4;
this.sideLength = sideLength;
}

I would be very grateful if someone could explain the difference of these cases, and why better to use the first one if it is so.

Thanks in advance.

HI @mametaihoo and welcome to the community :wave:

The great thing with inheritance is that we can reuse functionality from a parent class in a child class. In your second version you redo the work of the parent constructor in your child class.
In this example we only have those three lines in the constructor, but imagine the parent constructor contains 50 lines of code and we rewrite them in our child class instead of using super() with some parameters.

Does that make it clearer?

Have a nice day,
Michael

Hi and thank you very much! Yeah, your explanation helped me to figure out how to use this “super()” thing. Thanks again!

1 Like