An employee Is A person.

Not mentioned before is the HAS-A relationship. This is basically a property of a class.

The person Has A name. Since an employee will be an extension of a person, the employee Has A name as well.

IS-A, HAS-A relationships

Think of Person as a Super-class and Employee as a sub-class.

Add the following class, Employee, and test it.

Pretty Cool!

Observations

Employee is an extension of Person.

2. We inherited the properties and methods (get and set in this example) of Person.

3. To set the value of an inherited property, we can use super’s set() method. Notice the use of super’s set() in lines 25 and 34. (There are other ways such as calling super(n), in the constructor.)

5. To get the value of an inherited property, we can use super’s get() method. Notice the use of super’s get() in lines 30 and 36.

4. Critical. In the constructor, super(); MUST be invoked before local properties or inherited properties are set. In other words, super must be called first in the constructor. It might be the only item in the constructor.

6. We can still instantiate the Person class to get a person object as well, but they only have a name property.

Recommendation

If you have followed my other parts (especially part 3), you will recall I recommend the use of set and get methods to set and get property values, since we can add validation and other code to ensure we get what we want.

You can still give the consumer of the class the ability to set property values in the constructor as we see in the example. See part’s 2 and 3 for more information.

Note, however, item #4 above applies in all cases.

Re-usability

One of the main reasons to use inheritance is to re-use the base class. We will do this in the next example. We will create a Customer class that extends Person. That way our customer can have a name through inheritance as well as a locally defined title. . We will also add a method to our person class to return a greeting. This will be inherited by any sub-class.

Example 2

re-use of our base-class and inherited method, greet()

Since we modified Person to include the method greet(), our Employee inherits it as well.

Conclusion

We have learned the basics of a very powerful ability of JavaScript classes, inheritance. With this we can create a base-class and extend it as needed (and still use the base-class itself.)

We can extend any class, so we also could have extended Employee, for example. If we had different properties for full time employees versus contractors, we could have built,

class CompanyEmployee extends Employee

and

class ContractEmployee extends Employee

What’s Next

In the next and last part we will explore more about super and the concept of overriding.

We will also look at truly private properties and methods. This will give us true Object Oriented Programming encaspulation, data hiding. Sadly this is still in the proposal stage. There is a partial work-around we will explore. However.

In the meantime, thanks for reading!