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

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
Eclipse Helios Update Brings New PHP Tools
Internet Explorer 9 Ups Standards Support
JBoss Portal 5 Release Easier to Use

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.


People who read this tip also read these tips:

Look for similar tips by subject:

webref The latest from WebReference.com Browse >
Flashmaps' DynamicLocator: Interactive Maps for Small Areas · Flashmaps' AreaSelector: Interactive Maps for Wide Areas · The DB Mapper: Interactive Street-level Maps of U.S. and Canada
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
MS Access and MySQL · Cisco AutoQoS: VoIP QoS for Mere Mortals · While VoIP Adoption Explodes in Enterprise, Carrier Spending Lags