School Catalogue: Understanding Inheritance and Super

Question:

In the School Catalogue project, How does the Super call on the child class know where to assign the level (“Primary”) argument if the level argument was not passed in the constructor of the child class?

class School {
  constructor(name) {
    this._name = name;
    this._level = "";
    this._numberOfStudents = 0;
  }
}

class PrimarySchool extends School {
  constructor(name, numberOfStudents, pickupPolicy) {
    super(name, 'Primary', numberOfStudents);
    this._pickupPolicy = pickupPolicy;
  }
}
1 Like

The super keyword is used to access and call functions on an object’s parent.

so super will call the constructor of of School class. Are you sure you got it right? School constructor only got a single parameter, yet you give 3 arguments when calling the super function

1 Like

Thanks, @stetim94

The way I have written it here is the way they taught it in the Javascript Classes Inheritance I lesson

The way you discuss it, passing all three arguments into the parent class makes much more sense.

BUT

Why do they say not to pass the level argument into the child constructor?

They say in the project walkthrough video @ 5:52 that:

the level argument can be left out of the constructor because every Primary School will have the same level.

Should you always pass arguments into the constructor for all properties of the class??

1 Like

and the instructions agree with me:

Inside the School class, create an empty constructor() that accepts three parameters.

Because the level for all primary schools is Primary? So this is correct:

super(name, 'Primary', numberOfStudents);

For all primary schools, you know pass Primary as level to School constructor (once you fixed that)

no, you can perfectly add a property later:

class Example {
  constructor(name){
    this._name = name;
  }
  name(){
    console.log(this._name);
  } 
  addEmail( email ){
    this._email = email;
  }
  
  email(){
    console.log(this._email);
  }
}

example = new Example('test');
example.name();
example.addEmail('test@testmail.com');
example.email();

just a lame example, but it serves it purposes (hopefully)

its still not as good as a truly objected orientated programming language like java where we have class properties, instance properties and methods. These methods, class and instance properties can have different access modifiers (public, protected and private)

3 Likes

Hey @stetim94, is there a way to add a property to a parent class (School, in this case), using a method written into it? I was thinking about something like this:

addProperty(key, value){
  let this._key = value;
  
}

I would use .push() there, but I can’t figure out how to push it into a class

.push() is a method for an array

not that i know, but haven’t done JS in a while. But surely is must be possible.

1 Like