As far as I’ve learned, traits in Scala are similar to interfaces in Java except methods are allowed to have an implementation. Also, in contrast to Scala classes, you can’t pass them arguments for construction.
So far, so good. But why am I allowed to instantiate them? Really, I don’t see a good reason to allow this.
You don’t really instantiate them. As you drew a parallel with Java, let’s go further into it. You are able in Java to build a Anonymous class out of an abstract class or of an Interface. It is almost the same in Scala:
Note that the curly braces are mandatory when you create an object from a trait. For example, yon cannot do:
While it would works perfectly for a class:
Of course if some elements in your trait are not implemented, you need to implement them when you create the object: