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 ...

Get Pragmatic Scala now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.