Chapter 04 Class and Objects
Chapter 04 Class and Objects
Chapter 03
1
Lecture’s Contents of Classes
and Objects
1. Nature of an Object
2. Relationship among Objects
3. Nature of Class
4. Relationship among Classes
2
Class & Object
• In Computer Science an object is any entity
that can be manipulated by the commands of
a Programming Language, such as a Value,
Variable, Function or Data Structure. (With the
later introduction of Object-Oriented
Programming the same word, "object", refers
to a particular instance of a Class)
3
• In object-oriented programming, a class is a
construct that is used as a blueprint (or
template) to create objects of that class.
• This blueprint describes the state and
behavior that the objects of the class all
share. An object of a given class is called an
instance of the class.
• The class that contains (and was used to
create) instance can be considered as the
type of that object, e.g., an object instance of
the "Fruit" class would be of the type "Fruit".
4
• A class usually represents a noun, such as a
person, place or (possibly quite abstract)
thing - it is a model of a concept within a
computer program.
• Fundamentally, it encapsulates the state and
behavior of the concept it represents. It
encapsulates state through data placeholders
called attributes (or member variables or
instance variables); it encapsulates behavior
through reusable sections of code called
methods.
5
Objects
• We begin at the beginning as an object. The
world is made of objects.
• Just open your eyes and ears. Bank
customers, students, cats, elephants, cars,
balls of string, atoms, molecules, tubs of ice
cream, stars, bureaucrats.
• Objects are built of smaller objects. Objects
combine to make bigger objects. We already
live in an object-oriented world.
• Objects can be described by their attributes
and operations.
• Attributes are the changeable characteristics
of an object. Cats have color, size, weight.
• Operations are the things an object does or
can have done to it. Cats can catch mice,
eat, play with owners, etc.
• In our notation we draw an object such as a
cat like this.
• The name is shown at the top. The
attributes are listed underneath. The
operations are listed below that.
• Strictly speaking, this is a class diagram.
But we will explain that later.
• In an object model, all data is stored as
attributes of some object. The attributes of
an object are manipulated by the operations.
• The only way of getting at the attributes is
through an operation.
• In an object model, all functionality is
defined by operations. Objects may use
each others' operations, but the only legal
way one object can manipulate another
object is through an operation.
• Object modelling is
about finding objects,
their attributes and
their operations, and
tying them together in
an object model.
Nothing more. Here
are some more
objects:
• So, “An entity with a well-defined boundary and
identity that encapsulates state and behavior.
• State is represented by attributes and relationships,
behavior is represented by operations, methods. An
object is an instance of a class.”
Examples of Objects
14
– An attribute is an association where the class is ``small
enough'' to fit inside another class.
We denote it using:
rather than
15
Aggregation
– Aggregation is a special form of association. Maybe referred to
alternatively as:
– contains
– is composed of
– is comprised of
– is part of
Examples:
– A Car contains an Engine
– A Car contains a Transmission
– A Car contains a Body
– A Car is composed of many ( four, in this case ) Wheels
16
Composition
– Composition is the relation consists of where the components
cannot exist independently from the whole. Wheels can exist
independently from the car and are not necessarily destroyed when
the car is wrecked. The car registration cannot exist without a car
Multiplicity
– One-to-one (1:1)
– One-to-many (1:m)
– Many-to-many (m:m)
This can be shown on the association/aggregation /composition
line, where `*' means zero or more
17
– The following says that a Form can contain many (different) GUI
objects, including another Form. This is a potentially recursive
structure
M **
STUDENTS COURCES
18
Composition (has)
The below diagram shows the "has-a"
relationships between the Car, Motor and
Wheel classes
19
What is Object Oriented?
• ‘Object-orientation’ is a term used to describe the
object-oriented method of building software.
• In this approach, the data is treated as the most
important element, and it cannot flow freely around
the system.
• Restrictions (Limitation) are placed on the number
of units that can manipulate the data.
• Means, the approach binds (connect/combined) the
data & the methods that will manipulate the data.
• Object oriented (OO) methods blend (unify) data
and process into objects, and focuses on how those
objects interact using methods (passing messages)
20
• Traditional design creates entities (data tables),
and changes them using scripts, procedures,
macros, or other techniques
• OOA&D creates objects from classes,
and applies them using their methods
or operations
• In this given figure method1, method2, method3 &
method4 manipulate the data.
21
• OOP – Object Oriented Programming
• OOD – Object Oriented Design
• OOA – Object Oriented Analysis
OOP:
– Object Oriented Programming is a method of
implementation in which programs are organized as
cooperative collections of objects.
– Each of which represents an instance of some class, and
whose classes are all members of hierarchy of classes
united via inheritance relationships.
– There are three important parts:
» Uses objects not algorithms, consists fundamental logical
building blocks.
» Each object is an instance of some class and
» Classes are related to one another via inheritance relationship
22
– So, it satisfies the following requirements:
• Supports objects that are data abstractions with an interface
of named operations & a hidden local state.
• Objects have an associated type that is class.
• Types classes may inherit attributes from super type that is
super class.
OOD:
– Object Oriented Design is a method of design
encompassing the process of object-oriented
decomposition and
– It is a notation for depicting (show) both logical and
physical as well as static and dynamic models of the
system under design.
– There are two important parts:
» Leads to an object-oriented decomposition and
» Uses different notations to express different models of the
logical (class & object structure) and physical (module &
process architecture) design of the system.
23
OOA:
– Object Oriented Analysis is a method of
analysis that examines requirements from the
perspective of the classes and objects.
– So, the object-oriented analysis serve as the
models from which may start an object-oriented
design.
24
What is a Class?
• “A description of a set of objects that
share the same attributes, operations,
methods, relationships, and semantics.
A class may use a set of interfaces to
specify collections of operations it
provides to its environment.”
• Hence a class is a group of similar
objects
25
Class Example
Printers
information
behaviour
Relationship among Classes
Class Relationships
• Classes, by themselves, aren't particularly
useful. It’s the relationships among classes that
provide the foundation for the structure of a new
system. The following sections explore how you use
the UML to illustrate various kinds of class
relationships.
• Associations An association is a simple structural
connection between classes.
• There are two basic kinds of associations: binary
associations and n-ary associations.
28
Class Relationships
• A binary association, which exists between two classes,
appears as a straight line that connects them. Figure 2-1
shows some example associations between classes.
29
Binary Association:
• In a binary association is fully described by
indicating:
– Each class' relationship with the other
– Its cardinality and
– Its optionality.
Cardinality is expressed in terms of:
• one to one
• one to many
• many to many
30
N-ary Association:
• An n-ary association, which exists among three or
more classes, appears as a set of lines connected
to a central diamond.
31
Multiplicity Association:
• An association can show multiplicity. Figure the examples
of association multiplicity.
A ternary association is described in terms of the role that each class plays in the
association. For example:
• An agent negotiates a price between a buyer and a seller
• A buyer negotiates a price with a seller through an agent
• A seller negotiates a price with a buyer through an agent
33
Example
34
Overview of Various OOAD
Methodologies
• Booch Methodology:
– Grady Booch’s approach to OOAD is supported various
tools like Visio and Rational Rose.
– It covers requirement and domain analysis, but its major
strength is in design.
• Coad and Yourdon Methodology:
– The Coad and Yourdon methodology focuses on
analyzing the business problems. The analysis process
has five stages:
1. Subject
2. Objects
3. Structures
4. Attributes
5. Services
OO Objectives
• We want a development method which:
– can guide us through analysis, design, and
implementation using one consistent
paradigm throughout those phases
– is less likely to generate errors
– contains checks to help eliminate errors
– facilitates reuse of designs and code
36
The End
37