DEV Community

Bharath kumar
Bharath kumar

Posted on

Access Modifiers

Access Modifiers:
Access level modifiers determine whether other classes can use a particular field or invoke a particular method.
At the top level—public, or package-private (no explicit modifier).
** There are two levels of access control:**

At the top level— public, or package-private or default (no explicit modifier).// example: "public class One"
   Default word cannot be mention, simply write  "class One"

At the member level—public, private, protected, or package-private (no explicit modifier).
 Members level means inside the class members such as "Instance variables,methods and constructors".
Enter fullscreen mode Exit fullscreen mode

Public:

A class may be declared with the modifier public, in which case that class is visible to all classes everywhere.

Default or no modifier:
If a class has no modifier (the default, also known as package-private), it is visible only within its own package (packages are named groups of related classes — you will learn about them in a later lesson.)

Member level:
At the member level, you can also use the public modifier or no modifier (package-private) just as with top-level classes, and with the same meaning.
For members, there are two additional access modifiers: private and protected.

Private:
The private modifier specifies that the member can only be accessed in its own class.

Protected:
The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package.

Reference:https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

Top comments (0)