Relationships Among Classes

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 20

Relationships Among

Classes
Summary
Agenda
• Coupling, cohesion and modularity

• Dependency

• Association

• Aggregation

• Composition

• Inheritance

• Key Points
References
• Russ Miles and Kim Hamilton. Learning UML 2.0. O’Reilly, 2006.
Chapter 5

• Types Of Relationships In Object Oriented Programming (OOPS)


• https://www.c-sharpcorner.com/article/types-of-relationships-in-object-oriented-programming-oops/
Object-Oriented Principles

• The four principles (Pillars) of object-oriented programming are


• Abstraction
• Encapsulation
• Inheritance
• Polymorphism

These concepts must all be present in a programming language for it to be


considered an object-oriented programming language.
Types of Class Relationship
Classes work together using different types of relationships.

The strength of a class relationship is based on how dependent the classes involved in the relationship are on
each other
Coupling
Coupling is the measure of the degree of interdependence between the classes or
modules.

Causes of Coupling:

• A class may refer to objects that are instances of another class


• A class may call methods of another class and use the return values
• A class passes a complete data structure to another class
• Control flow is passed from one class to the other class.

• Ideally, modules should depend only on the published interfaces of other modules and not
on their internals.
Coupling Effects
If two classes are tightly coupled, changes to one class will most likely affect the other class

More interdependency Less interdependency


More coordination Less coordination
More information flow Less information flow
Cohesion
Cohesion of a module (or class) tells us how well the entities within a module (or class) work
together to provide a specific functionality

• To increase cohesion, we would like that all the constituents contribute to some well-defined
responsibility of the module (or class).

• Highly cohesive modules (or classes) tend to be more reliable, reusable, and understandable than less
cohesive ones.
Modularity
A system’s functionality must be provided by several well-designed, cooperating modules.
• Each module must provide certain functionality that is clearly specified by an interface.
• The interface also defines how other components may interact or communicate with the module.

• In OO programming, the layers of GUI, business logic, and data are a consequence of the idea of
modularity and separation of concerns principle.
The 3-layer Architecture
Our OO apps will have this architecture

Each layer has a clear responsibility


and a well-defined interface
Dependency
A dependency between two classes declares that a class needs to know about another class to use
objects of that class.

• In this example, the UserInterface is dependent on the BlogEntry class because it will need to read the
contents of a blog’s entries to display them to the user
• The UserInterface and BlogEntry classes simply work together at the times when the user interface
wants to display the contents of a blog entry.

A dependency implies only that objects of a class can work together; therefore, it is considered
to be the weakest direct relationship that can exist between two classes.
Association
Association means that a class will actually contain a reference to an object, or objects, of the other
class in the form of an attribute.

• A class works with an object of another class


• A class uses another class

it is usual to have only one class referencing the other in


One Blog account has zero or more Blog entries an association (Navigability)
(Multiplicity)
Aggregation
Aggregation is a specialized form of association between two classes in which each
object has its own life cycle but there exists an ownership as well.
• Aggregation is really just a stronger version of association and is used to indicate that a class
actually owns but may share objects of another class.
• Aggregation is a typical whole/part (or has-a) relationship, but it may or may not denote
physical containment.

The whole (or the owner) and the part can exist
independently
An Author owns a collection of blogs
Composition
Composition is a specialized form of aggregation. In composition, if the parent
object is destroyed, then the child objects also cease to exist.
• Composition is a strong type of aggregation and is sometimes referred to as a “death”
relationship.

Example: The relationship between a house and its


rooms.

A BlogEntry is made up of an
Example: The relationship between a book and its Introduction and a MainBody
chapters.
Inheritance Definition

Inheritance is the mechanism of deriving a class from existing ones (super classes). The new class
inherits attributes and behavior from the super classes.

• Hence, inheritance facilitates Reusability and is the way the “is a …” relationship is modelled.
• It is the strongest relationship between classes (the highest coupling)
Polymorphism Definition
Polymorphism is the ability to present the same interface for differing underlying
forms (data types).

• Class Polymorphism:
• An object of a subtype can play the role of a superclass object.
• For example, a Dog object can be treated as an Animal Object
Polymorphism Definition

Polymorphism is the ability to present the same interface for differing underlying
forms (data types).

• Method Polymorphism:
• In a program that manipulates different geometric shapes, the
methods to draw them would all have different
implementations.
• However, when we have a shape object, we know we can call
the draw function to draw the shape.
• The same interface is presented for all the different shape data
types.
UML Class Diagram - Example

Revised version
Key Points
• Simplicity is most valuable quality feature

• Highly cohesive classes (each class has one clear purpose)

• Low coupling (Each class has few well-defined relationships with other classes)

• UML is a graphic notation for representing the design of OO applications.

• A class diagram is a type of static structure UML diagram that describes the structure of a system
by showing the system's classes, their attributes, operations, and the relationships among
objects.
Exercise
• Create a Class Diagram for the SIA system.

• It should have at least 6 classes


• Each class must have at least one attribute and one method
• There must be all types of relationships between classes
• The most important multiplicities must be represented

You might also like