97. Trait methods
Trait-defined methods are much the same as we saw with classes but the private access modifier prevents us from calling a trait’s private methods. In the example below, the call to the private SportingEvent.rigWinner method (race.rigWinner()) will work but the call to the Running trait’s private method (race.slow()) will cause a groovy.lang.MissingMethodException:
trait Running {
void startEvent() {
println 'Ready, set, go!'
}
private String slow() {}
}
class SportingEvent implements Running {
String name
private String rigWinner() {
'We know that Jane will win this'
}
}
SportingEvent race = new SportingEvent(name: 'The Dodgy Race')
println "Welcome to $race.name"
println race.rigWinner()
race.slow()