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
Google Going Native With Chrome
Mozilla Fixes Firefox Flaws as 3.5 Release Nears
Microsoft and Novell Still Bosom Buddies

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 >
XML and PHP Simplified · Creating a ASP.NET Contact Form · Data Filtering with PHP
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Intel to Host Live Nehalem Q&A · 12 Tips to Troubleshoot Network File-Sharing · 10 Tips for Selling on Kijiji