Traits

Other topics

Basic Usage

A trait is a reusable set of methods and fields that can be added to one or more classes.

trait BarkingAbility {
    String bark(){ "I'm barking!!" }
}

They can be used like normal interfaces, using implements keyword:

class Dog implements BarkingAbility {}
def d = new Dog()
assert d.bark() = "I'm barking!!"

Also they can be used to implement multiple inheritance (avoiding diamond issue).

Dogs can scratch his head, so:

trait ScratchingAbility {
    String scratch() { "I'm scratching my head!!" }
}

class Dog implements BarkingAbility, ScratchingAbility {}
def d = new Dog()
assert d.bark() = "I'm barking!!"
assert d.scratch() = "I'm scratching my head!!"

Multiple inheritance problem

Class can implement multiple traits. In case if one trait defines method with the same signature like another trait, there is a multiple inheritance problem. In that case the method from last declared trait is used:

trait Foo {
  def hello() {'Foo'}
}
trait Bar {
  def hello() {'Bar'}
}

class FooBar implements Foo, Bar {}

assert new FooBar().hello() == 'Bar'

Contributors

Topic Id: 6687

Example Ids: 22756,25070

This site is not affiliated with any of the contributors.