starting out with programming logic & design second edition by tony gaddis

19
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis Chapter 14: Object-Oriented Programming

Upload: wyanet

Post on 05-Jan-2016

31 views

Category:

Documents


1 download

DESCRIPTION

Chapter 14: Object-Oriented Programming. Starting Out with Programming Logic & Design Second Edition by Tony Gaddis. 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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Starting Out with Programming Logic & Design   Second Edition by Tony Gaddis

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

Starting Out with Programming Logic & Design

Second Edition

by Tony Gaddis

Chapter 14:Object-Oriented Programming

Page 2: Starting Out with Programming Logic & Design   Second Edition by Tony Gaddis

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

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

Page 3: Starting Out with Programming Logic & Design   Second Edition by Tony Gaddis

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

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 object’s fields (variables,

arrays…)• The procedures that are performed are called methods

Page 4: Starting Out with Programming Logic & Design   Second Edition by Tony Gaddis

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

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 object’s 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

Page 5: Starting Out with Programming Logic & Design   Second Edition by Tony Gaddis

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

14.2 ClassesA 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

• It’s 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

Page 6: Starting Out with Programming Logic & Design   Second Edition by Tony Gaddis

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

14.2 ClassesCreating a class

Class ClassNameField 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

Page 7: Starting Out with Programming Logic & Design   Second Edition by Tony Gaddis

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

14.2 ClassesAccess 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

Page 8: Starting Out with Programming Logic & Design   Second Edition by Tony Gaddis

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

14.2 Classes

Continued…

Page 9: Starting Out with Programming Logic & Design   Second Edition by Tony Gaddis

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

14.2 Classes

Page 10: Starting Out with Programming Logic & Design   Second Edition by Tony Gaddis

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

14.2 ClassesInside 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

Page 11: Starting Out with Programming Logic & Design   Second Edition by Tony Gaddis

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

14.2 Classes

Page 12: Starting Out with Programming Logic & Design   Second Edition by Tony Gaddis

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

14.2 Classes

Page 13: Starting Out with Programming Logic & Design   Second Edition by Tony Gaddis

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

14.2 ClassesInside 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 object’s field by calling

the class methodsCall 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

Page 14: Starting Out with Programming Logic & Design   Second Edition by Tony Gaddis

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

14.2 ClassesConstructor is a method that is automatically

called when an object is created– The purpose is to initialize an object’s 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

Page 15: Starting Out with Programming Logic & Design   Second Edition by Tony Gaddis

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

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

Page 16: Starting Out with Programming Logic & Design   Second Edition by Tony Gaddis

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

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

Page 17: Starting Out with Programming Logic & Design   Second Edition by Tony Gaddis

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

14.3 Using the Unified Modeling Language to Design Classes

Figure 14-14 UML diagram for the CellPhone class with access specification notation

Page 18: Starting Out with Programming Logic & Design   Second Edition by Tony Gaddis

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

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

Page 19: Starting Out with Programming Logic & Design   Second Edition by Tony Gaddis

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

14.5 PolymorphismPolymorphism 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 object’s ability to take different forms