0% found this document useful (0 votes)
25 views63 pages

UNIT-3 Ooad

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 63

Unit-2

Objects and classes


Uml object notations
Uml class notations
scope
Object construction and destruction
What are objects?
“A discreet entity with a well-defined boundary
that encapsulates state and behavior,
an instance of a class.”

Objects combine data and function as a unit.


Objects having
• Identification: Every object is uniquely identifiable.
• State: An object’s state is determined by its
attribute values.
• Behavior: An object’s behavior is “what it can do
for us” – its operations.
How to retrieve data from an object
• The way to get the data part of an object is by
calling one of the functions that the object
makes available. These functions are called
operations .

• Hiding the data part of an object behind this


layer of operations is known as encapsulation
or data-hiding.
• Every object is itself an instance of some class
that defines the common set of features
(attributes and operations) that are shared by
all instances of that class .
Behavior
There are certain things the printer can do for
us:
switchOn()
switchOff()
printDocument()
pageFeed()
clearInkJetNozzles()
changeInkCartridge()
• Invoking an operation on an object will often cause a
change in the values of one or more of its attributes, or in
its relationships to other objects, and this may constitute
a state transition.
• This is a meaningful movement of the object from one
state to another state.
• An object’s state can also affect its behavior.
• For example, if the printer is out of ink (object state =
OutOfBlackInk),
• then invoking the operation printDocument() will cause it
to signal an error.
• The actual behavior of printDocument() is therefore state
dependent.
What is a method

• An operation is the specification of a piece of


behavior.
• An implementation of that behavior is called a
method
An object’s state is determined by its attribute
values.
• The state of this object is the set of attribute values (in this case
1234567801, “Jim Arlow”, 300.00) held by the object at any point in time.
• Typically, some of these attribute values will remain fixed, and some will
change over time.
• For example, the account number and name will remain fixed, but we
hope that the balance goes up steadily!
• As the balance changes with time, we see that the object’s state also
changes with time.
• For example, if the balance is negative, then we may say that object is in
the state Overdrawn.
• As the balance changes from negative to zero, the object makes a
significant change in its nature – it makes the state transition from the
state Overdrawn to the state Empty.
• Furthermore, as the Account object’s balance becomes positive, it makes
another state transition from the state Emptyto the state InCredit.
• In fact, any operation invocation that leads to a substantive change in the
object’s nature creates a state transition.
• UML provides a powerful set of modeling techniques called state
machines for modeling state changes
An object’s behavior is “what it can do for us” – its
operations.
How the object can generate the
system behavior
• Objects generate system behavior by sending
messages to each other over links. This is
collaboration.
UML object notation
The UML object icon is a box with two
compartments –
The top compartment contains the object
identifier, which is always underlined.
What are classes?
• A class describes the features of a set of objects.
• “The descriptor for a set of objects that share the
same attributes, operations, methods,
relationships, and behavior.”

• Every object is an instance of exactly one class


Points related to class
• – a class determines the structure (set of
features) of all objects of that class.
• All objects of the same class must have the same
set of operations, the same set of attributes, and
the same set of relationships, but may have
different attribute values.

• Each object of the class has specific values for the


attributes defined by the class, and will respond
to messages by invoking the operations defined
by the class.
Just think how many classes are there
Relationship between class and
objects
• The relationship between a class and objects of that class is an
«instantiate» relationship.

• The dotted arrow is actually a dependency relationship that has


been given a special meaning by the stereotype «instantiate».
• anything inside («…») is known as a stereotype, and stereotypes
are one of the three UML extensibility mechanisms.
• A stereotype is a way of customizing modeling elements – a way of
creating variants with new semantics.
• In this case, the stereotype «instantiate» turns an ordinary
dependency into an instantiation relationship between a class and
objects of that class.
Dependency
• defines a dependency as,
• “A relationship between two elements in
which a change to one element (the supplier)
may affect or supply information needed by
the other element (the client).”
Class instantiation

• It is the creation of new instances of model


elements.
• In this case we are instantiating objects from
classes –
• we are creating new instances of classes.
constructor

• In most OO programming languages, there are


special operations, called constructors, which
really belong to the class itself rather than the
objects of that class.
• The purpose of constructor operations is to
create new instances of the class.
• The constructor allocates memory for the new
object, gives it a unique identity, and sets the
initial attribute values for the object.
• It also sets any links to other objects.
UML class notation
Analysis of class
In analysis models, you typically only need to
show the following:
class name;
key attributes;
key operations;
stereotypes (if they have business significance).
You typically do not show:

tagged values;
operation parameters;
visibility;
initialization values (unless they have business
significance).
Name compartment
• Class name is in UpperCamelCase – it begins with an
uppercase letter, and then is in mixed upper and lowercase,
with each word beginning in uppercase.
• Special symbols such as punctuation marks, dashes,
underscores, ampersands, hashes, and slashes are always
avoided. .

• Never abbreviate class, attribute, or operation names.


For example: DepositAccount is always preferable to DpstAccnt.

If there are domain specific acronyms (e.g. CRM - Customer


Relationship Management) that are in common use, and will
be understood by all readers of the model,
• Classes represent “things”,
• so they should have a name that is a noun or a
noun phrase,
• e.g. Person, Money, BankAccount.
Attribute compartment

Attributes are named in lowerCamelCase - starting with a lowercase letter


and then mixed upper and lowercase.

