FAQ: Classes - Inheritance IV

#1

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!

#2

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

#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 ().

#4
console.log(nurseOlynyk.remainingVacationDays);
1 Like
#5

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

1 Like
#6

I got it nice solution

#7

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.

#8

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

#9

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?

#10

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

1 Like
#11

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).

1 Like
#12

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.