Java Reference
In-Depth Information
7.4 Class Relationships
The classes in a software system have various types of relationships to each
other. Three of the more common relationships are dependency, aggregation,
and inheritance.
We've seen dependency relationships in many examples in which one class
“uses” another. This section revisits the dependency relationship and explores
the situation where a class depends on itself. We then explore aggregation, in
which the objects of one class contain objects of another, creating a “has-a”
relationship. Inheritance, which we introduced in Chapter 1, creates an “is-a”
relationship between classes. We defer our detailed examination of inheritance
until Chapter 8.
Dependency
In many previous examples, we've seen the idea of one class being dependent on
another. This means that one class relies on another in some sense. Often the
methods of one class will invoke the methods of the other class. This establishes
a “uses” relationship.
Generally, if class A uses class B , then one or more methods of class A invoke
one or more methods of class B . If an invoked method is static, then A merely ref-
erences B by name. If the invoked method is not static, then A must have access to
a specific instance of class B in order to invoke the method. That is, A must have
a reference to an object of class B .
The way in which one object gains access to an object of another class is
an important design decision. It occurs when one class instantiates the objects
of another, but that's often the basis of an aggregation relationship. The
access can also be accomplished by passing one object to another as a method
parameter.
In general, we want to minimize the number of dependencies among classes.
The less dependent our classes are on each other, the less impact changes and
errors will have on the system.
Dependencies Among Objects of the Same Class
In some cases, a class depends on itself. That is, an object of one class interacts
with another object of the same class. To accomplish this, a method of the class
may accept as a parameter an object of the same class. Designing such a class
drives home the idea that a class represents a particular object.
 
Search WWH ::




Custom Search