Employee is a Person

Let’s understand inheritance in #TypeScript

It’s a very common statement in the real world when we say that “Employee is a Person” and that’s the center of our lesson on understanding inheritance in the world of #TypeScript.

I might have taken an example of “Dog is an Animal” too, but that does not impact so much emotionally to us and feels kind of robotic.

Context

In the world of computer programing, especially for the Object Oriented Programming we talk about inheritance. Inheritance makes our programs evolutionary. A properly designed application should have interfaces and classes and more classes evolved from them. Let’s take an example of some ERP application, where we are sure that there will be entity Employee and they can be, say an Executive or Manager.

Continuation

I have directed you to follow a few chapters on TypeScript during my previous post and if you have followed that, you might have seen that our discussion was there till Classes in TypeScript. With this example below I would like to continue our learning by understanding inheritance.

So, how an Employee class may look like

and here is how you will create an object for manager

This looks good, but answer a few questions

  1. Will you be able to identify with the object, if that contains a Manager or Executive, until unless you peek the properties of the object?
  2. Can you protect this object from unwanted changes of data (someone changes the value of Designation from Manager to Executive)?

What if I evolve this code a little bit more.

That’s great! Our system contains a definition of an employee but, whenever an instance is created, we are forced to say that this employee is Manager. (we can create a separate definition for Executive similar way)

Wait, there might be an another entity Contractor, which may not have a Designation in our organisation model, but a Role. Can you think how our entity model will look like in this scenario? Let’s see

At this point we have reached to smallest entity, from where everything can be evolved and other classes can inherit.

Employee is a Person, even a Contractor is a Person

As much as above statement holds a meaning in the programming of an application, I guess it might have a social impact too. But let’s not loose our focus. When we design an application (whatever object oriented language) we try to evaluate the smallest entity which may have a part in defining other entities in the system.

I have covered other such concepts of TypeScript other than inheritance in very brief with my tutorial series, which can be found here. Do leave your feedback on this or any other posts on my blog. I will be really happy to interact with you all.

Happy reading … have a nice day!

Link to my blog post: