Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

Share this Page URL

Chapter 20: Abstract Members > 20.5 Initializing abstract vals - Pg. 415

S ECTION 20.5 · Initializing abstract val s trait AbstractTime { def hour: Int def hour_=(x: Int) def minute: Int def minute_=(x: Int) } // getter for `hour' // setter for `hour' // getter for `minute' // setter for `minute' Listing 20.3 · How abstract var s are expanded into getters and setters. 20.5 Initializing abstract val s Abstract val s sometimes play a role analogous to superclass parameters: they let you provide details in a subclass that are missing in a superclass. This is particularly important for traits, because traits don't have a constructor to which you could pass parameters. So the usual notion of parameterizing a trait works via abstract val s that are implemented in subclasses. As an example, consider a reformulation of class Rational from Chapter 6, as shown in Listing 6.5 on page 111, as a trait: