9.20. Programming Exercises

 
[Page 320 ( continued )]

9.11. The protected Data and Methods

The modifier protected can be applied to data and methods in a class. A protected datum or a protected method in a public class can be accessed by any class in the same package or its subclasses , even if the subclasses are in different packages.

The modifiers private , protected , and public are known as visibility or accessibility modifiers because they specify how class and class members are accessed. The visibility of these modifiers increases in this order:


Table 9.2 summarizes the accessibility of the members in a class. Figure 9.9 illustrates how a public, protected, default, and private datum or method in class C1 can be accessed from a class C2 in the same package, from a subclass C3 in the same package, from a subclass C4 in a different package, and from a class C5 in a different package.

Table 9.2. Data and Methods Visibility
Modifier on members in a class Accessed from the same class Accessed from the same package Accessed from a subclass Accessed from a different package
public check mark check mark check mark check mark
protected check mark check mark check mark
(default) check mark check mark
private check mark

Figure 9.9. Visibility modifiers are used to control how data and methods are accessed.
(This item is displayed on page 321 in the print version)

Use the private modifier to hide the members of the class completely so that they cannot be accessed directly from outside the class. Use no modifiers to allow the members of the class to be accessed directly from any class within the same package but not from other packages. Use the protected modifier to enable the members of the class to be accessed by the subclasses in any package or classes in the same package. Use the public modifier to enable the members of the class to be accessed by any class.


[Page 321]

Your class can be used in two ways: for creating instances of the class, and for creating subclasses by extending the class. Make the members private if they are not intended for use from outside the class. Make the members public if they are intended for the users of the class. Make the fields or methods protected if they are intended for the extenders of the class but not the users of the class.

The private and protected modifiers can only be used for members of the class. The public modifier and the default modifier (i.e., no modifier) can be used on members of the class as well on the class. A class with no modifier (i.e., not a public class) is not accessible by classes from other packages.

Note

A subclass may override a protected method in its superclass and change its visibility to public. However, a subclass cannot weaken the accessibility of a method defined in the superclass. For example, if a method is defined as public in the superclass, it must be defined as public in the subclass.


 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net