Professional Documents
Culture Documents
OOAD Lect5 ClassModelling Part2
OOAD Lect5 ClassModelling Part2
Lecture 5
Class Modelling – Part 2
1
Contents
Class diagrams (continued) :
• Generalisation – specialisation
• Abstract Classes
• Polymorphism
• Aggregation
• Composition
• Association (Link) Classes
2
Generalisation (Inheritance)
UML@Classroom – 4.6
-employeeNo
-lastName
-firstName
-address
+getsalary()
+calculateLeaveLeft()
6
Generalisation in “Employee” class
Looking at this diagram, we can see that if a
Salesperson object were instantiated it would have
the following attributes:
• Area, carRegNo
Shape
area
colour
getArea()
triangle circle
square
angle1
angle2 sideLength radius
angle3 getArea()
getArea()
getArea() 8
There is a class hierarchy
• One class can be a subclass of another which is itself a subclass of
a third class.
• A subclass can also be a superclass. In the diagram below
Mammal is both a subclass of Animal and a superclass of Horse.
• There is no limit to the depth of this hierarchy.
9
From programming 1
10
From programming 1
11
The need for Generalisation
• Its common for an application to contain a
number of classes that are closely related :
12
The need for Generalisation
• In object-oriented programming, inheritance is a way to
form new classes using classes that have already been
defined.
15
Abstract Superclasses
In a generalisation structure it may be possible for an
object to be either:
– An instance of one of the subclasses of the generalisation
– An instance of the superclass of the generalisation
-employeeNo
-lastName
-firstName
-address
+getsalary()
+calculateLeaveLeft()
Employee
{abstract}
The above diagram indicates that there will never be any Employee
instances, only Salespersons, Engineers and/or Administrators 18
Polymorphism
UML@Classroom – 1.3.6 – Brief Description
19
Polymorphism
• A number of classes with a common superclass
• Same operation on many classes
• Same meaning, different implementation
Load Passengers
20
Polymorphism - note that each class has its own version
(implementation) of the calculateLeaveLeft() method
Employee
-employeeNo
-lastName
-firstName
-address
+getsalary()
+calculateLeaveLeft()
UML@Classroom – 4.5
UML@Classroom – 4.5.1
26
Aggregation Association
1..*
HonoursCourse Module
6..* 28
Aggregation Association
Home Computer
1
2 1 1 1 1
2 Speaker CPU Box Keyboard Monitor Mouse
1
1..* * 1 1 1..3 1
1
Sound Movement
Harddrive RAM DVD Graphics Button
Card sensor
Card
Is connected to 1
29
Composition Association
UML@Classroom – 4.5.2
30
Composition Association
• Aggregation implies a whole-part structure between
two classes
• Composition is also a whole-part structure, but has a
more defined property.
• It implies a coincident lifetime – the part object only
exists while the whole object exists.
• Or, to put it another way, the part object has only one
owner object (whole object).
• In composition a part cannot exist without being
part of a whole 31
Composition
Document
1 1..* 0..1
32
Association (Link) Classes
UML@Classroom – 4.4
33
Association (Link) Classes
• Sometimes the way in which two objects are associated is
just as important as the objects themselves.
• Consider, for example, the association between Student
and Module.
34
Association (Link) Classes
• Assume 2 students (Tom and Ruth ) taking two
modules (OOAD and Prog2) – and we need to store
their marks :
Tom 60 OOAD
Ruth 65 OOAD
Tom 45 Prog2
Ruth 40 Prog2
35
Association (Link) Classes
Tom 60 OOAD
65
45
Ruth 40
Prog2
• This would avoid confusing them conceptually with any other student’s
marks on this Module, or with this student’s marks on any other Module.
• We probably want operations too, if only to set and get the marks.
37
Association (Link) Classes
The result is a thing which is both an association and a class,
which is unsurprisingly called an association class.
::Mark
Date
ActualMark
38
Association (Link) Classes
::Student 1..* 6..* ::Module
::Mark
Date
ActualMark
Tom 60 OOAD
65
45
Ruth 40
Prog2 39
Association (Link) Classes