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
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs

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:

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint