0% found this document useful (0 votes)
24 views63 pages

Farrell PLD 10e Ch11 PowerPoint

Chapter 11 of 'Programming Logic and Design' covers advanced object-oriented programming concepts including constructors, destructors, composition, and inheritance. It explains how to create and utilize constructors and destructors, the principles of composition and inheritance, and the benefits of object-oriented programming. The chapter also includes practical examples and discussions to enhance understanding of these concepts.

Uploaded by

Paul Oberholzer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views63 pages

Farrell PLD 10e Ch11 PowerPoint

Chapter 11 of 'Programming Logic and Design' covers advanced object-oriented programming concepts including constructors, destructors, composition, and inheritance. It explains how to create and utilize constructors and destructors, the principles of composition and inheritance, and the benefits of object-oriented programming. The chapter also includes practical examples and discussions to enhance understanding of these concepts.

Uploaded by

Paul Oberholzer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Programming

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

When you complete this chapter, you will be able to:

• 11.01 Create constructors

• 11.02 Create destructors

• 11.03 Describe composition

• 11.04 Describe inheritance

• 11.05 Describe how predefined classes are used to create GUI objects

• 11.06 Describe exception handling

• 11.07 List the advantages of object-oriented programming

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)

− Nondefault or parameterized constructor


 Requires arguments
• Declare constructors to be public so that other classes can instantiate
objects that belong to the class
• A class can have three types of constructors:
− Default constructor
 A class consists of an automatically created default constructor
in which the programmer has not explicitly written any
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. 4
Understanding Constructors (3 of 3)

 A class contains an explicitly created default constructor and no


longer contains the automatically supplied one
• A class can have three types of constructors:
− Nondefault or parameterized constructor
 It is explicitly created with one of more parameters

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

When does a written constructor become the default constructor for a


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. 6
Discussion Activity: Answer

A constructor for a class can be created with or without parameters.


When a constructor is created without parameters, the automatically
supplied default constructor no longer exists and the constructor
becomes the default constructor for the 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. 7
Default Constructors (1 of 2)

• Default constructor for the Employee class


− Establishes one Employee object with the identifier provided
• Write any statement you want in a constructor
• Place the constructor anywhere inside the class, outside of any other
method
− Often, programmers list the constructor first

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)

• When the constructor executes Figure 11-6: Employee


constructor that accepts a
− Numeric value within the constructor parameter
call is passed to Employee()
• Once you write a constructor for a class,
you no longer receive the automatically
supplied default constructor
• If a class has only one constructor, you
must provide an argument for every
object of that class you create
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. 12
Overloading Instance Methods and
Constructors
• Overload methods
− Write multiple versions of a method with the same name but
different parameter lists
• Any method or constructor in a class can be overloaded
− Can provide as many versions as you want

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)

• A destructor has no return type


• Programs never explicitly call a destructor
− Invoked automatically
• The last object created is the first object destroyed

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

Identify a true statement about destructors.


a. Destructors in a class can be overloaded.
b. Destructors are not invoked automatically.
c. Destructors are declared using asterisks as identifiers.
d. Destructors must have an empty parameter list.

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

Identify a true statement about destructors.


Answer: d. Destructors must have an empty parameter list.
Since you never explicitly create destructors, parameters
cannot be provided to them. Therefore, they have an empty
parameter list.

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)

• When a class contains objects of another class as data fields, the


relationship is called a whole-part relationship or composition
− Called a has-a relationship because one class “has an” instance
of another
• Example
− A class named “Date” that contains a month, day, and year
 Employee class has two Date fields to hold Employee’s birth
date and hire date

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)

• Understanding classes helps you organize objects in real life


− Understanding inheritance helps you organize them more precisely
• Inheritance
− A principle that enables you to apply your knowledge of a general
category to more specific objects
− Reuse the knowledge you gain about general categories and apply
it to more specific categories

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)

• Create a class by making it inherit from another class


− The new class contains data fields and methods automatically
− Reuse fields and methods that are already written and tested
• Example
− Create a class named CommissionEmployee that inherits all the
attributes and methods of Employee 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. 27
Understanding Inheritance (3 of 4)

Figure 11-13: An Employee class Figure 11-14: CommissionEmployee


inherits 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. 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)

• Benefits of using inheritance to create the CommissionEmployee


class:
− Saves time
− Reduces chance of errors
− Makes it easier to understand
− Reduces errors and inconsistencies in shared fields

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

• What is a whole-part relationship of a class?


• When is a protected access specifier used in a program?
• What is a throw statement in 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. 62
Summary

Click the link to review the objectives for this presentation.


Link to Objectives

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

You might also like