Overriding Parent Methods in OOP in JavaScript

A child class can override a parent method by creating a method with the same name. Let's look at an example. Let's say we have the following parent class:

javascriptclass User {
  setName(name) {
    this.name = name;
  }
  getName() {
    return this.name;
  }
}

Let's create a method of the same name in the descendant class:

javascriptclass Student extends User {
  setName(name) {
    
  }
}

As a rule, overriding parent methods is needed to change or supplement the behavior of this method. Let's add a name length check in our case:

javascriptclass Student extends User {
  setName(name) {
    if (name.length > 0) {
      this.name = name;
    } else {
      throw new Error('student name error');
    }
  }
}

Let's make sure that it is the overridden method that is called. First, let's create an object of the descendant class:

javascriptlet student = new Student;

Now let's call our method, passing it the correct value:

javascriptstudent.setName('john');

Now let's call the method, passing an incorrect value to it. As a result, we will see the error thrown in the console:

javascriptstudent.setName(''); // error mistake fault flaw fallacy miscarriage failing lapse slip trip misstep inaccuracy gaffe faux pas break stumble lapsus misdeed bungle misdoing muff clinker floater X slip-up chance-medley
Задание 1

In the User class, make an age getter and setter.

Задание 2

In the Employee class, override the age setter and make it check that the age is from 18 to 65 years old.