0% found this document useful (0 votes)
78 views50 pages

Programming Logic and Design: Ninth Edition

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 50

Programming Logic and Design

Ninth Edition

Chapter 10
Object-Oriented Programming

Programming Logic and Design, Ninth Edition 1


Objectives
In this chapter, you will learn about:
• The principles of object-oriented programming
• Classes
• Public and private access
• Ways to organize classes
• Instance methods
• Static methods
• Using objects

Programming Logic and Design, Ninth Edition 2


Principles of Object-Oriented
Programming
• Object-oriented programming (OOP)
– A programming model that focuses on an application’s
components and data and the methods to manipulate
them
• Uses all of the familiar concepts from modular
procedural programming
– Variables, methods, passing arguments
– Sequence, selection, and looping structures
– But involves a different way of thinking

Programming Logic and Design, Ninth Edition 3


Principles of Object-Oriented
Programming (continued)

• Important features of object-oriented languages


– Classes
– Objects
– Polymorphism
– Inheritance
– Encapsulation

Programming Logic and Design, Ninth Edition 4


Classes and Objects
• Class
– Describes a group or collection of objects with common
attributes
• Object - One instance of a class
– Sometimes called one instantiation of a class
– When a program creates an object, it instantiates the
object
• Example
– Class name: dog
– Attributes: name, age, hasShots
– Methods: changeName, updateShots
Programming Logic and Design, Ninth Edition 5
Classes and Objects (continued -1)

• 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

Programming Logic and Design, Ninth Edition 6


Classes and Objects (continued -2)

Programming Logic and Design, Ninth Edition 7


Classes and Objects (continued -3)

• Think in an object-oriented manner


– Everything is an object
– Every object is a member of a class
• Is-a relationship
– “My oak desk with the scratch on top is a Desk”
• Class reusability
• Class’s instance variables
– Data components of a class that belong to every
instantiated object
– Often called fields
Programming Logic and Design, Ninth Edition 8
Classes and Objects (continued -4)

• 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

Programming Logic and Design, Ninth Edition 10


Polymorphism (continued)

Programming Logic and Design, Ninth Edition 11


Inheritance
• Inheritance
– The process of acquiring the traits of one’s predecessors
• Example
– A door with a stained glass window inherits all the
attributes (doorknob, hinges) and methods (open and
close) of a door
• Once you create an object
– Develop new objects that possess all the traits of the
original object
– Plus new traits

Programming Logic and Design, Ninth Edition 12


Encapsulation
• Encapsulation
– The process of combining all of an object’s attributes and
methods into a single package
• Information hiding (also called data hiding)
– Other classes should not alter an object’s attributes
• Outside classes should only be allowed to make a
request that an attribute be altered
– It is up to the class’s methods to determine whether the
request is appropriate

Programming Logic and Design, Ninth Edition 13


Defining Classes and Creating
Class Diagrams
• Class definition
– A set of program statements
– Characteristics of the class’s objects and the methods
that can be applied to its objects
• Three parts:
– Every class has a name
– Most classes contain data (not required)
– Most classes contain methods (not required)

Programming Logic and Design, Ninth Edition 14


Defining Classes and Creating
Class Diagrams (continued -1)

• 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)

Programming Logic and Design, Ninth Edition 16


Defining Classes and Creating
Class Diagrams (continued -3)

• Programmers call the classes they write user-


defined types
– More accurately called programmer-defined types
– OOP programmers call them abstract data types (ADTs)
– Simple numbers and characters are called primitive data
types
• “Black box”
– The ability to use methods without knowing the details of
their contents
– A feature of encapsulation

Programming Logic and Design, Ninth Edition 17


Creating Class Diagrams
• Class diagram
– Three sections
• Top: contains the name of the class
• Middle: contains the names and data types of the attributes
• Bottom: contains the methods

Programming Logic and Design, Ninth Edition 18


Creating Class Diagrams (continued -1)

• Purpose of Employee class methods


– Two of the methods accept values from the outside world
– Three of the methods send data to the outside world
– One method performs work within the class

Programming Logic and Design, Ninth Edition 19


Creating
Class
Diagrams (continued -2)

Programming Logic and Design, Ninth Edition 20


The Set Methods
• Set method (also called mutator method)
– Sets the values of data fields within the class
void setLastName(string name)
lastName = name
return
mySecretary.setLastName("Johnson")
– No requirement that such methods start with the set
prefix
– Some languages allow you to create a property to set
field values instead of creating a set method

Programming Logic and Design, Ninth Edition 21


The Set Methods (continued)

Programming Logic and Design, Ninth Edition 22


The Get Methods
• Get method (also called accessor method)
• Purpose is to return a value to the world outside the
class
string getLastName()
return lastName
• Value returned from a get method can be used as
any other variable of its type would be used

