Chapter 3. OOP in Scala
This chapter covers
- Building a MongoDB driver using Scala classes and traits
- Pattern matching with case classes
- Looking into named and default arguments
Up to this point the book has been focusing on Scala’s fundamentals. This chapter introduces the object-oriented features of Scala. Object-oriented programming isn’t new, but Scala has added a few new features that aren’t available in other statically typed languages.
In this chapter you’ll build a Scala driver for MongoDB (www.mongodb.org/display/DOCS/Home). MongoDB is a scalable, document-oriented database. You’ll build this driver incrementally using the object-oriented constructs provided by Scala, and along the way I’ll explain each concept in detail. Scala has made some object-oriented innovations, and one of them is the trait. Traits are similar to abstract classes with partial implementation. You’ll explore how to use traits when building Scala applications. As you move through the chapter, you’ll learn about Scala case classes. Case classes are useful when it comes to building immutable classes, particularly in the context of concurrency and data transfer objects. Case classes also allow Scala to bridge the gap between functional programming and OOP in terms of pattern matching. Without wasting any more time, let’s learn OOP programming in Scala by building a MongoDB driver.