What are the pros of using traits over abstract classes?

前端 未结 7 1463
-上瘾入骨i
-上瘾入骨i 2021-01-30 10:12

Can someone please explain traits in Scala? What are the advantages of traits over extending an abstract class?

7条回答
  •  心在旅途
    2021-01-30 10:34

    Similar to interfaces in Java, traits are used to define object types by specifying the signature of the supported methods.

    Unlike Java, Scala allows traits to be partially implemented; i.e. it is possible to define default implementations for some methods.

    In contrast to classes, traits may not have constructor parameters. Traits are like classes, but which define an interface of functions and fields that classes can supply concrete values and implementations.

    Traits can inherit from other traits or from classes.

提交回复
热议问题