Categories
Core JAVA

Access Specifier in java…

Access Specifier in javaAccess-specifier in JAVA:-

Access Specifier in java regulate access to class,field & method.

There are 4 types of access specifier present in java such as:-

  1. Public
  2. Protected
  3. Default
  4. Private
  • The only job of access specifier is to scope the boundary of variable method constructor class.

1.Public:-

a.Public has less restriction.

b.In other word is has no restriction.

c.It  can access anywhere in within same class, within same package and outside the package .

2.Protected:-

a.It has no restriction

b.We can access it within same class,within same package and outside the package.

c.But outside package required inheritance.

3.Default:-

a.Access in same class

b.Access in same package and we cannot access outside package.

4.Private:-

a.It is the most restrictive access-specifier in java.

b.We can access it only within same class.

c.We cannot access it within package and outside package.

Note :-

  1. Private member cannot access outside the class.
  2. A java file contain number of classes but only one class will be declared as public due to syntax restriction.
  3. By the syntax of java if a class declared as public the filename class name should be same.
  4. Protected and private keyword not allow in java class.

Diagrammatic Table:-

NAME OF ACESSPECIFIER WITHIN SAME CLASS WITHIN SAME PACKAGE OUTSIDE PACKAGE
PRIVATE ­✔
DEFAULT
PROTECTED ✔(in case of inheritance)
PUBLIC

3 replies on “Access Specifier in java…”

Leave a Reply

Your email address will not be published. Required fields are marked *