The access modifiers in Java helps to restrict the scope of a class, constructor, variable, method, or data member.
There are four types of access modifiers in java.
- Default
- Private
- Protected
- Public
1.Default access modifier
When we don’t use any access modifier for a class, method, or data member, it is treated as a default access modifier by default.
Access modifier is accessible only within the same package so, it can’t be accessed outside the package.
Simple Example of Default access modifier
//Java program to illustrate default modifier
package techPackage;
/* Since we didn't mention any access modifier here, it would
* be considered as default.
*/
class Student {
void reading() {
System.out.println("Student reading");
}
}
package roundPackage;
import techpackage.*;
class Test {
public static void main(String args[]) {
Student s = new Student();
s.reading();
}
}
Output:
Compile-time error
In the first Student class, we didn’t mention any access modifier as it is considered as default.
In the second Test class, we are importing the round Package package, but still, we will get an error, because the class we are trying to use has a default access modifier.
In this program, we are getting the error because we are trying to access the default method in another package.
2. Private access modifier
The scope of private modifier is accessible only within the class. It is specified using the keyword private.
Private data members and methods are only accessible within the class.
Parent Class Classes or interface can not be declared as private. If declared than we will get compile-time error.
If any class has a private constructor, then we can not create the object of that class outside of that class.
Simple Example of Private access modifier
class Student {
private int rolleNo = 101;
private void reading() {
System.out.println("student is reading");
}
}
public class Test {
public static void main(String args[]) {
Student s = new Student();
System.out.println(s.rolleNo);// Compile Time Error
s.reading();// Compile Time Error
}
}
Output:
Compile-time error
I the above example, we are getting a compilation error because we are trying to access the private data member and private method of class Student in the class Test.
3.Protected Access Modifier
The protected access modifier is accessible within the class, within the same package, outside package by subclass only. It is specified using the keyword protected.
The protected access modifier is not applied to the class, it is applied to a data member, method, and constructor only.
Simple Example of Protected access modifier
//Java program to illustrate
//protected modifier
package techPackage;
public class College {
protected void display() {
System.out.println("Technicla Round");
}
}
//Java program to illustrate
//protected modifier
package roundPackage;
import techPackage.*; //importing all classes in package techPackage
//Class Student is subclass of College
class Student extends College {
public static void main(String args[]) {
Student s = new Student();
s.display();
}
}
Output:
Technical Round
4.Public Access Modifier
The public access modifier is accessible from everywhere in the program. It is specified using the keyword public. There is no restriction on the public.
Simple Example of Protected access modifier
//Java program to illustrate
//public modifier
package techPackage;
public class Student {
public void reading() {
System.out.println("Technical Rond");
}
}
package roundPackage;
import techPackage.*;
class Test {
public static void main(String args[])
{
Student s = new Student();
s.reading();
}
}
Output:
Technical Round
Important conclusions Point.
1. The modifiers are applicable for inner classes but not for outer classes, are Private, Protected, Static.
2. The modifiers are applicable for classes but not for the interface is final.
3. The modifiers are applicable for classes but not for the enums is final and abstract.
4. The modifiers are applicable for methods and which we can’t use anywhere else native.
5. The only modifiers which are applicable for contractors are Public, Private, Protected, default.
6. The only application modifier for a local variable is final.
Read more topics related to java
- Multithreading in java
- Inter thread communication in java
- synchronization in java
- Exception in java
- Inner class in java
- Different between String and String Buffer
- Top 5 string programming interview questions in java
- String in java
- Object-Oriented Programming (OOPS) Concept in java
- Encapsulation in Java with Examples
Hope this was helpful for you. If you have any questions please feel free to leave a comment. Thank you for reading.