0% found this document useful (0 votes)
29 views

Relationships Between Classes

êwrgewg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Relationships Between Classes

êwrgewg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

Relationships

between classes

Ths. Huỳnh Anh Tuấn

2023
Contents:
❖ Relationships introduction
❖ Dependencies
❖ Association, Aggregation &
Composition
❖ Inheritance & Realize
Section 1

RELATIONSHIPS
INTRODUCTION
Relationships introduction

◼ Relationships describe interactions between


classes or between objects in a system.
◼ There are six main types of relationships
between classes: Dependency, Association,
Aggregation, Composition, Inheritance and
Realization / implementation.
Relationships introduction
Section 2

DEPENDENCIES
Dependencies
◼ A dependency expresses the relationship
between a supplier A and a client B that relies on
it. There is a dependency because changes in
the supplier A can effect the client B. In this case,
class B depends on class A
◼ Weaknest class relationship:
➢ A class using another as parameter passed in a
method (most cases)
➢ A class using another inside a method
Dependencies
◼ Ex: The car relies on gasoline. If there is no
gasoline, the car will not be able to drive
Dependencies

◼ A dependency relationship is a “use”


relationship. A change in a particular thing
may affect other things that use it
◼ We use a dependency when it is necessary
to indicate that one thing uses another
Dependencies

This “using relationship” meaning if something


changes in Meeting (ex: time change) then this
would affect Schedule but the reverse is not true
Dependencies
◼ Ex: the Registration manager and the
Scheduling Algorithm, the Student.
Section 3

ASSOCIATION, AGGREGATION
& COMPOSITION
Association relationships
◼ Association: Indicates that a property of a
class holds a reference to an instance (or
instances) of another class
◼ Association is the most commonly used
relationship between a class and a class,
which means that there is a connection
between one type of object and another
type of object.
Association relationships
◼ Ex1: Cars and Drivers, one Car
corresponds to a particular Driver, and one
Driver can drive multiple Cars
Association relationships
◼ Ex: The Student class and the Course Offering
class -> the Student is registering for the Course
Offering. So, in a sense, the Course Offering
“has” students – contains students.
Association relationships
We can add a label to an association to describe the
nature of the relationship. In this case, it clarifies that
the student registers for CourseOffering
Association relationships

◼ There are four kinds of associations: two-


way associations, one-way associations,
self-association, and multiple-number
associations.
Association relationships
◼ We can also
add a
triangle to
further
clarify the
direction of
the
relationship.
◼ In this case, the triangle will indicate
that it’s the student that registers for
the course offering, and not the other
way around
Association relationships
In a multiplicity relationship, you can add a
number directly to the associated line to
indicate the number of objects in the
corresponding class.
◼ 1..1: Only one
◼ 0..*: Zero or more
◼ 1..*:one or more
◼ 0..1: No or only one
◼ m..n: at least m, at most n (m<=n)
Association relationships
◼ We can define
multiplicity at
either end of
the
relationship. If
we look at the
student, the
student can
register for two ◼ Whereas, if we look at the course
or more course offering, we can say that each
offerings. course offering can have or can
enroll between 1 and 50 students
Association relationships

Another Association relationship example


Aggregation and Composition

◼ Aggregations and Compositions


(Combinations) also belong to Associative
relations, but relations between classes of
affiliations are weaker than the other two.
Aggregation relationships

◼ An aggregation is a relationship between 2


classes in which 1 represents a larger class
like a whole consists of smaller classes
which are the part(s) of this whole.
◼ The whole and part can be separated.
Aggregation relationships
Bus drivers
and work
clothes and
hats are part
of the overall
relationship,
Work clothes and hats can be
but they can
worn on other drivers. Bus drivers
be separated.
can also wear other work clothes
and hats.
Aggregation relationships
Let’s consider a Course and the Course
Offering: the course consists of multiple
Course Offerings.

A Course is a whole and the Course


Offerings are the parts of this whole.
Aggregation relationships
We will add a solid line with a diamond on the
side of the whole class to indicate that the
course consists of multiple course offerings.

We could also add multiplicity information on


the aggregation to indicate how many classes
of the two types are involved in the relationship
Aggregation relationships

Another Aggregation relationship example


Compositon relationships
◼ The Compositon (Combination) relationship
represents the relationship between the
whole and part of the class, and the overall
and part have a consistent lifetime (The
whole and part cannot be separated)
◼ Once the overall object does not exist, some
of the objects will not exist, and they will all
die in the same life.
Compositon relationships

For example, a person is composed of a head


and a body. The two are inseparable and
coexist.
Compositon relationships

Another Composition relationship example.


Aggregation and Composition

◼ The code structure of the three types of


relationships such as composition,
aggregation, and association is the same
as using attributes to store the references
of another class.
◼ Therefore, they must be distinguished by
the relationship between the contents
Section 4

INHERITANCE & REALIZE


Inheritance (Generalization)
◼ Inheritance - also called generalization - is used to
describe the relationship between a general class,
which we normally call super-class and the more
specific class, a class the refines the super-class
and that we normally call sub-class
◼ A super-class is also called a base class /parent
class, and a subclass is also called a derived
class.
Inheritance (Generalization)

Shape is parent class


Circle and Rectangle are sub classes
Inheritance (Generalization)

◼ In the inheritance relationship, the subclass


inherits and reuses all the non private functions
(attributes, methods) of the parent class (the
reverse is not true)
◼ Subclasses contain additional information in
addition to the same information as the parent
class.
Inheritance (Generalization)

Buses, Taxis are Cars, they all have names,


and they can all be on the road.
Realization / Implementation
◼ Realization (Implementation) is mainly
used to specify the relationship between
interfaces and implementation classes .
◼ An interface (including an abstract class )
is a collection of methods. In an
implementation relationship, a class
implements an interface, and methods in
the class implement all methods of the
interface declaration.
Realization / Implementation

Circles, Rectangles are Shapes. Shape is just


an abstract concept of those figures
Realization / Implementation

Cars and Ships are Vehicles, and the vehicle is just


an abstract concept of a mobile tool, and the ship
and the vehicle realize the specific mobile functions
Strong to weak

◼ Among the six types of relationships, the code


structure of combination, aggregation, and
association is the same, and it can be
understood from the strength of the relationship.
The order from strong to weak is: inheritance →
implementation → composition → aggregation
→ association → dependency.
Class Diagram - Order System
◼ The class diagram below models a customer
order from a retail catalog.
◼ The central class is the Order. Associated with it
are the Customer making the purchase and the
Payment.
◼ A Payment is one of four kinds: Cash, Check,
Credit or Wire Transfer.
◼ The order contains OrderDetails (line items),
each with its associated Item.
Thank you for attention
Huỳnh Anh Tuấn

You might also like