top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Explain the significant differences between a trait and an abstract class?

+1 vote
537 views
Explain the significant differences between a trait and an abstract class?
posted Oct 13, 2016 by Karthick.c

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

A class can only extend one superclass, and thus, only one abstract class. If you want to compose several classes the Scala way is to use mixin class composition: you combine an (optional) superclass, your own member definitions and one or more traits. A trait is restricted in comparison to classes in that it cannot have constructor parameters (compare the scala reference manual).

The restrictions of traits in comparison to classes are introduced to avoid typical problems with multiple inheritance. There are more or less complicated rules with respect to the inheritance hierarchy; it might be best to avoid a hierarchy where this actually matters. ;-) As far as I understand, it can only matter if you inherit two methods with the same signature / two variables with the same name from two different traits.

answer Oct 18, 2016 by Shyam
Similar Questions
+4 votes

Like Java’s Collection class, what is top level class/trait of Scala’s Collection API? Please explain the some high level hierarchy of Scala’s Collection API?

...