Java Reference
In-Depth Information
LISTING 9.3
//********************************************************************
// Dictionary.java Author: Lewis/Loftus
//
// Represents a dictionary, which is a book. Used to demonstrate
// inheritance.
//********************************************************************
public class
Dictionary
extends
Book
{
private int
definitions = 52500;
//-----------------------------------------------------------------
// Prints a message using both local and inherited values.
//-----------------------------------------------------------------
public double
computeRatio ()
{
return
(
double
) definitions/pages;
}
//----------------------------------------------------------------
// Definitions mutator.
//----------------------------------------------------------------
public void
setDefinitions (
int
numDefinitions)
{
definitions = numDefinitions;
}
//----------------------------------------------------------------
// Definitions accessor.
//----------------------------------------------------------------
public int
getDefinitions ()
{
return
definitions;
}
}
declared with protected visibility, a derived class can reference it. And protected
visibility allows the class to retain some encapsulation properties. The encapsula-
tion with protected visibility is not as tight as it would be if the variable or method
were declared private, but it is better than if it were declared public. Specifically, a
Search WWH ::
Custom Search