list of dots

User Manual    [Previous]   [Next]   


A trait is a group of functionality that serves as building blocks for classes. A trait implements such functionality through methods, state machines, and other modeling elements used for modeling behavior. Traits require a set of functionality which is defined by required methods. These required methods must be provided by clients of traits, either directly or indirectly. Clients of traits can be classes and other traits. In other words, a trait is a partial description of a class that can be reused in several different classes.

Traits can be used in place of standard inheritance where there is already a superclass, since multiple inheritance is not allowed in Umple to be consistent with Java and several other languages. Traits can be used to inject attributes, associations, state machines, constraints, methods and many other elements. They are one of several approaches in Umple to separation of concerns. The others are the mixin ability (ability to specify a class several times and have the elements added together), and the aspect oriented capabilities. Note that traits themselves are subject to being mixed in. You can declare two parts of the same trait in two different places in an Umple system.

Umple traits are defined through the keyword 'trait' followed by a unique name and a pair of curly brackets. The name must be alphanumeric and start with an alpha character, or the symbol (underscore). We also recommend capitalizing the first letter of traits names, as is the case for classes and interfaces in Umple. All elements of traits are defined inside the curly brackets except template parameters defined between the name and the curly brackets. In the current version, Umple traits can have methods, attributes, state machines, template parameters, required interfaces, and associations. Furthermore, Umple traits cannot be used as a type.

Required methods are defined similarly to the way abstract methods are defined in classes. They have exactly the same syntax, but it is also possible in traits to define required methods without the keyword abstract. If a method is defined like a normal method without a body (or implementation), the Umple compiler will consider that as a required method.

Provided methods are defined in the same way as concrete methods are defined in classes. Indeed, they have exactly the same syntax and semantics. Provided methods also support multiple code blocks, for generation of systems in different languages.

Traits are not part of UML. A UML class diagram drawn from an Umple file containing traits will 'flatten' the traits. The trait elements will appear in all classes that include that trait. In the first example below, the name and address attribute will appear in the class diagram of both Person and Company

The example 2 below shows a trait called TEquality. It has a required methods named isEqual and a provided method named isNotEqual. The provided methods uses the required method to satisfy part of its functionality.


 Example 1: use of a trait in Umple
 The trait has regular attributes, derived
 attribute and a method that are copied into
 Organization and Company  
trait Identifiable {
  fullName = {firstName + " " + lastName}
  Boolean isLongName() { return lastName.length() > 1;}  
class Person {
  isA Identifiable;
class Organization {
  Integer registrationNumber;
class Company {
  isA Organization, Identifiable;

Load the above code into UmpleOnline


Another Example

	Example 2: showing a trait designed for
	comparison regarding if two objects are equal
	or not.
trait TEquality{
  Boolean isEqual(Object object);
  Boolean isNotEqual(Object object){ 
    return isEqual(object) ? true : false;
// @@@skipcompile Java code not compilable no class

Load the above code into UmpleOnline



traitDefinition : trait [name] [[traitParameters]]? { [[traitContent]]* }

traitContent- : [[mixsetDefinition]]
    | [[requiredModelElements]]
    | [[comment]]
    | [[traitDefinition]]
    | [[trace]]
    | [[position]]
    | [[displayColor]]
    | [[abstract]]
    | [[keyDefinition]]
    | [[softwarePattern]]
    | [[depend]]
    | [[symmetricReflexiveAssociation]]
    | [[attribute]]
    | [[stateMachine]]
    | [[inlineAssociation]]
    | [[concreteMethodDeclaration]]
    | [[abstractMethodDeclaration]]
    | [[constantDeclaration]]
    | [[invariant]]
    | ;
    | [[exception]]
    | [[extraCode]]