FAQ: Classes - Inheritance III

For step 2, the program doesn’t let me pass the

Inside the Nurse class, create a constructor() that accepts two arguments. Use the list of properties above to name these arguments.

step with this code, even though when I click the solution, it’s the exact same code:

class Nurse extends HospitalEmployee {
  constructor(name, certifications)
}

So… what am I doing wrong here, cause from the solution, it’s correct.

Also, why has the “Show solution” button gone away from my workspace?

If i look at the constructor of the HospitalEmployee class, it looks slightly different:

constructor(name) {
    this._name = name;
    this._remainingVacationDays = 20;
  }

Maybe you miss something?

I thought the show solution button was after several failed attempts.

Even if I do this:

class Nurse extends HospitalEmployee {
  constructor (_name, _certifications)
}

It still isn’t working. But the solution says:

class Nurse extends HospitalEmployee {
  constructor(name, certifications) 
}

which I’ve already tried. So… what’s the deal? Also mb about the solution button.

just like any other method, your method needs a body:

constructor(name, certifications) {
   // body of your constructor
}

the body can be empty, but the curly brackets are crucial.

I get this same error. Here is my full code:

class HospitalEmployee {
  constructor(name) {
    this._name = name;
    this._remainingVacationDays = 20;
  }
  
  get name() {
    return this._name;
  }
  
  get remainingVacationDays() {
    return this._remainingVacationDays;
  }
  
  takeVacationDays(daysOff) {
    this._remainingVacationDays -= daysOff;
  }
}

class Nurse extends HospitalEmployee {
  constructor(name, certifications) {
    super(name);
    super(remainingVacationDays);
    this._certifications = certifications;
  }
}

const nurseOlynyk = new Nurse('Olynyk', ['Trauma', 'Pediatrics']);

I have tried running the final exercise both with and without ‘remainingVacationDays’ set as a parameter for the Nurse subclass, both times the exercise returns correct and both times I get the error stating that remainingVacationDays is undefined.

here:

super(remainingVacationDays);

remainingVacationDays is undefined. Besides, looking at the method you attempt to call with super:

constructor(name)

we see the constructor method of HospitalEmployee only has a single parameter. So we only need to supply one argument (for the name)

2 Likes

When I moved on to the next exercise (Inheritance IV) I saw the corrected code and tried running it back in Inheritance III. The error did not appear.

The difference was that the line super(remainingVacationDays); which I had put in the Nurse subclass wasn’t there. I presume that this line was the cause of the error.

I think that my mistake was assuming that the information is passed from the parent to the child class before the final creation of the object, an assumption I made because of how ‘inheritance’ works between humans. I thought that the properties in HospitalEmployee would be passed to Nurse through the parameters.

It seems to me now that the child passes the extra or ‘extended’ data to the parent for the creation of the new Nurse object. In other words, passing it up the family tree instead of down.

Is this correct?

Edit: Thanks stetim94, I just saw after publishing this that you posted a reply just before me. I think understand now. Thank you!

Information is generally passed down along the tree, a child class will inherit parent methods.

However, in your Nurse class, you overwrite the inherited constructor method, so that is where super comes to the rescue, allowing us to call the parent constructor (so name and remaining vacation days are set)

Perfect, that really clarifies it for me.

Thanks very much!

Hi,
I am doing the Inheritance III exercise, and I am confused about something. Why in our constructor do we except some properties such as name, but not remainingVacationdays? Or when we extend Class nurse to HospitalEmployees why does the constructor take in name and certification and not _remainingVacationDays.

3 Likes

Each hospital employee has its own name, while the vacation days are the same for every employee. Which is why some are passed as parameter/argument (so we can vary them at each instance creation), while some values are fixed for each instance creation

4 Likes

Oh ok that makes sense, thank you

Thanks for pointing that out. I was having an error even tho I wrote the syntax correctly and turns out I missed the second ‘y’ and typed Olynk.

1 Like

I think they moved the Show Solution button to ‘Get Help’ in the bottom right hand side of the page

Hi @stetim94 , I was wondering if you could help me please in understanding why the child class can not have getter in it? It says that the child inherit methods from the parent,but in this exercise, in the parent there is no get certifications() {return…} to inherit, so how do we get the certification except by writing it in the child class like the below example I wrote?
And the second question is why we are not having 2 parameters in the parent class, like this? :
constructor(name,remainingVacationDays ) which will be written in the super (name, remainingVacationDays) also.
It has been quite long since I am trying to figure it out, with googling etc but nothing :frowning:
I would really appreciate your help!

class HospitalEmployee {
  constructor(name) {
    this._name = name;
    this._remainingVacationDays = 20;
  }
  
  get name() {
    return this._name;
  }
  
  get remainingVacationDays() {
    return this._remainingVacationDays;
  }
  
  takeVacationDays(daysOff) {
    this._remainingVacationDays -= daysOff;
  }
}


class Nurse extends HospitalEmployee {
  constructor(name, certifications) {
    super(name);
    this._remaininingVacationDays = 20;
    this._certifications = certifications;
  }
 get certifications () {
return this._certifications;
};
const nurseOlynyk = new Nurse("Olynyk",["Trauma", "Pedicatrics"]);

If a parent class doesn’t have the property, you would need to implement the properties plus any getters/methods with this property might have in child class

Or you need to reconsider if all HospitalEmployee should have certificates and move the functionality to the parent class, and overwrite in child classes where needed

parameters for constructor are useful when you each instance of the class needs to have a different value. Each hospital employee has its own name (thankfully), but the vacation days all start with the same value, so you can just hard-code that value into the constructor.

3 Likes

Thank you so much you are really good at programming:)
@stetim94
I have one last question, I see that this following instance of an object can not be done like this and I can’t understand why(below is the solution) :

const lorrainHansbury = new PrimarySchool ({
name: ‘Lorraine Hansbury’,
numberOfStudents: 541 ,
pickupPolicy: ‘Students must be picked up by a parent, guardian, or a family member over the age of 13.’
});

// const lorrainHansbury = new PrimarySchool (‘Lorraine Hansbury’, 541 , ‘Students must be picked up by a parent, guardian, or a family member over the age of 13’)

lorrainHansbury.quickFacts();

here is the link of the project : https://www.codecademy.com/courses/introduction-to-javascript/projects/school-catalog

Thanks in advance!

Although not very conventional, its certainly possible. But you would have to modify your constructor to have a single parameter, and then within the constructor access the property of the object:

constructor(data){
   this.name = data.name
  // other properties
}

the reason we don’t take this approach, is because we would have to write extra code to verify the properties exists, and its less clear to read, you don’t know what properties the object needs to have

2 Likes
class HospitalEmployee {
  constructor(name) {
    this._name = name;
    this._remainingVacationDays = 20;
  }
  
  get name() {
    return this._name;
  }
  
  get remainingVacationDays() {
    return this._remainingVacationDays;
  }
  
  takeVacationDays(daysOff) {
    this._remainingVacationDays -= daysOff;
  }
}

class Nurse extends HospitalEmployee {
  constructor(name, certifications) {
    super(name);
    this._certifications = certifications;
  }
}

const nurseOlynyk = new Nurse('Olynyk', ['Trauma', 'Pediatrics']);

I assumed that i would use super(remainingVacationDays) to inherit that from HospitalEmployees like we do with super(name) but it throws an error. Anyone know why?

class Nurse extends HospitalEmployee{
  constructor(name,certifications){
    super(name)
    super(remainingVacationDays)
    this._certifications = certifications
  }