- Classes—Wrapping Data and Behavior Together
- Inheritance—Defining Classes in Terms of One Another
- Polymorphism—Overriding One Class Method with Another
- Summary
PolymorphismOverriding One Class Method with Another
The next OO trait we will discuss is polymorphism. Unlike inheritance, polymorphism is concerned with how a class presents itself to the outside world. Polymorphism roughly means "many forms," and alludes to the fact that a specific named behavior can be implemented in different ways by different classes.
In other words, classes can reuse behavior names but implement them differently.
Overriding
One of the common examples used to demonstrate this concept involves a class library that describes geometric shapes. One of the behaviors that we would like to imbue into our shape classes is the capability to draw themselves. Using our basic knowledge of geometry, we know that each shape will require different parameters and use different operations to actually accomplish the draw operations (pi may be used when drawing circles, squares will need to know a side length, and so on). Because a procedural programming language doesn't allow us to reuse behavior names (think methods), we would end up with a different routine for each shape type such as DrawCircle, DrawTriangle, and so on. Because we can reuse method names with polymorphism, we can simplify the programming model considerably by reusing one method called Draw; each shape class would implement this in a slightly different fashion. This is called overriding and specific manifestations of this in the Framework Class Library are discussed in Chapter 4.
Overriding further promotes the concept of information hiding that we talked about earlier: Each class knows internally how to implement its behaviors, but calling classes don't know and don't care. We just send a message saying, "Draw," and the target class worries about how to carry it out. You will often see overriding with inheritance. A child class may override a parent class's methods to implement specific functionality not relevant to the parent class.
Overloading
A class may also override its own methods based on parameter lists. Consider the class shown in Figure 3.8.
Figure 3.8 Method overloading.
It represents a class, Square, and its draw methods. The implementation of the draw behavior differs based on the information that is passed into the Draw method. This is a special case of overriding called overloading. In our example here, we want to avoid implementing methods called DrawFromLength and DrawFromCoords; we simplify our class architecture by implementing just one method, Draw, and let it determine which implementation of Draw to use based on the function signature. In this way, both of the following would be valid method calls:
mySquare.Draw(10,"inches") mySquare.Draw(topLeftPoint, bottomRightPoint)
Polymorphism is really all about keeping interfaces between classes the same while allowing actual implementations to differ. This encourages loosely coupled object designs and hopefully clarifies system architecture and reduces complexity.