Chapter 7
Traits

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 inheritance because you can mix them in or include them in other classes. With this facility you can enhance a class or an instance with crosscutting features. With Scala’s traits you can apply crosscutting concerns to arbitrary classes without the pain that arises from multiple implementation inheritance, as you’ll learn in this chapter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.14.87.152