Chapter 7Traits
Java permits only single implementation inheritance, but that forces a model of linear hierarchy. The real world, however, is full of crosscutting concerns—concepts that cut across and affect abstractions that do not fall under a single class hierarchy. Security, logging, validation, transactions, and resource management are all examples of such crosscutting concerns in a typical enterprise application. Since we’re limited to single class hierarchy, implementing these crosscutting concerns gets quite hard—it often requires duplication of code or use of heavyweight tools. Scala solves the problem using traits.
Traits are like interfaces with a partial implementation. Traits provide a middle ground between single and multiple ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access