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

Method Late Binding in Traits

In the previous example, the method check of the Check class was concrete. Our traits extended from this class to override that method. We saw how the call to super.check() within the traits were bound to either the trait on the left or the class that mixes in. Things get a bit more complicated if the method(s) in the base class are abstract—the method binding has to be postponed until a concrete method is known. Let’s explore this further here.

Let’s write an abstract class Writer with one abstract method, writeMessage:

UsingTraits/MethodBinding.scala
 
abstract​ ​class​ Writer {
 
def​ writeMessage(message: ​String​)
 
}

Any class extending from this class is required to implement the writeMessage method. If ...

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