Java OOP – Java Access Modifiers
August 15, 2020 3 min read

An access modifier restricts access of a class, constructor, data member, and method in another class. In java, we have four access modifiers that specify which classes can access a given class and its fields, constructors and methods.
- Default
- Private
- Protected
- Public
- If you want you can refer previous concepts Inheritance and Polymorphism.
Let’s go back to our today’s topic.
Default Access Modifier
When you do not mention an access modifier, it is called a default access modifier. Assume that the default attributes or the methods are in a class and that class is belonging to a package. That package might have more other classes also. Default access modifier can access those classes inside the relevant package. But it cannot access outside of that package. That means scope of the default access modifier is limited to that package only. Let’s see an example.
To understand this access modifier, you must have the knowledge of Java Packages.
Example using the same package:


Here we used the same package “abc” and two different classes “Test” and “Example”. There is no access modifier mentioned for the method “addition()”. Therefore it would be considered as default. And also it will give the output as 13 because both classes are in the same package.
Example using different packages:


The method with default access modifier is in the class “Test” which belongs to the package “abc”. But the method is called in the class “Example” which belongs to the package “xyz”. Both classes are in two different packages. Therefore it will give the error given below.

Private Access Modifier
The scope of the default access modifier is limited to the class only. Private data members or the methods are only accessible within the class. We cannot declare a class and a interface as private. Also if a class has a private constructor, we cannot create an object from that class outside of the class. Let’s see an example.
Example using the same class:

Here we used the method “addition()” with the private access modifier and called it in the same class. So it will run without any error by giving the output as 13.
Example using different classes:


Here we used the method “addition()” with the private access modifier in class “Test” and called it in the class “Example” inside the same package. But as we know private access modifiers should used inside the same class, it will give an error as the output given below.

Protected Access Modifier
Protected data members or the methods are only accessible within the classes of the same package and the sub classes present in any other package. We cannot declare a class as protected. Generally we use protected access modifier in a parent child relationship. Let’s see an example.


Here we used the method “addition()” with the protected access modifier inside the package “xyz” and called it in the sub class “Example” extended by the class “Test” inside the package “abc”. So it will run without any error by giving the output as 13.
Public Access Modifier
Public data members or the methods are accessible in anywhere. This modifier doesn’t put any restriction on the access. Let’s see an example.


Here all of the members are public. There for it will run without any error by giving the output as 13.
Guys, I hope you all got knowledge about java access modifiers. Let’s go back to the concepts of java with the next article on the topic “Encapsulation”.
Thank you.


