Categories: Java

Access Modifiers in Java

Modifiers in Java are divided into two categories: access modifiers and non-access modifiers.

The accessibility or scope of a field, method, constructor, or class is defined by the access modifiers in Java. The access modifier can be used to change the access level of fields, constructors, methods, and classes.

Types of access modifiers:

There are four types of Java access modifiers:

  • Private: A private modifier has only one level of access: within the class. It is not accessible outside the class.
  • Default: A default modifier’s access level is limited to the package. It is not accessible from the outside of the package. If you don’t specify an access level, the default will be used.
  • Protected: A protected modifier’s access level is both within and outside the package via a child class. The child class cannot be accessed from outside the package unless it is created.
  • Public: A public modifier’s access level is universal. It can be accessed from inside and outside the class, as well as from within and outside the package.
Access Modifierwithin classwithin packageoutside package by subclass onlyoutside package
PrivateYesNoNoNo
DefaultYesYesNoNo
ProtectedYesYesYesNo
PublicYesYesYesYes
1) Private

The private access modifier is accessible only within the class.

Example:

class A{  
private int data=40;  
private void msg(){System.out.println("Hello java");}  
}  
  
public class Simple{  
 public static void main(String args[]){  
   A obj=new A();  
   System.out.println(obj.data);//Compile Time Error  
   obj.msg();//Compile Time Error  
   }  
}  
2) Default

If no modifier is specified, it is assumed to be the default. The default modifier is only available within the package. It is not accessible from the outside of the package. It allows for more access than private. However, it is more restricted than protected and open.

Example:

//save by A.java  
package pack;  
class A{  
  void msg(){System.out.println("Hello");}  
}  
//save by B.java  
package mypack;  
import pack.*;  
class B{  
  public static void main(String args[]){  
   A obj = new A();//Compile Time Error  
   obj.msg();//Compile Time Error  
  }  
}  
3) Protected

The protected access modifier can be used both inside and outside of the package, but only through inheritance.

The data member, method, and constructor can all have the protected access modifier applied to them. It is not applicable to the class.

It is more accessible than the standard modifier.

Example:

//save by A.java  
package pack;  
public class A{  
protected void msg(){System.out.println("Hello");}  
}  
//save by B.java  
package mypack;  
import pack.*;  
  
class B extends A{  
  public static void main(String args[]){  
   B obj = new B();  
   obj.msg();  
  }  
}  
4) Public

The public access modifier can be used anywhere. It is the most versatile of all the modifiers.

Example:

//save by A.java  
  
package pack;  
public class A{  
public void msg(){System.out.println("Hello");}  
}  
//save by B.java  
  
package mypack;  
import pack.*;  
  
class B{  
  public static void main(String args[]){  
   A obj = new A();  
   obj.msg();  
  }  
}  

Note: also read about the Constructor in Java

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Find Intersection of Two Singly Linked Lists

You are given two singly linked lists that intersect at some node. Your task is…

1 month ago

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

1 month ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

2 months ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

2 months ago

Select a Random Element from a Stream

You are given a stream of elements that is too large to fit into memory.…

2 months ago

Estimate π Using Monte Carlo Method

The formula for the area of a circle is given by πr². Use the Monte…

3 months ago