Polymorphism

From CSSEMediaWiki
(Difference between revisions)
Jump to: navigation, search
m (Reverted edits by Ebybymic (Talk); changed back to last version by Matthew Harward)
 
(One intermediate revision by one user not shown)

Latest revision as of 03:23, 25 November 2010

Polymorphism.jpg

Polymorphism is merely the ability of subclasses to act as an instance of their superclass or interface, for instance a Car or Boat instance being used as a Vehicle instance, or a Java class implementing the Comparable interface. This is one of the key underlying purposes of the OO concept.

Polymorphism allows (ideally) the reuse and simplification of objects and their associated code where two objects share a functional role and have similar (but not identical) implementations.

That is definitely how polymorphism works in strongly typed languages.

The second form of polymorphism is exhibited in dynamically typed languages. In dynamically typed languages any object B that has the same methods as another object A can be substituted for object A.

Opinions differ on weather the third type of polymorphism actually is polymorphism. This is what is sometimes referred to as a polymorphic method where a method has more than one signature and so the method used by the object is the one that has the correct signature. The signature is defined by the parameter list of the function. For example object C has two methods doStuff with different parameter lists. There is no reason why the return type could not also be included in the signature for the purposes of overloading.


Personal tools