Information Technology Reference
In-Depth Information
principles is a feature of object-oriented programming languages known as
inheritance. Inheritance is a relationship between different classes in which one
class shares attributes of one or more different classes. In this way, the class in
question, the subclass, inherits the qualities of other classes that have already been
created, the superclasses. There are two general cases of inheritance, which are
defined by the number of superclasses, or parent classes, from which the subclass,
or child class, directly inherits attributes: single inheritance describes a rela-
tionship in which a class has only one ancestor from which it directly inherits its
attributes. Multiple inheritance, on the other hand, occurs when a class calls on
more than one superclass for properties. The rest of this section will be devoted to
discussing the various principles behind the use of inheritance.
For starters, inheritance offers an obvious solution for the elimination of redun-
dancy through the implementation of reuse. The creation of a new class from some
other class with a similar purpose and set of attributes saves the software engineer
from rewriting code that is already in use elsewhere. This implementation of
inheritance is often used to facilitate the creation of some number of differing classes
that share common attributes and a common purpose. Consider, again, the
monster truck example previously discussed. We described this as being an
instance of the truck class, along with a few other potential versions of that class:
fire truck and dump truck . In this example, each of our three subclasses of
truck can be thought of as more specific versions of the superclass. The superclass,
truck , can then be thought of as a generalization to be used in the formation of
those subclasses. This type of class is called an abstract class and is not meant to
ever be instantiated (that is, no object will be created from it). Rather, it exists only to
pass on common characteristics to more specific versions of itself. In our example,
these would be characteristics or important information common to all trucks, such
as numberOfWheels, allWheelDrive or groundClearance . Each
subclass of truck might then add more specific characteristics in their separate
implementations. Monster truck , for instance, might include a crushCar
method and a paintJob attribute. These are examples of single inheritance, which
we can now describe as a relationship in which one class inherits a set of charac-
teristics from a more general class. We often refer to this as an ''is-a'' relationship, a
term derived from the situational semantics. That is, a monster truck is a
truck , which is a vehicle , and so on from one class to another, more general
class.
To understand this further, we can extend our example again to an additional
superclass, vehicle , of which truck is a child. This new class is a step toward
greater generalization; an even more abstract class that can be used to create other
types of vehicles, such as car or spaceship . The vehicle class might contain a
few very general methods like move and stop , and some basic attributes like
fuelType and manufacturer ,
which
will
most
likely be
useful
to
all
subclasses. Figure 2.3 illustrates this example.
Search WWH ::




Custom Search