Define inheritance.
Is a way of defining a new class as an extension of an existing class.
Give two advantages of inheritance.
Software reuse
Enables polymorphism
What is superclass? Give another name.
What is subclass? Give another name.
Class being inherited from. Base class.
Class inheriting. Derived class.
Define “is a” test.
Every instance of subclass is also an instance of superclass.
T or F. In Scala, a class can inherit from multiple super classes. Give syntax.
False, can only inherit from one.
e.g. class Dog extends Mammal
Explain method dispatch. e.g. a.breathe Give another name. When does it happen?
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.
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?
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.
Define method overriding.
Method in subclass with same signature as inherited method overrides it using override key word. (Must include or will not compile)
What is refinement?
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.
Distinguish between method overloading and method overriding.
Overloading:
- Different signatures
- Semantically unrelated
Overriding:
- Same signatures
- Semantically related
T or F. Fields can be overridden. If True explain.
True.
A parameterless method can be overridden by a val.
Give three advantages of override keyword being compulsory.
Define final, open, sealed, public, protected and private.
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.
Explain abstract class.
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.
T or F. You must use override keyword when overriding an abstract method or class.
False
T or F. Dynamic Dispatch is based on the object type not the reference type?
True.
What does the term Inheritance is a “package deal” mean?
A subclass inherits everything from all its parent classes. No selection.
What is the Fragile Base Class Problem?
Give an example of the fragile base class problem. (code)
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.
Explain compostion. Give robot / robotarm code.
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()
Distinguish between inheritance and composition under the following headings:
Relationship
Coupling
Flexibility
Maintenance
Best For
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
Define type system.
Is a collection of rules that assign a property called a type to the various constructs that comprise a computer program.
What is a type?
It tells you what something does / should be used for.
What are the three main goals of type systems?