Java access modifiers determine whether a particular class can access the data members/methods or not. The access modifiers specify the scope of a class, variable or a method.

Types of Modifiers in Java:

In Java, there are 4 types of access modifiers namely:

  1. Public
  2. Default
  3. Private
  4. Protected

Public Access Modifier

When we declare a class or a method as public, it is accessible from any class or package in the Java program. The public class/method is accessible within the package as well as outside the package. In general, public access modifier does not restrict the entity(class/method) at all.

Example :

public class Sample {
    public static void main(String[] args) {
        System.out.println("A : " + new A().valueA());
    }
}
class A {
    private int a = 10;
    public int valueA() {
        a += 10;
        return a;
    }
}

Output :

A : 20

Class Level Visibility

If we declare a member as public, then we can access that member from anywhere from the application but the corresponding class should be visible. Hence we have to check the class visibility before checking the member visibility.

Example:

package pack1;
class A {
    public void m1() {
        System.out.println("Method 1");
    }
}

package pack2;
import pack1.A;
class B {
    public static void main(String args[]) {
        A a = new A();
        a.m1();    // Compilation Error
    }
}

In the above example, m1() method is public, but we can’t access m1() from outside the pack1 because the corresponding class A is not public. If we declare both the classes as public we can access the method m1().

Default Access Modifier:

If we declare a member as default, then we can access that member only within the current package and we cannot access it from outside the package. Hence default access is also known as package-level access. If we didn’t mention any access for a class, method or variable, by default the access modifier is default.

Example:

package access;
public class Sample {
    int a = 10;
    public static void main(String[] args) {
        System.out.println("A : " + new A().valueA());
    }
}

package access;
class A {
    int a = 10;
    int valueA() {
        a += 10;
        return a;
    }
}

package access2;
public class Sample {
    public static void main(String[] args) {
        access.Sample sample = new access.Sample();
        sample.valueA();    // Compilation Error
    }
}

In the above example Line number 39 generates Compilation Error, because we can’t access the default methods from outside the package.

Private Access Modifier

If a member is declared as privatethen we can access that member only within the current class. If you make any class constructor as privateyou cannot create its instance from outside the class.

public class Sample {
    int a = 10;
    public static void main(String[] args) {
        System.out.println("Value : " + new A().valueA());
        System.out.println("A : " + new A().a);   // Compilation Error
    }
}

class A {
    private int a = 10;
    int valueA() {
        a += 10;
        return a;
    }
}

Note:
Abstract methods should be visible in the child classes to provide the implementation for the declaration whereas private methods are not visible in child class. Hence private-abstract combination is illegal for methods.

Protected Access Modifier

Protected is the most misunderstood modifier in Java. If a member is declared as protected, then we can access that member within the current package, anywhere but outside the package only in case of child classes.
Within the current package, we can access the protected members either by parent reference or by child reference.
But from outside the package, we can access the protected members only by using the child reference. If we are trying to use parent reference we will get a Compilation Error.

Example:

package pack1;
public class A {
    protected void m() {
        System.out.println("Protected method");
    }
}

public class B extends A {
    public static void main(String args[]) {
        A a = new A();
        a.m();
        B b = new B();
        b.m();
        A a = new B();
        a.m();
    }
}

public pack2;
import pack1.A;
public class C extends A {
    public static void main(string args[]) {
        A a = new A();
        a.m();    // Compilation Error
        C c = new C();
        c.m();
        A a = new C();
        a.m();    // Compilation Error.
    }
}
Most restricted modifier – Private.
Most accessible modifier – Public.
Recommended modifier for variables – Private.
Recommended modifier for methods – Public.

References:

Happy Learning 🙂

Leave A Comment