Java Reference
In-Depth Information
5.5. Inheriting Nested Types
Nested types, whether static classes, interfaces, or inner classes, are in-
herited the same way that fields are inherited. A declaration of a nested
type with the same name as that of an inherited nested type hides the
definition of the inherited type. The actual type referred to is determined
by the type of the reference used. Within a given class, the actual nested
type referred to is the one defined in the current class or inherited in the
current class.
Consider a framework that models devices that can be connected by dif-
ferent ports. The class Device is an abstract class that captures some
common behavior of all devices. A port also has some common generic
behavior so it is also modeled as an abstract class and, because ports ex-
ist only within devices, the Port class is made an inner class of Device . A
concrete device class defines the state for the device and the concrete in-
ner port classes for that device. During construction of a concrete device
class, references to the concrete ports of that class are initialized:
abstract class Device {
abstract class Port {
// ...
}
// ...
}
class Printer extends Device {
class SerialPort extends Port {
// ...
}
Port serial = new SerialPort();
}
A concrete device can itself be extended, as can the concrete inner port
classes, to specialize their behavior.
 
Search WWH ::




Custom Search