Java Objects |
The Java language supports five distinct access levels for variables: private, private protected, protected, public, and, if left unspecified, "friendly". The following chart shows the access level permitted by each specifier.The first column indicates whether or not the class itself has access to the variable defined by the access specifier. The second column indicates whether or not subclasses of the class (regardless of which package they are in) have access to the variable. The third column indicates whether or not classes in the same package (regardless of their parentage) as the class have access to the variable. And finally, the fourth column indicates that all classes have access to the variable.sub- pack- Specifier class class age world ----------------------------------------------------- private X private protected X X protected X X* X public X X X X friendly X XYou'll note that the protected access specifier/sub-class intersection has an '*' -- this indicates that this particular access case has a special caveat which is discussed in detail below.
Let's look at each access level in more detail.
Private
Let's begin with the most restrictive access level--private. A private variable is only accessible to the class in which it is defined. To declare a private variable, use the keywordprivate
. For example, the following class defines one private variable within it:Objects of type Alpha can inspect or modify theclass Alpha { private int iamprivate; }iamprivate
variable, but objects of other types cannot. For example, the following class, regardless of which package it is in or its parentage, cannot access theiamprivate
variable within the Alpha class.You can tell when one of your classes is attempting to access a variable to which it does not have access--the compiler will print an error message similar to the following and refuse to compile your program.class Beta { void accessMethod() { Alpha a = new Alpha(); a.iamprivate = 10; // illegal } }Beta.java:9: Variable iamprivate in class Alpha not accessible from class Beta. a.iamprivate = 10; // illegal ^ 1 errorPrivate Protected
The next most restrictive access specified is private protected. This access level includes the same access level as private plus allows any of the class's subclasses to access the variable. To declare a private protected variable, use the keywordsprivate protected
. For example, the following class defines one one private protected variable within it:Objects of type Alpha can inspect or modify theclass Alpha { private protected int iamprivateprotected; }iamprivateprotected
variable. In addition, subclasses of Alpha also have access toiamprivateprotected
. For instance, this subclass of Alpha can assign itsiamprivateprotected
variable to that of another Alpha object.class Beta extends Alpha { void modifyVariable(Alpha a) { a.iamprivateprotected = this.iamprivateprotected; // legal } }Protected
The next access level specifier is protected which allows the class itself, subclasses (with a caveat), and all classes in the same package to access the variable. To declare a protected variable, use the keywordprotected
. For example, take this version of the Alpha class which is now declared to be within a package named "Greek" and which has a single protected variable declared within it.Now, suppose that the class, Beta, was also declared to be a member of the Greek package. The Beta class can legally access thepackage Greek; class Alpha { protected int iamprotected; }iamprotected
variable declared within the Alpha class.That's pretty straightforward. Now, let's investigate how subclasses of Alpha can access protected variables.package Greek; class Beta { void accessMethod() { Alpha a = new Alpha(); a.iamprotected = 10; // legal } }Let's introduce a new class, Gamma, that derives from Alpha but lives in a different package. The Gamma class can access the
iamprotected
variable, but only on objects of type Gamma or its subclasses. For example, theaccessMethod()
of the following class attempts to access theiamprotected
variable on an object of type ProtectedAlpha, which is illegal, and on an object of type Gamma, which is legal.If a class is both a subclass of and in the same package as the class with the protected variable, then the class has access to the protected variable (its package status takes precedence over its subclass status).class Gamma { void accessMethod(ProtectedAlpha a) { a.iamprotected = 10; //illegal this.iamprotected = 10; } }Public
Now for the easiest access specifier--public. To declare a public variable, use the keywordpublic
. For example,Any class, in any package, has access to a class's public variables. For example, this version of the Beta classclass Alpha { public int iampublic; }can legally inspect and modify theclass Beta { void accessMethod() { Alpha a = new Alpha(); a.iampublic = 10; // legal } }iampublic
variable in the Alpha class.Friendly
And finally, the last access level is what you get if you don't explicitly set a variable's access to one of the other levels. For example, this version of the Alpha class declares a single "friendly" variable and lives within the Greek package.The Alpha class has access topackage Greek; class Alpha { int iamfriendly; }iamfriendly
. In addition, all the classes declared within the same package as Alpha also have access toiamfriendly
. For example, suppose that both Alpha and Beta were declared as part of the Greek package, then this Beta classcould legally accesspackage Greek; class Beta { void accessMethod() { Alpha a = new Alpha(); a.iamprotected = 10; // legal } }iamfriendly
.
Table of Contents |