Saturday, April 3, 2010

Controlling Access to Members of a Class

Home Page > Learning the Java Language > Classes and Objects
Controlling Access to Members of a Class
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 level—public, or package-private (no explicit modifier).
  • At the member level—public, private, protected, or package-private (no explicit modifier).

A class may be declared with the modifier public, in which case that class is visible to all classes everywhere. If a class has no modifier (the default, also known as package-private), it is visible only within its own package (packages are named groups of related classes—you will learn about them in a later lesson.)

At the member level, you can also use the public modifier or no modifier (package-private) just as with top-level classes, and with the same meaning. For members, there are two additional access modifiers: private and protected. The private modifier specifies that the member can only be accessed in its own class. The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package.

The following table shows the access to members permitted by each modifier.

Access Levels
ModifierClassPackageSubclassWorld
publicYYYY
protectedYYYN
no modifierYYNN
privateYNNN
The first data column indicates whether the class itself has access to the member defined by the access level. As you can see, a class always has access to its own members. The second column indicates whether classes in the same package as the class (regardless of their parentage) have access to the member. The third column indicates whether subclasses of the class — declared outside this package — have access to the member. The fourth column indicates whether all classes have access to the member.

Access levels affect you in two ways. First, when you use classes that come from another source, such as the classes in the Java platform, access levels determine which members of those classes your own classes can use. Second, when you write a class, you need to decide what access level every member variable and every method in your class should have.

Let's look at a collection of classes and see how access levels affect visibility. The following figure shows the four classes in this example and how they are related.

Classes and Packages of the Example Used to Illustrate Access Levels

Classes and Packages of the Example Used to Illustrate Access Levels

The following table shows where the members of the Alpha class are visible for each of the access modifiers that can be applied to them.
Visibility
ModifierAlphaBetaAlphasubGamma
publicYYYY
protectedYYYN
no modifierYYNN
privateYNNN


Tips on Choosing an Access Level: If other programmers use your class, you want to ensure that errors from misuse cannot happen. Access levels can help you do this.
  • Use the most restrictive access level that makes sense for a particular member. Use private unless you have a good reason not to.
  • Avoid public fields except for constants. (Many of the examples in the tutorial use public fields. This may help to illustrate some points concisely, but is not recommended for production code.) Public fields tend to link you to a particular implementation and limit your flexibility in changing your code.


We welcome your participation in our community. Please keep your comments civil and on point. You may optionally provide your email address to be notified of replies — your information is not used for any other purpose. By submitting a comment, you agree to these Terms of Use.

Echo 15 Items
Admin
P
What is the meaning of "no explicit modifier"?
Saturday, September 26, 2009, 21:20:23
FlagLikeReply
Liked by
Guest
Guest
ramu
no explicit modifier means default modifier.
Tuesday, January 05, 2010, 09:56:25
FlagLikeReply
animesh
what's ur mind do u know the make the jcreater in java how u make jcreater in core java its challenge for begginer
Saturday, January 30, 2010, 01:42:44
FlagLikeReply
Trendkill
"no explicit modifier" means that you do not specify any modifier. In other words you use package-private modifier in this case.
Monday, September 28, 2009, 19:07:04
FlagLikeReply
s m g
it's just a package-private modifier . you should read more carefully.
Friday, January 08, 2010, 10:10:19
FlagLikeReply
puneet
why is there only one public class in a java program?????
Wednesday, January 27, 2010, 13:27:56
FlagLikeReply
Brett
What do you mean? There are a lot of public classes provided with the standard java libraries as well as thousands of open source libraries....
Thursday, January 28, 2010, 18:37:14
FlagLikeReply
animesh
because if more than one public class use program then create confusion to find the main class in program. use public only main class in program .
Saturday, January 30, 2010, 01:47:45
FlagLikeReply
Liked by
Guest
faris jalali
you can have any number of public classes... in a java program.. itz just that you cannot have more than one public class in a source file coz the source file needs to be saved by the name of the public class itself... thus only one public class file per source files... you can go ahead and create many public classes in different source files to tackle this issue..
Sunday, February 28, 2010, 19:35:05
FlagLikeReply
sn
Is package-private modifier same as protected?? and does it mean tat the default modifier is protected for any object\member??
Monday, February 08, 2010, 01:57:07
FlagLikeReply
Step
No, package-private modifier is same as no modifier. See the tables.
Wednesday, February 10, 2010, 11:05:39
FlagLikeReply
asett
I understand that a package-private modifier is the same as no modifier. What is not clear is how to write it down. For example if I want to declare a class as package-private, which is correct?
1. package-private class MyClass
2. class MyClass
Monday, March 08, 2010, 18:52:37
FlagLikeReply
gaurav
how can beta access alpha, when alpha is declared protected? As per my knowledge declaring a class protected allows only its subclasses to access that class. Can somebody please elaborate on this? Thanks !
Wednesday, March 10, 2010, 14:36:36
FlagLikeReply
John
gaurev, it clearly states that protected means that the member can be accessed within its own package as well as any sub-classes of alpha. Beta is in the same package as alpha and so can access members of alpha.
Friday, March 12, 2010, 10:11:30
FlagLikeReply
gaurav
oops !! I totally missed that .... Thanks John !!
Tuesday, March 16, 2010, 20:21:30
FlagLikeReply
Liked by
Guest

No comments:

Post a Comment