Traits

Traits can contain members, methods and abstract methods

Class can inherit only one class but one class can use many traits.

Traits can be used to implement multiple behaviours in one class.

Syntax

Declaring trait:

Using trait:

Example

Result:

Explanation

  • At line 29 the class Student uses both of the templates.
  • This means that the Student class inherits all of the traits members and functions.