FAQ: Classes - Inheritance V

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

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

Web Development

Introduction To JavaScript
Learn Intermediate JavaScript

FAQs on the exercise Inheritance V

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!

/home/ccuser/workspace/learn-javascript-classes-classes-inheritance-v/main.js:25
get certifications()
^

RangeError: Maximum call stack size exceeded
at Nurse.get certifications [as certifications] (/home/ccuser/workspace/learn-javascript-classes-classes-inheritance-v/main.js:25:21)
at Nurse.get certifications [as certifications] (/home/ccuser/workspace/learn-javascript-classes-classes-inheritance-v/main.js:27:44)
at Nurse.get certifications [as certifications] (/home/ccuser/workspace/learn-javascript-classes-classes-inheritance-v/main.js:27:44)
at Nurse.get certifications [as certifications] (/home/ccuser/workspace/learn-javascript-classes-classes-inheritance-v/main.js:27:44)
at Nurse.get certifications [as certifications] (/home/ccuser/workspace/learn-javascript-classes-classes-inheritance-v/main.js:27:44)
at Nurse.get certifications [as certifications] (/home/ccuser/workspace/learn-javascript-classes-classes-inheritance-v/main.js:27:44)
at Nurse.get certifications [as certifications] (/home/ccuser/workspace/learn-javascript-classes-classes-inheritance-v/main.js:27:44)
at Nurse.get certifications [as certifications] (/home/ccuser/workspace/learn-javascript-classes-classes-inheritance-v/main.js:27:44)
at Nurse.get certifications [as certifications] (/home/ccuser/workspace/learn-javascript-classes-classes-inheritance-v/main.js:27:44)
at Nurse.get certifications [as certifications] (/home/ccuser/workspace/learn-javascript-classes-classes-inheritance-v/main.js:27:44)

I have got the above message on the output screen although the code run fine. Can someone explain about this?

It’s like getters must be made before corresponding methods can work.

console.log(nurseOlynyk._certifications[2]) of genetics
This line of code generates the correct answer to the activity on screen 10 but doesn’t lead to correct response on webpage.

nurseOlynyk.addCertification(‘Genetics’);
console.log(nurseOlynyk.certifications); //[ ‘Trauma’, ‘Pediatrics’, ‘Genetics’ ]

2 Likes

No need to put the underscore because we are pushing the new string into the parameter, certifications.

2 Likes

Thanks, i’ll may check into that later. Having to deal with some practical stuffs/other stuff so haven’t look back into Javascript on CodeAcademy in a few weeks. That sounds like a good catch.

1 Like

Don’t be mislead into thinking the underscores are of no particular significance. Architecturally they are vitally important and cannot for a second be written off.

We’re in a different domain once setters and getters are introduced. Forget that we can sort of do the same thing without them. With them we have another level of functionality. Not just another level, a whole universe of inspection, validation, composition, reporting, &c. Not the sort of thing we would expect to find in a property definition.

Consider,

 this._prop = value;
 // ...
 get prop () {
     return this._prop;
 }
 set prop (value) {
     // validate value
     this._prop = value;
 }

We’ve essentially created a backing variable using the very setter intended to work with one. The getter will now work as expected.

Perhaps you can explain why we cannot perform both operations at once? There seems to be some restriction in place. Any ideas?


Proof the setter is run…

 > class a {
     constructor(value) {
       this._prop = value;
     }
     get prop () {
       return this._prop;
     }
     set prop (value) {
       // validate value
       this._prop = value;
    }
  }
<- undefined
 > b = new a('funny')
<- > a {_prop: "funny"}
 > b._prop
<- "funny"

At this point we confirm the endpoint. Now we want to validate the data before it reaches the endpoint.

Edit: May 21, 2021 - fix typo

3 Likes

I wasn’t writing off the underscores. The comment of pratical was in reference to needing to deal with some real life issues before getting back to codeacademy problem and finding a solution. Thanks for your assistance.

1 Like

Why is this not registering as correct for the first exercise? The solution looks exactly the same.

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

3 Likes

it seems like I am having the same issue it is very frustrating any answers anyone ?

2 Likes

My biggest issue going through codeacademy thus far has nothing to do with explanations. Actually, this question is rather easy. The way they present the questions are very confusing where I actually view the solution to understand the question.

Should never be like that. For example, this questions specfically states properties and methods but to a new developer can be confusing because thought process is … do we create new properties on the new object or is it already understood as it’s being inherited through extends.

So, I wish CodeAcademy would go through some of their questions and just ask the question and not convolute everything.

Other than that, no qualms with CA.

2 Likes

It says in the lesson that we should use this code to inherit a new child class that is exactly the same as its parent class:

But I just felt that this piece below should do it, and I tried it and it’s working well, is there anything wrong with it? and if not, why aren’t we using it since it’s more efficient?

Somehow I am getting a value of 3 back instead of [ ‘Trauma’, ‘Pediatrics’, ‘Genetics’ ] when calling console.log(nurseOlynyk.certifications);

Below you can find my JavaScript code. Thanks for letting me know!

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;
  } 

get certifications()
{
  return this._certifications;
}

addCertification(newCertification){
this._certifications = this.certifications.push(newCertification);
}

}

const nurseOlynyk = new Nurse('Olynyk', ['Trauma','Pediatrics']);
nurseOlynyk.takeVacationDays(5);
console.log(nurseOlynyk.remainingVacationDays);

nurseOlynyk.addCertification('Genetics');

console.log(nurseOlynyk.certifications);

Al ready solved it myself. I had to change:

this._certifications = this.certifications.push(newCertification);

into

this.certifications.push(newCertification);

However my solution was seen as correct by Codecademy on this problem.

You found your error, that’s great. What did you learn about push in the process? Did you find an explanation for why it was 3 in your first trial?

This is not so right implementation. According to object oriented concepts if a parent class constructor expects arguments then it is the responsibility of its child class to pass the value for parent when trying to create objects. Check this piece of code.

class Animal {

constructor(name) {

  this._name = name;

  this._behavior = 0;

}



get name() {

  return this._name;

}



get behavior() {

  return this._behavior;

}   



incrementBehavior() {

  this._behavior++;

}

}

class Dog extends Animal {

constructor(barks){

    this._barks = barks;

}

}

let D1 = new Dog(‘Loud’);

console.log(D1.name);

Here, we have supplied the arguments but the code will not run because it expects a call to super with the to initialize super’s constructor.

Great questions!

I did not find an explanation, I can do better ;). I can think myself of a reason why it happened.

If you put an array with the push function in the console log it will log the amount of items that are in the array after the push. So if you assign it to a variable (that contains an array) the variable will replace the array with a number instead. :smiley:

1 Like

Hi! why exactly on the inheritance V exercise when adding a method to the child class the console will reference error if I type the method like:
this.addCertifications(certifications) {
this._certifications.push(newCertification);
}
Nevertheless when I take the first this. everything starts working right? the code ends up like:

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

get certifications() {
return this._certifications;
}

addCertification(newCertification) {
this._certifications.push(newCertification);
}
}

Can a subclass have a subclass?