Thread: OO and U
View Single Post
Old 05-20-2010, 03:31 AM   #41
cheung31
Scoring Winger
 
cheung31's Avatar
 
Join Date: Sep 2004
Location: Calgary, AB
Exp:
Default

Photon's recent example is polymorphism in action.

That animal interface, like Photon mentioned, is contractual. So an implementation of Animal interface, say, Cat should expect to have an Eat method implemented. Same with Crocodile or Dog. However, Eat may have slightly different behaviors between each animal type, Cat/Crocodile/Dog. (Type-specific behavior)

As a fellow programmer who wanted to use your Animal interface, I know that any subclass implementation (any animal I choose to create in the future) will have a Eat method that I can call because I had to implement it (contractual interface). Now imagine my client code is a representation of my farm. I will represent all the animals of my farm with some collection, say, an array/list composed of Animal instances. I can iterate over each element of this array/list, and call Eat() on the Animal instance. Each Animal instance will make the correct method call of its respective Animal type.

Polymorphism is possible due to late binding, where at runtime, the correct method is invoked based on the type of a particular object.
__________________

Last edited by cheung31; 05-20-2010 at 03:46 AM.
cheung31 is offline   Reply With Quote