Starting Out With Programming Logic & Design - Chapter14_Object Oriented Programming

download Starting Out With Programming Logic & Design - Chapter14_Object Oriented Programming

of 19

Transcript of Starting Out With Programming Logic & Design - Chapter14_Object Oriented Programming

  • 7/24/2019 Starting Out With Programming Logic & Design - Chapter14_Object Oriented Programming

    1/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

  • 7/24/2019 Starting Out With Programming Logic & Design - Chapter14_Object Oriented Programming

    2/19

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

    Chapter Topics

    1.1 Procedural and !b"ect-!rientedProgra##ing

    1.2 Classes

    1.$ %sing the %ni&ied 'odeling (anguage to)esign Classes

    1. Inheritance

    1.* Poly#orphis#

  • 7/24/2019 Starting Out With Programming Logic & Design - Chapter14_Object Oriented Programming

    3/19

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

    14.1 Procedural and Object-Oriented

    Programming

    Procedural Progra##ing An early #ethod o& coding +here progra#s are centered on the

    procedures or actions that tae place in a progra#

    A procedure is si#ply a #odule

    As progra# get larger and #ore co#ple, this #ethod leads to

    proble#s

    !b"ect !riented Progra##ing A ne+er #ethod o& coding +here progra#s are centered on

    creating ob"ects

    An ob"ect is a so&t+are entity that contains both data and

    procedures he data in a ob"ect is no+n as the ob"ect/s &ields ariables,

    arrays3

    he procedures that are per&or#ed are called #ethods

  • 7/24/2019 Starting Out With Programming Logic & Design - Chapter14_Object Oriented Programming

    4/19

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

    14.1 Procedural and Object-Oriented

    Programming

    !b"ect !riented Progra##ing !!P3 addressesthe procedural proble# o& code4data separationby using t+o #ethods

    Encapsulation 5 re&ers to the co#bining o& dataand code into a single ob"ect

    )ata hiding 5 re&ers to an ob"ect/s ability to hide itsdata &ro# code that is outside the ob"ect

    Another !!P bene&it is !b"ect 6eusability 7or ea#ple, an ob"ect that renders $) i#ages can

    be used in #any di&&erent progra#s

  • 7/24/2019 Starting Out With Programming Logic & Design - Chapter14_Object Oriented Programming

    5/19

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

    14.2 Classes

    A class is code that speci&ies the &ields and#ethods &or a particular type o& ob"ect A class is coded and contains #ethods and &ields

    hin o& it lie a blueprint, such as a blueprint &or ahouse

    It/s a detailed description

    An ob"ect is then created &ro# the class It is an instance o& a class

    hin o& it as the actual house

  • 7/24/2019 Starting Out With Programming Logic & Design - Chapter14_Object Oriented Programming

    6/19

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

    14.2 Classes

    Creating a classClass ClassName

    Field declarations and method declarations

    End Class

    he &irst line starts +ith Class, &ollo+ed by thena#e o& the class

    he progra##er na#es the class &ollo+ing the sa#erules as na#ing ariables

    he &ield declarations ariables3 and #ethods arede&ined +ithin the class

  • 7/24/2019 Starting Out With Programming Logic & Design - Chapter14_Object Oriented Programming

    7/19

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

    14.2 Classes

    Access speci&iersPrivateallo+s class #e#bers to be hidden &ro#

    code outside the class

    Publicallo+s &or all parts o& the code to access theclass #e#bers

    It is co##on practice to #ae all &ields priate andto proide access only to those &ield through#ethods

    here&ore, the #ethods should be public

  • 7/24/2019 Starting Out With Programming Logic & Design - Chapter14_Object Oriented Programming

    8/19

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

    14.2 Classes

    Continued

  • 7/24/2019 Starting Out With Programming Logic & Design - Chapter14_Object Oriented Programming

    9/19

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

    14.2 Classes

  • 7/24/2019 Starting Out With Programming Logic & Design - Chapter14_Object Oriented Programming

    10/19

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

    14.2 Classes

    Inside Class (isting 1-$ he &ield are de&ined as priate to ensure data

    hiding

    he #ethods are public so they can be accessed by#ain

    When the set #odules are called, a 8tring is passedinto the #ethod as an argu#ent and that alue isset to the priate &ield

    When the get #odules are called, they si#plyreturn the alue o& the priate &ield

  • 7/24/2019 Starting Out With Programming Logic & Design - Chapter14_Object Oriented Programming

    11/19

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

    14.2 Classes

  • 7/24/2019 Starting Out With Programming Logic & Design - Chapter14_Object Oriented Programming

    12/19

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

    14.2 Classes

  • 7/24/2019 Starting Out With Programming Logic & Design - Chapter14_Object Oriented Programming

    13/19

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

    14.2 Classes

    Inside Progra# 1-1 An ariable is created myPhone

    myPhone is then used +ith the ey+ordNewtocreate the ob"ect in #e#ory

    9alues are then stored in the ob"ect/s &ield bycalling the class #ethods

    Call myPhone.setManufacturer(Motorola)

    9alues are then displayed by calling the class#ethodsDislay !he manufacturer is " myPhone.#etManufacturer( )

    he dot notation is used to associate an ob"ect +itha #e#ber o& the class

  • 7/24/2019 Starting Out With Programming Logic & Design - Chapter14_Object Oriented Programming

    14/19

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

    14.2 Classes

    Constructor is a #ethod that is auto#aticallycalled +hen an ob"ect is created he purpose is to initiali:e an ob"ect/s &ields +ith

    starting alues

    A progra##er can +rite their o+n constructor toinitiali:e &ields

    !r they can use the de&ault constructor that is

    aailable +ith #ost progra##ing languages

  • 7/24/2019 Starting Out With Programming Logic & Design - Chapter14_Object Oriented Programming

    15/19

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

    14.3 $sing the $ni%ied &odeling 'anguage to

    (esign Classes

    he %ni&ied 'odeling (anguage %'(3 is astandard +ay o& dra+ing diagra#s thatdescribe ob"ect-oriented syste#s

    Contains a set o& standard diagra#s &or graphicallydepicting !! syste#s

    Figure 14-1)eneral la*out o%

    a $&' diagram %or a class

  • 7/24/2019 Starting Out With Programming Logic & Design - Chapter14_Object Oriented Programming

    16/19

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

    14.3 $sing the $ni%ied &odeling 'anguage to

    (esign Classes

    )ata type, #ethod para#eter, and accessspeci&ication notation is also added to a %'(diagra#

    he data type speci&ies the data type o& the &ield orthe data type o& the #ethod

    he #ethod para#eter speci&ies the para#eterariables and their data types

    he access speci&ication indicates a ; &or public ora 5 &or priate

  • 7/24/2019 Starting Out With Programming Logic & Design - Chapter14_Object Oriented Programming

    17/19

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

    14.3 $sing the $ni%ied &odeling 'anguage to

    (esign Classes

    Figure 14-14 $&' diagram %or the

    CellPhoneclass +ith access

    speci%ication notation

  • 7/24/2019 Starting Out With Programming Logic & Design - Chapter14_Object Oriented Programming

    18/19

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

    14.4 ,nheritance

    Inheritance allo+s a ne+ class to etend aneisting class, +hereas the ne+ class inheritsthe #e#bers o& the class it etends he superclass is the base class

    he subclasses3 is the deried class

    Figure 14-1!umblebees and

    grasshoppers are specialied /ersions

    o% an insect

  • 7/24/2019 Starting Out With Programming Logic & Design - Chapter14_Object Oriented Programming

    19/19

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

    14.5 Pol*morphism

    Poly#orphis# allo+s you to create #ethods+ith the sa#e na#e in di&&erent classes thatare related through inheritance he progra##er has the ability to call the correct

    #ethod depending on the type o& ob"ect that isused to call it

    Poly#orphis# re&ers to an ob"ect/s ability to taedi&&erent &or#s