Ask Your Question

What do you mean by Trait in Scala and when it is used?

What do you mean by Trait in Scala and when it is used?

A trait in Scala is a unique kind of Class that enables developers to use multiple inheritances. Although a trait can extend to only one class, a class can have many traits. However, unlike classes in Scala, you cannot instantiate traits.

Traits are often used when the user requires dependency injection (DI). In Scala, you do not have annotations or special packages for import. You must initialize the class with trait and dependency gets injected.