0% found this document useful (0 votes)
30 views25 pages

Lecture 7

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)
30 views25 pages

Lecture 7

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/ 25

Lecture 7:

OOP Basics 2
UML Class Diagram

Object Oriented Programming


1
Introduction
• The UML Class diagram is a graphical notation used to construct and
visualize object oriented systems. A class diagram in the Unified Modeling
Language (UML) is a type of static structure diagram that describes the
structure of a system by showing the system's:

– classes,
– their attributes,
– operations (or methods),
– and the relationships among objects.

2
What is a Class
• A Class is a blueprint for an object. Objects and classes go hand in hand.
We can't talk about one without talking about the other. And the entire
point of Object-Oriented Design is not about objects, it's about classes,
because we use classes to create objects. So a class describes what an
object will be, but it isn't the object itself.

• In fact, classes describe the type of objects, while objects are usable
instances of classes. Each Object was built from the same set of
blueprints and therefore contains the same components (properties and
methods). The standard meaning is that an object is an instance of a class
and object - Objects have states and behaviors.

3
What is a Class
• A dog has
– states
• color, name, breed
– as well as behaviors
• wagging, barking, eating. An object is an instance of a class.

4
UML Class Notation
• A class represent a concept which encapsulates state (attributes) and
behavior (operations). Each attribute has a type. Each operation has a
signature. The class name is the only mandatory information.

5
UML Class Notation
• Class Name:
– The name of the class appears in the first partition.

• Class Attributes:
– Attributes are shown in the second partition.
– The attribute type is shown after the colon.
– Attributes map onto member variables (data members) in code.

• Class Operations (Methods):


– Operations are shown in the third partition. They are services the class
provides.
– The return type of a method is shown after the colon at the end of the method
signature.
– The return type of method parameters are shown after the colon following the
parameter name. Operations map onto class methods in code

6
Class Visibility
• The +, - and # symbols before an attribute and operation name in a class
denote the visibility of the attribute and operation.

• + denotes public attributes or operations


• - denotes private attributes or operations
• # denotes protected attributes or operations

7
Parameter Directionality
• Each parameter in an operation (method) may be denoted as in, out or
inout which specifies its direction with respect to the caller. This
directionality is shown before the parameter name.

8
Perspectives of Class Diagram
• The choice of perspective depends on how far along you are in the
development process. During the formulation of a domain model, for
example, you would seldom move past the conceptual perspective.

• Analysis models will typically feature a mix of conceptual and


specification perspectives.

• Design model development will typically start with heavy emphasis on the
specification perspective, and evolve into the implementation
perspective.

9
Perspectives of Class Diagram
• A diagram can be interpreted from various perspectives:

– Conceptual: represents the concepts in the domain

– Specification: focus is on the interfaces of Abstract Data Type (ADTs)


in the software

– Implementation: describes how classes will implement their interfaces

• The perspective affects the amount of detail to be supplied and the kinds
of relationships worth presenting. As we mentioned above, the class name
is the only mandatory information.

10
What is Class Diagram
• UML CLASS DIAGRAM gives an overview of a software system by
displaying classes, attributes, operations, and their relationships. This
Diagram includes the class name, attributes, and operation in separate
designated compartments.

• Class Diagram defines the types of objects in the system and the different
types of relationships that exist among them. It gives a high-level view of
an application. This modeling method can run with almost all Object-
Oriented Methods. A class can refer to another class. A class can have its
objects or may inherit from other classes.

• Class Diagram helps construct the code for the software application
development.

11
Purpose of Class Diagrams
• The main purpose of class diagrams is to build a static view of an
application. It is the only diagram that is widely used for construction, and
it can be mapped with object-oriented languages. It is one of the most
popular UML diagrams.

• Following are the purpose of class diagrams given below:

– It analyses and designs a static view of an application.


– It describes the major responsibilities of a system.
– It is a base for component and deployment diagrams.
– It incorporates forward and reverse engineering.

12
Vital components of a Class Diagram
• The class diagram is made up of three sections:

• Upper Section (class name): The upper section encompasses the name
of the class. A class is a representation of similar objects that shares the
same relationships, attributes, operations, and semantics. Some of the
following rules that should be taken into account while representing a
class are given below:

– Capitalize the initial letter of the class name.


