JScript .NET, Part IV: Inheritance: Extending Classes - Doc JavaScript
JScript .NET, Part IV: Inheritance
Extending Classes
Inheritance is one of the major advantages of object oriented programming languages. When you have a class that provides most of the functionality you need, you can create a new class that is based on the existing class. The new class extends the functionality of the existing class, usually called the base class. The new class does everything the base class does, and more. Inheritance avoids duplication of code, promotes code reuse, cuts down the number of lines of code, and optimizes the maintenance burden.
There are several classic examples for inheritance. They usually define a class for a generic entity as the base class, and then extend it to a more refined, specialized class. A person
class in an organization may be extended to an employee
class of that organization. An employee
class may be further extended to a manager
class. Likewise, an automobile
class may be the base class in a car dealership. It may be extended to a sedan
class, a truck
class, or a sports
class. Each of these classes may be further refined to classes of specific makes and models.
The syntax for extending a base class is as follows:
class NewClass extends BaseClass { [classmembers] }
where NewClass is the new class, BaseClass is the base class, and classmembers are the additional class members of the new class. They are added to whatever class members are defined in the base class.
Let's take the employee/manager example. 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 create an instance of the Employee
class, and set its name and address:
var Bob : Employee = new Employee("Bob Smith"); Bob.address = "2010 University Avenue, Palo Alto, CA 94301";
The command Bob.printMailingLabel()
will yield the following output:
Bob Smith 2010 University Avenue, Palo Alto, CA 94301
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
Next: How to write an interface
Produced by Yehuda Shiran and Tomer Shiran
All Rights Reserved. Legal Notices.
Created: May 20, 2002
Revised: May 20, 2002
URL: https://www.webreference.com/js/column110/2.html