FAQ: Classes - Inheritance IV

Hello, @digital8256053093.

Does your code by chance include a getter similar to this?

  get remainingVacationDays() {
    return this._remainingVacationDays;

Yes. Why is that significant, relative to the naming convention?

When this line is executed:

The getter method is invoked. The getter returns the value. Your original code accessed the backing variable directly. We are trying to avoid direct access by using the getter instead. By not accessing the backing variable directly we have a bit of protection in place for the value assigned to it.

Methods are typically invoked like this nurseOlynyk.remainingVacationDays(); with the () at the end. Since we used the keyword get we are able to invoke the method without the ().


Thank you. It seems so obvious now. It seems odd to me that I missed that before.

1 Like

Maybe a strange question… but why is it that when we create the nurse class that inherits properties from the HospitalEmployee class we only use ‘super’ for the name? why would it not also inherit the remainingDaysOff? my initial thought was to include it but I got it wrong and had to look at the hint to see what I had done wrong. Not a huge deal but I just am not clear on the why…

name is the only variable we need instantiated by the parent class.

It is inherited with a value already set. If we want a different value then we need to initialize it in the subclass so that it overrides the inherited value.

okay I think I get it!! That was really helpful! ty!

1 Like

For the Animal and Cat example in the Inheritance IV lesson:

I wanted to create a new const variable bryceCatBehavior to store the incremented values from calling bryceCat.incrementBehavior();. The problem is, I am getting ‘undefined’ as a result instead of the incremented value which is 1 when I console log. Is this a local/global scope issue or have I lost connection to the parent class?

const bryceCatBehavior = bryceCat.incrementBehavior(); // Outputs “undefined” when console logged.

My intention is to see how many times the incrementBehavior method is getting called and possibly even store the values (this may or may not be a good idea to do so but I’m curious if the possibility exists).

None of the above! You’re assigning the result of a function call to bryceCatBehavior - note the parentheses in bryceCat.incrementBehavior(). (A method call to be specific, but same thing.)

When the method is called it runs the code in .incrementBehavior(). The problem here is that there’s no return value specified, it just increments the property and that’s it. When there’s no other return value, JS’s default return value is undefined. So undefined is returned and assigned to your variable, bryceCatBehavior.

For your purposes, since incrementBehavior increments by 1 for each call, you can see how many times it’s been called by simply const bryceCatBehavior = bryceCat.behavior;

If there wasn’t a one-to-one mapping, you could still capture how many times the method has been called by adding some code to it. Say we added another property to our Animals:

class Animal {
  constructor(name) {
    this._name = name;
    this._behavior = 0;
    this._timesMisbehaved = 0;
  get timesMisbehaved() {
    return this._timesMisbehaved;
  set timesMisbehaved(newValue) {
    this._timesMisbehaved = newValue;
  // rest of class

then every time we run the method, we can have it add one to our count of behavior incidents.

incrementBehavior() {
    // rest of method

Hmm…for a while, I thought the variable bryceCatBehavior couldn’t be incremented and instead returns undefined by default because I used the const keyword but I don’t believe thats the case anymore. I did try to play around with the code and its just like you said for when there is no return…it only logs the property.

This seems right though but I have totally forgotten this, getters, setters so I’ll have to freshen up and get back to this example.

Thank you!

Is there any difference between

//Output: Olynyk

//Output: Olynyk

This also works with and without _ for remainingVacationDays, shouldn’t we suppose to follow what we wrote in the code?

backing variables (the variables that begin with an _) are not meant to be accessed directly. Use `console.log(nurseOlynyk.name) so that it uses your getter

I wrote:

You see, _remainingVacationDays has _ in front of the “remainingVariableDays” which tells the code reviewer or other party of the same sort, that the property should not be directly accessed. Therefore a getter method is present in the object which when invoked would return the value of property that the original programmer did not want you to access directly.

The solution code specified:

This invokes the getter method present in our object, "get remainingVacationDays(){ // return something } "

Hope that it helps.

How can I console the second certificate of nurseOlynyk? I was trying multiple solutions this is one of them. console.log(HospitalEmployee.Nurse.nurseOlynyk[certifications][1])


I noticed that in you console.log statement, you have written down the parent class HospitalEmployee and the child class Nurse. They were accessed and used to allow you to construct the nurseOlynyk object and do not need to be written again. You used something similar to the below to generate the nurseOlynyk object, right?

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

To access the nurseOlynyk object you simply need to write nurseOlynyk and then use either dot or square bracket notation to access the properties of that object.


Furthermore, in your comment you have used square bracket notation for accessing the certifications property on the nurseOlynyk object. However, you need to wrap the property name (in this case certifications) within commas, as without the commas it will see it as a variable, and since it’s undefined it throws an error. The commas allow it to be recognised as a string, which when within the square brackets allow it be recognised as a property accessor.

You would write it instead as:

I hope this helps, I am just learning this myself. Apologies if I have not been absolutely correct with the terminology but I hope I get the points across. :slight_smile:

thank you so much for your reply. I really appreciate it! The solution I am trying to find is how can I track back to the parent class, to the absolute root, or if it is not possible that I would like to know that (however I doubt that would be impossible). Nurse Olynyk is a nurse in the hospital so she’s part of the Nurse class that is part of the HospitalEmployee parent class and has two certificates. I want to reach the second certificate from the certifications array with an index [1].