Objects, Classes, and Interfaces |
One of the benefits of classes is that classes can protect their member variables and methods from access by other objects. Why is this important? Well, consider this. You're writing a class that represents a query on a database that contains all kinds of secret information, say employee records or income statements for your startup company.Certain information and queries contained in the class, the ones supported by the publicly accessible methods and variables in your query object, are OK for the consumption of any other object in the system. Other queries contained in the class are there simply for the personal use of the class. They support the operation of the class but should not be used by objects of another type--you've got secret information to protect. You'd like to be able to protect these personal variables and methods at the language level and disallow access by objects of another type.
In Java, you can use access specifiers to protect both a class's variables and its methods when you declare them. The Java language supports four distinct access levels for member variables and methods: private, protected, public, and, if left unspecified, package.
Note: The 1.0 release of the Java language supported five access levels: the four listed above plusprivate protected
. Theprivate protected
access level is not supported in versions of Java higher than 1.0; you should no longer be using it in your Java programs.
The following chart shows the access level permitted by each specifier.
Specifier class subclass package world private
X protected
X X* X public
X X X X package
X X The first column indicates whether the class itself has access to the member defined by the access specifier. As you can see, a class always has access to its own members. The second column indicates whether subclasses of the class (regardless of which package they are in) have access to the member. The third column indicates whether classes in the same package as the class (regardless of their parentage) have access to the member. The fourth column indicates whether all classes have access to the member.
Note that the protected/subclass intersection has an '*' . This particular access case has a special caveat discussed in detail later.
Let's look at each access level in more detail.
Private
The most restrictive access level is private. A private member is accessible only to the class in which it is defined. Use this access to declare members that should only be used by the class. This includes variables that contain information that if accessed by an outsider could put the object in an inconsistent state, or methods that, if invoked by an outsider, could jeopardize the state of the object or the program in which it's running. Private members are like secrets you never tell anybody.To declare a private member, use the
private
keyword in its declaration. The following class contains one private member variable and one private method:Objects of typeclass Alpha { private int iamprivate; private void privateMethod() { System.out.println("privateMethod"); } }Alpha
can inspect or modify theiamprivate
variable and can invokeprivateMethod
, but objects of other types cannot. For example, theBeta
class defined here:cannot access theclass Beta { void accessMethod() { Alpha a = new Alpha(); a.iamprivate = 10; // illegal a.privateMethod(); // illegal } }iamprivate
variable or invokeprivateMethod
on an object of typeAlpha
becauseBeta
is not of typeAlpha
.When one of your classes is attempting to access a member varible to which it does not have access, the compiler prints an error message similar to the following and refuses to compile your program:
Also, if your program is attempting to access a method to which it does not have access, you will see a compiler error like this:Beta.java:9: Variable iamprivate in class Alpha not accessible from class Beta. a.iamprivate = 10; // illegal ^ 1 errorNew Java programmers might ask if oneBeta.java:12: No method matching privateMethod() found in class Alpha. a.privateMethod(); // illegal 1 errorAlpha
object can access the private members of anotherAlpha
object. This is illustrated by the following example. Suppose theAlpha
class contained an instance method that compared the currentAlpha
object (this
) to another object based on theiriamprivate
variables:This is perfectly legal. Objects of the same type have access to one another's private members. This is because access restrictions apply at the class or type level (all instances of a class) rather than at the object level (this particular instance of a class).class Alpha { private int iamprivate; boolean isEqualTo(Alpha anotherAlpha) { if (this.iamprivate == anotherAlpha.iamprivate) return true; else return false; } }Note:
this
is a Java language keyword that refers to the current object. For more information about how to usethis
see The Method Body.Protected
The next access level specifier is protected, which allows the class itself, subclasses (with the caveat that we referred to earlier), and all classes in the same package to access the members. Use the protected access level when its appropriate for a class's subclasses to have access to the member, but not unrelated classes. Protected members are like family secrets--you don't mind if the whole family knows, and even a few trusted friends but you wouldn't want any outsiders to know.To declare a protected member, use the keyword
protected
. First, let's look at how the protected specifier affects access for classes in the same package. Consider this version of theAlpha
class which is now declared to be within a package namedGreek
and which has one protected member variable and one protected method declared in it:Now, suppose that the classpackage Greek; class Alpha { protected int iamprotected; protected void protectedMethod() { System.out.println("protectedMethod"); } }Gamma
was also declared to be a member of theGreek
package (and is not a subclass ofAlpha
). TheGamma
class can legally access anAlpha
object'siamprotected
member variable and can legally invoke itsprotectedMethod
:That's pretty straightforward. Now, let's investigate how thepackage Greek; class Gamma { void accessMethod() { Alpha a = new Alpha(); a.iamprotected = 10; // legal a.protectedMethod(); // legal } }protected
specifier affects access for subclasses ofAlpha
.Let's introduce a new class,
Delta
, that derives fromAlpha
but lives in a different package--Latin
. TheDelta
class can access bothiamprotected
andprotectedMethod
, but only on objects of typeDelta
or its subclasses. TheDelta
class cannot accessiamprotected
orprotectedMethod
on objects of typeAlpha
.accessMethod
in the following code sample attempts to access theiamprotected
member variable on an object of typeAlpha
, which is illegal, and on an object of typeDelta
, which is legal. Similarly,accessMethod
attempts to invoke anAlpha
object'sprotectedMethod
which is also illegal:If a class is both a subclass of and in the same package as the class with the protected member, then the class has access to the protected member.import Greek.*; package Latin; class Delta extends Alpha { void accessMethod(Alpha a, Delta d) { a.iamprotected = 10; // illegal d.iamprotected = 10; // legal a.protectedMethod(); // illegal d.protectedMethod(); // legal } }Public
The easiest access specifier ispublic
. Any class, in any package, has access to a class's public members. Declare public members only if such access cannot produce undesirable results if an outsider uses them. There are no personal or family secrets here; this is for stuff you don't mind anybody else knowing.To declare a public member, use the keyword
public
. For example,Let's rewrite ourpackage Greek; public class Alpha { public int iampublic; public void publicMethod() { System.out.println("publicMethod"); } }Beta
class one more time and put it in a different package thanAlpha
and make sure that it is completely unrelated to (not a subclass of)Alpha
:As you can see from the above code snippet,import Greek.*; package Roman; class Beta { void accessMethod() { Alpha a = new Alpha(); a.iampublic = 10; // legal a.publicMethod(); // legal } }Beta
can legally inspect and modify theiampublic
variable in theAlpha
class and can legally invokepublicMethod
.Package
The package access level is what you get if you don't explicitly set a member's access to one of the other levels. This access level allows classes in the same package as your class to access the members. This level of access assumes that classes in the same package are trusted friends. This level of trust is like that which you extend to your closest friends but wouldn't trust even to your family.For example, this version of the
Alpha
class declares a single package-access member variable and a single package-access method.Alpha
lives in theGreek
package:Thepackage Greek; class Alpha { int iampackage; void packageMethod() { System.out.println("packageMethod"); } }Alpha
class has access both toiampackage
andpackageMethod
. In addition, all the classes declared within the same package as Alpha also have access toiampackage
andpackageMethod
. Suppose that bothAlpha
andBeta
were declared as part of theGreek
package:package Greek; class Beta { void accessMethod() { Alpha a = new Alpha(); a.iampackage = 10; // legal a.packageMethod(); // legal } }Beta
can legally accessiampackage
andpackageMethod
as shown.
Objects, Classes, and Interfaces |