Access Modifiers in Java

The access modifiers in java define accessibility (scope) of variable, method, constructor or class.

There are 4 types of access modifiers in java.

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

You don’t have to explicitly put the default modifier.JVM will use default modifier if you do not provide Public, Private and Protected.

Public access modifier

Public modifier is accessible in the whole java world. If you put class as the public that means that class is available everywhere.

Let’s see with help of example:

Create a class named A.java in package com.shishirkant.

package com.shishirkant;
 
public class A {

  public int a;

  public A(int a)
  {
   this.a=a;
  }
 public void methodA()
 {
  System.out.println("In method of class A");
 }
}

Create another class named “B.java” in package com.shishirkant.

package com.shishirkant;
 
import com.A;
 
public class B {
 
public static void main(String args[])
{
A objA=new A(20);
objA.methodA();
System.out.println("Value of variable a is: "+objA.a);
}
}

When you run above program, you will get below output:

In method of class A
Value of variable a is: 20

Private access modifier

The private access modifier is accessible only within class.
You can not use private and protected with class unless and until it is nested class.
Let’s understand it with the help of example:

package com.shishirkant;
 
public class A {

private int a;

A(int a)
{
this.a=a;
}
private void methodA()
{
System.out.println("In method of class A");
}
}
class B {
 
public static void main(String args[])
{
A objA=new A(20);
objA.methodA();
System.out.println("Value of variable a is: "+objA.a);
}
}

You will get compilation error at line no.22 and 23, as you can not access private variables or methods from outside the class.

Default access modifier

If you do not provide any access, JVM considers it as default access. In the case of default access modifier, you can not access method, variable or class outside of the package.

Create a class named A.java in package com.shishirkant.

package com.shishirkant;
 
public class A {

int a;

public A(int a)
{
this.a=a;
}
void methodA()
{
System.out.println("In method of class A");
}
}

Create another class named “B.java” in package com.shishirkant.

package com.shishirkant;
 
import com.A;
 
public class B {
 
public static void main(String args[])
{
A objA=new A(20);
objA.methodA();
System.out.println("Value of variable a is: "+objA.a);
}
}

Here you will get compilation error at line no.11 and 12,as we are trying accesss variable a and methodA of class A outside the package com

Protected access modifier

Protected access modifiers can be accessed within the same package or outside the package by inheritance only.
Let’s understand with the help of example:

Create a class named A.java in package com.shishirkant

package com.shishirkant;
 
public class A {
 
protected int a;
 
protected A(int a)
{
this.a=a;
}
protected void methodA()
{
System.out.println("In method of class A");
}
}

Create another class named “B.java” in package com.shishirkant

package com.shishirkant;
 
import com.A;
 
public class B extends A{
 
B(int a) {
super(a);
}
 
public static void main(String args[])
{
B ObjB=new B(20);
ObjB.methodA();
System.out.println("Value of variable a is: "+ObjB.a);
}
}

When you run above program, you will get below output:

In method of class A
Value of variable a is: 20

As you can see, we are able to access class A’s variable a and methodA using inheritance.

Follow Us On