O'Reilly logo

Pragmatic Scala by Venkat Subramaniam

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required