Polymorphism means the ability to take more than one form.
For example, an operation may exhibit different behaviour in different instances.
- A method defined in a class is inherited by all descendants of that class
- When a message is sent to an object to use method draw() , any messages that draw() sends will also be sent to the same object
- If the object receiving a message does not a have definition of the method requested, an inherited definition is invoked
- If the object receiving a message has a definition of the requested method, that definition is invoked
Polymorphism plays an important role in allowing objects having different internal structures to share the same external interface. This means that a general class of operations may be accessed in the same manner even though specific actions associated with each operation may differ.
Any Java object that can pass more than one IS-A test can be considered polymorphic. Other than object of types of type Object, all java objects are polymorphic in that they pass the IS-A test of their own type and for class Object.
Polymorphism is extensively used in implementing inheritance. In Java, a class cannot extend more than one class. That means one parent per class. However a class can have multiple ancestors. For example, class B can extend class A and class C can extend class B, and so on. So any given class might have multiple classes up its inheritance tree.