Access Modifier in Java
An access modifier changes the visibility of classes, constructors, methods or variables. The access modifier is used to restrict the scope or accessibility of classes, constructors, methods or variables.
An access modifier can never be used inside methods and constructors. When using an access modifier, it must always be used as the first keyword in the declaration.
There are four types of access modifiers available for use in java:
- default
- private
- protected
- public
The default access modifier
The default access modifier is accessible only within the same package. It is not accessible from outside the package. The default access modifier is declared by not using any access modifier at all. If you don't use any modifier on a method, field, constructor or class, it is treated as default by default.
package com.example.student; public class Student { /*Instance variables with default access modifier */ String name; String email; /* A method with default access modifier*/ void read() { System.out.println("Student is reading!"); } }
Here is an example of a School class which is in a different package than the Student class. This example will give compile time error because the read() method of the student object has default access modifier which is not accessible in another package.
package com.example.school; //Importing Student class because it is in a different package import com.example.student.Student; public class School { //Main method public static void main(String[] args){ //Creating object of Student class. Student student = new Student(); /* *Calling read() method on student object. This will * give a compile-time error. */ student.read(); } }
The private access modifier
The private access modifier is used to restrict access of a variable, class, method, and object from outside the class in which it is declared. Such private variables, methods, and constructors are accessible only within the same class where they are declared and are not accessible in its subclasses and from any external class:
package com.example.actor; public class Actor { /*Instance variables with private access modifier*/ private String name; private int age; /*Constructor with private access modifier*/ private Actor() { } //A method with private access modifier. private void act() { System.out.println("Actor can act!"); } }
Here, the following Doctor class will give compile-time error because we are trying to create an object from the Actor class that has a private default constructor. We are also calling private method on the actor object which is not visible in a different class:
package com.example.doctor; /*importing Actor class*/ import com.example.actor.Actor; public class Doctor { public static void main(String[] args) { /*This will give you error. The default constructor of the Actor class *is private so it is not possible to create an actor object here*/ Actor actor = new Actor(); /* Calling act() method on actor object will give * compile-time error because it is private.*/ actor.act(); } }
The protected access modifier
The protected access modifier is accessible from anywhere within the same package. Additionally, subclasses can access protected methods and member variables of the superclass even if the subclass is located in different packages from the superclass.
package com.example.office; public class Employee { /*Instance variables with protected * access modifier */ protected String name; protected String email; /*A method with protected access modifier*/ protected void work() { System.out.println("Employee working!"); } }
The protected methods, fields within the same package or in any subclasses are accessible. In this example, we can call the work() method on the employee object which is within the same package:
package com.example.office; import com.example.office.Employee; public class Office { public static void main(String[] args) { //Creating an object of Employee. Employee employee = new Employee(); /* Calling protected work() method of Employee class * will not give any error. Because both the classes are * within the same package.*/ employee.work(); } }
The public access modifier
The public access modifier is accessible everywhere. The class, field, constructor or method declared with public access modifier can be accessed from anywhere in the code, regardless of where they are located. The accessing code can be in any class and in any package.
Here is an example of a Teacher class with public fields, public constructor, and a public method which can be accessed from any classes within the application:
package com.example.teacher; public class Teacher { public String name; public String email; public Teacher(String name, String email) { this.name = name; this.email = email; } public void printTeacherProfile() { System.out.println("Teacher name is =" + name); System.out.println("His email is =" + email); } }
Let's create the teacher object using its parameterized constructor and also call it's printTeacherProfile() method in a different TeacherExample class:
package com.example.school; import com.example.teacher.Teacher; public class TeacherExample { public static void main(String [] args) { Teacher teacher = new Teacher("Peter","peter@tutorialsbuddy.com"); teacher.printTeacherProfile(); } }
The output of the above code is as follows:
Teacher name is =Peter His email is =peter@tutorialsbuddy.com