Sie sind auf Seite 1von 19

Chapter 14:

Object-Oriented Programming

Starting Out with Programming Logic & Design

Second Edition

by Tony Gaddis

Copyright 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Chapter Topics
14.1 Procedural and Object-Oriented
Programming
14.2 Classes
14.3 Using the Unified Modeling Language to
Design Classes
14.4 Inheritance
14.5 Polymorphism

Copyright 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

14-2

14.1 Procedural and Object-Oriented


Programming
Procedural Programming
An early method of coding where programs are centered on the
procedures or actions that take place in a program
A procedure is simply a module
As program get larger and more complex, this method leads to
problems

Object Oriented Programming


A newer method of coding where programs are centered on
creating objects
An object is a software entity that contains both data and
procedures
The data in a object is known as the objects fields (variables,
arrays)
The procedures that are performed are called methods
Copyright 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

14-3

14.1 Procedural and Object-Oriented


Programming
Object Oriented Programming (OOP) addresses
the procedural problem of code/data separation
by using two methods
Encapsulation refers to the combining of data
and code into a single object
Data hiding refers to an objects ability to hide its
data from code that is outside the object

Another OOP benefit is Object Reusability


For example, an object that renders 3D images can
be used in many different programs
Copyright 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

14-4

14.2 Classes
A class is code that specifies the fields and
methods for a particular type of object
A class is coded and contains methods and fields
Think of it like a blueprint, such as a blueprint for a
house
Its a detailed description

An object is then created from the class


It is an instance of a class
Think of it as the actual house

Copyright 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

14-5

14.2 Classes
Creating a class
Class ClassName
Field declarations and method declarations
End Class

The first line starts with Class, followed by the


name of the class
The programmer names the class following the same
rules as naming variables

The field declarations (variables) and methods are


defined within the class
Copyright 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

14-6

14.2 Classes
Access specifiers
Private allows class members to be hidden from
code outside the class
Public allows for all parts of the code to access the
class members
It is common practice to make all fields private and
to provide access only to those field through
methods
Therefore, the methods should be public

Copyright 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

14-7

14.2 Classes

Continued
Copyright 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

14-8

14.2 Classes

Copyright 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

14-9

14.2 Classes
Inside Class Listing 14-3
The field are defined as private to ensure data
hiding
The methods are public so they can be accessed by
main
When the set modules are called, a String is passed
into the method as an argument and that value is
set to the private field
When the get modules are called, they simply
return the value of the private field

Copyright 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

14-10

14.2 Classes

Copyright 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

14-11

14.2 Classes

Copyright 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

14-12

14.2 Classes
Inside Program 14-1
An variable is created myPhone
myPhone is then used with the keyword New to
create the object in memory
Values are then stored in the objects field by
calling the class methods
Call myPhone.setManufacturer(Motorola)

Values are then displayed by calling the class


methods
Display The manufacturer is , myPhone.getManufacturer( )

The dot notation is used to associate an object with


a member of the class
Copyright 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

14-13

14.2 Classes
Constructor is a method that is automatically
called when an object is created
The purpose is to initialize an objects fields with
starting values
A programmer can write their own constructor to
initialize fields
Or they can use the default constructor that is
available with most programming languages

Copyright 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

14-14

14.3 Using the Unified Modeling Language to


Design Classes
The Unified Modeling Language (UML) is a
standard way of drawing diagrams that
describe object-oriented systems
Contains a set of standard diagrams for graphically
depicting OO systems
Figure 14-10 General layout of
a UML diagram for a class

Copyright 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

14-15

14.3 Using the Unified Modeling Language to


Design Classes
Data type, method parameter, and access
specification notation is also added to a UML
diagram
The data type specifies the data type of the field or
the data type of the method
The method parameter specifies the parameter
variables and their data types
The access specification indicates a + for public or
a for private

Copyright 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

14-16

14.3 Using the Unified Modeling Language to


Design Classes

Figure 14-14 UML diagram for the


CellPhone class with access
specification notation

Copyright 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

14-17

14.4 Inheritance
Inheritance allows a new class to extend an
existing class, whereas the new class inherits
the members of the class it extends
The superclass is the base class
The subclass(es) is the derived class
Figure 14-17 Bumblebees and
grasshoppers are specialized versions
of an insect

Copyright 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

14-18

14.5 Polymorphism
Polymorphism allows you to create methods
with the same name in different classes (that
are related through inheritance
The programmer has the ability to call the correct
method depending on the type of object that is
used to call it
Polymorphism refers to an objects ability to take
different forms

Copyright 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

14-19

Das könnte Ihnen auch gefallen