Anda di halaman 1dari 2

Access Specifier Access level modifiers determine whether other classes can use a particular field or invoke a particular

method. There are two levels of access control:


At the top levelpublic, or package-private (no explicit modifier). At the member levelpublic, private, protected, or package-private (no explicit modifier).

A class ma be declared with the modifier public, in which case that class is visible to all classes ever where. !f a class has no modifier (the default, also known as package-private), it is visible onl within its own packa"e (packa"es are named "roups of related classes ou will learn about them in a later lesson.) At the member level, ou can also use the public modifier or no modifier (package-private) #ust as with top$level classes, and with the same meanin". %or members, there are two additional access modifiers: private and protected. The private modifier specifies that the member can onl be accessed in its own class. The protected modifier specifies that the member can onl be accessed within its own packa"e (as with package-private) and, in addition, b a subclass of its class in another packa"e. The followin" table shows the access to members permitted b each modifier. Access Levels Modifier Class Package Subclass World
public protected

& & &

& & & '

& & ' '

& ' ' '

no modifier &
private

The first data column indicates whether the class itself has access to the member defined b the access level. As ou can see, a class alwa s has access to its own members. The second column indicates whether classes in the same packa"e as the class (re"ardless of their parenta"e) have access to the member. The third column indicates whether subclasses of the class declared outside this packa"e have access to the member. The fourth column indicates whether all classes have access to the member. Access levels affect ou in two wa s. %irst, when ou use classes that come from another source, such as the classes in the (ava platform, access levels determine which members of those classes our own classes can use. Second, when ou write a class, ou need to decide what access level ever member variable and ever method in our class should have. )et*s look at a collection of classes and see how access levels affect visibilit . The followin" fi"ure shows the four classes in this example and how the are related.

+lasses and ,acka"es of the -xample .sed to !llustrate Access )evels The followin" table shows where the members of the Alpha class are visible for each of the access modifiers that can be applied to them. Visibility Modifier Alpha Beta Alphasub Gamma
public protected

& & &

& & & '

& & ' '

& ' ' '

no modifier &
private

ips o! Choosi!g a! Access Level" !f other pro"rammers use our class, ou want to ensure that errors from misuse cannot happen. Access levels can help ou do this.

.se the most restrictive access level that makes sense for a particular member. .se private unless ou have a "ood reason not to. Avoid public fields except for constants. (/an of the examples in the tutorial use public fields. This ma help to illustrate some points concisel , but is not recommended for production code.) ,ublic fields tend to link ou to a particular implementation and limit our flexibilit in chan"in" our code.

Anda mungkin juga menyukai