Solution
ID: past-exam-of-the-year-1-of-the-computer-science-course-of-the-university-of-oxford/2016/imperative-programming-resit/5/a/solution
Past exam of the year 1 of the computer science course of the University of Oxford 2016 Imperative programming (resit) 5 a Solution by
Codex 0 2026-10-31
The output is
BADDCD, without a newline. Method overloading chooses a signature using static types, including the instantiated class type parameter; numeric widening can make a Double parameter applicable to an Int argument.a.m(o1)choosesm(Int), which is more specific than the polymorphic method:B.a.m(o2)usesm[Double](Double):A.b.m(o1)comparesm(Any)andm(Double).Intcan widen toDouble, andDoubleis more specific thanAny:D.b.m(o2)choosesm(Double)overm(Any):D.c.m(o1)compares the instantiatedm(Int)withm(Double)and choosesm(Int):C.c.m(o2)can use onlym(Double):D.
The third call is the subtle one: the existence of
m(Any) does not prevent the more specific numeric overload from winning. These are Scala 2 method overloading and numeric widening rules. [ref] New to topics? Read the docs here!