OO Basics
1
Introduction to Object Orientation Topics
•
•
•
•
Overview
Basic Principles of Object Orientation
Basic Concepts of Object Orientation
Strengths of Object Orientation
2
Overview
• What are object-oriented (OO) methods?
– OO methods provide a set of techniques for
analyzing, decomposing, and modularizing software
system architectures
– In general, OO methods are characterized by
structuring the system architecture on the basis of its
objects (and classes of objects) rather than the
actions it performs
• What are the benefits of OO?
– OO enhances key software quality factors of a system
and its constituent components
3
OOA, OOD, and OOP
• Object-oriented methods may be applied to different
phases in the software lifecycle
– e.g., analysis, design, implementation, etc.
• Object-oriented analysis (OOA) is a process of
discovery
– models and understands the requirements
of the system
• Object-oriented design (OOD) is a process of
invention and adaptation
– creates the abstractions and mechanisms
necessary to meet the system's behavioral requirements
determined during analysis
4
Basic Definitions OOD, and OOP
• Object-Oriented Design (OOD)
– A method for decomposing software architectures based on
the objects every system or subsystem manipulates
– Rather than "the" function it is meant to ensure
• Object-Oriented Programming (OOP)
– construction of software systems using object oriented
concepts as inheritance, polymorphism, encapsulation,
dynamic binding.
• Distinguish between OOD and OOP
– OOD is relatively independent of the programming language
used.
– OOP is primarily concerned with programming language and
software implementation issues
5
Object-Oriented Approach
• Model objects that are part of the problem
• Have objects exhibit their normal behavior
• Add objects that do not have problem-space
analogs
• Have the objects work together to create a
solution
Design patterns help you identify less-obvious
abstractions and the objects that can capture them.
- GOF
6
Five Basic Characteristics of OO
"Pure" object-orientation by Alan Kay (Smalltalk):
• Everything is an object.
• A program is a bunch of objects telling each
other what to do by sending messages.
• Each object has its own memory made up of
other objects.
• Every object has a type.
• All objects of a particular type can receive the
same message.
7
Basic Principles of Object Orientation
Hierarchy
Polymorphism
Encapsulation
Abstraction
Object Orientation
8
What Is Abstraction?
• The essential characteristics of an entity that
distinguishes it from all other kinds of entities.
• Defines a boundary relative to the perspective of the
viewer.
• Is not a concrete manifestation, denotes the ideal
essence of something.
Manages Complexity
Customer
Salesperson: Not
saying Which
salesperson – just a
salesperson in
general!!!
Product
9
What Is Encapsulation?
• Hides implementation from clients.
– Clients depend on interface.
Improves Resiliency
10
Encapsulation
• "Encapsulation is a mechanism used to hide the
data, internal structure, and implementation details
of an object. All interaction with the object is through
a public interface of operations." (Craig Larman)
• A boundary exists around each object;
– the boundary encapsulates the object’s characteristics
(data elements) and behaviors (functionality).
• The reason for hiding features is to:
(1) keep users from touching parts of the object
they shouldn’t touch;
(2) allows creator of the object to change the object’s
internal working without affecting the users of the object.
11
What Is Polymorphism?
• The ability to hide many different
implementations behind a single interface.
Manufacturer A
OO Principle:
Encapsulation
Manufacturer B
Manufacturer C
Remote Control
12
Polymorphism
• Occurs with inheritance.
• Different subclasses may have different
implementations of the identical operations
• Allows you to treat an object as the base class,
rather than as a specific inherited type.
• Programmer doesn’t have to keep track of the
specific subclasses, the system selects the correct
operation based on the object type.
• Accomplished at run time using dynamic binding.
– lets you substitute objects that have identical interfaces
for each other at run-time.
13
What is Hierarchy?
Increasing
abstraction
Levels of abstraction Asset
BankAccount
Security
Savings Checking Stock
Decreasing
abstraction
RealEstate
Bond
Elements at the same level of the hierarchy
should be at the same level of abstraction
14
Basic Concepts of Object Orientation
•
•
•
•
•
•
•
Object
Class
Attribute
Operation
Interface
Package
Relationships
15
What is an Object?
• Informally, an object represents an entity, either
physical, conceptual, or software
– Physical entity
Truck
– Conceptual entity
Chemical Process
– Software entity
Linked List
16
A More Formal Definition
• An object is a concept, abstraction, or thing with
sharp boundaries and meaning for an
application
• An object is something that has:
– State
– Behavior
– Identity
17
What is a Class?
• A class is a description of a group of objects with
common properties (attributes), behavior
(operations), relationships, and semantics
– An object is an instance of a class
• A class is an abstraction in that it:
– Emphasizes relevant characteristics
– Suppresses other characteristics
OO Principle: Abstraction
18
Sample Class
Class
Course
Properties
Name
Location
Days offered
Credit hours
Start time
End time
a + b = 10
Behavior
Add a student
Delete a student
Get course roster
Determine if it is full
19
Representing Classes in the UML
• A class is represented using a rectangle with
three compartments:
– The class name
– The structure (attributes)
– The behavior (operations)
Professor
- name
- employeeID : UniqueId
- hireDate
- status
- discipline
- maxLoad
+ submitFinalGrade()
+ acceptCourseOffering()
+ setMaxLoad()
+ takeSabbatical()
+ teachClass()
20
Classes of Objects
• How many classes do you see?
21
Classes and Objects Relationship
• A class is an abstract definition of an object
– It defines the structure and behavior of each object in
the class
– It serves as a template for creating objects
• Objects are grouped into classes
Objects
Class
Professor
Professor Smith
Professor Mellon
Professor Jones
22
What Is an Attribute?
• An attribute is a named property of a class that
describes the range of values that instances of
the property may hold.
– A class may have any number of attributes or no
attributes at all.
Attributes
Student
- name
- address
- studentID
- dateOfBirth
23
What Is an Operation?
• A service that can be requested from an object
to effect behavior. An operation has a signature,
which may restrict the actual parameters that are
possible.
• A class may have any number of operations or
none at all.
Student
Operations
+ get tuition()
+ add schedule()
+ get schedule()
+ delete schedule()
+ has prerequisites()
24
What is an Interface?
• Interfaces formalize polymorphism
• Interfaces support “plug-and-play” architectures
Tube
<<interface>>
Shape
Pyramid
Draw
Move
Scale
Rotate
Cube
Realization relationship
(stay tuned for realization
relationships)
25