Access and Non-Access Modifiers in java

Modifiers are keywords that you add to those definitions to change their meanings.
There are two types of modifiers in java: access modifiers and non-access modifiers. The access modifiers in java specifies accessibility (scope) of a data member, method, constructor or class.
There are 4 types of java access modifiers:
 1. private
 2. default
 3. protected
 4. public

1.  private:

private modifiers access only within class. If we try access private modifiers outside of class ,it produce compilation error. We There is two class user and test.User class have private variable and methods. If we try access private data out side of class then it gives error.
packagecom.javamodifier;
classUser {
     privateString name=“java”;
      privatevoiddisplay(){
          System.out.println(” name of user is =”+ name);
    }
 }
publicclassTest{
publicstaticvoidmain(String[] args) {
            User user = newUser();
            System.out.println( user.name); //compilation error   
            user.display()////compilation error
        }
 }
How we can access private data outside class?
For this see public modifiers . Public modifies explain below in this post.

2 Default :

 Default has scope only inside the same package. If we try access default modifiers outside of class , it gives error.
There is two classes Demo and User in different packages.
packagecom.javamodifier;
importcom.javatest.User;
publicclassDemo {
publicstaticvoidmain(String[] args) {
           User user = newUser();
           System.out.println(user.name);// CE
     }
}
packagecom.javatest;
publicclassUser {
         String name;
         publicvoiddisplay(){
         System.out.println(“name of user =”+name);
    }
}

3. protected :

Protected has scope within the package and all sub classes. In this Example We access Student’s protected method by inheritance.
packagecom.javamodifier;
importcom.javatest.Student;
publicclassDemo extendsStudent{
publicstaticvoidmain(String[] args) {
           Student student = newStudent();
           student.print();
     }
}
packagecom.javatest;
publicclassStudent {
          protectedvoidprint(){
               System.out.println(” Student Called”);
         }
}
Output:
Studnet Called

4. Public:

Public scope is visible everywhere. In previous examples we can access print method of student class without inherintance ,if it declare as public.
packagecom.javamodifier;
importcom.javatest.Student;
publicclassDemo {
        publicstaticvoidmain(String[] args) {
            Student student = newStudent();
              student.print();
        }
}
packagecom.javatest;
publicclassStudent {
      publicvoidprint(){
           System.out.println(” Student Called”);
       }
}
How to Access Private Modifiers Outside Class:
We can access private modifiers outside class using public method of this class(withoout using inherintance). Below we give example. Emp class have private data mamber empId and private method diplaySalary. These private modifiers we access throw public accessEmp() method.
packagecom.javamodifier;
publicclassMainTest {
publicstaticvoidmain(String[] args) {
Emp emp = newEmp();
emp.AccessEmp();
}
}
classEmp{
privateintempId=10;
privatevoiddisplaySalary(){
System.out.println(“salary is = 10000”);
}
publicvoidAccessEmp(){
System.out.println(“Emp id is =”+empId);
displaySalary();
}
}
Output:
Emp id is =10
salary is = 10000
 
 

Non Access Modifiers:

 
There is some non-access modifiers of java.
 
The static modifier for creating class methods and variables
 
The final modifier for finalizing the implementations of classes, methods, and variables.
 
The abstract modifier for creating abstract classes and methods.
 
The synchronized and volatile modifiers, which are used for threads.
 
 
 
 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

41 − 37 =