FAQ: Classes - Inheritance IV

This community-built FAQ covers the “Inheritance IV” exercise from the lesson “Classes”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Introduction To JavaScript

FAQs on the exercise Inheritance IV

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

I did everything right and the checkboxes lit up but I see an error. Is this what is supposed to happen?

// code begins here
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’]);
nurseOlynyk.takeVacationDays(5); // I did this right and it took me to the next task
console.log(nurseOlynyk.remainingVacationDays()); // I did this right but instead of printing something I got an error, but it still said I got it right.

// here is the error code
/home/ccuser/workspace/learn-javascript-classes-classes-inheritance-iv/main.js:29
console.log(nurseOlynyk.remainingVacationDays());
^

TypeError: nurseOlynyk.remainingVacationDays is not a function
at Object. (/home/ccuser/workspace/learn-javascript-classes-classes-inheritance-iv/main.js:29:25)
at Module._compile (module.js:571:32)
at Object.Module._extensions…js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)
at bootstrap_node.js:542:3

Nevermind. I figured it out. It was the ().

Even though I included it which I should not have it still said it was the right answer. I went back in, reset and asked for the solution and that’s how I found out that is should not have included the ().

2 Likes
console.log(nurseOlynyk.remainingVacationDays);
4 Likes

Thanks! I think I figured it out just a few seconds after and tried to delete my post :slight_smile:

2 Likes

I got it nice solution

I think a slight wording issue with the opening paragraph of the Inheritance IV exercise:

Now that we know how to create an object that inherits properties from a parent class let’s turn our attention to methods.

I was thinking… shouldn’t object here read class?

That’s what we just did in the previous exercise; learned to extend a parent class to a new class.

1 Like

No, a class is the overall blueprint for new object instances. The purpose of a class is to generate objects.

4 Likes

Ok, yes, I was just about to revise the above into a question.

Which would be:

What is doing the inheriting, here? Is the extending class inheriting from the parent class, or is it the object that is inheriting the properties and methods?

It is the object that inherits from its extended class and its parent class.

3 Likes

hmm I am gonna have to quote Wikipedia here though, which whilst I hold up my hands and do not claim to be an expert, does chime with the OO design course I have done previously – and with the intro to the topic in this very course material:

Wikipedia

An inherited class is called a subclass of its parent class or super class.
The term “inheritance” is loosely used for both class-based and prototype-based programming, but in narrow use the term is reserved for class-based programming (one class inherits from another), with the corresponding technique in prototype-based programming being instead called delegation (one object delegates to another).

Intro to the course topic

With inheritance, you can create a parent class (also known as a superclass) with properties and methods that multiple child classes (also known as subclasses) share. The child classes inherit the properties and methods from their parent class.

What we’re creating in this part of the course is a super class and a child class; surely as the course itself states above, the child class is doing the inheriting of properties and methods from the superclass?

The objects we create here are just instantiating the child class, no?

(only persisting with this 'cos I wanna make sure I understand this solidly).

3 Likes

Walking back, somewhat, one must admit that a class is an object, and an instance is a new Object() So clearly inheritance is related to the class. I think my brain has just not twisted objects as being a class. When they are treated synonymously, the answer is clear. In my mind it has always been, variable = instance of class which differentiates between the object (instance) and model (class). Funny how our brain works, sometimes.

class Animal {
constructor(name) {
this._name = name;
this._behavior = 0;
}

get name() {
return this._name;
}

get behavior() {
return this._behavior;
}

incrementBehavior() {
this._behavior++;
}
}

class Cat extends Animal {
constructor(name, usesLitter) {
super(name);
this._usesLitter = usesLitter;
}
}

I am not able to understand that why isn’t ‘behaviour’ passed as an argument in the constructor of ‘cat’ class. Please explain.

The Cat class automatically inherits the properties and methods of the Animal class. In the Animal class constructor you are assigning a default value of 0 to the _behavior property. In this exercise all instances of the Animal class are intended to start with a behavior value of 0 , and in theory their behavior will improve with training. Since we want them all to start with 0 there is no need to have the behavior value included as an argument. It’s simpler to have the constructor do this by default since they will all always be the same. Hope that made sense.

1 Like

I didn’t understand the first sentence. If the ‘cat’ class automatically inherits the properties and methods of the ‘animal’ class, then how is ‘name’ an argument in the constructor ?

The name will be unique to each instance of the class, so when you create an instance you have to supply the name as an argument.
In the example from the lesson, I added a few console.log() statements, and called the incrementBehavior() method to illustrate the properties a new Cat has when instantiated:

class Animal {
  constructor(name) {
    this._name = name;
    this._behavior = 0;
  }

  get name() {
    return this._name;
  }

  get behavior() {
    return this._behavior;
  }

  incrementBehavior() {
    this._behavior++;
  }
} 


class Cat extends Animal {
  constructor(name, usesLitter) {
    super(name);
    this._usesLitter = usesLitter;
  }
}

const bryceCat = new Cat('Bryce', false);

console.log('Name: ' + bryceCat.name);
console.log('Starting Behavior: ' + bryceCat.behavior);
bryceCat.incrementBehavior();
console.log('New Behavior: ' + bryceCat.behavior);

Output:

Name: Bryce
Starting Behavior: 0
New Behavior: 1

Hopefully this will all make more sense as you complete this lesson.

2 Likes

Yeah, it makes sense now. Thanks for helping.

1 Like

How come the Nurse class can access HospitalEmployee's remainingVacationDays without setting it on super?

Edit: I think I got it, I was under the impression that super was necessary to access and inherit anything that’s inside the parent’s constructor - whether its properties has a constructor argument or not - but that’s not the case.

We only need super to instantiate parameters as given. That way the properties are in the same execution context as the parent (inherited) methods that are accessing them.

3 Likes

I too have question about this exercise.

For the very last statement, I wrote:
console.log(nurseOlynyk._remainingVacationDays);

The code worked, and the number 15 logged to the console.

However, the module reported it as an error. I cheated and looked at the solution code, and the only difference between my code and the solution code was in that statement.

I wrote:
console.log(nurseOlynyk._remainingVacationDays);
The solution code specified:
console.log(nurseOlynyk.remainingVacationDays);

Why does the correct solution even work, when no where in the code does remainingVacationDays previously exist?

Why are _remainingVacationDays and remainingVacationDays functionally equivalent, yet the one that matches the rest of the code is wrong, while the one that deviates from the rest of the code is right?

1 Like