Java Access Modifiers

This post is about Access/Visibility modifiers in Java and how to use them.

Access Modifiers

In Java, access modifiers are used to set the accessibility (visibility) of classes, constructors, data members and methods in another classes. 

There are four types of access modifiers available in java:

  1. Default : visible only within the package.
  2. Private: visible within the class only.
  3. Protected: visible within the package or all subclasses (any package).
  4. Public: visible everywhere in entire project.

Let’s understand each modifier in detail with simple example.

Default Access Modifier

When no access modifier is specified, its the default access modifier by default.

The data members, class or methods which are not declared using any access modifiers i.e. having default access modifier are accessible only within the same package.

Example

In this example we have two classes, TestC class is trying to access the default members of TestA class, since class TestC belongs to a different package, this program would throw compilation error, because the scope of default modifier is limited to the same package in which it is declared.

TestA.java

package com.c1ctech.pkg1;

//public class
public class TestA {

    //default data member
    int a;

    //default method
    void defaultMethod() {
        System.out.println("defaultMethod of class TestA");
    }
}

TestC.java

package com.c1ctech.pkg2;

import com.c1ctech.pkg1.TestA;

//default class
class TestC {

    public static void main(String[] args) {

        //error: cannot access default members outside the package
        new TestA().a = 10;
        new TestA().defaultMethod();

    }
}

Private Access Modifier

The scope of private modifier is limited to the class only.

  • Private Data members and methods are only accessible within the class.
  • Class and Interface cannot be declared as private.
Example

This example throws compilation error because we are trying to access the private data member and method of class TestA in class TestB. The private data member and method are only accessible within the class.

TestA.java

package com.c1ctech.pkg1;

//public class
public class TestA {

    //private data member
    private int a;

    //private method
    private void privateMethod() {
        System.out.println("privateMethod of class TestA");
    }

    void defaultMethod() {
        System.out.println("defaultMethod of class TestA");
        //access private method within class
        privateMethod();
    }
}

TestB.java

package com.c1ctech.pkg1;

class TestB {

    public static void main(String[] args) {

        //error: cannot access private members outside the class TestA
        new TestA().a = 10;
        new TestA().privateMethod();
    }

}

Protected Access Modifier

Protected data member and method are only accessible by the classes of the same package and the subclasses present in any package.

Protected access modifier is similar to default access modifier with one exception that it has visibility in sub classes.

Classes cannot be declared as protected

Example

In this example the class TestC which is present in another package is able to access the protected members of class TestA. This is because the TestC class extends class TestA and the protected modifier allows the access of protected members in subclasses (in any packages).

TestA.java

package com.c1ctech.pkg1;

//public class
public class TestA {

    //protected data member
    protected int a;

    //protected method
    protected void protectedMethod() {
        System.out.println("protectedMethod of class TestA");
    }
}

TestC.java

package com.c1ctech.pkg2;

import com.c1ctech.pkg1.TestA;

//default class
class TestC extends TestA {

    public static void main(String[] args) {

        // compile time error
        //new TestA().a = 10;
        //new TestA().protectedMethod();

        // works, accessing super class protected members using subclass
        new TestC().a = 10;
        new TestC().protectedMethod();
    }
}

Public Access Modifier

The data members, methods and classes that are declared public can be accessed from anywhere. This modifier doesn’t put any restriction on the access.

Example

In this example we have two classes, TestC class is trying to access the public members of TestA class (belongs to different package ) and it works as public members are accessible everywhere.

TestA.java

package com.c1ctech.pkg1;

//public class
public class TestA {

    //public data member
    public int a;

    //public method
    public void publicMethod() {
        System.out.println("publicMethod of class TestA");
    }
}

TestC.java

package com.c1ctech.pkg2;

import com.c1ctech.pkg1.TestA;

//default class
class TestC {

    public static void main(String[] args) {

        //works, public members are accessible everywhere.
        new TestA().a = 10;
        new TestA().publicMethod();
    }
}

Leave a Reply