CHAPTER 15

image

Access Levels

There are four access levels available in Java. These are public, protected, private and package-private. Package-private cannot be explicitly declared by using a keyword. Instead, it is the default access level for every member in Java.

public    int myPublic;   // unrestricted access
protected int myProtected;// package or subclass access
          int myPackage;  // package access
private   int myPrivate;  // class access

Private access

The most restrictive access level is private. Members with this level can only be used inside of the enclosing (containing) class.

package mypackage;
public class MyApp
{
  public    int myPublic;
  protected int myProtected;
            int myPackage;
  private   int myPrivate;
 
  void test()
  {
    myPublic    = 0; // allowed
    myProtected = 0; // allowed
    myPackage   = 0; // allowed
    myPrivate   = 0; // allowed
  }
}
 

Package-private access

Package-private members can be accessed anywhere within the containing package, but not from another package.

package mypackage;
public class MyClass
{
  void test(MyApp m)
  {
    m.myPublic    = 0; // allowed
    m.myProtected = 0; // allowed
    m.myPackage   = 0; // allowed
    m.myPrivate   = 0; // inaccessible
  }
}
 

Protected access

Protected members are accessible within subclasses and within the containing package. Note that the meaning of protected in Java is different from other languages – such as C++ and C# – where protected members are only accessible from subclasses and the containing class.

package newpackage;
import mypackage.MyApp;
 
public class MyClass extends MyApp
{
  void test()
  {
    myPublic    = 0; // allowed
    myProtected = 0; // allowed
    myPackage   = 0; // inaccessible
    myPrivate   = 0; // inaccessible
  }
}
 

Public access

The public modifier gives unrestricted access from anywhere the member can be referenced.

package newpackage;
import mypackage.MyApp;
 
public class MyClass
{
  void test(MyApp m)
  {
    m.myPublic    = 0; // allowed
    m.myProtected = 0; // inaccessible
    m.myPackage   = 0; // inaccessible
    m.myPrivate   = 0; // inaccessible
  }
}
 

Top-level access

Members declared directly in the package – top-level members – may only choose between package-private and public access. For instance, a top-level class without an access modifier will default to package-private. Such a class will only be accessible within the containing package. On the other hand, a top-level class explicitly declared as public can be reached from other packages as well.

// Accessible only from containing package
class PackagePrivateClass {}
 
// Accessible from any package
public class PublicClass {}

Nested class access

Java allows classes to be defined within other classes, so called nested classes. Such a class can have any one of the four access levels. If a class is inaccessible, it cannot be instantiated or inherited.

public class MyClass
{
  // Only accessible within MyClass
  private class PrivateNestedClass {}
}
 

Access level guideline

As a guideline, when choosing an access level it is generally best to use the most restrictive level possible. This is because the more places a member can be accessed the more places it can be accessed incorrectly, which makes the code harder to debug. Using restrictive access levels will also make it easier to modify the class without breaking the code for any other programmers using that class.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.152.157