Access Modifiers in Java
Video Tutorial
Overview
Access Modifiers in Java provide a restriction on the scope of the class, instance variables, methods, and constructors.
There are four types of Access modifiers in Java- Default, Private, Protected, and Public. They are different from each other due to the restricted scope they provide. If not specified, then Java uses the default modifier itself.
Introduction to Access Modifiers in Java
Let’s understand the Access Modifiers in Java with the help of real-life examples.
There are many things that you want to share publicly or keep some things hidden. For example, The details of your salary are only known to your family members so this is a restriction.This restriction is similar to the Default modifier.
The second aspect is your name which is known to all without any restriction. The public modifier follows the same concept. The protected modifier also used the public modifier concept but has little restriction on the scope (We will study this later in the article).
You must be having some secrets that you don’t want to disclose to anyone, even with your family members. This is the same behavior as the private modifier.
We will discuss all these different kinds of restrictions in this article. Let’s dive deep and understand the concept of Access Modifiers in Java.
These are the four types of access modifiers in Java that are used to control the visibility/access of instance variables, constructors, and class methods:
- private
- protected
- default
- public
Let's discuss each access modifier in Java one by one.
Private Access Modifier
As the name suggests, the scope of this modifier is very restricted. When the data members and the class methods are prefixed with a private modifier, they can be accessed only in the class itself even the other classes of the same package cannot access the private members or methods.
For example: Let’s take an example of a Facebook account when you change the privacy of your status to only for me, only you will be able to access the status, and no one even your friends will not be able to see your status..
Let's understand the private modifier with the help of a Java program
In the below class, we have two private instance members and a constructor as well as a method of private type.
Let's try to understand the accessibility of private members and methods by accessing them in different scenarios.
Let's try to run the above code and see what will happen.
Output:
We got a lot of errors, but why?. Because we are trying to access the private members and private methods in another class (Even in the same package) rather than in its own class.
Public Access Modifier
The public modifier provides no restriction on the methods, classes, and instance members of the particular class.
When they are prefixed by the public modifier, they can be accessed in any package, in any class. The scope of the public modifier is very flexible as compared to the other modifiers.
Real-life example:
Let's take an example of Facebook. when you set the privacy of status to the public everyone on Facebook can access your status whether it is your friend, friend of your connection, or not a friend.
Let's understand the public modifier with the help of a Java program
Consider two packages named First and Second in which there are two public classes.
The Scaler class of the First package contains one instance member and one method i.e. print(). Both are of public type. Let's try to access them in another class of different packages i.e. in the Second package.
First package:
Second package:
Output:
We got the correct output, because we can access the public modifier's methods or instance variables in any class of the same or different package.
Default Access Modifier
When the instance members, methods of the class are not specified with any modifier. It is said to be having a default modifier by default. The instance members and methods declared with default modifier can be accessed only within the same package. Hence it is sometimes called package-private.
Real-life example: In the Facebook account, when you set your privacy status to "visible to your friends only".You and your's known friends can access your status.
Any other person will not be able to access your status info.
Let's understand the default modifier with the help of a Java program
- Accessing the default members and method in the same package.
In the below example, there is a package First that contains two classes. We are trying to access the first class default method in the second class. Let's see what will happen.
First Package
Output:
The program will run successfully because default members and methods can be accessed in the same package.
- Accessing the default members of one package's class in another package's class.
In the below example, we are accessing the default method of the First package class in another package i.e Second.
Second Package
Output:
We got an error because the default modifier doesn't allow us to access the members and methods in another package. We are accessing the default method in another package in the above program.
Protected Access Modifier
We can use protected modifier methods, instance members within in the same package and access them in another package with the help of a child class which means we have to extend the class that contains protected members in another package.
We cannot access them directly by creating the object of a class that contains protected members and functions.
Let's understand the protected modifier with the help of a Java program
There are two packages, First and Second, The First package contains one public class and a protected prefixed method, and we are trying to access this method in another package in two ways.
- Creating an instance of the Scaler class (declared in First package)
- Inheriting the Scaler class in the InterviewBit class of the Second package.
Let's see what will happen.
First package:
Second package:
The line ob.print() will cause an error because we cannot access the protected method outside its own package, but we can access it by inheriting it in some other class of different packages, that's why ob1.print() will not cause any error.
That's it, these are the four Access Modifiers in Java.
Pictorial Explanation of Access Modifiers in Java
Access Modifier | Same class | Same package subclass | Same package non-subclass | Difference Package subclass | Different package non-subclass |
---|---|---|---|---|---|
Default | Yes | Yes | Yes | No | No |
Private | Yes | No | No | No | No |
Protected | Yes | Yes | Yes | Yes | No |
Public | Yes | Yes | Yes | Yes | Yes |
The above table depicts the different kinds of restriction levels of different kinds of Access Modifiers. With the help of this table, we can distinguish between all the modifiers available in Java.
Java Access Modifiers With Method Overriding
The main rule of the method overriding is that the override method should not be more restricted.
The private access modifier in Java has the most restricted scope, and this modifier doesn't allow method override in the same package.
Whereas the other access modifiers allow method overriding in the same package as well as in other packages.
Let's understand the behavior of the private access modifier in the method overriding concept.
Output:
When we try to override the private method of the parent class, JVM will throw a compilation error. Because we can access the private methods only in their own class, we can see the scope of private modifiers in the pictorial explanation of Access Modifiers in Java. Still, we are trying to access that method in the other class using the method overriding concept. This will cause an error.
Algorithm to Use Access Modifier in Java
Following steps can be followed to choose an appropriate access modifier based on the desired level of visibility.
- Determine the scope of the variable or method that needs to be accessed.
- Use the public access modifier if the variable or method needs to be accessible from anywhere in the program, including outside classes.
- Use the protected access modifier if the variable or method needs to be accessible within the same package or subclass, but not from outside the package or subclass.
- Use the default (package-private) access modifier if the variable or method needs to be accessible within the same package only.
- Use the private access modifier if the variable or method needs to be accessible only within the same class.
Algorithm to Use Access Modifier in Java
Following steps can be followed to choose an appropriate access modifier based on the desired level of visibility.
- Determine the scope of the variable or method that needs to be accessed.
- Use the public access modifier if the variable or method needs to be accessible from anywhere in the program, including outside classes.
- Use the protected access modifier if the variable or method needs to be accessible within the same package or subclass, but not from outside the package or subclass.
- Use the default (package-private) access modifier if the variable or method needs to be accessible within the same package only.
- Use the private access modifier if the variable or method needs to be accessible only within the same class.
FAQs
Why are Access Specifiers important?
Access specifiers are used for encapsulation purposes and provide different types of restrictions according to the requirement so that we can protect our sensitive information from direct access.
How many Access Modifiers are there in Java?
Four Access modifiers are present in Java, public, private, protected, and default.
Which Modifiers are not used for the class?
The private and protected modifier cannot be used for the class because if they are used for the class, due to their restricted scope, JVM will not be able to execute the class because of the external calling system of JVM.
Conclusion
- There are four types of Access modifiers in Java - Public, Private, Default and Protected,
- Private modifier has the most restricted access scope. Whereas public modifier has the least.
- Private data members or methods are accessible from within the class only.
- Public methods or instance members are accessible from anywhere.
- Default Access Modifier is package-private. The methods or data members declared as default are accessible within the package only.
- The methods or data members declared as protected are accessible within the same package or subclasses in different packages.
- Private access modifier doesn't allow method overriding. However, other access modifiers allow method overriding in the same package as well as in other packages.