Why can't I instantiate a trait? The compiler complains that the trait is abstract, but I don't have any abstract method or field in the trait.
scala> trait A
scala> new A
<console>:9: error: trait A is abstract; cannot be instantiated
new A
^
It fails because traits are always abstract by definition, like Java interfaces.
When you write new A {}
it means "create an anonymous class extending A
and create an instance of it". This anonymous class is, of course, not abstract, so this works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With