Programming Logic and Design, Ninth Edition 23


Work Methods
• Work method (also called help method, or
faciltator)
– performs tasks within a class

void calculateWeeklyPay()
Declarations
num WORK_WEEK_HOURS = 40
weeklyPay = hourlyWage * WORK_WEEK_HOURS
return

Programming Logic and Design, Ninth Edition 24


Work Methods (continued)

Programming Logic and Design, Ninth Edition 25


Understanding Public and Private
Access
• You do not want any outside programs or methods
to alter your class’s data fields unless you have
control over the process
• Prevent outsiders from changing your data
– Force other programs and methods to use a method that
is part of the class
• Specify that data fields have private access
– Data cannot be accessed by any method that is not part
of the class

Programming Logic and Design, Ninth Edition 26


Understanding Public and Private
Access (continued -1)

• 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

Programming Logic and Design, Ninth Edition 27


Understanding
Public and
Private Access (continued -2)

Programming Logic and Design, Ninth Edition 28


Understanding Public and Private
Access (continued -3)

• Don’t do it:
– myAssistant.hourlyWage = 15.00
• Instead:
– myAssistant.setHourlyWage(15.00)
• Methods may be private; don’t do it:
– myAssistant.calculateWeeklyPay()

Programming Logic and Design, Ninth Edition 29


Understanding Public and Private
Access (continued -4)

Programming Logic and Design, Ninth Edition 30


Organizing Classes
• Most programmers place data fields in logical order
at the beginning of a class
– An ID number is most likely used as a unique identifier
• Primary key
– Flexibility in how you position data fields
• In some languages, you can organize a class’s data
fields and methods in any order

Programming Logic and Design, Ninth Edition 31


Organizing Classes (continued)

• Class method ordering


– Alphabetical
– Pairs of get and set methods
– Same order as the data fields are defined
– All accessor (get) methods together and all mutator (set)
methods together

Programming Logic and Design, Ninth Edition 32


Understanding Instance Methods
• Every object that is an instance of a class is assumed
to possess the same data and have access to the
same methods

Programming Logic and Design, Ninth Edition 33


Understanding Instance Methods (continued -1)

Programming Logic and Design, Ninth Edition 34


Understanding Instance Methods (continued -2)

• 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

Programming Logic and Design, Ninth Edition 35


Understanding
Instance
Methods (continued -3)

Programming Logic and Design, Ninth Edition 36


Understanding Instance Methods (continued -4)

• 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

Programming Logic and Design, Ninth Edition 37


Understanding Instance Methods (continued -5)

• Identifiers within the method always mean exactly


the same thing
– Any field name defined in the class
– this, followed by a dot, followed by the same field
name
• Example of an occasion when you might use the
this reference explicitly

Programming Logic and Design, Ninth Edition 38


Understanding Instance Methods (continued -6)

Programming Logic and Design, Ninth Edition 39


Understanding Static Methods
• Some methods do not require a this reference
• displayStudentMotto()
– A class method instead of an instance method
• Two types of methods
– Static methods (also called class methods)
• Methods for which no object needs to exist
– Nonstatic methods
• Methods that exist to be used with an object
• Student.displayStudentMotto()

Programming Logic and Design, Ninth Edition 40


Understanding Static Methods (continued)

Programming Logic and Design, Ninth Edition 41


Using Objects
• You can use objects like you would use any other
simpler data type
• InventoryItem class
– Pass to an object to a method
– Return an object from a method
– Use an array of objects

Programming Logic and Design, Ninth Edition 42


Using Objects (continued)

Programming Logic and Design, Ninth Edition 43


Passing an Object to a Method

Programming Logic and Design, Ninth Edition 44


Returning an
Object from a
Method

Programming Logic and Design, Ninth Edition 45


Returning an
Object from a
Method (continued)

Programming Logic and Design, Ninth Edition 46


Using Arrays of
Objects

Programming Logic and Design, Ninth Edition 47


Summary
• Classes
– Basic building blocks of object-oriented programming
• Class definition
– A set of program statements that tell you the
characteristics of the class’s objects and the methods that
can be applied to its objects
• Object-oriented programmers
– Specify that their data fields will have private access
• As classes get more complex, organizing becomes
more important
Programming Logic and Design, Ninth Edition 48
Summary (continued -1)

• Instance method operates correctly yet differently


for every object instantiated from a class
• A class can contain two types of methods:
– Static methods, which are also known as class methods
and do not receive a this reference as an implicit
parameter
– Nonstatic methods, which are instance methods and do
receive a this reference implicitly

Programming Logic and Design, Ninth Edition 49


Summary (continued -2)

• Objects can be used in many of the same ways you


use items of simpler data types, such as passing
them to and from methods and storing them in
arrays

Programming Logic and Design, Ninth Edition 50

You might also like