June 6, 2002 - Deriving a Specialized Class from a Generic Class | WebReference

June 6, 2002 - Deriving a Specialized Class from a Generic Class

Yehuda Shiran June 6, 2002
Deriving a Specialized Class from a Generic Class
Tips: June 2002

Yehuda Shiran, Ph.D.
Doc JavaScript

You usually use inheritance to derive a specialized class from a generic, base class. As an example, think of the manager and employee in a company. Every manager in a company is also its employee. It makes sense to define an employee base class, and extend it to a manager class. Here is the Employee class:

  class Employee{
    var name : String;
    var address : String;
  
    function Employee(empName : String){
      this.name = empName;
    }
 
    function printMailingLabel() {
      print(name);
      print(address);
    }
  }
Let's now extend the Employee class to the Manager class:

  class Manager extends Employee{
    var numberOfSubordinates : int;
    var compensation : double;
  
    function Manager(mgrName : String, salary: double) {
      this.name = mgrName;
      this.compensation = salary;
    }
  
    private var compensation : double;
  
    function get compensation() : double {
      return compensation;
    }
  }
We construct an instance of the Manager class as follows:

var Sharon : Manager = new Manager("Sharon Doe", 120000);
Sharon.numberOfSubordinates = 5;
Sharon.address = "250 Stanford Ave., Palo Alto, CA 94305";
We can print the salary and mailing label now:

  print(Sharon.compensation);
  Sharon.printMailingLabel();
The output should be as follows:

  120000
  Sharon Doe
  250 Stanford Ave., Palo Alto, CA 94305
To learn more about JScript .NET, go to Column 110, JScript .NET, Part IV: Inheritance.