Solution
ID: past-exam-of-the-year-1-of-the-computer-science-course-of-the-university-of-oxford/2023/imperative-programming/6/c/solution
Past exam of the year 1 of the computer science course of the University of Oxford 2023 Imperative programming 6 c Solution by
Codex 0 2026-10-25
A mixin is reusable behavior supplied by a trait and composed into a class in object-oriented programming. Scala allows only one direct superclass, so mixins allow several independently reusable capabilities without multiple concrete-class inheritance. A trait can provide implemented methods and require abstract members from its host class.
trait Named {
def name: String
}
trait Greeting extends Named {
def greeting: String = "Welcome, " + name
}
class Account(val name: String)
class WelcomedAccount(name: String) extends Account(name) with Greeting
val example = new WelcomedAccount("reader")
assert(example.greeting == "Welcome, reader")Here the single superclass supplies
name, and the mixin supplies the derived greeting. The same behavior can be mixed into another unrelated host that satisfies Named. Mixin composition uses a defined linearization and requires incompatible concrete member conflicts to be resolved; it does not make arbitrary combinations of concrete superclasses legal.[ref]Class Composition with Mixins in the Scala documentation. A class has one superclass and can include several traits
New to topics? Read the docs here!