Inner Classes

   

Beginning with JDK 1.1, Java has supported the concept of nested, or inner, class declarations. Inner classes aren't found in most other programming languages, so its usefulness might not be clear to you at first. This section describes why inner classes exist and what purposes they serve.

Inner Classes Defined

Inner classes are classes that are declared within the body of another class. In fact, you can even declare a class within the body of a method. You might wonder why you would ever want to declare one class inside another. You are never required to use inner classes when you develop in Java, but in certain situations they provide you with the ability to organize your code in a more understandable fashion, and, occasionally, they provide the compiler with a means to further optimize your code.

Reasons to Use Inner Classes

The best way to demonstrate the usefulness of inner classes is by example. Interfaces are described in detail in Chapter 9, but for now think of an interface as a set of methods that a class must implement to provide some behavior needed by another class. No matter how many methods an interface defines, a class that supports it must provide an implementation for each one. It often happens that a class does not need to implement any behavior for one or more of an interface's methods. The class must still include a do-nothing implementation for each of these methods. If you write a class that has a lot of methods that don't do anything, the class declaration will begin to look cluttered and the purpose of the class will become less obvious to another programmer reading through the method implementations .

You can avoid the declaration of do-nothing implementations of interface methods if you use an adapter class. An adapter class implements an interface by providing a do-nothing implementation for each method. On its own, an adapter class isn't useful, but to a class that wants to implement an interface without implementing all the methods, it provides a clean approach. A class uses an adapter class by extending it and then overriding only those methods that have behavior that needs to be implemented.

This relates to inner classes because a class that extends an adapter class is often limited to a specific use given that it only implements a subset of an interface's methods. If such a class is too specific to reuse, using an inner class reduces the overhead of the class declaration by defining it within the only class that needs that particular implementation. Adapter classes provided within the Swing packages are commonly used as shown in Listing 7.11.

Listing 7.11 MyFrame.java ” Use of Adapter Classes
 import java.awt.event.*; import javax.swing.*; public class MyFrame extends JFrame {   public MyFrame() {     // declare an inner class that extends an adapter class     class MyWindowAdapter extends WindowAdapter {       // only do something when the window is closing       public void windowClosing(WindowEvent e) {         System.out.println("Window about to close");         System.exit(0);       }     }     this.addWindowListener( new MyWindowAdapter() );     // use an anonymous adapter class instead     this.addWindowListener( new WindowAdapter() {       // only do something when the window is activated       public void windowActivated(WindowEvent e) {         System.out.println("Window is active");       }     }  );   }   public static void main(String[] args) {     MyFrame frame = new MyFrame();     frame.setVisible(true);   } } 

Notice in Listing 7.11 that the WindowAdapter class is used in two different ways. The windowClosing method is implemented within the declaration of the MyWindowAdapter inner class that extends WindowAdapter. Multiple instances of MyWindowAdapter could be declared if MyFrame required it. Now examine the inner class that implements windowActivated. There is no name because this declaration demonstrates the use of an anonymous inner class. An anonymous inner class is an unnamed class declared for a single use. There is no need for it to have a name because it will never be referred to outside its declaration. You will see anonymous inner classes used extensively in Swing applications.

How Inner Classes Work

Under Java 1.0, inner classes were not available. Java designers were able to make the programs that you write using inner classes work with virtual machines that were designed from the 1.0 specification because inner classes aren't really new. When you write a class with an inner class inside it, the compiler takes the inner class outside of the containing class and adjusts the compiled result.

   


Special Edition Using Java 2 Standard Edition
Special Edition Using Java 2, Standard Edition (Special Edition Using...)
ISBN: 0789724685
EAN: 2147483647
Year: 1999
Pages: 353

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net