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 () 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 () below!
Agree with a comment or answer? Like () to up-vote the contribution!
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
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 ().
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?
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).
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.
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.
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:
How come the Nurse class can access HospitalEmployee'sremainingVacationDays 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.
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?