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

Class diagram

This document provides an introduction to Class Diagrams in UML, detailing their purpose, components, and the concept of visibility in object-oriented design. It explains various types of relationships such as association, aggregation, composition, generalization, and realization, with examples for each. Additionally, it illustrates a simplified banking system as a practical application of class diagrams.

Uploaded by

its2002sara
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Class diagram

This document provides an introduction to Class Diagrams in UML, detailing their purpose, components, and the concept of visibility in object-oriented design. It explains various types of relationships such as association, aggregation, composition, generalization, and realization, with examples for each. Additionally, it illustrates a simplified banking system as a practical application of class diagrams.

Uploaded by

its2002sara
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Introduction to Software Engineering

(CS-313)

Dr Naeem Aslam
Class Diagram
Introduction to Class Diagrams
• A Class Diagram is a type of UML (Unified Modeling Language)
diagram that represents the structure of a system by showing its
classes, their attributes, methods, and the relationships among them.
• Purpose:
• To model the static structure of a system.
• To serve as a blueprint for implementation.
• To facilitate communication between developers and stakeholders.
Components of a Class Diagram
A. Classes
B. Relationships
C. Visibility
Class Diagram
• Think about the things in the world around you. The things that surround you have attributes
(properties) and they behave in certain ways. We can think of these behaviors as a set of operations.
• You'll also see that things naturally fall into categories (automobiles, furniture, washing machines...).
We refer to these categories as classes.
• A class is a category or group of things that have the same attributes and the same behaviors.
• Here's an example:
• Anything in the class of washing machines has attributes such as brand name, model, serial number,
and capacity. Behaviors for things in this class include the operations "accept clothes," "accept
detergent," "turn on," and "turn off."
• Figure 1.1 shows an example of the UML notation that captures these attributes and behaviors of a
washing machine.
• A rectangle is the icon that represents the class. It's divided into three areas. The uppermost area
contains the name, the middle area holds the attributes, and the lowest area holds the operations.
Class

Attributes

Methods/Functions
Visibility in Object-Oriented Design
In object-oriented programming (OOP), visibility defines the accessibility of a class's members
(attributes and methods) to other parts of the system. It determines who can access or modify a
particular member of a class.
Types of Visibility
1 Public(+):

Definition: Members marked as public are accessible from anywhere in


the program.
Usage: Used for methods or attributes that need to be accessed by all
parts of the application.
Types of Visibility
2 Private(-):
Definition: Members marked as private are accessible only within the
class they are declared in.
Usage:Used to encapsulate the implementation details and prevent
direct modification from outside.
Types of Visibility
3 Protected(#):
Definition: Members marked as protected are accessible within the
same class, its subclasses, and other classes in the same package (in
some languages like Java).
Usage:Useful for providing controlled access in inheritance scenarios.
Types of Visibility
• 4 Package/ Default(~)
Definition: If no visibility modifier is specified, the member has
package or default visibility. It is accessible only within classes in the
same package.
Usage: Used to restrict access within the same package.
Relationships in Class Diagram
Class diagrams illustrate the structural relationships between different
classes in a system. Here are the key types of relationships
• Association
• Aggregation
• Composition
• Generalization
• Realization
Association
• Definition: Represents a generic relationship between two classes
where one class uses or interacts with another.
• Let's examine one- the association between a player and a team. You
can characterize this association with the phrase "a player plays on a
team.“ You visualize the association as a line connecting the two
classes, with the name of the association (“Plays on") just above the
line. You show how to read the relationship with a filled triangle
pointing in the appropriate direction. Figure shows how to visualize
the Plays on association between the player and the team.
Aggregation

• Definition: A weaker "whole-part" relationship where the part can


exist independently of the whole.
• The part and the whole have independent lifecycles.
• In Aggregation, the part can exist independently of the whole. This
means if the whole object is destroyed, the part object will still exist
on its own.
• UML Notation: A solid line with a hollow diamond at the end of the
class that represents the whole.
Aggregation Examples

•Aggregation represents a "has-a"


Whole
relationship without ownership. The
whole does not take ownership of
the part.
•Example: A Team and Players.
Players can exist even if the team is
dissolved.
Part
Aggregation Examples

A Library aggregates
Books.
If the library closes
(whole is destroyed),
the books (parts) can
still exist elsewhere.
Composition

• Definition: A strong "whole-part" relationship where the part cannot


exist independently of the whole.
• The part is tightly bound to the lifecycle of the whole. If the whole is
destroyed, the part is also destroyed.
• UML Notation: A solid line with a filled diamond at the end of the
class that represents the whole.
Composition examples

If the Car object is


destroyed, the
Engine object is
also destroyed.
Composition examples

A House consists of Rooms.


The rooms exist as part of the house and cannot function or
exist on their own without the house.
Generalization
• Definition: Represents an "is-a" relationship or inheritance between a
parent (superclass) and child (subclass).

•The subclass inherits attributes and methods from the


superclass.

•Promotes reuse of code.

• UML Notation: A solid line with a hollow triangle pointing towards


the superclass.
Generalization Examples

Example:

•A Dog is a subclass of Animal.


Realization
• Definition: Represents the relationship between an interface and a
class that implements it
• The class commits to implementing the operations defined by the
interface.
•UML Notation: A dashed line with a hollow triangle pointing towards
the interface.
Realization Examples

Example:
•A Printer class realizes the Printable interface.
Class Diagram of a System

• Let us consider a simplified Banking System.


• A bank has many branches. In each zone, one branch is designated as
the zonal head office that supervises the other branches in that zone.
Each branch can have multiple accounts and loans. An account may
be either a savings account or a current account. A customer may
open both a savings account and a current account. However, a
customer must not have more than one savings account or current
account. A customer may also procure loans from the bank.
• The following figure shows the corresponding class diagram.

You might also like