Previous | Next | Trail Map | Writing Java Programs | Objects, Classes, and Interfaces


Controlling Access to Methods

When you declare a method in a Java class, you can allow or disallow other classes and object to call that method. You do this through the use of access specifiers.

The Java language supports five distinct access levels for methods: private, private protected, protected, public, and, if left unspecified, "friendly". The following chart shows the access level permitted by each specifier.

                                sub-    pack-
Specifier               class   class   age     world
-----------------------------------------------------
private                 X
private protected       X       X
protected               X       X*      X
public                  X       X       X       X

friendly                X               X
The first column indicates whether the class itself can call the method defined with the access specifier. The second column indicates whether subclasses of the class (regardless of which package they are in) can call the method. The third column indicates whether classes in the same package (regardless of their parentage) as the class can call the method. And finally, the fourth column indicates whether any class can call the method.

You'll note that the protected/subclass 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. Only the class in which a private method is defined can call that method. To declare a private method, use the keyword private. For example, the following class defines one private method within it:
class Alpha {
    private void iamprivate() {
	System.out.println("iamprivate");
    }
}
Objects of type Alpha can call the iamprivate() method, but objects of other types cannot. For example, the following class, regardless of which package it is in or its parentage, cannot call the iamprivate() method within the Alpha class.
class Beta {
    void accessMethod() {
        Alpha a = new Alpha();
        a.iamprivate();		// illegal
    }
}
You can tell when one of your classes is attempting to access a method to which it does not have access--the compiler will print an error message similar to the following and refuse to compile your program.
Beta.java:12: No method matching iamprivate() found in class Alpha.
        a.iamprivate();		// illegal
1 error

Private Protected

The next most restrictive access specified is private protected. This access level includes the private access level plus allows any of the class's subclasses to call the method. To declare a private protected method, use the keywords private protected in the method declaration. For example, the following class defines one private protected method within it:
class Alpha {
    private protected void iamprivateprotected() {
	System.out.println("iamprivateprotected");
    }
}
Objects of type Alpha can call the iamprivateprotected() method. In addition, subclasses of Alpha also have access to iamprivateprotected(). For instance, this subclass of Alpha, Beta, can call the iamprivateprotected() method of an Alpha object.
class Beta extends Alpha {
    void modifyVariable(Alpha a) {
        a.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 call the method. To declare a protected method, use the keyword protected. 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 method declared within it.
package Greek;

class Alpha {
    protected void iamprotected() {
	System.out.println("iamprotected");
    }
}
Now, suppose that the class, Gamma, was also declared to be a member of the Greek package. The Gamma class can legally call the iamprotected() method declared within the Alpha class because it is within the same package as Alpha.
package Greek;

class Gamma {
    void accessMethod() {
        Alpha a = new Alpha();
        a.iamprotected();	// legal
    }
}
That's pretty straightforward. Now, let's investigate how a the protected access level affects how subclasses of Alpha can access methods.

Let's introduce a new class, Delta, that derives from Alpha but lives in a different package. The Delta class can access the iamprotected() method, but only on objects of type Delta or its subclasses. The Delta class cannot call the iamprotected method on objects of type Alpha. For example, the accessMethod() of the following class attempts to access the iamprotected() method on an object of type Alpha, which is illegal, and on an object of type Delta, which is legal.

class Gamma extends Alpha {
    void accessMethod(Alpha a) {
        a.iamprotected = 10;		// illegal
        this.iamprotected = 10;		// legal
    }
}
If a class is both a subclass of and in the same package as the class with the protected method, then the class has access to the protected method.

Public

Now for the easiest access specifier--public. Any class, in any package, can call a class's public methods. To declare a public method, use the keyword public. For example,
package Greek;

class Alpha {
    public void iampublic() {
	System.out.println("iampublic");
    }
}
Now, let's go back to our Beta class which is now in a different package than Alpha and is completely unrelated to Alpha (not a subclass of).
package Roman;

class Beta {
    void accessMethod() {
	Alpha a = new Alpha();
        a.iampublic();			// legal
    }
}
can legally call the iampublic() method in the Alpha class.

Friendly

And finally, the last access level is what you get if you don't explicitly set a method's access level to one of the previous levels. This access level is known as "friendly". For example, this version of the Alpha class declares a single friendly method and lives within the Greek package.
package Greek;

class Alpha {
    void iamfriendly() {
	System.out.println("iamfriendly");
    }
}
Of course, the Alpha class can call the iamfriendly() method. In addition, all the classes declared within the same package as Alpha can also call iamfriendly(). For example, suppose that both Alpha and Beta were declared as part of the Greek package, then this Beta class
package Greek;

class Beta {
    void accessMethod() {
        Alpha a = new Alpha();
        a.iamfriendly();	// legal
    }
}
can legally call iamfriendly().


Previous | Next | Trail Map | Writing Java Programs | Objects, Classes, and Interfaces