Programming Logic and Design: Ninth Edition
Programming Logic and Design: Ninth Edition
Programming Logic and Design: Ninth Edition
Ninth Edition
Chapter 10
Object-Oriented Programming
• Attributes
– Characteristics that define an object as part of a class
– Example
• Automobile’s attributes: make, model, year, and purchase price
• Methods
– Actions that alter, use, or retrieve the attributes
– Example
• Methods for changing an automobile’s running status, gear,
speed, and cleanliness
• State
– A set of all the values or contents of a class object’s
instance variables
• Every object that is an instance of a class possesses
the same methods
• Create classes from which objects will be
instantiated
• Class client or class user
– A program or class that instantiates objects of another
prewritten class
Programming Logic and Design, Ninth Edition 9
Polymorphism
• The world is full of objects
– A door is an object that needs to be open or closed
– But an “open” procedure works differently on different
objects
• Open a door
• Open a drawer
• Open a bank account
• Open a file
• Open your eyes
– One “open” procedure can open anything if it gets the
correct arguments
• Declaring a class
– Does not create any actual objects
• After an object is instantiated
– Methods can be accessed using an identifier, a dot, and a
method call
– myAssistant.setHourlyWage(16.75)
• Employee myAssistant
– Declare the myAssistant object
– Contains all the data fields
– Access to all methods contained within the class
Programming Logic and Design, Ninth Edition 15
Defining Classes and Creating
Class Diagrams (continued -2)
void calculateWeeklyPay()
Declarations
num WORK_WEEK_HOURS = 40
weeklyPay = hourlyWage * WORK_WEEK_HOURS
return
• Public access
– Other programs and methods may use the methods that
control access to the private data
• Access specifier
– Also called an access modifier
– An adjective defining the type of access that outside
classes will have to the attribute or method
• public or private
• Don’t do it:
– myAssistant.hourlyWage = 15.00
• Instead:
– myAssistant.setHourlyWage(15.00)
• Methods may be private; don’t do it:
– myAssistant.calculateWeeklyPay()
• Instance method
– Method that works appropriately with different objects
– If you create 100 Students and assign grade point
averages to each of them, you would need 100 storage
locations in computer memory
• Only one copy of each instance method is stored in
memory
– The computer needs a way to determine whose
gradePointAverage is being set or retrieved
• this reference
– An automatically created variable
– Holds the address of an object
– Passes it to an instance method whenever the method is
called
– Refers to “this particular object” using the method
– Implicitly passed as a parameter to each instance method