Access Modifiers in Java
Access modifiers are used to specify the accessibility for variables, methods, class or constructor.
Imagine as you are living in a house and another family is living nearby your house.
Can the other family come into your house and take anything without your permissions? No. Right?
Access specifiers are similar to permissions which specifies the accessibility between classes, methods, constructors and variables.
This is also called as access specifiers since it specifies access level.
Different types of access specifiers are
- public
- protected
- private
- default
public
public keyword is used to specify the access level as public.
Example
public class Sun{}
public int number=5;
public void sunsets(){}
public Sun(){}
If we give a variable or a class or method as public, the corresponding variables or class or method can be accessed from anywhere in any class.
Example program
class Sun{
public Sun(){
System.out.println("Sun object is created");
}
public void sunrise(){
System.out.println("Sun Rises");
}
public void sunsets(){
System.out.println("Sun sets");
}
}
public class PublicTest{
public static void main(String args[]){
Sun s1=new Sun();
s1.sunrise();
s1.sunsets();
}
}
Try In CompilerOutput
Sun object is created
Sun Rises
Sun sets
public specified can be accessed
- Within class and outside class
- Within package and outside package
protected
protected keyword is used to specify the access level as protected.
Example
protected int number=5;
protected void teaching(){}
protected Teacher(){
If we give a variable or a class or method as protected, the corresponding variables or constructor or method can be accessed from any class within its package, but not outside the package.
Example program
package 1
package oldschool;
public class Teacher{
protected Teacher(){
System.out.println("Teacher teach us lesson");
}
}
package 2
package newschool;
import oldschool.Teacher;
public class Student{
public static void main(String args[]){
Teacher t1=new Teacher();
}
}
Output
error: Teacher() has protected access in Teacher
protected specified can be accessed
- Within class and outside class within its package
protected specified cannot be accessed
- outside its package
private
private keyword is used to specify the access level as private.
Example
private int number=5;
private void sleep(){}
private Doctor(){}
If we give a variable or a method or a constructor as private, the corresponding variables or constructor or method can be accessed only within its class, cannot be accessed outside class.
Example program
class Doctor{
private Doctor(){
System.out.println("Doctor is free");
}
}
public class PrivateTest{
public static void main(String args[]){
Doctor d1=new Doctor();
}
}
Try In CompilerOutput
error: Doctor() has private access in Doctor
private specified can be accessed
- only within class
private specified cannot be accessed
- outside its class
- outside its package
default
If the access modifier is not specified in any case, it is considered as default access modifier.
It is similar to protected in which it allows access only within package and not outside package.
Example program
class Taxi{
void available(){
System.out.println("Taxi is available");
}
void booked(){
System.out.println("Taxi is booked");
}
}
public class InnerCity{
public static void main(String args[]){
Taxi t=new Taxi();
t.available();
}
}
Try In CompilerOutput
Taxi is available