Game Development Reference
In-Depth Information
class
Motorbike : MotorizedVehicle {...}
class
Bicycle : Vehicle {...}
class
Airplane : MovingGameObject {...}
class
Boat : MovingGameObject {...}
class
MotorBoat : Boat {...}
class
SteamBoat : MotorBoat {...}
class
SpeedBoat : MotorBoat {...}
class
SailBoat : Boat {...}
Figure
10.3
shows the inheritance structure, using arrows to indicate an in-
heritance relation between classes. At the very base of the inheritance tree is a
GameObject
class. This class contains only very basic information such as the po-
sition or the velocity of the game object. For each subclass, new members (vari-
ables, methods or properties) can be added, which are of relevance for the particular
class and its subclasses. For example, the variable
numberOfWheels
typically belongs
in the
Vehicle
class and not in
MovingGameObject
(since boats do not have wheels).
The variable
flightAltitude
belongs in the
Airplane
class and the variable
bellIsWorking
belongs to the
Bicycle
class.
10.6.2 Designing Class Hierarchies
When you determine the way that your classes are structured, you have to make
many decisions. There is not a single 'best' hierarchy and, depending on the ap-
plication, one hierarchy might be more useful than another. For instance, in this
example we have opted to first divide the
MovingGameObject
class according to the
Fig. 10.3
A complicated hierarchy of classes