Lecture 7
Lecture 7
OOP Basics 2
UML Class Diagram
– 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.
6
Class Visibility
• The +, - and # symbols before an attribute and operation name in a class
denote the visibility of the attribute and operation.
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.
• 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:
• 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.
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:
14
Relationships
• There are mainly three kinds of relationships in UML:
– Dependencies
– Generalizations
– Associations
• Dependency
15
Generalization
• Generalization:
16
Association
• Association:
17
Association - Multiplicity
• 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.
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.
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.
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