15.2. Polymorphism
I mentioned in Chapter 1 that polymorphism is one of the three pillars of object-oriented programming. At that time I dismissed the topic, saying that the word polymorphism is derived from the Greek meaning "many shapes." Now that you understand what inheritance is, you are ready to more completely appreciate what polymorphism is.
Instead of sticking with the concept of "many shapes," perhaps I should amend the definition to mean "many messages." In essence, polymorphism means that you can send the same message to a group of different classes and that each class will know how to respond correctly to that message.
Try It Out: Using PolymorphismConsider the following example. Where I live, if more than two inches of snow falls, you are supposed to get out and shovel your walkways. Let's further assume that our software should notify someone at each property location when our property manager sees that two or more inches of snow has fallen. (I will just pretend that there is an electrical hookup between the software and the phone system. I'll use things displayed in the listbox object as an indicator that the call(s) have been made.) What we want to do is add a button that the manager can click when he sees that more than two inches of snow is on the ground. A sample run of the program is shown in Figure 15-3. Figure 15-3. Figure 15-3Notice that the apartment ... |
Get Beginning C# 3.0 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.