Access Modifiers In Java defines the scope of data member,variable,method class or constructor specifing security, accessibility of those elements based on access modifiers used. Four types of Access Modifiers in Java are Private,Default,Protected and Public

Private: Access of a private modifier is only within the class and cannot be accessed from outside the class.

Default: Access of a default modifier is only within the package and cannot be accessed from outside the package. If no access modifier is speficy then it will be default

Protected: Access of a protected modifier is within the package and outside the package through child class.

Public: Public access modifier can be accessed everywhere. It can be accessed from within and outside the class, within and outside the package.

1) Private

Access of a private modifier is only within the class and cannot be accessed from outside the class.

In below example, we have created two classes Student and Semester. Stduent class contains private data member and private method. If we access these private members from outside the class,we will get a compile-time error.

class Student{  
private int rollNumber = 50;  
private void display(){System.out.println("Roll Number is 50");}  
}  
public class Semester{  
 public static void main(String args[]){  
   Student stud=new Student();  
   System.out.println(stud.rollNumber); //Compile Time Error  
   stud.display();//Compile Time Error  
   }  
}  

Role of Private Constructor

You cannot create the instance of class constructor which is private,from outside the class

class Student{  
private Student(){}//private constructor  
void display(){System.out.println("Roll Number is 50");}  
}  
public class Semester{  
 public static void main(String args[]){  
   Student obj=new Student();//Compile Time Error  
 }  
}  

Note: A class cannot be private or protected except nested class

2) Default

If no access modifier is speficy then it will be default.Default modifier is accessible only within the package and cannot be accessed from outside the package. It provides more accessibility than private but is more restrictive than protected, and public.

Example of default access modifier

//save by Studuent.java  
package com.student;  
class Student{  
  void display(){System.out.println("Roll Number is 50");}  // since no access modifier is defined it is default
}  
//save by Semester.java  
package com.student.semester;  
import com.student.*;  
class Semester{  
  public static void main(String args[]){  
   Student stud= new Student();//Compile Time Error  
   stud.display();//Compile Time Error  
  }  
}  

In the above example, the scope of class Student and its method display() is default so it cannot be accessed from outside the package.

3) Protected

Protected modifier is accessible within the package and outside the package through child class i.e inheritance only.The protected access modifier can be applied on the data member, method and constructor. It cannot be applied on the class.It provides more accessibility than the default modifer.

//save by Student.java  
package com.student;  
public class Student{  
protected void display(){System.out.println("Roll Number is 50");}  
}  
//save by Semester .java  
package com.semester;  
import com.student.*;  
  
class Semester extends Student{  
  public static void main(String args[]){  
   Semester sem = new Semester ();  
   sem.display();  
  }  
}  
Output:Roll Number is 50

4) Public

Public access modifier can be accessed everywhere. It can be accessed from within and outside the class, within and outside the package.

//save by Student.java  
  
package com.student;  
public class Student{  
public void display(){System.out.println("Roll Number is 50");}  
}  
//save by Semester.java  
  
package com.semester;  
import com.student.*;  
  
class Semester{  
  public static void main(String args[]){  
   Student stud = new Student();  
   stud.display();  
  }  
}  

Output: Roll Number is 50

Java Access Modifiers with Method Overriding

If you are overriding any method, overridden method (i.e. declared in subclass) must not be more restrictive.

class Student{  
protected void display(){System.out.println("Roll Number is 50");}  
}  
public class Semester extends Student{  
void display(){System.out.println("Roll Number is 50");}//Compile Time Error  
 public static void main(String args[]){  
   Semester sem=new Semester();  
   sem.display();  
   }  
}  

Since default modifier is more restrictive than protected in above example we get a compile-time error.

Scroll to Top