站内搜索: 请输入搜索关键词
当前页面: 在线文档首页 > Java Tutorial 5.0 英文版

Questions and Exercises: Nested Classes - Java Tutorial 5.0 英文版

The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Learning the Java Language
Lesson: Classes and Inheritance

Questions and Exercises: Nested Classes

Questions

1. The program Problem.java (in a .java source file) doesn't compile. What do you need to do to make it compile? Why?

2. Use the 5.0 API documentation for the Box (in the API reference documentation) class (in the javax.swing package) to help you answer the following questions.

a. What static nested class does Box define?
b. What inner class does Box define?
c. What is the superclass of Box’s inner class?
d. Which of Box’s nested classes can you use from any class?
e. How do you create an instance of Box’s Filler class?

Exercises

1. Get the file Class1.java (in a .java source file).

a. Compile and run Class1. What is the output?

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?

Check your answers. (in the Learning the Java Language trail)


Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

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