Access modifiers
- Keshari Abeysinghe

- Mar 19, 2020
- 2 min read
Updated: Nov 30, 2020
The access modifiers in Java specifies the accessibility or scope of a field, method, constructor, or class.There are four types of Java access modifiers:
Private
Default
Protected
Public
Private
The access level of a private modifier is only within the class.The member can only be accessed by other members within the same class.
class Girl{
private int age=40;
private void msg(){
System.out.println("Hello java");
}
public class Boy{
public static void main(String args[]){
Girl obj=new Girl();
System.out.println(obj.age);//Compile Time Error
obj.msg();//Compile Time Error
}
} If you make any class constructor private, you cannot create the instance of that class from outside the class.
A class can not be private or protect if it is not a nested in a class
Default
Whenever a specific access level is not specified, then it is assumed to be ‘default’. The scope of the default level is within the package. It provides more accessibility than private. But, it is more restrictive than protected, and public.
//save by Girl.java
package message;
class Girl{
void msg(){
System.out.println("Hello");
}
}
//save by Boy.java
package mymessage;
import message.*;
class Boy{
public static void main(String args[]) {
Girl obj = new Girl();//Compile Time Error
obj.msg();//Compile Time Error
}
} In the above example, the scope of class A and its method msg() is default so it cannot be accessed from outside the package.
Protected
The protected access level has a scope that is within the package. A protected entity is also accessible outside the package through inherited class or child class.The protected access modifier can be applied on the data member, method and constructor. It can't be applied on the class.It provides more accessibility than the default modifier. The protected access modifier is usually used in parent-child relationships.
//save by Girl.java
package message;
public class Girl{
protected void msg(){
System.out.println("Hello");}
}
//save by Boy.java
package mymessage;
import message.*;
class Boy extends Girl{
public static void main(String args[]){
Boy obj = new Boy();
obj.msg();
}
}Expected out put
HelloPublic
This is the most common access level and whenever the public access specifier is used with an entity, that particular entity is accessible throughout from within or outside the class, within or outside the package, etc. It has the widest scope among all other modifiers.
//save by Girl.java
package message;
public class Girl{
public void msg(){
System.out.println("Hello");
}
//save by Boy.java
package mymessage;
import message.*;
class B{
public static void main(String args[]){
A obj = new A();
obj.msg();
}} Modifiers limit the scope of data members like classes, constructors, methods, and variables and define the limit as to which classes or packages can access them. Access specifiers encourage encapsulation and re usability in Java.
Note that classes and interface cannot be protected or private.
Happy Coding!
Comments