☰ Topics

Access Specifiers

Access Protection: Classes and packages are both means of encapsulating and containing the name space & scope of variables and methods. Packages act as containers for classes and other subordinate packages. Classes act as containers for data and code. The class is Java's smallest unit of abstraction because of the interplay between classes and packages. Java addresses four categories of visibility for class members:

  1. Subclasses in the same package.
  2. Non-subclassses in the same package.
  3. Sub-classes in different package.
  4. Classes that are neither in the same package nor subclasses.


Access Specifier: If a class has default access, then it can only be accessed by other code within its same package.
  • Private- Cannot be seen outside the class.
  • Protected- Allows element to be seen outside your current package but only to subclasses of that class.
  • Public- Can be accessed from anywhere.

Private No Modifier Protected Public
Same Class Yes Yes Yes Yes
Same Package Subclass No Yes Yes Yes
Same Package Non-subclass No Yes Yes Yes
Different Package Subclass No No Yes Yes
Different Package Non-subclass No No No Yes
Table: Class Member access