530 Object-Oriented Programming Chapter 9 An inner
530 Object-Oriented Programming Chapter 9 An inner class can also be defined inside a method of a class. Such an inner class has access to its outer class s members and to the final local variables for the method in which it is defined. Inner class definitions are used mainly in event handling. Class JFrame provides the basic attributes and behaviors of a window a title bar and buttons to minimize, maximize and close the window. An inner class object has access to all the variables and methods of the outer class object. Because an anonymous inner class has no name, one object of the anonymous inner class must be created at the point where the class is defined in the program. An anonymous inner class can implement an interface or extend a class. The event generated when the user clicks the window s close box is a window closing event. Method addWindowListener registers a window event listener. The argument to addWindowListener must be a reference to an object that is a WindowListener (package java.awt.event). For event handling interfaces with more than one method, Java provides a corresponding class (called an adapter class) that already implements all the methods in the interface for you. Class WindowAdapter implements interface WindowListener, so every WindowAdapter object is a WindowListener. Compiling a class that contains inner classes results in a separate .classfile for every class. Inner classes with class names can be defined as public, protected, package access or private and are subject to the same usage restrictions as other members of a class. To access the outer class s this reference, use OuterClassName.this. The outer class is responsible for creating objects of its nonstatic inner classes. An inner class can be declared static. TERMINOLOGY abstract class hierarchical relationship abstract method implementation inheritance abstract superclass implicit reference conversion abstraction indirect superclass anonymous inner class infinite recursion error base class inheritance Boolean class inheritance hierarchy Character class inner class class hierarchy Integer class client of a class interface composition interface inheritance direct superclass is a relationship Double class JFrame class dynamic method binding late binding extends Long class extensibility member access control final class member object final instance variable method overriding final method multiple inheritance garbage collection Number class has a relationship Object class Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01