O'Reilly logo

Beginning Scala, Second Edition by Vishal Layka, David Pollak

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 7

image

Traits

In this chapter you will learn how to construct reusable parts of a program and deal with the tribulations of multiple inheritance, sidestepping the disadvantages of single inheritance by means of the mixing of compositions made possible by traits.

A trait provides code reusability in Scala by encapsulating method and state and then offering the possibility of mixing them into classes thus allowing code reuse. In this way a class can be mixed in with a myriad of traits unlike inheritance where each class is allowed to inherit from just one superclass. Moreover, other than using the keyword trait a trait definition resembles a ...

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