Inheritance, Type Systems, Poly Intro Flashcards

(35 cards)

1
Q

Define inheritance.

A

Is a way of defining a new class as an extension of an existing class.

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

Give two advantages of inheritance.

A

Software reuse
Enables polymorphism

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

What is superclass? Give another name.
What is subclass? Give another name.

A

Class being inherited from. Base class.
Class inheriting. Derived class.

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

Define “is a” test.

A

Every instance of subclass is also an instance of superclass.

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

T or F. In Scala, a class can inherit from multiple super classes. Give syntax.

A

False, can only inherit from one.

e.g. class Dog extends Mammal

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

Explain method dispatch. e.g. a.breathe Give another name. When does it happen?

A

First a method called breathe is sought in class to which a belongs.

If this fails search continues up inheritance hierarchy until a method called breathe is found in a superclass.

Method binding.

At runtime.

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

We have class Person(val name: String, var age: Int) Make an employee class which also has salary and is a Person. Explain your reasoning. How is superclass constructor invoked? Which constructor executes first superclass or subclass?

A

class Employee(name: String, age: Int, val salary: Double) extends Person(name, age)

name, age cannot be val / var as they will override superclass.

You never invoke the superclass constructor directly only pass arguments to it.

Superclass.

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

Define method overriding.

A

Method in subclass with same signature as inherited method overrides it using override key word. (Must include or will not compile)

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

What is refinement?

A

Means the overriding method does the same thing as the method it overrides and then does a little bit more. Method invokes a method of the same signature using super.

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

Distinguish between method overloading and method overriding.

A

Overloading:
- Different signatures
- Semantically unrelated

Overriding:
- Same signatures
- Semantically related

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

T or F. Fields can be overridden. If True explain.

A

True.

A parameterless method can be overridden by a val.

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

Give three advantages of override keyword being compulsory.

A
  • No accidental overriding
  • No accidental not overriding (e.g. signature mistake)
  • Prevents new public method being added to superclass which subclass has already defined.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Define final, open, sealed, public, protected and private.

A

Final: Prevents concrete class from being subclassed or a method from being overriden.

Open: Allows class to be subclassed anywhere.

Sealed: Class can only be subclassed by classes defined in the same file.

Public: Completely visible.

Protected: Only visible to the class and its subclasses.

Private: Only visible within the class.

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

Explain abstract class.

A

Is an incomplete class. Used to capture what its subclasses have in common. Methods need no implementation but all subclasses must give implementations for all non-implemented methods or themselves be declared abstract. Creates obligations for subclasses.

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

T or F. You must use override keyword when overriding an abstract method or class.

A

False

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

T or F. Dynamic Dispatch is based on the object type not the reference type?

17
Q

What does the term Inheritance is a “package deal” mean?

A

A subclass inherits everything from all its parent classes. No selection.

18
Q

What is the Fragile Base Class Problem?

A
  • A subclass depends on all classes from it to inheritance hierarchy root.
  • -As software evolves, inheritance hierarchies get wider and deeper.
  • More and more dependency.
  • Change to superclass = changed behavior of dependent subclass
  • A lot depends on root class
  • A fragile root leads to problems especially if overriding is being used
19
Q

Give an example of the fragile base class problem. (code)

A

open class Person(…):
def walk: String =
def run(…): Int =

This is the original base class. Let’s say we get a new requirement to start running after a certain time walking. We add line run(…) in walk. We unit test Person and all seems fine. But code breaks because there is a subclass.

class Athlete extends Person
def run(…): Int =
//Take Break
walk()

Creates infinite loop in subclass and we wouldn’t even know when changing Person.

20
Q

Explain compostion. Give robot / robotarm code.

A

Rather than use inheritance we simply create a variable which has an instance of the class we would inherit from.

class Robot:
private val arm = RobotArm()

21
Q

Distinguish between inheritance and composition under the following headings:
Relationship
Coupling
Flexibility
Maintenance
Best For

A

Inheritance:
Relationship: “is-a”
Coupling: Tightly coupled
Flexibility: Less flexible
Maintenance: Complex and brittle with deep hierarchies.
Best For: Natural hierarchies

Composition:
Relationship: “has-a”
Coupling: Loosely coupled
Flexibility: More flexible
Maintenance: Easier to maintain and modify
Best For: Dynamic relationships and modular design

22
Q

Define type system.

A

Is a collection of rules that assign a property called a type to the various constructs that comprise a computer program.

23
Q

What is a type?

A

It tells you what something does / should be used for.

24
Q

What are the three main goals of type systems?

A
  1. Reduce bugs (compiler prevents runtime type errors)
  2. Make code more readable (count: Int tells you range of values count can take)
  3. Enable compiler optimisations
25
Distinguish between strong and weak typing.
A strong type system prevents type errors. A weak type system allows the possibility of type errors. Assumes programmer knows what they are doing.
26
Distinguish between static and dynamic typing with examples.
Static: Catches errors at compile-time. Used in serious production development. Tedious but safe. E.g. Java Dynamic: Catches errors at runtime. Used for prototyping and quick development. Fun but unsafe. E.g. Python
27
T or F. Static typing is unrelated to a strong type system.
True
28
Type system of Scala.
Strongly and statically typed and employs type inference. (gives dynamic feel)
29
Relationship between class and type.
class = type + implementation
30
What is the type of an empty List? What does Nothing return is it the same as Unit?
List[Nothing] It is not the same as Unit it never returns anything.
31
Define polymorphism.
Is the ability of different objects to respond to the same message. Single interface, many forms.
32
Name the three types of polymorphism.
Ad hoc polymorphism Parametric polymorphism Subtype polymorphism
33
What is Ad Hoc Polymorphism?
Method Overloading
34
Explain Parametric Polymorphism with an example.
Implementing a List uses parametric polymorphism. List is a generic class / type constructor it requires a type parameter in order to create a class. E.g. List[Int] is a parameterised class. List has a single interface but an infinite number of classes possible.
35
Syntax for creating a generic class SortedList which only allows for types that can be ordered. What method does it force to be implemented?
class SortedList[A <: Ordered[A]] Required method compare must now be implemented in types.