? 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 12 - 1
Chapter 12
Reusable Classes and Packages
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 12 - 2
Chapter 12 Objectives
After you have read and studied this chapter,you
should be able to
Describe four different object categories and
use them effectively in designing classes.
Define a package and place reusable classes in
it.
Write a method that calls the superclass’s
method explicitly by using the reserved word
super.
Define overloaded methods.
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 12 - 3
Reusability
We say a class is reusable if it can be used in
different programs.
Classes in the javabook package is highly reusable.
Not all classes have to be reusable,We may design a class specifically for a given program,Such class is
not reusable.
We must design the classes accordingly,If we intend
the class to be reusable,then we need to design it so
that the class is indeed reusable.
One way to increase the reusability is to design a
class that implements a single well-defined task.
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 12 - 4
Object Categories
To aid in designing a single-task class,we divide
object tasks into four categories:
A user interface object handles the interaction
between the end user and the program.
A controller object supervises other objects in the
program.
An application logic object captures the logic of
the real world for which the program is written.
A storage object takes care of file input and
output.
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 12 - 5
Method Overriding
Two techniques to make a class more reusable are method overriding and method overloading.
We say a method of a subclass overrides the inherited method when we redefine the method in the subclass.
Example,setVisible of DrawingBoard overrides setVisible of
MainWindow
public class DrawingBoard extends MainWindow
{
.,,
public void setVisible( boolean state )
{
super.setVisible( state );
graphic = getGraphics( );
}
.,,
}
The reserved word
super refers to the
superclass.
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 12 - 6
Method Overloading
The signature of a method is determined by the
method name and the number and data types of
parameters,
If multiple methods have the same method name but
different signatures,the method name is overloaded
and we call these methods overloaded.
public void drawRectangle( int x,int y,int width,int length )
{
.,,
}
public void drawRectangle( Rectangle rect )
{
.,,
}
drawRectangle is
overloaded.
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 12 - 7
Overloading Constructors
As we can overload regular methods,we can
overload constructors,
A class with multiple constructors allows the
programmer a flexibility in creating instances,This
improves the usability of the class.
public DrawingBoard( )
{
this(,DRAWING BOARD”);
}
public DrawingBoard( String title )
{
setTitle( title );
setResizable( false );
setBackground( Color.white );
}
This calls the other
constructor.
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 12 - 8
Reusable EggyPeggy
Problem Statement
Write a class that converts a given string to an Eggy-Peggy
string by placing the programmer-specified word in front of all
vowels in the given string,If the programmer does not
specify the word,then the default prefix,egg” will be used.
Capabilities
The class is not tied to any particular program or user
interface.
The class provides an application logic of playing the
Eggy-Peggy game.
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 12 - 9
EggyPeggy Development Steps
1,Start with a class skeleton,Define the necessary data
members and constructors.
2,Implement the setPrefixWord and convert methods,Add
any private methods as necessary.
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 12 - 10
Reusable HiLo
Problem Statement
Write an application logic class that will play Hi-Lo games,
The objective of the game is for the user to guess the
computer-generated secret number in the least number of
tries,The programmer can set the low and high bounds of a
secret number and the number of guesses the user is allowed,
If the number of guesses allowed is not set by the
programmer,then it is set by the HiLo object based on the
designated low and high bounds,The default low and high
bounds are 1 and 100,The default number of guesses for the
default low and high bounds is 7.
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 12 - 11
HiLo Development Steps
1,Start with a class skeleton,Define the necessary data
members and constructors,Add any other methods
necessary to implement the constructors.
2,Implement the newGame method,Add any other
methods necessary to implement this method.
3,Implement the nextGuess method,Add any other
methods necessary to implement this method.
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 12 - 12
Putting Classes in a Package
1,Create a folder (directory) and name this folder using the package name.
Suppose you want to call your package game,then name the
folder game.
2,Insert the declaration
package <package-name> ;
at the beginning of the classes you want to add to the package.
If the package is game,then the declaration is
package game;
3,Compile the source files and place the bytecode files in the package folder,
The End