Why does the Scala trait extend the class?

I see that features in Scala are similar to interfaces in Java (but interfaces in Java extend other interfaces, they do not extend a class). I saw an example in SO about the use of tags , where the dash extends the class.

What is the purpose of this? Why traits can extend classes?

+48
scala traits
Oct 12 '12 at 8:20
source share
1 answer

Yes they can, a trait that extends a class limits what classes can extend this trait , namely, all classes that mix in that trait should extend this class .

 scala> class Foo defined class Foo scala> trait FooTrait extends Foo defined trait FooTrait scala> val good = new Foo with FooTrait good: Foo with FooTrait = $anon$1@773d3f62 scala> class Bar defined class Bar scala> val bad = new Bar with FooTrait <console>:10: error: illegal inheritance; superclass Bar is not a subclass of the superclass Foo of the mixin trait FooTrait val bad = new Bar with FooTrait ^ 
+65
Oct 12 '12 at 8:23
source share



All Articles