Historia wymaga pasterzy, nie rzeźników.

In Adventure.java, add an interface called CanClimb,
following the form of the other interfaces. Feedback
10. Write a program that imports and uses Month.java. Feedback
11. Following the example given in Month.java, create an
enumeration of days of the week. Feedback
12. Create an interface with at least one method, in its own package.
Create a class in a separate package. Add a protected inner class
that implements the interface. In a third package, inherit from
your class and, inside a method, return an object of the protected
inner class, upcasting to the interface during the return. Feedback
13. Create an interface with at least one method, and implement that
interface by defining an inner class within a method, which
returns a reference to your interface. Feedback
14. Repeat Exercise 13 but define the inner class within a scope within
a method. Feedback
15. Repeat Exercise 13 using an anonymous inner class. Feedback
16. Modify HorrorShow.java to implement DangerousMonster
and Vampire using anonymous classes.
17. Create a private inner class that implements a public interface.
Write a method that returns a reference to an instance of the
private inner class, upcast to the interface. Show that the inner
class is completely hidden by trying to downcast to it. Feedback
Chapter 8: Interfaces & Inner Classes
391

18. Create a class with a nondefault constructor (one with arguments)
and no default constructor (no “no-arg” constructor). Create a
second class that has a method which returns a reference to the
first class. Create the object to return by making an anonymous
inner class that inherits from the first class. Feedback
19. Create a class with a private field and a private method. Create
an inner class with a method that modifies the outer class field and
calls the outer class method. In a second outer class method,
create an object of the inner class and call its method, then show
the effect on the outer class object. Feedback
20. Repeat Exercise 19 using an anonymous inner class. Feedback
21. Create a class containing a nested class. In main( ), create an
instance of the inner class. Feedback
22. Create an interface containing a nested class. Implement this
interface and create an instance of the nested class. Feedback
23. Create a class containing an inner class that itself contains an
inner class. Repeat this using nested classes. Note the names of
the .class files produced by the compiler. Feedback
24. Create a class with an inner class. In a separate class, make an
instance of the inner class. Feedback
25. Create a class with an inner class that has a nondefault constructor
(one that takes arguments). Create a second class with an inner
class that inherits from the first inner class. Feedback
26. Repair the problem in WindError.java. Feedback
27. Modify Sequence.java by adding a method getRSelector( )
that produces a different implementation of the Selector
interface that moves backward through the sequence from the
end to the beginning. Feedback
28. Create an interface U with three methods. Create a class A with a method that produces a reference to a U by building an
anonymous inner class. Create a second class B that contains an
392
Thinking in Java
www.BruceEckel.com

array of U. B should have one method that accepts and stores a
reference to a U in the array, a second method that sets a reference
in the array (specified by the method argument) to null and a
third method that moves through the array and calls the methods
in U. In main( ), create a group of A objects and a single B. Fill the B with U references produced by the A objects. Use the B to call back into all the A objects. Remove some of the U references
from the B. Feedback
29. In GreenhouseControls.java, add Event inner classes that
turn fans on and off. Configure GreenhouseController.java to
use these new Event objects. Feedback
30. Inherit from GreenhouseControls in
GreenhouseControls.java to add Event inner classes that turn
water mist generators on and off. Write a new version of
GreenhouseController.java to use these new Event objects.
31. Show that an inner class has access to the private elements of its
outer class. Determine whether the reverse is true. Feedback

Chapter 8: Interfaces & Inner Classes
393

9: Error Handling
with Exceptions
The basic philosophy of Java is that “badly formed code
will not be run.”
The ideal time to catch an error is at compile time, before you even try to
run the program. However, not all errors can be detected at compile time.
The rest of the problems must be handled at run time, through some
formality that allows the originator of the error to pass appropriate
information to a recipient who will know how to handle the difficulty
properly. Feedback
C and other earlier languages often had multiple error-handling schemes,
and these were generally established by convention and not as part of the
programming language. Typically, you returned a special value or set a