Access Modifiers in java



There are 2 types of modifiers in java:

  1. Access modifiers

  2. Non-access modifiers


In this post, we will learn about access modifiers.


Access Modifier


It defines the scope of the class and its elements.



There are 4 types of access modifiers in java:



  1. Default

  2. private

  3. protected

  4. public


1. Default:


When you do not specify type of modifiers, then it is considered as default by default.

Default fields are accessible within the same package.

you can not use your default fields outside the package.

2. Private
when you declare any field with prefix private then it is called private fields.

Private fields are accessible within the same class.

3. Protected
when you declare any field with prefix protected then it is called protected fields.

Protected fields are accessible only through inheritance.

4. Public
when you declare any field with prefix public then it is called public fields.

Public fields are accessible Everywhere.



Comments