The JavaTM Tutorial

| Start of Tutorial > Start of Trail > Start of Lesson | Search Feedback Form

Trail: Learning the Java Language
Lesson: Classes and Inheritance

## Answers to Questions and Exercises: Implementing Nested Classes

### Questions

> Question 1: Match each situation in the first column with the most appropriate type of nested class in the second column.

  a. The only users of this nested class will be instances of the enclosing class or instances of the enclosing class’s subclasses. | 1. anonymous inner class
  b. Anyone can use this nested class. | 2. protected inner class
  c. Only instances of the declaring class need to use this nested class, and a particular instance might use it several times. | 3. public static nested class
  d. This tiny nested class is used just once, to create an object that implements an interface. | 4. protected static nested class
  e. This nested class has information about its enclosing class (not about instances of the enclosing class) and is used only by its enclosing class and perhaps their subclasses. | 5. private static nested class
  f. Similar situation as the preceding (choice e), but not intended to be used by subclasses. | 6. private inner class

> Answer 1:
> - a. 2. (protected inner class)
> - b. 3. (public static nested class)
> - c. 6. (private inner class)
> - d. 1. (anonymous inner class)
> - e. 4. (protected static nested class)
> - f. 5. (private static nested class)

> Question 2: The program `Problem.java` doesn't compile. What do you need to do to make it compile? Why?
> Answer 2: Add `final` in front of the declaration of the `timer` variable. A nested class declared within a method has access to any final, local variables in scope.
> See `ProblemSolved.java`.

> Question 3: Use the 1.3 API documentation for the `Box` class (in the `javax.swing` package) to help you answer the following questions.

> > a. What static nested class does `Box` define?
> > Answer 3a: `Box.Filler
> > ` b. What inner class does `Box` define?
> > Answer 3b: `Box.AccessibleBox
> > ` c. What is the superclass of `Box`’s inner class?
> > Answer 3c:`[java.awt.]Container.AccessibleAWTContainer
> > ` d. Which of `Box`’s nested classes can you use from any class?
> > Answer d: `Box.Filler
> > ` e. How do you create an instance of `Box`’s `Filler` class?
> > Answer 3e:`new Box.Filler(minDimension, prefDimension, maxDimension)`

### Exercises

> Exercise 1: First, get the source file `InnerClassDemo.java`.

> > a. Compile and run `InnerClassDemo.java`.

> > Answer 1a: N/A. [This step is here just to make sure you can compile and run Swing programs, before you try to change the programs.]

> > b. Make a copy of `InnerClassDemo`. Add to it an inner class named `MyActionListener` that implements the `ActionListener` interface. The `ActionListener` interface defines a single method. Put the following code into your implementation of the method: `quit();`
> > Delete the double forward slashes (`//`) in front of the following line of code:

> > `//button.addActionListener(new MyActionListener());`

> > Now compile and run the program. What is the difference in behavior between this version and the previous version of `InnerClassDemo`?

> > Answer 1b: See `InnerClassDemo2.java`. In this version, the button actually does something; it makes the program exit.

> > c. Make a copy of the program you created in exercise 1b. Change your `ActionListener` implementation to be an anonymous inner class. (Hint: The program has another anonymous inner class, a `WindowAdapter`, which you can refer to for syntax help.)

> > Answer 1c: See `InnerClassDemo3.java`.

> Exercise 2: Get the file `Class1.java`.

> > a. Compile and run `Class1`. What is the output?
> > Answer 2a:
> > `InnerClass1: getString invoked.
> > InnerClass1: getAnotherString invoked.`

> > b. Create a file called `Class2.java` that defines subclasses of both `Class1` and its inner class, `InnerClass1`. (Call the subclasses `Class2` and `InnerClass2`, respectively.) `InnerClass2` should override the `getAnotherString` method to return `"InnerClass2 version of getAnotherString invoked"`. `Class2` should define one constructor and one method:

> > - A no-argument constructor that initializes the inherited `ic` instance variable to be an instance of `InnerClass2`
> > - A main method that creates an instance of `Class2` and invokes `displayStrings` on that instance

> > What is the output when you run `Class2`?

> > Answer 2b:
> > `InnerClass1: getString invoked.
> > InnerClass2 version of getAnother String invoked.`

> > See `Class2.java`.

| Start of Tutorial > Start of Trail > Start of Lesson | Search Feedback Form

Copyright 1995-2004 Sun Microsystems, Inc. All rights reserved.