Farrell PLD 10e Ch11 PowerPoint
Farrell PLD 10e Ch11 PowerPoint
Logic and
Design, 10e
Chapter 11: More
Object-Oriented
Programming Concepts
Farrell, Programming
Farrell, Programming Logic and
Logic Design,
and Design,1010 Edition.
th th
©©
Edition. 2024 Cengage.
2024 Cengage. AllAll
Rights Reserved.
Rights May
Reserved. not
May bebe
not scanned, copied
scanned, oror
copied
duplicated,
duplicated, oror
posted toto
posted aapublicly accessible
publicly accessible website,
website,inin
whole oror
whole inin
part.
part. 1
Chapter Objectives
• 11.05 Describe how predefined classes are used to create GUI objects
Farrell, Programming Logic and Design, 10th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 2
Understanding Constructors (1 of 3)
• Constructor
− A method that has the same name as the class
− Establishes an object
• Constructors fall into two categories:
− Default constructor
Requires no arguments
Farrell, Programming Logic and Design, 10th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 3
Understanding Constructors (2 of 3)
Farrell, Programming Logic and Design, 10th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 5
Discussion Activity
Farrell, Programming Logic and Design, 10th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 6
Discussion Activity: Answer
Farrell, Programming Logic and Design, 10th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 7
Default Constructors (1 of 2)
Farrell, Programming Logic and Design, 10th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 8
Figure 11-1: Employee Class with a Default
Constructor that Sets hourlyWage and
weeklyPay
Farrell, Programming Logic and Design, 10th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 9
Default Constructors (2 of 2)
Figure 11-3: Program that Figure 11-4: Output of Figure 11-5: Improved
declares Employee objects using program in Figure 11-3 version of the Employee
class in Figure 11-1 class constructor
Farrell, Programming Logic and Design, 10th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 10
Nondefault Constructors (1 of 2)
• Choose to create Employee objects with values that differ for each
employee
− Initialize each Employee with a unique hourlyWage
• Write constructors that receive arguments
− Employee partTimeWorker = new Employee (18.81)
− Employee partTimeWorker = new Employee (valueEnteredByUser)
Farrell, Programming Logic and Design, 10th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 11
Nondefault Constructors (2 of 2)
Farrell, Programming Logic and Design, 10th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 13
Figure 11-7: Employee Class with
Overloaded Constructors
Farrell, Programming Logic and Design, 10th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 14
Figure 11-8: A Third Possible
Employee Class Constructor
Farrell, Programming Logic and Design, 10th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 15
Understanding Destructors (1 of 4)
• Destructor
− A method that contains the actions you require when an instance
of a class is destroyed
• Instance of a class is destroyed
− When the object goes out of scope
• If you do not explicitly create a destructor for a class, one is provided
automatically
Farrell, Programming Logic and Design, 10th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 16
Understanding Destructors (2 of 4)
• Declare a destructor
− Use an identifier that consists of a tilde (˜) followed by the class
name
• Cannot provide any parameters to a destructor
− Empty parameter list
• Destructors cannot be overloaded
− Only one destructor per class
Farrell, Programming Logic and Design, 10th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 17
Understanding Destructors (3 of 4)
Farrell, Programming Logic and Design, 10th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 18
Understanding Destructors (4 of 4)
Figure 11-9: Employee class with Figure 11-10: Program that declares two
destructor Employee objects using the class version with
the explicitly coded destructor
Farrell, Programming Logic and Design, 10th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 19
Figure 11-11: Output of Program in
Figure 11-10
Farrell, Programming Logic and Design, 10th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 20
Knowledge Check Activity
Farrell, Programming Logic and Design, 10th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 21
Knowledge Check Activity: Answer
Farrell, Programming Logic and Design, 10th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 22
Understanding Composition (1 of 2)
Farrell, Programming Logic and Design, 10th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 23
Understanding Composition (2 of 2)
• Composition
− Placing a class object within another class object
Farrell, Programming Logic and Design, 10th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 24
Figure 11-12: Diagram of Typical
Composition Relationships
Farrell, Programming Logic and Design, 10th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 25
Understanding Inheritance (1 of 4)
Farrell, Programming Logic and Design, 10th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 26
Understanding Inheritance (2 of 4)
Farrell, Programming Logic and Design, 10th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 27
Understanding Inheritance (3 of 4)
Farrell, Programming Logic and Design, 10th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 28
Figure 11-15: CommissionEmployee
Class
Farrell, Programming Logic and Design, 10th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 29
Understanding Inheritance (4 of 4)
Farrell, Programming Logic and Design, 10th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 30
Think, Pair, Share
• Suppose you have an existing class called Students and need to add a
new type of class for students who require extra classes each week.
Think about an efficient way to do this without duplicating the work
and effort.
• Pair with a classmate or create a breakout group of four people.
• Share your ideas with your partner(s) regarding inheriting attributes
from the existing class to save time and effort. Create a program that
demonstrates reusing the data fields and methods of the existing
class to create the new class.
Farrell, Programming Logic and Design, 10th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 31
Understanding Inheritance Terminology (1
of 4)
• Base class
− A class that is used as a basis for inheritance
− Also called superclass or parent class
• Derived class or extended class
− A class that inherits from a base class
− Also called subclass or child class
Farrell, Programming Logic and Design, 10th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 32
Understanding Inheritance Terminology (2
of 4)
• Discover which class is the base class and which is the derived class
− The base class is also called the superclass
− The derived class is also called the subclass
− Use the two classes in a sentence with the phrase “is a”
− Try saying the two class names together
− Compare the classes by size
• The derived class can be further extended
Farrell, Programming Logic and Design, 10th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 33
Understanding Inheritance Terminology (3
of 4)
• Ancestors of a subclass
− The entire list of parent classes from which a child class is derived
− A child inherits all the data fields and methods of all its ancestors
− A parent class does not gain any child class data or methods
Farrell, Programming Logic and Design, 10th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 34
Understanding Inheritance Terminology (4
of 4)
Figure 11-16: Program that declares an Figure 11-17: Output of the program
Employee and a CommissionEmployee in Figure 11-16
Farrell, Programming Logic and Design, 10th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 35
Accessing Private Fields and Methods
of a Parent Class (1 of 5)
• It is common for methods to be public but for data to be private
• When a data field within a class is private:
− No outside class can use it, including a child class
− Can be inconvenient
− Inaccessible
Farrell, Programming Logic and Design, 10th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 36
Figure 11-18: Class Diagram for
HourlyEmployee Class Inheriting from
Employee
Farrell, Programming Logic and Design, 10th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 37
Figure 11-19: Implementation of
HourlyEmployee Class that Attempts to Access
weeklySalary
Farrell, Programming Logic and Design, 10th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 38
Accessing Private Fields and Methods
of a Parent Class (2 of 5)
• Protected access specifier
− Used when you want no outside classes to be able to use a data
field except classes that are children of the original class
• If the Employee class’s creator did not foresee that a field would need
to be accessible, then weeklySalary will remain private
− Possible to correctly set an HourlyEmployee’s weekly pay using the
public method setWeeklySalary()
Farrell, Programming Logic and Design, 10th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 39
Accessing Private Fields and Methods
of a Parent Class (3 of 5)
Figure 11-20: Employee class with a Figure 11-21: Employee class
protected field diagram with a protected field
Farrell, Programming Logic and Design, 10th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 40
Figure 11-22: The HourlyEmployee
Class When weeklySalary Remains
Private
Farrell, Programming Logic and Design, 10th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 41
Accessing Private Fields and Methods
of a Parent Class (4 of 5)
• Approaches to create a child class that can access a private field of its
parent’s class:
− Modify the parent class to make the field protected
− The child class can use a public method within the parent class
that modifies the field
• Protected access specifier improves program performance
− Use protected data members sparingly
Farrell, Programming Logic and Design, 10th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 42
Accessing Private Fields and Methods
of a Parent Class (5 of 5)
• Classes that depend on field names from parent classes are called
fragile because they are prone to errors
• Multiple inheritance
− The capability to inherit from more than one class
• Abstract class
− A class from which you cannot create any concrete objects, but
from which you can inherit
Farrell, Programming Logic and Design, 10th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 43
Overriding Parent Class Methods
in a Child Class
• Overriding
− The mechanism by which a child class method is used by default
when a parent class contains a method with the same signature
Farrell, Programming Logic and Design, 10th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 44
Using Inheritance to Achieve Good
Software Design
• You can create powerful computer programs more easily if many of
their components are used either “as is” or with slight modifications
• Advantages of creating a useful, extendable superclass:
− Subclass creators save development and testing time
− Superclass code has been tested and is reliable
− Programmers who create or use new subclasses already
understand how the superclass works
− Neither the superclass source code nor the translated superclass
code is changed
Farrell, Programming Logic and Design, 10th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 45
An Example of Using Predefined
Classes: Creating Graphical user
interface
• Libraries or packages(GUI) Objects (1 of 2)
− Collections of classes that serve related purposes
• Graphical user interface (GUI) objects
− Created in a visual development environment
− Frames, buttons, labels, and text boxes
− Placed within interactive programs so that users can manipulate
them using input devices
Farrell, Programming Logic and Design, 10th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 46
An Example of Using Predefined
Classes: Creating Graphical user
interface
• Disadvantages (GUI)
of creating Objects
your own (2 of 2)
GUI object classes:
− Lots of work
− Repetitious
− Components would look different in various applications
• Visual development environment
− Known as an IDE (integrated development environment)
− Create programs by dragging components such as buttons and
labels onto a screen and arranging them visually
Farrell, Programming Logic and Design, 10th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 47
Understanding Exception Handling
• A lot of the effort that goes into writing programs involves checking
data items to make sure they are valid and reasonable
• Procedural programs
− Programmers handled errors in various ways that were effective
− Techniques had some drawbacks
• Object-oriented programming
− Led to a new model called exception handling
Farrell, Programming Logic and Design, 10th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 48
Drawbacks to Traditional Error-
Handling Techniques (1 of 2)
• The most often used error-handling outcome in traditional
programming was to terminate the program
− Unforgiving
− Unstructured
• Forcing the user to reenter data
− May allow no second chance at all
Farrell, Programming Logic and Design, 10th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 49
Figure 11-23: A method that Handles
An Error in An Unstructured Manner
Farrell, Programming Logic and Design, 10th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 50
Drawbacks to Traditional Error-
Handling Techniques (2 of 2)
• More elegant solution
− Repeating data entry in a loop until the data item becomes valid
• Shortcomings
− Method is not very reusable
− Method is not very flexible
− Only works with interactive programs
Farrell, Programming Logic and Design, 10th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 51
Figure 11-24: A Method that Handles
an Error Using a Loop
Farrell, Programming Logic and Design, 10th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 52
The Object-Oriented Exception-Handling
Model (1 of 6)
• Exception handling
− A group of techniques for handling errors in object-oriented
programs
• Exceptions
− The generic name used for errors in object-oriented languages
• Try some code statements that might throw an exception
• If an exception is thrown, it is passed to a block of code that can
catch an exception
Farrell, Programming Logic and Design, 10th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 53
The Object-Oriented Exception-Handling
Model (2 of 6)
• Throw statement
− Sends an Exception object out of the current code block or method
so it can be handled elsewhere
Farrell, Programming Logic and Design, 10th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 54
The Object-Oriented Exception-Handling
Model (3 of 6)
• Try block
− A block of code you attempt to execute while acknowledging that
an exception might occur
− Consists of the keyword “try” followed by any number of
statements
− If a statement in the block causes an exception, the remaining
statements in the try block do not execute and the try block is
abandoned
Farrell, Programming Logic and Design, 10th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 55
The Object-Oriented Exception-Handling
Model (4 of 6)
• Catch block
− A segment of code that can handle an exception
− Create a catch block using the following pseudocode elements:
The keyword “catch” followed by parentheses that contain an
Exception type and an identifier
Statements that take action to handle the error condition
The “endcatch” keyword indicates the end of the catch block in
the pseudocode
Farrell, Programming Logic and Design, 10th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 56
The Object-Oriented Exception-Handling
Model (5 of 6)
Figure 11-25: A method that creates and Figure 11-26: A program that contains
throws an Exception object a try…catch pair
Farrell, Programming Logic and Design, 10th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 57
The Object-Oriented Exception-Handling
Model (6 of 6)
• General principle of exception handling:
− The method that uses data should be able to detect errors but not
be required to handle them
− Handling should be left to the application that uses the object
• Sunny day case
− When there are no exceptions and nothing goes wrong with a
program
Farrell, Programming Logic and Design, 10th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 58
Using Built-in Exceptions and Creating
Your Own Exceptions
• Many OOP languages provide many built-in Exception types
− Built-in Exceptions in a programming language cannot cover every
condition
• Create your own throwable Exception
− Extend a built-in Exception class
Farrell, Programming Logic and Design, 10th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 59
Reviewing the Advantages of Object-
Oriented Programming (1 of 2)
• Save development time
− Each object automatically includes appropriate, reliable methods
and attributes
• Develop new classes more quickly
− By extending classes that already exist and work
• Use existing classes
− Concentrate only on the interface to those classes
Farrell, Programming Logic and Design, 10th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 60
Reviewing the Advantages of Object-
Oriented Programming (2 of 2)
• Overload methods
− Use reasonable, easy-to-remember names for methods
Farrell, Programming Logic and Design, 10th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 61
Self-Assessment
Farrell, Programming Logic and Design, 10th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 62
Summary
Farrell, Programming Logic and Design, 10th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 63