Java Reference
In-Depth Information
Another way to distinguish between a composition and specialization relationship is to say whether one class
“has a” class, or whether one class “is a” class. For example, EmpFrame is a kind of
Frame
but Employee has a
Frame
.
So in the first case, EmpFrame and
Frame
are related through specialization. To say it another way, EmpFrame is
an extension of the
Frame
class. Or, even more simply, EmpFrame “is a”
Frame
. In the original example, Employee
and
Frame
are related through composition. You can say that the
Frame
object is part of Employee or Employee
“has a”
Frame
.
Figure
3-6
is a UML (Unified Modeling Language) diagram. UML provides a standard way of graphically showing
class relationships. For instance, each class is represented by a rectangle with the class name. A line between classes
represents a relationship. A line with a diamond on one end and an arrowhead on the other end means the classes are
related through composition (“has a”). A line with an open triangle at one end means the classes are related through
specialization. The open triangle indicates the superclass.
String
Frame
has a
is a
Employee
Frame
EmpFrame
Label
Figure 3-6.
Ready for another big concept? Ready or not, here it comes. The classes in the JRE are related through
composition and specialization.
For instance, if we use the Package Explorer and expand the JRE System Library, rt.jar, graphics.jar, java.awt, and
then Frame.class, all the
Frame
components will be displayed. Scrolling down in the Package Explorer would show a
String
component called title (see the first arrow in Figure
3-7
). This means that one of the
Frame
class's components
is a
String
variable named title. Notice that the Package Explorer also shows the getTitle method (the second arrow
in Figure
3-7
) and, if you scroll down, you can see that there is also a setTitle method. These two methods are used
to access the
Frame
class's title property. As mentioned earlier, when a variable has setters and getters, the variable
is not only a component of the class but it is considered a class property. So, title is a property of
Frame
—which you
already knew!