In this post Manuel takes a look under the UML skirt of a class and how the structure of a class is represented.

Firstly, a UML class is shown with a box. It wasn’t always like that – there used to be clouds and dazies… But I digress. See the paragraph “Outside the box” at the end of the post for more on the shape evolution.

As we are now stuck with a box, though, best to picture it as a toolbox with specific compartments for each type of tool. For the three ladies who read this blog: feel free to imagine a well-compartmentalized make-up bag instead:

Class Name

The name compartment in most cases contains the class name – i.e. the type it represents. For example: Programmer, Car, Food.

If needed however, the instance name – the name of a concrete object of that type – can be shown: that could be Me, MyChevrolet, MyCroissant.

Or, to be explicit, both can be shown as Me :  Programmer, MyChevrolet : Car, MyCroissant : Food.

 

 

Abstractions – be careful how you write my name

The way a class name is shown can mean different things. Italic is used to signify abstract classes or interfaces.

This example shows the abstract class Vehicle, which the class Car implements (or derives from) – more on that in the post about relationships between classes.

 

Attributes (it’s not what you think…)

An attribute is what a property of the class is called – most often this would be a class field.

Let us take the Programmer as an example – first making an appearance in pseudocode:

class Programmer
{
public:
const string name = "Mystery Man";
protected:
void m_salary;
private:
int m_age;
};

Our class has three fields:

  • name – a public one of type string, which is also read-only (constant) and comes with a default value;
  • m_salary – a protected property; void (in all honesty, a numeric type would be more suitable for it, but I couldn’t resist the pun);
  • m_age – private, of type int (wonderful, isn’t it: even if you are 39.992 years old and are turning 40 tomorrow, the class will think you are still in your 30s!)

Of course, UML says these nearly thousand words with a single picture:

 

Attributes can also have multiplicity, but I’ll talk about that when I get on the topic of relationships.

Operations – what can it do for me?

An operation would normally map to a class method – an action that the class can do.

The syntax for representing these in UML is similar to what we have seen for attributes with the addition that operations can have parameters.

I’ll let my pseudocode and left-handed drawing tell the rest. Adding operations to my Programmer class:

class Programmer
{
public:
int fixBug( int bugNumber ); 
int estimateTask( string taskName, int maximumHours = 8 ); 
const string name = "Mystery Man";
protected:
void m_salary;
private:
int m_age;
};

Outside the box…

Do you know the Three Amigos? If you don’t or if the name of a famous tenor comes to mind, don’t worry – I won’t embarass you. The Three Amigos are the three developers, who gave start to UML: James RumbaughGrady Booch and Ivar Jacobson.

So, in the beginning were clouds… That was Grady Booch’s idea of representing a class back in the dawn of UML:

 

Raumbach however argued for a box, and thank goodness for that! I’ll let you in on a secret: my colleagues and I (when I’m not… volunteered to blog here) are working on a diagramming tool and, as wonderful as the cloud idea may be, it’s a relief that we only have to work with boxes!

Definitions

  • UML – Unified Modelling Language
  • UML Sequence Diagram – a diagram, which describes a scenario in the behaviour of groups of collaborating objects
  • actor – something or someone, interacting with a system, but not part of it
  • lifeline – a line, representing the life span of a participant in the scenario
  • message – a line of communication between participants in the scenario (see “Messages in UML 2.0 Sequence Diagrams” for details on the types of messages and their use)
  • Class Diagram – a digram, which shows classes (object types), participating in a system, their properties and the relationships between them
  • Class Properties represent the features of a class, which can be attributes or associations

Resources

As much as I would like to take credit for the knowledge in this article, all the UML stuff comes from:

 

The cool cartoons are courtesy of zlotence.

About the Author

Manuel grew up in a small town. Went on to study Computer Science in a low level university in a slightly bigger town. Tried smoking for the first time. Didn't like it. Has been an employee of A Corporation, writing mostly C++ code for the last nine years. Bear with him.