Access Control in Java

Category: Java   Tags: Java, Java Basic, Java Access Control

Access control specify what parts of a program can access the members of a class and so prevent misuse. Java’s access modifiers are public, private, and protected. Java also defines a default access level.

The following table summarize it all:

Modifier Class SubclassPackageWorld
private Y N N N
protected Y Y Y N
default Y N Y N
public Y Y Y Y

Access Control: private

A private member is accessible from within class only. You can't access a private member from out side of the its class.

Access Control: public

A public member is accessible from anywhere.

Here is a simple Java program showing both access modifier:

AccessControlDemo.java

                          class TestAccess {
                              private int privateVariable = 10;
                              public int publicVariable;

                              private void privateMethod() {
                                  System.out.println("I am private to this class only");
                              }

                              public void publicMethod() {
                                  System.out.println("Can't reach me from outside");
                              }

                              public void accessPrivateMember() {
                                  System.out.println("privateVariable inside privateMethod = " + privateVariable);
                                  privateMethod();
                              }
                          }

                          public class AccessControlDemo {

                              public static void main(String s[]) {
                                  TestAccess ob = new TestAccess();
                                  ob.publicVariable = 100;
                                  System.out.println(ob.publicVariable);
                                  ob.publicMethod();

                                  /*
                                  This will not even compile as privateVariable is declared
                                  private inside TestAccess class
                                  */

                                  // ob.privateVariable = 200;

                                  /*
                                  This will not even compile as privateMethod is declared
                                  private inside TestAccess class
                                  */

                                  // ob.privateMethod();

                                  ob.accessPrivateMember();
                              }
                          }
                        

Lets see what is happening here:

                          ob.publicVariable = 100;
                        

This line of code will modify and assign 100 to publicVariable as it is declared public inside class TestAccess. Therefore the new value of publicVariable is 100.

                          ob.publicMethod();
                        

This is perfectly valid as publicMethod method is defined public.

                          // ob.privateVariable = 200;
                        

If you uncomment this line, program will not even compile. privateVariable is declared private inside TestAccess and therefore it is only accessible from inside TestAccess. Uncommenting this line may show you compile time error: privateVariable has private access in TestAccess.

                          // ob.privateMethod();
                        

Uncommenting this line will not allow compiling the code as privateMethod is declared a private method inside TestAccess. Uncommenting this line may show you compile time error: privateMethod() has private access in TestAccess.

                          public void accessPrivateMember() {
                              System.out.println("privateVariable inside privateMethod = " + privateVariable);
                              privateMethod();
                          }
                        

This is a valid piece of code as both privateVariable and privateMethod() are being used inside TestAccess class only.

Running this Java program will have this output:

                          100
                          Can't reach me from outside
                          privateVariable inside privateMethod = 10
                          I am private to this class only
                        

Access Control: protected

The protected modifier specifies that the member can only be accessed within its own package and, in addition, by a subclass of its class in another package. We will explain this with two example:

ProtectedAccessExample1.java

                          package com.tutorial.javabasic;

                          class Parent {
                              protected void parentClassMethod() {
                                  System.out.println("This is parent classs method");
                              }
                          }

                          class Child extends Parent {
                              public void childClassMethod() {
                                  parentClassMethod();
                                  System.out.println("This is child classs method");
                              }
                          }

                          public class ProtectedAccessExample1 {

                              public static void main(String s[]) {
                                  Child child = new Child();
                                  child.childClassMethod();

                                  System.out.println("Calling a protected method directly");
                                  Parent parent = new Parent();
                                  parent.parentClassMethod();
                              }
                          }
                        

Here, everything is inside same package com.tutorial.javabasic.

  • parentClassMethod method is defined as protected and is accessible from child class Child.
  • As Parent is in the same package, its protected methods are accessible from other class also. For example, parentClassMethod method can be used inside ProtectedAccessExample1 class also.

Output:

                          This is parent classs method
                          This is child classs method
                          Calling a protected method directly
                          This is parent classs method
                        

Now here we will discuss other example to show how protected work when used in same or different package.

ProtectedAccessExample2.java

                          package com.example.packageexample1;

                          // Class A

                          public class A {
                              protected void a() {
                                  System.out.println("I am protected inside A.");
                              }
                          }


                          // Class B

                          package com.example.packageexample2;
                          import com.example.packageexample1.A;

                          public class B extends A {
                              public void b() {
                                  a();
                              }
                          }


                          // Class C

                          package com.example.packageexample2;
                          import com.example.packageexample1.A;

                          public class C {
                              public C() {
                                  A ob = new A();
                                  /*
                                   This will not work!. As "a() has protected access in A" and C is
                                   defined in different package.
                                  */
                                  // ob.a();
                              }
                          }


                          // Main class

                          package com.tutorial.javabasic;
                          import com.example.packageexample2.B;

                          public class ProtectedAccessExample2 {

                              public static void main(String s[]) {
                                 B ob = new B();
                                 ob.b();
                              }
                          }
                        

Output:

                          I am protected inside A.
                        

  • B is subclass of A therefore, protected method a is accessible from B
  • Class C and A are in different package and also C is not a subclass of A. Therefore, protected method a can't be called from class C.

Access Control: default

If you don't use any modifier, it is treated as default modifier. The default modifier is accessible only within package.

DefaultAccessExample.java

package com.example.packageexample;


// Class A

public class A {
    void a() {
        System.out.println("Inside method a()");
    }
}


// Class B

package com.tutorial.javabasic;

import com.example.packageexample.A;

class B {
    void b() {
        System.out.println("Inside method b()");
    }
}


public class DefaultAccessExample {

    public static void main(String s[]) {
       A a = new A();

       /*
         Won't work as class A is in different package and a() access modifier is default.
       */
//       a.a();

       B b = new B();
       b.b();
    }
}
                        

Output:

                          Inside method b()
                        

  • Class A and DefaultAccessExample are in different package, therefore a.a() will not work as method a() access modifier is default.
  • Class B and DefaultAccessExample are in the same package, therefore b.b() will work even though method b() access modifier is default.