Book Image

Learning Object-Oriented Programming

By : Gaston C. Hillar
Book Image

Learning Object-Oriented Programming

By: Gaston C. Hillar

Overview of this book

Table of Contents (16 chapters)
Learning Object-Oriented Programming
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Taking advantage of polymorphism


We can use the same method, the same name, and same arguments to cause different things to happen according to the class in which we invoke a method. In object-oriented programming, this feature is known as polymorphism.

For example, consider that we define a talk method in the Animal class. The different subclasses of Animal must override this method to provide its own implementation of talk.

A Dog class will override this method to print the representation of a dog barking, that is, a Woof message. On the other hand, a Cat class will override this method to print the representation of a cat meowing, that is, a Meow message.

Now, let's think about a CartoonDog class that represents a dog that can really talk as part of a cartoon. The CartoonDog class will override the talk method to print a Hello message because the dog can really talk.

Thus, depending on the type of the instance, we will see a different result after invoking the same method along with the same...