How it comes that I instantiate an abstract class?
abstract class A {
val a: Int
}
val a = new A {val a = 3}
Or is some concrete cla
If you know Java, this is similar to:
new SomeAbstractClass() {
// possible necessary implementation
}
Due to Scalas uniform access it looks like you're not implementing any abstract functions, but just by giving a
a value, you actually do "concretesize" the class.
In other words, you're creating an instance of a concrete subclass of A
without giving the subclass a name (thus the term "anonymous" class).
you're instantiating an anonymous class that inherits from A and overloads its abstract a member. For reference, see the part about anonymous class instantiations in A Tour of Scala: Abstract Types
Scala allows you to create not only anonymous functions but anonymous classes too.
What you have done is simular to
class Anon extends A {
val a = 3
}
but without Anon
name
With this, you implicitly extend A
. What you did is syntactic sugar, equivalent to this:
class A' extends A {
val a = 3
}
val a = new A'
These brackets merely let you extend a class on the fly, creating a new, anonymous class, which instantiates the value a
and is therefore not abstract any more.