Object-oriented Programming
Concepts
AGENDA
1. What is OOP?
2. Classes and Objects
3. Principles of OOP
Inheritance
Abstraction
Encapsulation
Polymorphism
What is OOP?
Object-oriented programming is all about
creating a society of cooperating, active "agents"
(objects) that,
working together, accomplish the desired task.
AGENDA
1. What is OOP?
2. Classes and Objects
3. Principles of OOP
Inheritance
Abstraction
Encapsulation
Polymorphism
Objects
Objects are the physical and conceptual things
we find in the universe around us.
Hardware, software, documents, human beings,
and even concepts are all examples of objects.
For purposes of modeling his or her company, a
chief executive officer could view employees,
buildings, divisions, documents, and benefits
packages as objects.
Objects
Objects are the physical and conceptual things
we find in An automotive engineer would see
tires, doors, engines, top speed, and the current
fuel level as objects.
Atoms, molecules, volumes, and temperatures
would all be objects a chemist might consider in
creating an object-oriented simulation of a
chemical reaction.
A software engineer would consider stacks,
queues, windows, and check boxes as objects.
Objects
Objects are thought of as having state.
15
The state of an object is the condition of the object, or a
set of circumstances describing the object.
For example, the state of a bank account object would
include the current balance, the state of a clock object
would be the current time, the state of an electric light
bulb would be "on" or "off."
For complex objects like a human being or an automobile, a
complete description of the state might be very complex.
When we use objects to model real world or imagined
situations, we typically restrict the possible states of the
objects to only those that are relevant to our models.
Objects
We also think of the state of an object as something that is
internal to an object.
For example, if we place a message in a mailbox, the
(internal) state of the mailbox object is changed, whereas
the (internal) state of the message object remains
unchanged.
Sometimes people think of objects as being strictly static.
That is, the state of an object will not change unless
something outside of the object requests the object to
change its state.
Indeed, many objects are passive (static).
A list of names does not spontaneously add new names to
itself, nor would we expect it to spontaneously delete
names from itself.
Objects
However, it is possible for some objects to change
their own state.
If an object is capable of spontaneously changing its
own state, we refer to it as an "object with life."
Objects with life are sometimes also called "active
objects" or "actors.
Clocks and timers are common examples of objects
with life.
If we were modeling a business process, we would
recognize that salespeople and customers were also
objects with life.
Classes and Objects
A class describes the data and the methods of its objects
(also called instances).
Every object belongs to some class.
An object contains data (instance variables) representing
its state, and instance methods, which are the things it
can do.
Each object has its own copies of the instance variables.
A class describes the data and the methods of its objects
(also called instances). Every object belongs to some class.
An object contains data (instance variables) representing
its state, and instance methods, which are the things it
can do. Each object has its own copies of the instance
variables.
Classes
There are three commonly used (and different) views on
the definition for "class":
A class is a pattern, template, or blueprint for a category of
structurally identical items. The items created using the class
are called instances. This is often referred to as the "class as a
`cookie cutter'" view. As you might guess, the instances are the
"cookies."
A class is a thing that consists of both a pattern and a
mechanism for creating items based on that pattern. This is the
"class as an `instance factory'" view; instances are the individual
items that are "manufactured" (created) using the class's
creation mechanism.
A class is the set of all items created using a specific pattern.
Said another way, the class is the set of all instances of that
pattern.
Methods and massages
A method is a named executable chunk of code.
All executable statements must be in methods (or initializer blocks).
A method has a signature consisting of its name and the number
and types of its parameters (also called arguments or actual
parameters). The parameters in the declaration of the method are
its formal parameters.
A method has a return type, which is not part of its signature. If the
return type is other than void, then the method must return a
value of the specified type.
A method may have local variables (also called method variables).
These follow the scope rules, and are never available anywhere
outside the method. Formal parameters are a kind of local variable,
but have initial values as supplied by the corresponding actual
parameters.
Methods and massages
An instance method is executed by sending a message to an object.
The message consists of: a reference to the object (typically its
name), a dot, the name of the method, and zero or more actual
parameters enclosed in parentheses.
The object will respond by executing the corresponding method in
the actual class of the object, which may be different from the type
of the variable holding the object.
A class method is executed by sending a message to the class. The
message consists of: the name of the class, a dot, the name of the
method, and zero or more actual parameters enclosed in
parentheses. The class will respond by executing the corresponding
static method in that class.
When a message is sent, and before the method executes, the
values of the actual parameters are copied into the corresponding
formal parameters. Then the method body executes. Then the
return value replaces the message, and all local names are forgotten.
AGENDA
1. What is OOP?
2. Classes and Objects
3. Principles of OOP
Inheritance
Abstraction
Encapsulation
Polymorphism