Polymorphism Flashcards
Describe a monomorphic language
A language in which every value and variable can only be interpreted to be of one type
Define polymorphism
Some values and variables may have more than one type
What are the two categories for polymorphism?
Ad hoc
Universal
What are the four types of polymorphism?
Overloading
Coercion
Inclusion
Parametric
What are the two types of ad hoc polymorphism?
Overloading
Coercion
What are the two types of universal polymorphism?
Inclusion
Parametric
State some advantages of polymorphism
- allows method to perform similar tasks accessed under a common name
- allows you to define multiple constructors
- less repetition of code
Describe overloading polymorphism
Same function name is used to denote different types, operator can be applied to operands of different types
i.e.
2 + 3, “hello” + “world”
Describe coercion polymorphism
This is where an instance of one type is forced to be interpreted as another
e.g. if operator + could only add real numbers, integer types converted before operator applied
What does it mean for an object to be a subtype of another?
One type is a sub type of another if it related by extends or implements cause
What is another name for inclusion polymorphism?
Subtype polymorphism
Describe inclusion polymorphism
Instance of a subtype can be manipulated as if it was an instance of the supertype
Describe late binding
When a method call is made, dynamically select the version of the method that is used
Don’t know statically what will happen
Describe parametric polymorphism
The code features an implicit or explicit type parameter
Can be used for value of any suitable type
The code does not depend on the value given at run time
Define recursion
Process in which a problem is defined in terms of itself. The problem is solved by repeatedly breaking it into smaller problems which are similar in nature to the original problem.