Explain the concept of Traits. Why would you use them instead of traditional inheritance?
Last updated
Traits share concrete methods across unrelated classes. A class can
usemany traits but extend only one parent, so you avoid artificial parent classes.Trait methods are copied into the class at compile time. They suit cross-cutting behavior like logging or serialization.
Traits have no type identity, so pair them with an interface for polymorphism. Overuse hides where behavior lives and risks naming collisions.