Polymorphism Flashcards

1
Q

Describe a monomorphic language

A

A language in which every value and variable can only be interpreted to be of one type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Define polymorphism

A

Some values and variables may have more than one type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the two categories for polymorphism?

A

Ad hoc

Universal

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the four types of polymorphism?

A

Overloading
Coercion
Inclusion
Parametric

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the two types of ad hoc polymorphism?

A

Overloading

Coercion

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the two types of universal polymorphism?

A

Inclusion

Parametric

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

State some advantages of polymorphism

A
  • allows method to perform similar tasks accessed under a common name
  • allows you to define multiple constructors
  • less repetition of code
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Describe overloading polymorphism

A

Same function name is used to denote different types, operator can be applied to operands of different types
i.e.
2 + 3, “hello” + “world”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Describe coercion polymorphism

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does it mean for an object to be a subtype of another?

A

One type is a sub type of another if it related by extends or implements cause

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is another name for inclusion polymorphism?

A

Subtype polymorphism

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Describe inclusion polymorphism

A

Instance of a subtype can be manipulated as if it was an instance of the supertype

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Describe late binding

A

When a method call is made, dynamically select the version of the method that is used
Don’t know statically what will happen

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Describe parametric polymorphism

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Define recursion

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Advantages of recursion

A
  • reduces the length of the code
  • useful for dealing with inherently recursive structures, such as trees and graphs
  • makes the code more readable
17
Q

State some inefficiencies of recursion

A
  • excessive memory requirements (memory required by Java to keep track of all the recursive calls)
  • excessive re-computation
18
Q

What does it mean for a function to be tail recursive?

A

When the recursive call is the last thing executed by the function

19
Q

How does tail call optimisation work?

A

Since the recursive call is the last statement, there is nothing less to do in the current function, so saving the current function’s stack frame is pointless and it can be disregarded