Java Reference
In-Depth Information
The output of this program is the following:
Item ID: D01
Name: T SHIRT
Retail Price: $16.99
Price: $11.89
Quantity: 90
Item ID: C02
Name: LG MUG
Retail Price: $12.99
Price: $9.09
Quantity: 82
Item ID: C01
Name: MUG
Retail Price: $9.99
Price: $6.99
Quantity: 150
Item ID: C03
Name: MOUSEPAD
Retail Price: $10.49
Price: $5.25
Quantity: 800
Many implementation details of these classes are hidden from
GiftShop
and other
classes that would use the package.
For instance, the programmer who developed
GiftShop
doesn't need to know that
Storefront
uses a linked list to hold the entire store's product data. If the developer of
Storefront
decided later to use a different data structure, as long as
getSize()
and
getItem()
returned the expected values,
GiftShop
would continue to work correctly.
The classes you have worked with thus far are all members of a package either because
you specified a package name with the
package
declaration or because the default pack-
age was used. Classes that belong to a package are known as
top-level
classes. When
Java was introduced, they were the only classes supported by the language.
6
In the current version of Java, you can define a class inside a class as if it were a method
or a variable.
These types of classes are called
inner
classes. Listing 6.5 contains the
SquareTool
application, which uses an inner class called
Square
to square a floating-point number
and store the result.