Access Modifiers

The Access Modifiers in Java specifies the scope of a method or field or class

There are 4 types of access modifiers in Java. They are :

  1. Private
  2. Public
  3. Default
  4. Protected

Private

  • It is accessible only inside the class

Example


The output will give an error as


check output here

Public

  • The data in public access modifier is accessible everywhere

Example


check output here

Default

  • The data in default is accessible only within package
  • If no access modifier is present, it is treated as default by default

Example



It will give an error as the scope is limited the package

Protected

  • It is accessible to all packages which are inherited

Example



We will get output as p1.