Subtype Polymorphism, Template Methods, Intro to Traits, Stackable Traits Flashcards

(24 cards)

1
Q

Give another name for subtype polymorphism.

A

Inclusion polymorphism.

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

Explain subtype polymorphism.

A

Enables an object of a subclass to be used wherever a value of its superclass is expected. Also when object is sent a message, chooses method to invoke based on runtime type.

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

Define polymorphic references.

A

Allows a single reference to refer to objects of different types during execution.

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

obj.foo(). What is the object called in terms of dynamic binding?

A

The receiving object

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

pet.speak is a method invocation but what would be a more accurate term and why?

A

message send, we send the pet object message speak and we do not know which method will be invoked it decides that.

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

If we (a) want default behavior with each subclass redefining this behavior slightly or (b) we want any subclasses to be able to respond to some message without default behavior, what should we use?

A

(a) Overriding
(b) Abstract Methods

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

Explain template method.

A

Is a skeletal method in the superclass (trait) that invokes several other methods which are not implemented in the superclass. The subclasses inherit it and must implement each of the abstract methods it invokes.

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

When should we use template methods?

A

When there is a related method for the classes.
No suitable default implementation exists.

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

T or F. The template method and any methods in it already fully implemented in the superclass should be declared final, we don’t want to let people change the implementation.

A

True

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

T or F. The flow of control in a Template method stays in the superclass.

A

False, it jumps between classes, which can make them hard to understand.

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

What is a hook method?

A

Is one that has an implementation in the superclass but is intended to be overridden.

Is a part of the template method where there is variability and you may change if you wish. No obligation like other abstract methods.

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

T or F. Traits can be instantiated unlike abstract classes.

A

False, neither can.

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

Define trait.

A

A trait is like a class, it can have a constructor, methods and implementations but it cannot be instantiated, it must be extended like an abstract class and it forces implementation of abstract methods in contains.

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

What is a concrete method?

A

A fully implemented method.

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

What is the name of the problem caused by multiple inheritance?

A

The Diamond Problem.

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

Explain how the Diamond Problem works.

A

Occurs where a class inherits from two or more parent classes who themselves share a common superclass. Issue is it is not clear if class D should inherit once or twice from A.

 A    /   \   B   C
\/
D
17
Q

How would you practically demonstrate diamond problem? (code)

A

class c1(val someValue)
class c2 extends c1
class c3 extends c1
class c4 extends c2, c3

And come up with a case where it makes sense for c4 to have someValue twice and another where it should have someValue only once.

Compiler cannot decide between the two options as it doesn’t have domain knowledge.

18
Q

What is the solution to the diamond problem?

A

Extending multiple traits instead (+ precise linearisation to enforce ordering)

19
Q

Distinguish between thin and rich interfaces.

A

Thin interface provides just enough for client to use the class. Not easy to use and code duplicate across clients.

Rich interface provides lots of methods. More work for implementor and hard to find relevant methods.

20
Q

Syntax for using the Ordered trait.

A

class X(val myVal: Int) extends Ordered[X]
def compare(other: X) = myVal - other.myVal

Must define compare method!!!

21
Q

Define stackable traits.

A

Multiple trait implementations can be mixed into a class and combined in a linear order. Where each trait can augment or modify behavior before delegating to next trait in stack using super.

22
Q

Explain how stackable traits works.

A
  • All traits extend abstract base class. (Can only mix with each other)
  • 1st trait in extends list fully implemented
  • 2nd trait in extends list overrides previous
  • 3rd trait in extends list overrides 2nd and so on.
  • Overrides are abstract (as overriden method unknown to them)

e.g. 2nd Trait in Extends List:
trait SomeTrait extends SomeBaseClass
abstract override def someMethod(i : Int) = super.someMethod(i + 1)

23
Q

Syntax for calling stackable traits using variables. What is var foo?

A

val foo = new default with SomeTrait with SomeOtherTrait

foo is an instance of an anonymous class.

24
Q

val myQueue = new BasicIntQueue
with Doubling with Incrementing. Explain how myQueue.put(1) would execute.

A

Search for put starts in BasicIntQueue, the one there is overridden by Doubling which is in turn overridden by incrementing.

So Incrementing.put is called first, it calls super.put which binds to Doubling.put which in turn calls super.put which binds to BasicIntQueue.put.