As the name suggests 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 available in java:
- Public
- Private
- Protected
- Default – No keyword required
If we are not using private, protected and public keywords, then JVM is by default taking as default access modifiers.
Access modifiers are always used for, how to reuse the features within the package and access the package between class to class, interface to interface and interface to a class. Access modifiers provide features accessing and controlling mechanism among the classes and interfaces.

1. Access Modifiers for Class:-
Classes in Java can use only public and default access modifiers.
– Public
When set to public, the given Class will be accessible to all the classes available in the Java world.
– Default
When set to default, the given Class will be accessible to the classes which are defined in the same package.
Java Access Modifiers Table for Class:
| Visibility | Public Access Modifier | Default Access Modifier |
| Within Same Package | Yes | Yes |
| From Outside the Same Package | Yes | No |
2. Access Modifiers for Instance & Static Variables:-
Variables are eligible for all of the above mentioned modifiers.
- Default
- Public
- Protected
- Private
Note*: Visibility of the Class should be checked before checking the visibility of the variable defined inside that Class. If the Class is visible then the variables defined inside that Class will be visible. If the Class is not visible then no variable will be accessible, even if it is set to public.
– Private
A variable if defined private will be accessible only from within the Class in which it is defined. Such variables are not accessible from outside the defined Class, not even in its subclass.
– Public
If a variable is set to public it can be accessible from any class available in the Java world. Any Method in any Class can access the given variable via Inheritance or Direct access.
– Protected
If a variable is set to protected inside a Class, it will be accessible from its sub classes defined in the same or different package only via Inheritance.
– Default
If a variable is set to default, it will be accessible to the classes which are defined in the same package. Any method in any Class which is defined in the same package can access the variable via Inheritance or Direct access.
Note*: The only difference between protected and default is that protected access modifiers respect class subclass relation while default does not.
Java Access Modifiers Table for Variable:
| Visibility | Public Access Modifier | Private Access Modifier | Protected Access Modifier | Default Access Modifier |
| Within Same Class | Yes | Yes | Yes | Yes |
| From Any Class in Same Package | Yes | No | Yes | Yes |
| From Any Sub Class in Same Package | Yes | No | Yes | Yes |
| From Any Sub Class from Different Package | Yes | No | Yes(Only By Inheritance) | No |
| From Any Non Sub Class in Different Package | Yes | No | No | No |
3. Access Modifiers for Methods:-
Methods are eligible for all of the following modifiers.
– Default
When a Method is set to default it will be accessible to the classes which are defined in the same package. Any Method in any Class which is defined in the same package can access the given Method via Inheritance or Direct access.
– Public
When a Method is set to public it will be accessible from any Class available in the Java world. Any Method in any Class can access the given method via Inheritance or Direct access depending on Class level access.
– Protected
If a Method is set to protected inside a Class, it will be accessible from its sub classes defined in the same or different package.
Note*: The only difference between protected and default is that protected access modifiers respect class-subclass relation while default does not.
– Private
A Method that is defined private will be accessible only from within the Class in which it is defined. Such Methods are not accessible from outside the defined Class, not even its subclass.
Java Access Modifiers Table for Method:
| Visibility | Public Access Modifier | Private Access Modifier | Protected Access Modifier | Default Access Modifier |
| Within Same Class | Yes | Yes | Yes | Yes |
| From Any Class in Same Package | Yes | No | Yes | Yes |
| From Any Sub Class in Same Package | Yes | No | Yes | Yes |
| From Any Sub Class from Different Package | Yes | No | Yes(Only By Inheritance) | No |
| From Any Non Sub Class in Different Package | Yes | No | No | No |
4. Access Modifier for Local Variable:-
No Access Modifiers can be applied to local variables. Only final can be applied to a local variable which is a Non Access Modifierr.
*Difference between Inheritance or Direct Access: –
Below is illustrated the difference between inheritance and direct access.
Super Class:
package pck1;
public class FirstClass {
public int i;
protected int j;
private int k;
}
Sub Class:
package pck2;
import pck1.FirstClass;
class SecondClass extends FirstClass {
void method() {
System.out.println(i);
/*
* Here you are trying to access protected variable directly. So it will
* not be accessible and compile will give an error.
*/
System.out.println(j);
/*
* As k is private so it will not be accisible to subclass neither way.
* Neither it can be accessed via Inheritance nor direct.
*/
System.out.println(k); // Compilation Error
FirstClass cls = new FirstClass();
/*
* Here property j is accessed via Inheritance hence it will be
* accessible. But the same variable can not be accessed if you try to
* access via instance because modifier used here is protected so it
* will be available to sub class only via inheritance.
*/
System.out.println(cls.j);
// Private variable will not be accessible here also.
System.out.println(cls.k); // Compilation error
}
}
No comments:
Post a Comment