home account info subscribe login search FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
[an error occurred while processing this directive]
Previous Table of Contents Next


How It Works

In the first three steps, the declarations for Vehicle, MotorVehicle, and Truck are reproduced from previous How-Tos. This is done merely for the convenience of the reader.

Although each derived thing inherits a service from its parent thing, you expect that each derived thing will re-implement each service it inherits. This is considered specialization. Each derived thing will specialize the functionality defined by its parent thing. The following is an example to provide some clarification:

thing RaceCar descendantOf MotorVehicle
//...

Considering the previous declaration of RaceCar, it can be assumed that the service accelerate will function differently than the accelerate service for Truck. I hope that a RaceCar object accelerates at a much faster rate than a Truck object. Let’s look at what is happening in step 4.

The first line of code

Truck aTruck := create Truck.

creates an instance of a Truck. The object (instance) will be known by the declared name aTruck. Using the object, you can call upon the various services defined for a Truck thing. The next line of code demonstrates this:

aTruck->setSpeed(55).

This call to setSpeed is made for the setSpeed service implemented by the Truck thing. The implementation explicitly sets the value of the speed part to 55.

The next line of code

Vehicle baseVehicle := aTruck.

creates an reference variable of type Vehicle and is named baseVehicle. This is only a reference; baseVehicle is not actually an object of type Vehicle. The assignment expression baseVehicle := aTruck binds the reference (baseVehicle) to refer to the object aTruck. Although baseVehicle is a Vehicle reference, it is referring to a type Truck object. It is important to understand this. Figure 2.3 expresses the relationship.


Figure 2.3  Base reference relationship to derived object.

The line of code that follows

integer speed := baseVehicle->getSpeed.

creates a program variable of type integer; the service getSpeed is called using the reference baseVehicle. Which getSpeed is called: the one implemented in Vehicle, the one defined in MotorVehicle, or the one implemented in Truck? The getSpeed called is the one implemented in Truck. This is the expected behavior, considering the definition of polymorphism. The service called depends on the actual object referred to, not the reference to the object. Because baseVehicle actually refers to a Truck object, it is Truck->getSpeed that is called. The next two lines of code verify that fact by printing the value of aTruck->speed. First, the value stored in the local variable speed is printed. Next, the value stored in aTruck->speed is printed. The next two lines of code in the program fragment

MotorVehicle anotherVehicle := create MotorVehicle.
anotherVehicle->setSpeed(40).

create an object named anotherVehicle of type MotorVehicle. The second line then sets the speed part of this object to 40. The line of code that follows

baseVehicle := anotherVehicle.

sets the reference baseVehicle to refer to the object anotherVehicle.

The last three lines of code from the program follow:

Print(baseVehicle->getSpeed).
baseVehicle->setSpeed(85).
Print(anotherVehicle->getSpeed).

The first line prints the contents of anotherVehicle->speed, which is 40. The next line sets the contents of anotherVehicle->speed to 85 through the reference baseVehicle. Finally, the value of anotherVehicle->speed, which is now 85, is printed out, thus verifying the call to setSpeed through baseVehicle.

Comments

This How-To provided a definition for polymorphism and demonstrated its mechanism using a sample program. In the next chapter, polymorphism is demonstrated using the C++ programming language.

The descriptions and definitions in this chapter are a good introduction to thinking in “object mode” rather than structure mode.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.