Same signatures and return types

Consider an example where we want to mix two traits into a class and their declaration of a method is identical:

trait FormalGreeting {  def hello(): String}trait InformalGreeting {  def hello(): String}class Greeter extends FormalGreeting with InformalGreeting {  override def hello(): String = "Good morning, sir/madam!"}object GreeterUser {  def main(args: Array[String]): Unit = {    val greeter = new Greeter()    System.out.println(greeter.hello())  }}

In the preceding example, the greeter is always polite and mixes both formal and informal greetings. While implementing, it just has to implement the method once.

Get Scala Design Patterns - Second Edition 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.