When we have the following basic classes
class X {
def f = println("X")
}
class Y extends X {
override def f = println("Y")
}
val a : X = Y
I think I am happy with why we get
scala> a.f
Y
But then I don't understand why we then have
scala> val b : AnyRef = new Array(10)
scala> b(0)
<console>:9: error: AnyRef does not take parameters
b(0)
since as far as I can tell AnyRef
is a superclass of Array
in a similar way to how X
was a superclass of Y
. If someone could explain this I would be very grateful.
If you look at the API documentation for AnyRef
you will note that it does not provide an apply
method - so the error makes sense.
You can see the same behavior if you change your example to add a method to Y
that is not available on X
:
class X {
def f = println("X")
}
class Y extends X {
override def f = println("Y")
def f2 = println("Not in X")
}
val a : X = new Y
scala> a.f2
<console>:11: error: value f2 is not a member of X
a.f2
^