OOP In WinJS #4 : Inheritance

In our previous article, we discussed Classes. We defined a few classes, instantiated them, and used an example. Before starting with part four, let's first look at the previous parts of the series.

This article will cover how to inherit from a base class with all properties without redefining them. We'll be using Universal Windows Apps for showing the demo.

Generally a class in inherited using this method:
  1. var InheritedClass = WinJS.Class.derive(BaseClass,  
  2.     function (properties) {  
  3.         //properties section  
  4.     });  
WinJS has a function named "derive" to create new classes that inherit from base class.

Let's make an example to understand how to use it.

Building the App

Create a class with some members and then create inherited classes from base classes changing properties. Don't use base class to create new instances.

Solution

We shall create a base class named Employee:
  1. var Employee = WinJS.Class.define(  
  2.     function (name, surname, age, profession, nationality, salary) {  
  3.         this.name = name;  
  4.         this.surname = surname;  
  5.         this.age = age;  
  6.         this.profession = profession;  
  7.         this.nationality = nationality;  
  8.         this.salary = salary;  
  9.     });  
This will be our base class.

According to the scenario; we shall never create instances from that. So,what we are going to do is create inherited classes that derive from base class:
  1. var TurkishEmp = WinJS.Class.derive(Employee,  
  2.     function (name) {  
  3.         this.name = name;  
  4.         this.nationality = "Turkey";  
  5.     });  
  6.   
  7. var IndianEmp = WinJS.Class.derive(Employee,  
  8.     function (name) {  
  9.         this.name = name;  
  10.         this.nationality = "India";  
  11.     });  
  12.   
  13. var EnglishEmp = WinJS.Class.derive(Employee,  
  14.     function (name) {  
  15.         this.name = name;  
  16.         this.nationality = "UK";  
  17.     });  
Now let's create our instances:
  1. var emp1 = new TurkishEmp("Ibrahim");  
  2. var emp2 = new IndianEmp("Suthish");  
  3. var emp3 = new EnglishEmp("Tommy");  
And show them their values in JavaScript Console:
  1. console.log(emp1.name + " is from " + emp1.nationality);  
  2. console.log(emp2.name + " is from " + emp2.nationality);  
  3. console.log(emp3.name + " is from " + emp3.nationality);  
Running the app will display results as shown below:


Now that we made a simple inheritance sample, why don't we try something extra?

Let's create two more inherited classes:
  1. var FreelancerEmp = WinJS.Class.derive(TurkishEmp,  
  2.     function (name, surname) {  
  3.         this.name = name;  
  4.         this.surname = surname;  
  5.         this.salary = 2400;  
  6.     }  
  7.     );  
  8.   
  9. var OutSourcing = WinJS.Class.derive(EnglishEmp,  
  10.     function (name, surname) {  
  11.         this.name = name;  
  12.         this.surname = surname;  
  13.         this.salary = 3000;  
  14.   
  15.     }  
  16.     );  
These classes inherited from another inherited class which is now base class for them.

What we are going to do is change their salaries as we see fit. We broke the assigned value there.
  1. var emp4 = new FreelancerEmp("Semih""Temih");  
  2. var emp5 = new OutSourcing("Hasan""Vehid");  
  3.   
  4. emp4.salary = 3000;  
  5. emp5.salary = 4000;  

The reason for doing this sample is to show you how you can create inherited classes endlessly.

It's up to your structure.
 
Read more articles on Universal Windows App: