| FazBrowse Wikipedia Viewer | Main Page | | |
| Viewing: https://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming |
| Contents |
|---|
| Subtyping |
| Row polymorphism |
| Rank polymorphism |
| Implementation aspects |
| Static and dynamic polymorphism |
| Polymorphism | |
|---|---|
| Ad hoc polymorphism | |
| Parametric polymorphism | |
| Subtyping | |
| classList<T>{
classNode<T>{
Telem;
Node<T>next;
}
Node<T>head;
intlength(){...}
}
List<B>map(Func<A,B>f,List<A>xs){
...
}
John C. Reynolds (and later Jean-Yves Girard) formally developed this notion of polymorphism as an extension to lambda calculus (called the polymorphic lambda calculus or System F). Any parametrically polymorphic function is necessarily restricted in what it can do, working on the shape of the data instead of its value, leading to the concept of parametricity. SubtypingeditIn the following Java example, the types Cat and Dog are, respectively, types of Animal. The procedure letsHear() accepts an Animal, which types of Animal obviously are (Cat and Dog both being Animal): Number :> Integer (Rational and Integer as subtypes of a type Number that is a supertype of them), a function written to take a Number will work equally well when passed an Integer or Rational as when passed a Number. The actual type of the object can be hidden from clients into a black box, and accessed via object identity. If the Number type is abstract, it may not be possible to access an object whose most-derived type is Number (see abstract data type, abstract class). This particular kind of type hierarchy is known, especially in the context of the Scheme language, as a numerical tower, and usually contains many more types.
Object-oriented programming languages offer subtype polymorphism using subclassing (also known as inheritance). In typical implementations, each class contains what is called a virtual table (shortly called vtable) – a table of functions that implement the polymorphic part of the class interface—and each object contains a pointer to the vtable of its class, which is then consulted whenever a polymorphic method is called. This mechanism is an example of:
The same goes for most other popular object systems. Some, however, such as Common Lisp Object System, provide multiple dispatch, under which method calls are polymorphic in all arguments. The interaction between parametric polymorphism and subtyping leads to the concepts of type variance and bounded quantification. Row polymorphismeditA related concept is polytypism (or data type genericity). A polytypic function is more general than polymorphic, and in such a function, "though one can provide fixed ad hoc cases for specific data types, an ad hoc combinator is absent". Rank polymorphismeditRank polymorphism is one of the defining features of the array programming languages, like APL. The essence of the rank-polymorphic programming model is implicitly treating all operations as aggregate operations, usable on arrays with arbitrarily many dimensions, which is to say that rank polymorphism allows functions to be defined to operate on arrays of any shape and size. Implementation aspectseditStatic and dynamic polymorphismedit
|