how2examples.com
Home - Introduction to Java Programming

Access Modifiers

A feature is a general term which may refer to a class, member variable, method or constructor. Access modifiers are applied to features to specify which other classes can access them.

Contents of access_modifiers_example/a/A.java:
package access_modifiers_example.a;

public class A {
   public String publicMethod() {
      return "A.publicMethod()";
   }
   
   private String privateMethod() {
      return "A.privateMethod()";
   }
   
   protected String protectedMethod() {
      return "A.protectedMethod()";
   }
   
   String defaultMethod() {
      return "A.defaultMethod()";
   }
}

public Access Modifier

The least restrictive access modifier is public. A class, variable or method that has been marked as public will be accessible by any other class.

Example of calling a method marked as public.
Contents of access_modifiers_example\Example.java:
package access_modifiers_example;

import access_modifiers_example.a.A;

public class Example {
   public static void main(String[] args) {
      A a = new A();
      System.out.println(a.publicMethod());
   }
}
Command to compile access_modifiers_example\Example.java:
javac access_modifiers_example\Example.java
Command to run access_modifiers_example\Example:
java access_modifiers_example.Example
Output from running access_modifiers_example\Example:
A.publicMethod()

private Access Modifier

The most restrictive access modifier is private. A variable or method that has been marked as private will only be accessible by an instance of the class it is has been defined in.

Example of trying to call a private method on another instance.
Contents of access_modifiers_example\Example.java:
package access_modifiers_example;

import access_modifiers_example.a.A;

public class Example {
   public static void main(String[] args) {
      A a = new A();
      System.out.println(a.privateMethod());
   }
}
Command to compile access_modifiers_example\Example.java:
javac access_modifiers_example\Example.java
Result of trying to compile access_modifiers_example\Example.java:
access_modifiers_example\Example.java:8: privateMethod() has private access in access_modifiers_example.a.A
      System.out.println(a.privateMethod());
                          ^
1 error

protected Access Modifier

The protected access modifier is more restrictive than public but less restrictive than private. A feature (i.e. class, variable or method) that has been marked as protected will only be accessible by subclasses of the class or a class in the same package.

Example of trying to call a protected method on another instance of another package.
Contents of access_modifiers_example\Example.java:
package access_modifiers_example;

import access_modifiers_example.a.A;

public class Example {
   public static void main(String[] args) {
      A a = new A();
      System.out.println(a.protectedMethod());
   }
}
Command to compile access_modifiers_example\Example.java:
javac access_modifiers_example\Example.java
Result of trying to compile access_modifiers_example\Example.java:
access_modifiers_example\Example.java:8: protectedMethod() has protected access in access_modifiers_example.a.A
      System.out.println(a.protectedMethod());
                          ^
1 error
Example of trying to call a protected method on another instance in the same package.
Contents of access_modifiers_example\a\Example.java:
package access_modifiers_example.a;

public class Example {
   public static void main(String[] args) {
      A a = new A();
      System.out.println(a.protectedMethod());
   }
}
Command to compile access_modifiers_example\a\Example.java:
javac access_modifiers_example\a\Example.java
Command to run access_modifiers_example\a\Example:
java access_modifiers_example.a.Example
Output from running access_modifiers_example\a\Example:
A.protectedMethod()
Example of how a class can call a protected method of a class it extends - even if the parent class is in another package.
Contents of access_modifiers_example\Example.java:
package access_modifiers_example;

import access_modifiers_example.a.A;

public class Example {
   public static void main(String[] args) {
      B b = new B();
      System.out.println(b.subclassMethod());
   }
}

class B extends A {
   public String subclassMethod() {
      return protectedMethod();
   }
}
Command to compile access_modifiers_example\Example.java:
javac access_modifiers_example\Example.java
Command to run access_modifiers_example\Example:
java access_modifiers_example.Example
Output from running access_modifiers_example\Example:
A.protectedMethod()
Example of attempting to call a protected method of an object's parent class defined in a different package.
Contents of access_modifiers_example\Example.java:
package access_modifiers_example;

import access_modifiers_example.a.A;

public class Example {
   public static void main(String[] args) {
      B b = new B();
      System.out.println(b.protectedMethod());
   }
}

class B extends A {
   public String subclassMethod() {
      return protectedMethod();
   }
}
Command to compile access_modifiers_example\Example.java:
javac access_modifiers_example\Example.java
Result of trying to compile access_modifiers_example\Example.java:
access_modifiers_example\Example.java:8: protectedMethod() has protected access in access_modifiers_example.a.A
      System.out.println(b.protectedMethod());
                          ^
1 error

Default Access Modifier

If you don't explicitly specify an access modifier for a class, variable or method then it will default to only being accessible from classes in the same package as itself.

Example of attempting to call a "default" access method on an object of a different package.
Contents of access_modifiers_example\Example.java:
package access_modifiers_example;

import access_modifiers_example.a.A;

public class Example {
   public static void main(String[] args) {
      A a = new A();
      System.out.println(a.defaultMethod());
   }
}
Command to compile access_modifiers_example\Example.java:
javac access_modifiers_example\Example.java
Result of trying to compile access_modifiers_example\Example.java:
access_modifiers_example\Example.java:8: defaultMethod() is not public in access_modifiers_example.a.A; cannot be accessed from outside package
      System.out.println(a.defaultMethod());
                          ^
1 error
Example of calling a "default" access method on an object in the same package.
Contents of access_modifiers_example\a\Example.java:
package access_modifiers_example.a;

public class Example {
   public static void main(String[] args) {
      A a = new A();
      System.out.println(a.defaultMethod());
   }
}
Command to compile access_modifiers_example\a\Example.java:
javac access_modifiers_example\a\Example.java
Command to run access_modifiers_example\a\Example:
java access_modifiers_example.a.Example
Output from running access_modifiers_example\a\Example:
A.defaultMethod()
Example of attempting to call a "default" access method of an object's parent class defined in a different package.
Contents of access_modifiers_example\Example.java:
package access_modifiers_example;

import access_modifiers_example.a.A;

public class Example {
   public static void main(String[] args) {
      B b = new B();
      System.out.println(b.subclassMethod());
   }
}

class B extends A {
   public String subclassMethod() {
      return super.defaultMethod();
   }
}
Command to compile access_modifiers_example\Example.java:
javac access_modifiers_example\Example.java
Result of trying to compile access_modifiers_example\Example.java:
access_modifiers_example\Example.java:14: defaultMethod() is not public in access_modifiers_example.a.A; cannot be accessed from outside package
      return super.defaultMethod();
                  ^
1 error