Objects And Classes

Understanding Objects and Classes
Barb Ericson
[email protected]
Jan 2005
Georgia Institute of Technology
Learning Goals
•
•
•
•
What does object-oriented mean?
What are objects?
What are classes?
Exercises for conceptually understanding
objects and classes
– Scenarios
– Role-playing
Georgia Institute of Technology
What is Object-Oriented?
• This means that we
focus on the objects
not just the
procedures
–
–
–
–
Focus on
who (what objects?)
as well as
what (what do the
objects do?)
Georgia Institute of Technology
What is an Object?
• A person, place, or thing
– That knows something about
itself
• Has data (attributes, fields)
• A cashier has a id, name, and a
password
– And can do something
• Has operations (methods)
• A cashier can total the items,
take payment, make change
Georgia Institute of Technology
What is a Class?
• The type of an object
– The way we classify
an object
• “The Idiot” by
Dostoevsky is a book
• “War and Peace” by
Tolstoy is a book
• Mary is a cashier
• Tasha is a cashier
• Grouping of objects
with the same data
and operations
Georgia Institute of Technology
Class: Example
• Mary is a cashier
• Tasha is a cashier
• Cashier is a class
– All cashiers have an id,
name and password
• Each will have a different id,
name, and password
– All cashiers can total an
order, take payment, make
change
Georgia Institute of Technology
Object Data
• Each object has its own
data
– Tasha’s id is 4 and
password is mhall
– Mary’s id is 3 and
password is smile4
• All cashier objects have
an id, name, and
password
• Changing Mary’s data
won’t affect Tasha’s data
Georgia Institute of Technology
Understanding Objects and Classes
• What “types” of objects
are in the classroom?
• What data is known about
each type and what
operations can objects of
that type do?
• Are there several objects
of the same “type”.
– How are they the same and
how different?
Georgia Institute of Technology
Simulation
• Object-oriented
development means
creating a simulation
of the problem
– We need to know the
objects in the problem
• So we can create
software objects to
represent them
Georgia Institute of Technology
Classes Define the Objects
• The computer doesn’t know what we
mean by a car or cashier
• We define the class Cashier so that the
computer will understand what a cashier
or bank account “is” and what it can “do”
– In the context of the problem we are trying to
solve
• Then the computer can create objects to
do the simulation
Georgia Institute of Technology
Classes Create Objects
• The class can be thought of
as a recipe, blueprint, or
factory
• Many objects can be created
from one class
• Objects keep track of the
class that created them
– I am an object (instance) of
the Cookie class
Georgia Institute of Technology
Software Objects are Models
• The objects we create
in software are
models of the
physical object
– We can’t stick a
person in our software
– We can create a
model of the person
with the information
we need to know for
that person for our
task
Georgia Institute of Technology
Cashier
id
name
password
Using Scenarios to Identify Objects
• What objects are
involved in going to
the doctor because
you are sick?
• What objects are
involved in going to
the movies?
• What objects are
involved in a card
game like blackjack?
Georgia Institute of Technology
Objects have Responsibilities
• An object-oriented design
– Determines the tasks to be done
– Determines what objects will be responsible
for each task
• No one object does everything
• Objects work together to accomplish tasks
– The assignment of responsibilities is the key
skill in object-oriented design
Georgia Institute of Technology
Identifying Classes
• What type of thing was each thing in the
scenario? How would you classify it?
– Patient, Doctor, Nurse, Insurance …
– Movie, Ticket Seller, Ticket Taker, Ticket,
Popcorn, Drink …
– Card, Deck, Player, Suit …
• How many objects of that types were in
each scenario?
– What data did each object have associated
with it? What could it do?
Georgia Institute of Technology
Learning about Objects and Classes
• Walk through scenarios
• Do role-playing
• Underline nouns in scenarios to identify
the objects and classes
• Create CRC cards
• Draw UML diagrams
• Write methods and classes
• Specialize a parent class
• Use a debugger to step through execution
Georgia Institute of Technology
Role Playing Exercises
• Page at
http://web.sbu.edu/cs/dlevine/RolePlay/rol
eplay.html has links to exercises
• Powerpoint slides you can hand out
developed by Joseph Bergin of Pace
University
– FirstDayRolePlay-Bergin.ppt
Georgia Institute of Technology
Summary
• Object-oriented programs are simulations
– Objects simulate the “real world” and interact to
accomplish a task
– Classes define the data all objects will have and what
things they can do
• Ways to start understanding Objects and
Classes
– Scenarios
– Role-Playing
Georgia Institute of Technology