Controlling Access to Members of a Class
One of the benefits of classes is that classes can protect
their member variables and methods from access by other objects. Normally all
variables declared within a class are private or protected from other parts of a
program. Variables should normally only be accessible through the classes
methods.
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.
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:
class Alpha {
private int iamprivate;
private int privateMethod() {
return iamprivate;
}
}
Protected
The next access level specifier is protected, which
allows the class itself, subclasses, and all classes in the same package to
access the members (to be discussed later). Use the protected access level when
it's 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.
Consider the following class which has one protected member variable and one
protected method declared in it:
public class Alpha {
protected int iamprotected;
protected int protectedMethod() {
return iamprotected;
}
}
Public
The easiest access specifier is public. Any class 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,
public class Alpha {
public int iampublic;
public int publicMethod() {
return iampublic;
}
}
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 (or directory) 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, the class below declares a single
package-access member variable and a single package-access method.
class Alpha {
int iampackage;
int packageMethod() {
return iampackage;
}
}