|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
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 classb. Anyone can use this nested class. 2. protected inner classc. 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 classd. This tiny nested class is used just once, to create an object that implements an interface. 4. protected static nested classe. 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 classf. Similar situation as the preceding (choice e), but not intended to be used by subclasses. 6. private inner class2. The program
Problem.javadoesn't compile. What do you need to do to make it compile? Why?
3. Use the 1.3 API documentation for the
Boxclass (in the
javax.swingpackage) to help you answer the following questions.a. What static nested class does
Boxdefine?
b. What inner class doesBoxdefine?
c. What is the superclass ofBoxs inner class?
d. Which ofBoxs nested classes can you use from any class?
e. How do you create an instance ofBoxsFillerclass?
Check your answers.1. First, get the source file
InnerClassDemo.javaa. Compile and run
InnerClassDemo.java.
b. Make a copy ofInnerClassDemo. Add to it an inner class namedMyActionListenerthat implements theActionListenerinterface. TheActionListenerinterface 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?
c. Make a copy of the program you created in exercise 1b. Change yourActionListenerimplementation to be an anonymous inner class. (Hint: The program has another anonymous inner class, aWindowAdapter, which you can refer to for syntax help.)
2. Get the file
Class1.java.
a. Compile and run
Class1. What is the output?b. Create a file called
Class2.javathat defines subclasses of bothClass1and its inner class,InnerClass1. (Call the subclassesClass2andInnerClass2, respectively.)InnerClass2should override thegetAnotherStringmethod to return"InnerClass2 version of getAnotherString invoked".Class2should define one constructor and one method:
- A no-argument constructor that initializes the inherited
icinstance variable to be an instance ofInnerClass2- A main method that creates an instance of
Class2and invokesdisplayStringson that instanceWhat is the output when you run
Class2?
|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2004 Sun Microsystems, Inc. All rights reserved.