Professional Documents
Culture Documents
Polymorphism: Polymorphism in Programming Languages A Taxonomy of Polymorphism Overloading and Dynamic Binding
Polymorphism: Polymorphism in Programming Languages A Taxonomy of Polymorphism Overloading and Dynamic Binding
Example
Graphite, diamond are modifications of carbon.
In programming
something: variables, functions, procedures, objects
shape: type
System.out.print(100)
System.out.print("100")
Polymorphic languages
Values and variables may have more than one type
Polymorphic functions
Actual parameters can have more than one type
Polymorphic types
Define operations that are applicable to actual parameters of more than
one type
s = dr.psDescription();
DisplayableObject
Universal
Polymorphism Inclusion
Polymorphism
Polymorphism
Operator
Overloading
Ad-hoc
Polymorphism
Type Coercion
Cardelli, L., Wegner, P. (1985): On Understanding Types, Data Abstraction, and Polymorphism,
ACM Computing Surveys, Vol. 17, No. 4
© Horst Lichter, RWTH Aachen University 8
Universal and Ad-hoc Polymorphism Parametric
Polymorphism
Universal
Universal polymorphism
Polymorphism Inclusion
Polymorphism
Ad-hoc polymorphism
Synonym: apparent polymorphism
Universal
Polymorphism Inclusion
Polymorphism
Polymorphism
Operator
Overloading
Ad-hoc
Polymorphism
Type Coercion
Operation usually has one ore more type parameters which determine the type of
the arguments for each application.
input, output and result
Admissible types usually have a common structure
Universal
Polymorphism Inclusion
Polymorphism
DisplayableRectangle
Universal
Overloading of operations
Polymorphism Inclusion
Polymorphism
Polymorphism
Type coercion
Coercion is a semantic operation needed to convert an argument to the
expected type
Programmer needs not to specify type conversions that are semantically
necessary
Type conversions are determined by the compiler and inserted to generate
the respective conversion code
Redef. Overl.
p1.equals(p2)
p1.equals(c1)
c1.equals(p1)
© Horst Lichter, RWTH Aachen 19
The Yoyo Effect
Taenzer, D. et al. (1989): Object-Oriented Software Reuse: The Yoyo Problem.
JOOP, Vol. 2, No. 3, pp 30-35.
draw getBitmap
void draw(){
this.getBitmap();}
getFactor
draw
void draw(){
super.draw();
this.getFactor();}
draw getTransMode
void draw(){
super.draw(); TranspIcon myIcon = new TranspIcon();
this.getTransMode();} myIcon.draw();
© Horst Lichter, RWTH Aachen 20
The Yoyo Effect
draw
void draw(){
super.draw();
this.getFactor();} draw getTransMode
g = c;
a = g.area();
g = r;
a = g.area();
}
g = c; dynamic type
By means of g = r;
Multiple dispatch
The types of all arguments together determine which method is executed
plus(a, b)
No emphasized message receiver
Printer.print(vg, pdf)
print can be used unchanged, even if new subclasses of DisplayableObject are added.
Construction is open and can be extended easily!
lead to: