Java Fundamentals

UML Class Diagrams and Relationships

Lecture 2

Class diagrams, properties, methods, and object interactions

Review of the last lecture

  • We approximately defined the object concept
  • We've tried to design an application diagram in order to share its functioning with other developers
  • It has been difficult to find a way to express ideas in a standard way, while designing the schemas representing the application

What we need as programmers : general needs

Basically, the utmost need is to be able to communicate in a professional environment

  • We need a common vocabulary
  • We need a standard diagramming "language"
  • We have to stay not to far from the technical aspect, say stay concrete

What we need as programmers : diagramming needs

In OOP (and almost in any kind of programming), we have to cover three types of diagram:
  • The general application lifecycle : the Activity Diagram
  • The description of the application technical entities (Classes) : the Class Diagram
  • A diagram showing the usage of the application parts by human participants : the Use Case Diagram

Solution matching our needs: UML

UML allows to give an answer to each of these needs
  • It defines a standardized vocabulary
  • UML gives predefined semantic shapes, in several diagramming categories
  • UML is a diagramming tool, so it can be understood by non-developer people
  • It is a language so it can be used by or be generated by other language applications

UML: Class diagrams

The class diagram allows to show the class organization, how they depend between each other, what are their interactions, etc.
This brings these questions:
  • How to define and represent an object regarding the UML syntax?
  • What can be the interactions between the several objects of an application ?

Class definition : properties

The first description of a real "object" is a factual description, with an enumeration of each property of that object
This property set is also called fields of that object
The dog object example

Class interactions : Messages and methods

The most basic interaction with an object is to send a message (an instruction) to it in order for this object to perform an operation.
To be able to receive and to treat this message, this object has to have a method corresponding to this message

Class interactions : Exercise

Remember our contact phone book? use the class diagram to represent each class, without representing their interaction

Class interactions : relations between classes

In UML each class relation is represented by a specific "link"
What are the different relations?

Class interactions : Association

An association between two classes is symbolized by a link between those two classes

Class interactions : Association(2)

An association has a cardinality, indicating how many object references the association is concerning
This should be read as: "An account can have up to two owners, and a person can have several accounts"

Class interactions : Aggregation

An aggregation is the fact for a class to be composed with an other class
There are 2 types of aggregation
  • The weak aggregation
  • The composition

Class interactions : the Weak Aggregation

A good example of a weak aggregation is the association between a Playlist and the Tracks of that playlist
Exercise : define the cardinality of the Playlist - Track association

Class interactions : the Weak Aggregation (2)

The weak aggregation is when:
  • The composed class can share its components with other composed class
  • The composed class has a reference to its components

The Weak Aggregation (3)

  • The schema above represents that an Email can be "aggregated" with a File
  • The aggregation is weak because the Email and the File lifecycles are not linked
  • The "aggregated" object can be used by several other objects

Class interactions : the Composition link

The composition is a sort of aggregation where the composite and the component objects are strongly linked

Class interactions : the Composition link

The composition is a sort of aggregation where the composite and the component objects are strongly linked
The composition link implies that :
  • the component object is attached to only one composite
  • The destruction of the composite implies the destruction of all component objects
In general, the aggregation link is known as the HAS-A relationship

Class Hierarchy

  • The class hierarchy represents the fact that several object can share the same characteristics
  • This description is very close to the species inheritance description
  • This relationship can be represented through this sentence :

    "A Wolf is a QuadrupedMammal, a QuadrupedMammal is a Mammal"

Class Hierarchy (2)

The inheritance mechanism is very useful to factor the common properties and methods shared by several objects
The inheritance is also known as the "IS-A" relationship
The previous sentence can be represented as follows

Class Hierarchy (3)
Specialization vs Generalization

You can read the diagram from right to left, in this way the description is called Generalization
In the other way, you start from a general Object and you finish on a specific one, this is called Specialization

When you specialize an object, the result object owns all the characteristics of its super-class plus its own characteristics

Class Hierarchy : Exercise

What are the properties and methods belonging to the wolf, according to the schema ?
What if I should include the Dog in that Class Diagram?

Class Diagram: (Real) Exercise

  • A bank system contains data on customers (identified by name and address) and their accounts.
  • Each account has a balance and there are 2 type of accounts: one for savings which offers an interest rate, the other for investments, used to buy stocks.
  • Stocks are bought at a certain quantity for a certain price (ticker) and the bank applies commission on stock orders.

Solution

UML - Activity Diagrams

Activity diagrams are used to define a succession of operations, describing the algorithm of the application
We will complete that section after a few time on eclipse

Slide Overview