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

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

3 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

2 years ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

2 years ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

2 years ago