Java Access Modifiers

    Access Modifiers

    In Java, access modifiers specify the scope or the accessibility of the class, method, fields, or any data member. You can use the modifiers in front of the data members to define their scope. Java supports four types of access modifiers.

    • Private- the scope limits within the class. You cannot access the private data of members from outside the class.
    • Default- this scope of the default limits only within the package. You cannot access the default modifier outside of the package. If there is no modifier defined, then it will be considered as default.
    • Protected- the scope of the protected modifier will limit to the package and you can use it outside the package using the child class. Without the child class, you cannot access the protected modifier.
    • Public- the scope of the public is not limited. You can access public modifiers anywhere within any class, package, sub-package, etc.

    Modifiers scope table-

    Access Modifier

    within class

    within package

    outside package by subclass only

    outside package

    Private

    Y

    N

    N

    N

    Default

    Y

    Y

    N

    N

    Protected

    Y

    Y

    Y

    N

    Public

    Y

    Y

    Y

    Y

    Private Example-

    class demo{  
    
    private int data=40;  
    
    private void msg(){System.out.println("Private");}  
    
    }  
    
      
    
    public class Sample{  
    
     public static void main(String args[]){  
    
      demo obj=new demo();  
    
       System.out.println(obj.data);
    
       obj.msg(); 
    
       }  
    
    } 

    In the above example, you cannot access the private variable and the method outside the class demo.

    Output-

    With private constructor-

    class demo{  
    
    private demo()
    
    private int data=40;  
    
    private void msg(){System.out.println("Private");}  
    
    }  
    
      
    
    public class Sample{  
    
     public static void main(String args[]){  
    
      demo obj=new demo();  
    
       System.out.println(obj.data);
    
       obj.msg(); 
    
       }  
    
    } 

    You will again get the compile-time error for accessing the private constructor.

    Default Example-

    Pack.java

    
    package demo_pack;  
    
    public class Pack{  
    
    void m1(){
    
       System.out.println("First Package");  }
    
     public static void main(String args[]){  
    
    
    
    Pack p=new Pack();
    
    p.m1();
    
       }  
    
    }  
    

    Simple.java

    package Simple_pack;
    
    import demo_pack.*;
    
    public class Simple {
    
    public static void main(String args[]){  
    
       demo_pack.Pack d= new demo_pack.Pack();
    
    d.m1();  
    
      }  
    
    } 

    You will get a compile-time error while accessing the method from the Pack class. As we dint mention, the modifier for the method m1 is considered to be the default, and the Simple class cannot access it.

    Protected Example-

    Pack.java

    package demo_pack;  
    
    public class Pack{  
    
    protected void m1(){
    
       System.out.println("First Package");  }
    
     public static void main(String args[]){  
    
    
    
    Pack p=new Pack();
    
    p.m1();
    
       }  
    
    }  

    Simple.java

    package Simple_pack;
    
    import demo_pack.*;
    
    public class Simple extends Pack{
    
    public static void main(String args[]){  
    
       Simple d=new Simple();
    
    d.m1();  
    
    
    
      }  
    
    } 


    Output-

    Public Example-

    Pack.java-

    package demo_pack;  
    
    public class Pack{  
    
    public void m1(){
    
       System.out.println("First Package");  }
    
     public static void main(String args[]){  
    
    
    
    Pack p=new Pack();
    
    p.m1();
    
       }  
    
    }  

    Simple.java

    package Simple_pack;
    
    
    
    import demo_pack.*;
    
    class Simple {
    
    public static void main(String args[]){  
    
       Pack d= new Pack();
    
    d.m1();  
    
      }  
    
    } 

    Output-

    Run the commands in the same order as below to compile and run the files. First, compile the class, then compile the package. You can access the class of other packages using the package name only. (simple_pack.simple)- package_name.class_name.