|
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
[an error occurred while processing this directive]
You now have a language that describes the entities a program needs to fulfill its tasks. It is also helpful to have a notation to visually describe a class hierarchy. The notation you will use is simple. A box will be used to represent a thing. A line will be used to show the direction of inheritance. The thing (box) pointed to is the parent thing. The thing at the other end of the line is the child. I will also refer to the parent as the superthing and the child as the subthing. Figure 2.1 demonstrates a parent-child relationship.
For this How-To, lets use a hierarchy for vehicles. You first have to decide the base thing and then determine the other subthings that are descendants of the base thing. Steps
How It Works You must first identify the base things that will make up your application. In this How-To, you create a base thing named Vehicle. The declaration looks like this: thing Vehicle exposed service powerSwitch exposed service accelerate exposed service decelerate Vehicle is a generic classification that can be extended through inheritance. The interface (or services) that all Vehicles must exhibit is powerSwitch, accelerate, and decelerate. Think about it: All Vehicles must start up somehow; the powerSwitch provides that functionality. The keyword exposed designates that these services are accessible to users of a Vehicle object. For each thing that inherits from a Vehicle, you have to specify the functionality for powerSwitch. For example, a powerSwitch for an F-14 fighter aircraft will operate differently than a powerSwitch for a MotorVehicle. The intent is the same, but the actual implementation of each is different. A Vehicle must also accelerate and decelerate. Notice that the terms accelerate and decelerate are used instead of gas pedal and brake. Why? Not all Vehicles have a brake; a boat is a good example. Now that you have defined what a basic Vehicle must do, you will specialize, or inherit from, Vehicle. The thing Vehicle is hardly useful considering its current definition, so the next step is to define a new thing by inheriting from Vehicle. This is specified by the line thing MotorVehicle descendantOf Vehicle This definition is very straightforward. This statement declares that MotorVehicle is a descendant (child) of Vehicle (the parent). A MotorVehicle is a specialization of a Vehicle; the MotorVehicle will require a steering wheel, brake pedal, and gas pedal (among other obvious things). You could continue the definition of a MotorVehicle by specifying tires, an engine, a transmission, and so on. The intent in this How-To is to explain the concept of inheritance without making the things overly complicated. Notice that the names of things are capitalized. In the case of MotorVehicle, each word of the compound word is capitalized. This is accepted naming practice in object-oriented programming. If some thing is a descendant of a Vehicle, it inherits the services defined in Vehicle. You then extend the functionality of Vehicle (otherwise, why inherit from it?). If you dont need to specialize Vehicle, simply create an instance of Vehicle in your application. Next, you have to inherit from MotorVehicle, namely a Truck. That is reflected in the following declaration: thing Truck descendantOf MotorVehicle exposed service fourWheelDrive A Truck inherits all the functionality of a MotorVehicle, specifically powerSwitch, accelerate, decelerate, and steering. This Truck extends the functionality of a MotorVehicle by providing four-wheel drive capability. This is reflected in the declaration of a new service named fourWheelDrive. Last, you draw the hierarchy to provide a visual representation of the inheritance tree.
|
![]() |
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.
|