Java Reference
In-Depth Information
Figure 9.8
Access levels: private,
protected, and public
SXEOLF
Client
SomeClass
SULYDWH
Subclass1
Subclass2
SURWHFWHG
While protected access can be applied to any member of a class, it is usually reserved for
methods and constructors. It is not usually applied to fields, because that would be a weaken-
ing of encapsulation. Wherever possible, mutable fields in superclasses should remain private.
There are, however, occasional valid cases where direct access by subclasses is desirable.
Inheritance represents a much closer form of coupling than does a normal client relationship.
Inheritance binds the classes closer together, and changing the superclass can more easily break the
subclass. This should be taken into consideration when designing classes and their relationships.
Exercise 9.6 The version of display shown in Code 9.2 produces the output shown in
Figure 9.9. Reorder the statements in the method in your version of the network project so that
it prints the details as shown in Figure 9.10.
Figure 9.9
Possible output from
display : superclass
call at the beginning
of display (shaded
areas printed by su-
perclass method)
Leonardo da Vinci
40 seconds ago - 2 people like this.
No comments.
Had a great idea this morning.
But now I forgot what it was. Something to do with flying...
Figure 9.10
Alternative output
from display
(shaded areas
printed by superclass
method)
Had a great idea this morning.
But now I forgot what it was. Something to do with flying...
Leonardo da Vinci
40 seconds ago - 2 people like this.
No comments.
 
Search WWH ::




Custom Search