Access Control

In this chapter you will learn:

  1. What is Java access control level
  2. What is default access control level
  3. How does the access control act on the inheritance
  4. What is the access matrix for Java
  5. What are the targets for different access modifiers

Access control level

Java's access specifiers are public, private, protected and a default access level.

A public class member can be accessed by any other code. A private class member can only be accessed within its class.

Default (without an access modifier)

A class's fields, methods and the class itself may be default. A class's default features are accessible to any class in the same package.

A default method may be overridden by any subclass that is in the same package as the superclass.

protected features are more accessible than default features. Only variables and methods may be declared protected.

A protected feature of a class is available to all classes in the same package(like a default). A protected feature of a class can be available to its subclasses.

Here is an example for a public member variable

public int i;

The following code defines a private member variable and a private member method:

private double j; 
private int myMethod(int a, char b) { // ...

To understand the effects of public and private access, consider the following program:

class Test {/*from j  a  va 2 s.com*/
  int a;        // default access
  public int b; // public access
  private int c; // private access
  // methods to access c
  void setc(int i) {
    c = i;
  }
  int getc() {
    return c;
  }
}
public class Main {
  public static void main(String args[]) {
    Test ob = new Test();
    ob.a = 1;
    ob.b = 2;
    // This is not OK and will cause an error
    // ob.c = 100; // Error!
    // You must access c through its methods
    ob.setc(100); // OK
    System.out.println("a, b, and c: " + ob.a + 
         " " + ob.b + " " + ob.getc());
  }
}

The output:

Member Access and Inheritance

A subclass cannot access the private members of the superclass. For example, consider the following simple class hierarchy. If you try to compile the following program, you will get the error message.

class A {/*  j a va 2  s. c  o  m*/
  private int j; // private to A
}
class B extends A {
  int total;

  void sum() {
    total = j; // ERROR, j is not accessible here
  }
}

The output:

Access matrix for Java

The following table shows the access matrix for Java. Yes means accessible, no means not accessible.

Position PrivateNo modifierProtectedPublic
Same class Yes Yes Yes Yes
Same package subclass No Yes Yes Yes
Same package non-subclass No Yes Yes Yes
Different package subclass No No Yes Yes
Different package non-subclassNo No No Yes

Access Modifiers and their targets

Not all modifiers can be applied to all features. Top-level classes may not be protected. Methods may not be transient. Static can apply it to free-floating blocks of code.

The following table shows all possible combinations of features and modifiers. yes means we can use that modifier to control the access for the corresponding entities.

Modifier ClassVariableMethodConstructorCode Block
public yes yes yes yes no
protected no yes yes yes no
empty accessoryes yes yes yes yes
private no yes yes yes no
final yes yes yes no no
abstract yes no yes no no
static no yes yes no yes
native no no yes no no
transient no yes no no no
volatile no yes no no no
synchronized no no yes no yes

Next chapter...

What you will learn in the next chapter:

  1. What is a Java Interface
  2. How to define an interface
  3. How to implement an interface