Core Java Interview Questions – Access Modifiers
This Core Java Interview Questions-Access Modifiers page provides interview questions about access modifiers such as private, package, public and protected in Java language including what is access modifier, need of access modifier, applying access modifiers.
How many access modifiers java supports?
<package> or <default>
What is Access modifier?
Access modifier describes what are the limitations(boundaries) of object members or class members to be accessed in java application.
Where can we apply access modifiers?
We can apply access modifiers to class, variable, method, and constructor.
Why can’t we apply access modifiers to blocks?
Access modifiers can be applied only to members which we can access.
Blocks we cannot access explicitly as they have no identities.
What are the limitations of private modifier?
private members can be accessed only within the class.
These members are not visible to outside world to access.
What is default access modifier?
In java application, default access modifier is package level.
If we do not apply any modifier, we can access that member in all the classes of the same package.
Why a class can’t be private?
The class represents Object in Java application.
If the complete object is private, that is not visible to outer objects to communicate.
Hence complete object cannot be defined as private.
What are the limitations of protected member?
Protected members can be accessed within the package and only from subclasses of the outer package.
We define protected members only in Parent-Child relation.
Why java class can’t be protected?
protected members can be accessed only in Parent-Child relation.
If the class is protected, it is visible only in the hierarchy. Outer objects cannot communicate.