Java Access modifier are keywords that defines the accessibility and visibility of java classes,variables,methods and constructors within a Java application. The Access modifier are important part for building a secure and encapsulated java application.
Types of Access modifiers:
There are 4 types of access modifiers available in Java:
- Default
- Private
- Protected
- Public
1. Default Access Modifier:
When no access modifier is specified for a class,variables,methods or constructor, it is said to have default access modifier. Any variables,methods or constructor declared with default modifier can only be accessed by classes within the same package.
Example :
Let’s understand default access modifier within same package and different package.
In this example, we will create two packages and the classes in the both packages will have default access modifiers and we will try to access a class of one package from a class of another package.
SweetFruits.java
package pkg1;
//class SweetFruits is having default access modifier
class SweetFruits{
void taste(){
System.out.println(“Sweet fruits”);
}
}
package pkg1;
class Fruits{
public static void main(String args[]){
SweetFruits f1=new SweetFruits();
f1.taste(); // as default taste method is called from the same package class, it will not throw compile error.
}
package pkg2;
import pkg1;
class SourFruits{
void taste(){
System.out.println(“Sour fruits”);
}
public static void main(String args[]){
SweetFruits f2=new SweetFruits();
f2.taste(); //calling default taste method from class of another package pkg2 throw compile time error.
}
}
In the above example, the program will show compile time error when we try to call default method from another package pkg1.
2. Private Access Modifier:
The private access modifier in java is specified using the keyword private.
The variables,methods or constructors declared using the private keyword are accessible only within the class in which they are declared.
Any other class of the same package cannot access the private members.
Interfaces and top level classes can’t be declared as private as other classes are implementing the interface and extending the top level class.
Example:
In this example,we will create two classes SourFruits and Fruits within the same package p1. We will declare one method in class SourFruit as private and then try to call this method from another class Fruits and see the result.
package p1;
class SourFruits{
private void display(){
System.out.println(“Sour fruits “);
}
}
class Fruits{
public static void main(String args[]){
SourFruits sf=new SourFruits();
//Accessing the private method of another class SourFruits
sf.display();
}
}
Result:
The above code will show a compile-time error when trying to access a private method from another class SourFruits, even though within the same package.
Protected Access Modifier:
The protected access modifier is defined using the keyword protected. The method or data members declared as protected can be accessed within the same package or subclasses in different packages.
Example1:
In this example, we will create two classes SourFruits and Fruits within the same package p1. The method display in class SourFruits is protected and we will try to access this protected method display() from another class Fruits within the same package p1 and let’s see the result.
package p1;
class SourFruits{
protected void display(){
System.out.println(“sour fruits”);
}
}
class Fruits{
public static void main(String args[]){
SourFruits sf=new SourFruits();
//Accessing the protected method display() from another class SourFruits within the same package p1
sf.display();
}
}
Result:
sour fruits.
It will display the result without any error which demonstrate that protected members of another class are accessible within the same package.
Example2:
In this example , we will declare two classes SourFruits and Fruits within different packages p1 & p2 respectively where SourFruits class is a subclass of Fruits class. Then we will declare one method named display() as protected in class SourFruits which is present in the package p1. After that we will try to call this protected method display() from another class Fruits of package p2 and let’s see the result.
package p1;
class SourFruits{
protected void display(){
System.out.println(“Sour fruits”);
}
}
package p2;
import package p1.*;
class Fruits{
public static void main(String args[]){
SourFruits sf=new SourFruits();
//Accessing the protected method display() from the another class SourFruits of another package p1
sf.display();
}
}
Result:
Sour fruits
Above example demonstrate that protected members of a another package class are accessible in the subclass of different package using inheritance.
4. Public Access Modifier:
The keyword public is used to specify the public access modifier .
Classes,methods or data members that are declared as public are accessible from everywhere in java application program. There is no restriction on the scope of public data members.
Example1:
In this example we will declare two classes SourFruits and Fruits within same package p1. Then we will declare a public method display() in class SourFruit and try to access this public method from another class Fruits and let’s see the result.
package p1;
class SourFruits{
public void display(){
System.out.println(“Sour fruits”);
}
}
class Fruits{
public static void main(String args[]){
SourFruits sf=new SourFruits();
//Accessing the public method display() from another class SourFruits in the same package p1
sf.display();
}
}
Result:
Sour fruits
This example demonstrate that public method of another class in same package can be accessible from another class
Example2:
In this example, we will declare two classes SourFruits and Fruits in two different packages p1 and p2 respectively.
Then we will declare one public method display() inside the class SourFruits and after that we will try to access this public method display() from another class Fruits of package p2 . Let’s see the result.
package p1;
class SourFruits{
public void display(){
System.out.println(“Sour fruits”);
}
}
package p2;
import package p1.*;
class Fruits{
public static void main(String args[]){
SourFruits sf=new SourFruits();
//Accessing the public method of another class SourFruits of another package p1 inside this class of package p2
sf.display();
}
}
Result:
Sour fruits
This example demonstrate that public method of another class from different package can be accessible in a class of another package.
When to use each Access modifier in real time applications?
- Private: This access modifier is used for encapsulating sensitive data and internal methods that shouldnot be accessed outside the class
Example: private fields in an encapsulated class with getter and setter methods.
- Default: This access modifier is suitable for classes and methods that should only be accessible within the same package,most commonly used in package-scoped helper classes.
- Protected: This access modifier is suitable for methods and fields that should be accessible within the same package or in the subclasses ie. Protected members are accessible within the subclass of different package also. This modifier is commonly used in inheritance-based designs.
public: This access modifier is used for classes, methods or fields which are accessible from anywhere in the application, such as service classes or utility methods which are shared across the different parts of the application.