You are on page 1of 26

Topics to be covered

 Class modelling
 Associations
 Generalization/Specialization
 UML Representation of Generalization/Specialization

1
Objectives

 Objective is to study the concept of a class modelling


and Association i.e. how two different classes are
connected with each other and how they communicate.
 Another main objective of this lecture is to know the
UML Representation of Generalization/Specialization.

2
Class Diagram

 Provides a graphic notation for modeling classes and their


relationships, thereby describing possible objects.
 Class diagrams are concise, easy to understand and work well in
practice.
 Easiest way to represent the structure of application.
 Drawn with a rectangle, divided into 3 compartments
 Name compartment (class)

 Attribute compartment (Instance variables)

 Operations compartment (Methods)

3
Class Diagram Example

 Consider a Polynomial class (Poly)


 Form: a1x0+a2x12+ …. Anxn-1n
 Key attributes – terms and degree
 Instance Variables : - private variable; + public variable

Name of class
Poly
Instance Variables
- trms[] : int
- deg : int Methods will go here

4
Operations and Methods
 It is a function or procedure that may be applied to or by
objects in a class
 The same operation may be applied to many different
classes such operation is called polymorphic
 Operation has to be listed in the third compartment and
use lower case letter for the first letter
 Parenthesis encloses argument list and colon precedes
result type

5
Poly Class Diagram

Basic syntax: visibility name (parameter-list): return-type-expression


{property string}

Name of class
Poly
Instance Variables
trms[] : int
deg : int Methods will go here

add (thePoly: Poly): Poly


mul (thePoly: Poly): Poly

6
UML representation of classes/objects:

 We also occasionally use object diagrams.


 Object diagrams shows individual objects and their relationships.
 Object diagrams are helpful for documenting test cases.
 Class diagram corresponds to an infinite set of object diagrams.

7
Values and attributes

 Value : piece of data.


 Attribute: a named property of a class that describes a value held
by each object of the class.
 Attributes may be discovered by looking for adjectives or by

abstracting typical values.


 Don’t confuse values with objects:

 An attribute should describe values, not objects.

 Unlike objects, values lac identity

Person Smith:Person
Name : string John:Person
Name=“Smith”
Bdate : date
Bdate=01 Jan 1999

Class with attributes Objects with values 8


Summary of Notation of Classes

ClassName

attributeName1:dataType1=defaultValue1
attributeName2:dataType2=defaultValue2
operationName1(argumentList1):resultType1
operationName2(argumentList2):resultType2

9
Link and Association Concepts
 A link is a physical or conceptual among objects
 Most link relate two objects but some links relate three
or more objects.
 Association is a description of a group of links with
common structure and semantics

10
Associations

 A connection between classes.


 Usually classes “know” about each other (bidirectional.)
 Name of association necessary (“Uses” in below case.)

Uses
Author Computer

Specifically, an author uses a computer; hence, Author


has an association with Computer.

11
Multiplicity

 It specifies the number of instances of one class that may relate


to instance of associated class
 You can specify multiplicity for each association end.
 The most common multiplicities are

1. “1” exactly one


2. “0..1” At most one
3. “*” Many, zero or more
4. “0..*” Zero to many
5. “M .. N” from M to N
6. “M .. *” from M to many

12
The commonest multiplicities are:
 One-to-one
tax_coding

John Smith
760901-1234

Mary Jones
691205-5678

People Tax_codes
13
 One-to-many
mother_of

M.E. Meinke
K. Meinke

N. Meinke

Women
People

14
 Many-to-many

parent_of

M.E. Meinke
K. Meinke

K.W. Meinke
N. Meinke

People People

15
Examples: one-to-one

1 1
Class_A Class_B

one-to-many
1 *
Class_A Class_B

16
Examples: many-to-many

* *
Class_A Class_B

17
More Normal Associations

Owned
1..* by
Person Car
Owns 0..*

• A person owns 0-many cars. A car is owned by 1 to


many people.
• If relationships are not specified, then it is assumed to
be 1-1.
• Relationships can begin and end with any number ….
i.e. – can have 5..11 or 19..25.
• Associations are inherently bidirectional

18
Recursive Association

 Class that calls itself.


 Example: Network Nodes (Class Diagram)

*
Node

Connects
19
Association Class

 A class is attached to an association.


 Not connected at any of the ends of the associations.
 Has attributes and operations also.

20
Association Class

 Association “Queue” class.

Queue

Elevator Control 4 Elevator

Button

21
Visibility
 The simple idea is that any class has public and private
and protected elements.
 Public elements can be used by any other class;
 private elements can be used only by the owning class.
However, each language makes its own rules. Although
many languages use terms such as "public,"
"private," and "protected," they mean different things in
different languages.
 These differences are small, but they lead to confusion,
especially for those of us who use more than one
language.
22
 Within the UML, you can tag any attribute or operation with
a visibility indicator.
 However, the UML provides four abbreviations for visibility:
 UML denotes visibility with a prefix.
 + (public) features, -- any method can freely access public
features.
 - (private) features -- Only methods of the containing class
can access.

 # (protected) features -- Only methods of the containing class


and its descendants via inheritence can access protected features.

 ~ (package) features. -- Methods of classes defined in the same


package as target class can access package features. 23
Visibility Example
Class

+publicAttribute: Type
-privateAttribute: Type
# protectedAttribute: Type
~ packageAttribute: Type

+publicMethod()
-privateMethod()
# protectedMethod()
~ packageAttribute()
24
Question-answer session

 Q 1 What is a class model?


 Q 2 What is association?
 Q 3 What is Qualified Association?
 Q 4 What is ‘or’ association?
 Q 5 What is mean by Generalization and specialization?

25
Home Work

 Q 1 What is a class model?


 Q 2 What is association?
 Q 3 What is Qualified Association?
 Q 4 What is mean by Generalization and specialization?
 Q 5 What is ‘or’ association?
 Q 6 Explain the UML Representation of Generalization

and Specialization

26

You might also like