Java Reference
In-Depth Information
LISTING 9.12
//********************************************************************
// Pizza.java Author: Lewis/Loftus
//
// Represents a pizza, which is a food item. Used to demonstrate
// indirect referencing through inheritance.
//********************************************************************
public class
Pizza
extends
FoodItem
{
//-----------------------------------------------------------------
// Sets up a pizza with the specified amount of fat (assumes
// eight servings).
//-----------------------------------------------------------------
public
Pizza (
int
fatGrams)
{
super
(fatGrams, 8);
}
}
Even though the
Pizza
class cannot explicitly reference
calories
,
fatGrams
, or
CALORIES_PER_GRAM
, they are available for use indirectly when the
Pizza
object
needs them. A
Pizza
object cannot be used to invoke the
calories
method, but
it can call a method that can. Note that a
FoodItem
object was never created or
needed.
SELF-REVIEW QUESTIONS
(see answers in Appendix N)
SR 9.16
Are all members of a parent class inherited by the child? Explain.
SR 9.17
Could the
Pizza
class refer to the variable servings explicitly? What
about the
calories
method? Explain.
9.5
Designing for Inheritance
As a major characteristic of object-oriented software, inheritance
must be carefully and specifically addressed during software design.
A little thought about inheritance relationships can lead to a far
more elegant design, which pays huge dividends in the long term.
KEY CONCEPT
Software design must carefully and
specifically address inheritance.
Search WWH ::
Custom Search