|< Day Day Up >|
So far, we have covered the mechanics of object composition. We saw the evolution of the class definition to an object instance, variations on method and property definitions, and encapsulation by restricting access to those class members . We saw that we can create a Loan class that has a property called rate and a method called getPayment() .
All this is known as object composition. In the coming sections of this chapter, we establish relationships between objects. We call this process inheritance. We also cover the importance of inheritance in an object-oriented language and the mechanics of implementing inheritance in ActionScript 2.0.
With inheritance, we want to establish an "is a" relationship. We can say a banana "is a" fruit or a garage "is a" building. In both cases, we have established a relationship that equates to inheritance. Throughout this chapter, we cover the mechanics of developing "is a" relationships, the importance of them, and how they facilitate polymorphism.
What Is Inheritance Exactly?
We can equate inheritance in object-oriented programming (OOP) to human inheritance. Just as our children can potentially inherit all our characteristics and capabilities, so too can one object inherit from another. In the human genetic code, mother nature randomly selects which characteristics our offspring will have and which will be overridden.
In OOP we, as programmers, play mother nature. An object that is "a child" of another object has all the characteristics of the parent, unless we decide to replace or override the characteristic. We do not, however, use random logic to include or exclude a characteristic. Instead, we carefully plan our object hierarchy based on sound and proven patterns and methodology. A good object hierarchy is the foundation of a well-built, object-oriented application.
Where object-oriented inheritance differs from human inheritance is that in object-oriented inheritance, we can extend an object definition by adding methods and properties that don't exist in the ancestor. This enables us to start with a generalized set of characteristics in the ancestor and move to a more specialized set in the descendant.
Why Is Inheritance Important?
Reusability is one of the primary characteristics of OOP. Inheritance helps us to write application components that are efficient, compact, and reusable. Inheritance enables us to create new classes that are based on an existing class.
By creating a subclass of an existing class, the subclass contains, by virtue of inheritance, all the properties and methods of the superclass . The subclass typically defines additional methods and properties, which is also known as extending the superclass. Subclasses can also change an inherited behavior by overriding superclass methods.
For example, you might create a Loan class that defines certain properties and methods common to all loans. From the Loan class, you could then create a CarLoan class that extends the Loan class. This promotes code reuse. All loans have common elements, so instead of recreating all the code common to both classes, you can simply extend the Loan class. You can say a CarLoan is a kind of Loan. In Figure 5.1, you can say that an apple is a kind of fruit and fruit is a kind of food.
Figure 5.1. Fruit hierarchy.
In a complex application, the structure of the objects and their inheritance hierarchy is determined during the design process. But in many cases, inheritance is a natural progression during the development process where you find yourself writing the same code more than once. This is the first clue that you should consider an inheritance structure.
|< Day Day Up >|