– Place the class name in the center of the upper section.
– A class name must be written in bold format.
– The name of the abstract class should be written in italics format. 13
Vital components of a Class Diagram
• Middle Section (attributes): The middle section constitutes the attributes,
which describe the quality of the class. The attributes have the following
characteristics:
– The attributes are written along with its visibility factors, which are public (+),
private (-), protected (#), and package (~).
– The accessibility of an attribute class is illustrated by the visibility factors.
– A meaningful name should be assigned to the attribute, which will explain its
usage inside the class.

• Lower Section (methods): The lower section contain methods or


operations. The methods are represented in the form of a list, where each
method is written in a single line. It demonstrates how a class interacts
with data.

14
Relationships
• There are mainly three kinds of relationships in UML:
– Dependencies
– Generalizations
– Associations

• Dependency

– A dependency means the relation between two or more classes in which a


change in one may force changes in the other. However, it will always create a
weaker relationship. Dependency indicates that one class depends on another.

– In the following example, Student has a dependency on College

15
Generalization
• Generalization:

– A generalization helps to connect a subclass to its superclass. A sub-class is


inherited from its superclass. Generalization relationship can't be used to model
interface implementation. Class diagram allows inheriting from multiple
superclasses.
– For example, The Current Account, Saving Account, and Credit Account are
the generalized form of Bank Account.

16
Association
• Association:

• This kind of relationship represents static relationships between classes A


and B. For example; an employee works for an organization.

• Here are some rules for Association:


– Association is mostly verb or a verb phrase or noun or noun phrase.
– It should be named to indicate the role played by the class attached at the end
of the association path.
– Mandatory for reflexive associations

• For example, a department is associated with the college.

17
Association - Multiplicity
• Multiplicity

• A multiplicity is a factor associated with an attribute. It specifies how many


instances of attributes are created when a class is initialized. If a
multiplicity is not specified, by default one is considered as a default
multiplicity.

• Let's say that that there are 100 students in one college. The college can
have multiple students.

18
Aggregation
• Aggregation is a special type of association that models a whole- part
relationship between aggregate and its parts.

• For example, the class college is made up of one or more student. In


aggregation, the contained classes are never totally dependent on the
lifecycle of the container. Here, the college class will remain even if the
student is not available.

19
Composition
• The composition is a special type of aggregation which denotes strong
ownership between two classes when one class is a part of another class.

• For example, if college is composed of classes student. The college could


contain many students, while each student belongs to only one college.
So, if college is not functioning all the students also removed.

20
Abstract Classes
• It is a class with an operation prototype, but not the implementation. It is
also possible to have an abstract class with no operations declared inside
of it. An abstract is useful for identifying the functionalities across the
classes. Let us consider an example of an abstract class. Suppose we
have an abstract class called as a motion with a method or an operation
declared inside of it. The method declared inside the abstract class is
called a move ().

• This abstract class method can be used by any object such as a car, an
animal, robot, etc. for changing the current position. It is efficient to use
this abstract class method with an object because no implementation is
provided for the given function. We can use it in any way for multiple
objects.

• In UML, the abstract class has the same notation as that of the class. The
only difference between a class and an abstract class is that the class
name is strictly written in an italic font.
21
Abstract Classes

• In the above abstract class notation, there is the only a single abstract
method which can be used by multiple objects of classes.

22
Class Diagram Example
• A class diagram describing the sales order system is given below.

23
Class Diagram in Software
Development Lifecycle
• Class diagrams can be used in various software development phases. It
helps in modeling class diagrams in three different perspectives.

1. Conceptual perspective: Conceptual diagrams are describing things in the


real world. You should draw a diagram that represents the concepts in the
domain under study. These concepts related to class and it is always language-
independent.

2. Specification perspective: Specification perspective describes software


abstractions or components with specifications and interfaces. However, it does
not give any commitment to specific implementation.

3. Implementation perspective: This type of class diagrams is used for


implementations in a specific language or application. Implementation
perspective, use for software implementation.

24
Best practices of Designing of the
Class Diagram
• Class diagrams are the most important UML diagrams used for software
application development. There are many properties which should be
considered while drawing a Class Diagram. They represent various
aspects of a software application.

• Here, are some points which should be kept in mind while drawing a class
diagram:
– The name given to the class diagram must be meaningful. Moreover, It should
describe the real aspect of the system.
– The relationship between each element needs to be identified in advance.
– The responsibility for every class needs to be identified.
– For every class, minimum number of properties should be specified. Therefore,
unwanted properties can easily make the diagram complicated.
– User notes should be included whenever you need to define some aspect of
the diagram. At the end of the drawing, it must be understandable for the
software development team.
– Lastly, before creating the final version, the diagram needs to be drawn on
plain paper. Moreover, It should be reworked until it is ready for final
submission. 25

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy