Click on the above link to see a complete list of pages in this category.
PolyMorphism (what a horrible WikiName, but what can you do?) is the development of algorithms or data structures (or more concrete language constructs, such as objects or functions) which operate successfully on multiple types, doing things appropriate for the type.
Some take issue with polymorphism being based solely on "types". Answering a particular message (method call) does not necessarily depend on typing. Your viewpoint probably depends on where you stand in the static-versus-dynamic HolyWar.
[Note that even when there isn't a formal type system per se, one can still divine the concept of type. Smalltalk is often called untyped. However, if you have a set of classes that respond to the message
doThis: andThat: andTheOther:
one could say that this set represents a type, distinct from its inverse (which doesn't respond to the above message). Of course, you are getting away from type hierarchies, and into set-based or predicate-based type systems. Which are more flexible than hierarchies, but arguably more difficult to implement efficiently. --
ScottJohnson]
I am not sure set-based types or predicate-based types could even be called "types". I suppose one's world view could be that EverythingIsaType?, for good or bad. But, that is a subjective perception, not necessarily something objectively in say Smalltalk.
Types of polymorphism:
Is StaticPolymorphism GenericPolymorphism??
- SubtypePolymorphism?: Polymorphism based on subtyping (the notion that one type can extend/implement the behavior of another in such fashion that any valid operation on the supertype is valid on the subtype), subsumption (allowing a subtype to be used in a context where a supertype is expected), and DynamicDispatch (selecting a function or data based on the runtime type of its arguments - see SingleDispatch, DoubleDispatch, MultipleDispatch (aka MultiMethods)). JavaLanguage, CeePlusPlus, etc., all have subtype polymorphism. See also VirtualFunctions?, DynamicCast, RunTimeTypeInformation
- BoxingConversions: Automagically converting expressions of a type which has undergone TypeErasure to one that hasn't or vice versa. Going from "int" (an intrinsic type) to "Integer" (a class) in Java 1.5 (and CsharpLanguage, though the names might be different) is one example.
- AdHocPolymorphism: Anything else.
Also see
GenericVsObjectOrientedProgramming,
AlternateObjectOrientedProgrammingView,
OnUnderstandingTypes
Issues in polymorphism:
- InternalPolymorphism vs ExternalPolymorphism. Does a type itself "contain" knowledge of its identity, and participate in the implementation of polymorphism? Or is it entirely external to the type definition?
- PointerCastPolymorphism. What to do if you are working in a low-level language (like CeeLanguage)
- MultipleInheritance. Having more than one supertype. MultipleInheritanceIsNotEvil, MultipleInheritanceInSmalltalk, MixIn
- Inheritance vs delegation vs redirection. Inheritance (subclassing) is when all parts of a base type object are pasted into a derived type object (with some of them capable of being overwritten). Delegation (DelegationPattern) is kindasorta "inheritance by pointer", where a reference to a base type object is stored in the derived object; however, the subtype relationship is preserved. (Sometimes, multiple instances of a derived object can share the same base object instance). Redirection is similar to delegation, but it's "one-way". PolymorphismAndInheritance, DelegationInheritance, DelegationIsInheritance, PrototypeBasedProgramming, UseCompositionAndInterfacesWithoutClassInheritance, CompositionInsteadOfInheritance
- The infamous FragileBaseClassProblem.
- A few well-known modeling prolems. CircleAndEllipseProblem (see also CirclesAintEllipses, CirclesAreEllipses)
- Some argue that subtyping (and/or types themselves) is flawed, disagree on how subtypes ought to act with regards to their supertypes, a UsefulLie, or perhaps a not-so-useful lie. See PolymorphismLimits, ContextSensitiveSubtyping, LimitsOfHierarchies, LiskovSubstitutionPrinciple, WhatAreTypes, ThereAreNoTypes, SetsVersusTypes
- Are there other better ways of implementing polymorhpism than the ones we've seen implemented so far?
See Also: CategoryConditionalsAndDispatching, CategoryLanguageTyping
CategoryCategory