UML and Class Diagrams
UML and Class Diagrams
UML and Class Diagrams
Object
Oriented
Programming
Lecture 1
Unified Modelling Language (UML)
Class Diagrams
Hansika Samanthi
UML
Unified Modeling Language
• The Unified Modeling Language is a standard visual modeling language intended to be used for,
• UML is a common language for business analysts, software architects and developers used to
describe, specify, design, and document existing or new business processes, structure and
behavior of artifacts of software systems.
Structural Diagrams
• Interaction diagrams are a subclass of behavioral diagrams that give emphasis to object
interactions and also depicts the flow between various use case elements of a system.
• In simple words, it shows how objects interact with each other and how the data flows within
them.
• Class diagram is UML structure diagram which shows structure of the designed system at the level
of classes and interfaces, shows their features, constraints and relationships - associations,
generalizations, dependencies, etc.
• Object diagram could be considered as instance level class diagram which shows instance
specifications of classes and interfaces (objects), slots with value specifications, and links
(instances of association).
Why Class Diagrams?
Name(Mandatory)
Attributes(Optional)
Operations(Optional)
Full Notation of a Class
Visibility Types
There are three common visibility types in UML class diagrams:
▪Public (+):
▪The attribute or method is accessible from anywhere.
▪Both within the class and from external classes.
▪It is denoted by a plus sign (+).
▪Private (-):
▪Restricts access to the attribute or method to only within the class in which it is
defined.
▪It is denoted by a minus sign (-).
▪Protected (#):
▪Allows access to the attribute or method within the class and its subclasses (i.e.,
classes that inherit from it).
▪It is denoted by a hash (#).
Objects Vs. Classes
▪ The instances of a classes are called as objects.
▪ Objects are represented as:
▪ These relationships define how the classes interact with each other and can represent
various aspects of the system's structure and behavior.
• Four main types of relationships:
• Association
• Aggregation
• Composition
• Inheritance (generalization)
• Dependency
Strength of the Relationships
Association
• Association connects one instance of a class with another instance of another class.
Bi-directional Uni-directional
• Here, it means that;
• Class A is associated with class B.
• Class A uses and contains one instance of class B, but B does not know about or contain any
instances of class A.
• In an association relationship, the dependent class (A) defines an instance of the associated class
(B) within its class scope.
Employee Department
▪ If yes, then the association is optional at the Employee end –zero ore more (0...*)
• Specialization takes place from super-class to sub-class: the sub-class is a specialization of the
super-class.
• The functionality of the child should be a specialization of the functionality of the parent.
Parent?
Child?
▪ Notes:
• The football club has lots of members. A member may be a playing member or a social member. A
playing member may be an adult member or a junior member.
• A playing member may be chosen to play for one or more teams, and each team has 11 playing
members. Each team plays a number of matches.
Misuse of Inheritance
1. Inheritance in place of aggregation:
• using “is a” instead of “has a”.
Car Engine
• The same rule for multiplicity can be applied for aggregation relationship.
1 0..*
Course Student
• Whole: Course
• Part: Student
• This implies that the Course can exist without any Students.
• This Implies that the whole can exist without the parts.
Composition
• Composition is a strong form of whole-part relationship.
• Whole: Building
• Part: Room
• A Building is composed of at least one Room.
• If there are no rooms, there is no Building.
• Implies that the “whole cannot exist without the parts”.
Dependency
▪ It is the simplest form of relationship between two classes.
▪ It implies that a change to one class may affect the other but not vice versa.
▪ As it may have a broad meaning, it is best not to overuse the dependency relationship.
• It means that;
• A uses B, But A does not contain an instance of B as part of its own state.
• “A” declares an instance variable of “B”. Use association to do that (covered next).
Cart class depends on a Product class because the Cart class uses the Product class as a parameter to
Add() method.
Changing the Product Class will affect the Cart Class.
What UML Diagrams can show??
▪ Division of Responsibility.
Operations that objects are responsible for providing.
▪ Subclassing.
Inheritance, generalization.
▪ Navigability/ Visibility.
When object need to know about other objects to call their operations.
▪ Aggregation / Composition.
When objects are part of other objects.
▪ Dependencies
When Changing the design of a class will affect other classes.
▪ Interfaces
Used to reduce coupling between objects.
Exercise 1
Person Class:
Properties:
First Name, Last Name, Contact No, Address
Operations:
displayFullName(): Output the full name (first name and last name) of the person.
setContactNo(): Modify the contact number of the person.
Exercise 2
Bank Account Class:
Attributes:
Account Number, Balance
Operations:
deposit (amount): Adds the specified amount to the account balance and return
the balance.
withdraw (amount): Subtracts the specified amount from the account balance and
return the balance.
Exercise 3
Imagine a university student system where students are categorized as either
Undergraduate or Postgraduate. All students have a unique student ID and a name.
Undergraduate students has a major field of study during their program. Postgraduate
students have a research area (e.g., "Machine Learning") and first degree qualifications. As
operations, postgraduate can request a supervisor and undergraduate can request for
hostel.
Exercise 4
In an institute building, there are multiple floors, each with its own number and name.
Classrooms are located on these floors and are considered a part of the floors. Each
classroom is uniquely named. Students attend classes in the classrooms but are not
exclusively assigned to one classroom. Some classrooms may not have any students at all.
Exercise 5
1. Identify a relationship that represent
aggregation?
Design the system for the online bookstore dealing with manual inventory, order
processing, and supplier management. Draw a use case diagram to outline the system's
functionalities and a class diagram to represent its structure.