Lecture 11-Week11 - Relationships in OOP
Lecture 11-Week11 - Relationships in OOP
Lecture 11-Week11 - Relationships in OOP
(CS1143)
Week 11
2
Association
3
Association
4
Association Diagram
8
9
Aggregation
10
Aggregation
11
Aggregation Contd.
12
Example
13
14
15
16
17
18
19
Composition
20
Composition
21
Composition Contd.
22
Example
23
24
25
26
27
28
Dependency
29
Dependency
The third type of relationship that we can define between two classes
is dependency.
Dependency is a weaker relationship than inheritance or association.
Dependency models the “uses” relationship. Class A depends on class
B if class A somehow uses class B.
It other words, class A depends on class B if A cannot perform its
complete task without knowing that class B exists.
This happens when
Class A uses an object of type B as a parameter in a member function.
Class A has a member function that returns an object of type B.
Class A has a member function that has a local variable of type B.
30
UML Class Diagram for Dependency
31
UML Sequence Diagram
32
Example
We have two classes, First and Second. Class First has a member function
called fun() that the user cannot call directly in the application.
We want to use another function in the Second class called funny() to call
the function fun() in the First class.
We must pass an instance of the First class inside funny() so the First class
can use it when it calls its fun() class
The main function instantiates an object of the class First and an object of
the class Second.
The main function calls the funny(. . .) member function of the class Second
and passes an object of the class First as a parameter.
An object of the class Second can then call the fun() function of the class
First using the name of the object that received from main.
The relationship between objects First and Second is dependency. Object
Second uses class First in its member function funny(. . .), and object Second
calls the member function of object First.
33
34
Example
35
Class Diagram
36
Sequence Diagram
The main function must instantiate two objects of type Product and
one object of type Invoice.
The main function then calls the add function in the Invoice class to
add the products to the invoice, but it must get the price of each
product from the corresponding object.
37
38
39
40
41
42
43
This is all for Week 11
44