Inheritance and Polymorphism


As mentioned at the beginning of the chapter, inheritance allows a program to reuse preexisting object types without rewriting their functionality from scratch. By way of example let's examine the Array object type. If you wanted to have the functionality of a linked list, but did not have the Array.prototype property or did not want to use it, you could create a new class called List that extended the Array class. The new List class would have all the functionality of the original Array class, but you could add more functionality into it automatically.

Inheritance in JavaScript is slightly different than in other languages. Following is a classic example used in many computer science classes:

 Employee.prototype = new Person(); Employee.prototype.constructor = Employee; Employee.superclass = Person.prototype; // Employee constructor function Employee() {    ... } 

In this example, the Employee class extends the functionality of the Person class. Person is considered to be the super class and Employee is considered the subclass. In the first line, the prototype property of the Employee class is being assigned to an instance of the Person class. This provides an instance of Employee with all the functionality of the Person class. Unfortunately, this statement overrides the constructor for the Employee class, so the second line is to make sure the proper constructor is executed when an Employee object is created. The third line provides a way for the JavaScript interpreter to understand the class hierarchy by specifying which class is a subclass and which is the super class.

When inheriting properties and methods from a super class, there may come a point at which you need to perform some similar function but don't know the exact type of the object you are working with. A classic case is the shape example. To begin with, a super class called Shape is declared. Shape has a single method paint(). Later, two subclasses of Shape are created, Circle and Square. Both subclasses inherit the method paint() from the Shape class, but both override it to provide specific support for their data. The Circle.paint() method provides a way to draw a circle to the screen, while the Square.paint() provides code to draw a square. If you were using a lot of these shape objects, you might store them in an array such as the following:

 var shapeArray = new Array( n ); shapeArray = new Circle(); shapeArray = new Square(); ... 

Later on in your code, if you wanted to draw this array of shapes to the screen but did not keep track of the order in which you stored them, you would not need to worry. Since the super class, Shape, has the method paint() both subclasses will also have the method. As long as you know your array is full of some type of Shape object, you can call paint() and have it perform the proper operations that will draw the subclass's actual shape. This results in a method that has been implemented differently in multiple subclasses in order to provide class specific functionality. This is called polymorphism.




JavaScript Professional Projects
JavaScript Professional Projects
ISBN: 1592000134
EAN: 2147483647
Year: 2002
Pages: 130
Authors: Paul Hatcher

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net