Menu
Topics Index
...
`


Interfaces, Packages and Access Control >
Siva Nookala - 14 Apr 2016
The most strict level of access control allowed in Java is private. Any member variable or method marked as private will be only accessible in that class. But we might not need such a strong restriction all the time. So Java supports the other levels like default (no-modifier), protected, and public.

Please note that while private, protected and public are keywords, the default access level is NOT a keyword. If we do not place either private, protected or public before a member variable or method, then it is automatically considered as default access level.
  • default (No Modifier) access level : If a variable or method is marked as default by not placing any access specifier, then that variable or method can be only accessed in that class, its same package sub-classes or in any other class belonging to the same package. But that variable or method can not be accessed in a sub-class or non-subclass belonging to a different package.
  • protected access level : If a variable or method is marked as protected, then that variable or method can be only accessed in that class, its sub-classes (same-package or different package) or in any other class belonging to the same package. That variable or method is only not accessible in a non-subclass belonging to a different package.
  • public access level : If a variable or method is marked as public, then that variable or method is accessible everywhere. There is no restriction on its access.
The following table summarizes the access control.
Private No Modifier Protected Public
Same class Yes Yes Yes Yes
Same package sub-class No Yes Yes Yes
Same package non sub-class No Yes Yes Yes
Different package sub-class No No Yes Yes
Different package non sub-class No No No Yes
Please note that all the programs we have written till now had been using the default (No Modifier) access level.

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App