Access-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:-
- The only job of access specifier is to scope the boundary of variable method constructor class.
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 .
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.
a.Access in same class
b.Access in same package and we cannot access outside package.
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.
- Private member cannot access outside the class.
- A java file contain number of classes but only one class will be declared as public due to syntax restriction.
- By the syntax of java if a class declared as public the filename class name should be same.
- Protected and private keyword not allow in java class.
|NAME OF ACESSPECIFIER||WITHIN SAME CLASS||WITHIN SAME PACKAGE||OUTSIDE PACKAGE|
|PROTECTED||✔||✔||✔(in case of inheritance)|