Java Reference
In-Depth Information
Book
# pages : int
+ setPages(numPages : int) : void
+ getPages() : int
Dictionary
Words
- definitions : int
+ computeRatio() : double
+ setDefinitions (numDefinitions : int) : void
+ getDefinitions () : int
+ main (args : String[]) : void
FIGURE 9.1 A UML class diagram showing an inheritance relationship
variable or method declared with protected visibility may be accessed
by any class in the same package, in addition to being accessible by
any derived classes. The relationships among all Java modifiers are
explained completely in Appendix E.
In a UML diagram, protected visibility can be indicated by pre-
ceding the protected member with a hash mark (#). The pages vari-
able of the Book class has this annotation in Figure 9.1.
Each variable or method retains the effect of its original visibility modifier. For
example, the setPages method is still considered to be public in its inherited form
in the Dictionary class.
Let's be clear about our terms. All methods and variables, even those declared
with private visibility, are inherited by the child class. That is, their definitions
exist and memory space is reserved for the variables. It's just that they can't be
referenced by name. This issue is explored in more detail in section 9.4.
Constructors, however, are not inherited. Constructors are special methods
that are used to set up a particular type of object, so it doesn't make sense for a
class called Dictionary to have a constructor called Book . But you can imagine
that a child class may want to refer to the constructor of the parent class, which
is one of the reasons for the super reference, described next.
KEY CONCEPT
Protected visibility provides the best
possible encapsulation that permits
inheritance.
VideoNote
Overview of inheritance.
 
Search WWH ::




Custom Search