top of page

Access modifiers

  • Writer: Keshari Abeysinghe
    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:

  1. Private

  2. Default

  3. Protected

  4. 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

Hello

Public

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!

Recent Posts

See All
Inheritance

Inheritance is a mechanism in which one class acquires the property of another class.For example, a child inherits the traits of his/her...

 
 
 
Aggregation in java

In Java, aggregation represents HAS-A relationship, which means when a class contains reference of another class known to have...

 
 
 
Polymorphism

Polymorphism in Java occurs when there are one or more classes or objects related to each other by inheritance. In other words, it is the...

 
 
 

Comments


Subscribe Form

Thanks for submitting!

©2020 by Quick Code. Proudly created with Wix.com

bottom of page