Attribute names are usually nouns or noun phrases as attributes indicate


some “thing” such as an account balance.

You must avoid special symbols and abbreviations.


Visibility
• Visibility controls access to the features of a
class.
Type

• The type of an attribute may be another class or a


primitive type.
• The UML specification defines four primitive types
that are used in the UML specification itself,
• The Object Constraint Language (OCL) is a formal
language for expressing constraints in UML models
• OCL defines standard operations for these types
(except UnlimitedNatural) and adds a new type
called Real.
Multiplicity
• multiplicity allows you to model collections of things or null
values.
• This constraints relating to the “number of things”
participating in a relationship.
• Multiplicity allows you to model two distinctly different
things by using a multiplicity expression on an attribute
• Collections – if the multiplicity expression results in an
integer greater than 1, then you are specifying a collection
of the type.
• For example, colors : Color[7]
• Null values – there is a difference in most languages
between an attribute that contains an empty or
uninitialized object,
example
Initial value
The initial Value allows you to set the value of an
attribute at the point of object creation.
• The initial Value allows you to specify the
value an attribute will take when an object is
instantiated from the class.
• This is known as the initial value of the
attribute because
Advanced attribute syntax
• «stereotype» attribute { tag1 = value1, tag2 = value2, …}

• Example: address {addedBy=“RAM", dateAdded="20MAR2004"}

• In this example, we have recorded that RAMadded the address


attribute to some class on 20 March 2004.
Operation compartment
• Operations are functions that are bound to a
particular class. As such, they have all of the
characteristics of functions:
• Name
• Parameters list
• Return type
Operation signature

• The combination of the operation name, types of


all the parameters, and the return type is the
operation signature
• Every operation of a class must have a unique
signature, as it is this signature that gives the
operation its identity.
• When a message is sent to an object, the
message signature is compared to the operation
signatures defined in the object’s class, and if a
match is found the appropriate operation is
invoked on the object.
Parameter direction

• Operation parameters can be given a


direction:
• operation( in p1:Integer, inout p2:Integer,
out p3:Integer, return p4:Integer, return
p5:Integer )
• If the direction is not specified, then it
defaults to in.
Parameter default values

• You can give an operation parameter a default


value.
• When the operation is called, if no value is
given for that parameter, its default value will
be used.
Query operations

• Each operation has a property called


isQuery.
• Is query : true(no change ) on
• Is query : flase
example
Scope
• There are two types of scopes
1)instance scope(object scope)
2)class scope.
• Scope.
• —Instance scope attributes and operations belong
to or operate on specific objects:
– –instance scope operations can access other instance
scope operations or instance scope attributes;
– –instance scope operations can access all class scope
attributes or operations.
• —Class scope attributes and operations belong to,
or operate on, the whole class of objects:
• –class scope attributes and operations can only
access other class scope operations.
Instance scope
objects have their own attributes defined in
their class.
so that different objects can have different
attribute values.
Similarly, the operations that you have seen so
far all act on specific objects.
This is the normal case, and we say that these
attributes and operations have instance scope.
Class scope
• Class scope attributes and operations belong
to, or operate on, the whole class of objects.
Notation of instance and class scope
What Scope determines access

• Whether an operation can access another feature of the class or not is


determined by the scope of the operation and the scope of the feature it
is trying to access.

• Instance scope operations can access other instance scope attributes


and operations, and also all of the class scope attributes and operations.

• Class scope operations can only access other class scope operations
and attributes.

• Class scope operations can’t access instance scope operations


because:
• there might not be any class instances created yet;
• even if class instances exist, you don’t know which one to use.
Constructors & destructors
• Constructors are special operations that
create new instances of classes – these
operations must be class scope.
Naming constructors
• Different languages have different standards
for naming constructors.
• A completely generic approach is just to call
the constructor create(…).

• Java, C#, and C++ all demand that the


constructor name is the same as the class
name.
A class may have many constructors, all with the
same name,
but each distinguished by a different parameter
list.
The constructor with no parameters is known as
the default constructor.
You can use constructor parameters to initialize
attribute values at the point of object construction.
Example on constructor &destructors
• The numberOfMembers attribute is a private class scope
attribute of type int.
• This attribute is therefore shared by all objects of the Club
Member class, and will have the same value for each of
those objects.

• At the point at which the numberOfMembers attribute


is created, it is initialized to zero.
• Now, if this were an instance scope attribute,
• each object would get its own copy of it when the
object was created.
• However, it is class scope – this means that there is
only one copy, and this single copy is only initialized
once.
• create(...) operation you invoke the class scope
operation incrementNumberOfmembers().
• As you might expect from its name, this operation
increments the value of the class scope attribute
numberOfMembers.
• Every time an instance of the class is created,
numberOfMembers is incremented – a counter has
been added to the class!
• You can query the value of numberOfMembers by
using the class scope operation
getNumberOfMembers(). This returns a number that
is equal to the number of ClubMember objects
created.
destructors

• Destructors are special operations that “clean


up” when objects are destroyed.
• an operation to the class to decrement
numberOfMembers, then ensuring that this
operation is called every time an instance of
ClubMember is destroyed.
• Some OO languages have special instance scope
operations called destructors that are
automatically called at the point of object
destruction.
• In C++, for example, the destructor is always of
the form ~ClassName( parameterList ).
• In C++ the destructor operation is guaranteed to
be called at the point at which an object is
destroyed.

You might also like