Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis."— Presentation transcript:

1 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

2 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

3 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

4 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

5 Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 14-5 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 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

6 Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 14-6 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

7 Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 14-7 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

8 Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 14-8 14.2 Classes Continued…

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

10 Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 14-10 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

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

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

13 Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 14-13 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 object’s 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

14 Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 14-14 14.2 Classes Constructor 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

15 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

16 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

17 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

18 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

19 Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 14-19 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 object’s ability to take different forms


Download ppt "Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis."

Similar presentations


Ads by Google