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:-
- Public
- Protected
- Default
- 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 :-
- 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.
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…”
Thanks-a-mundo for the blog.Thanks Again. Much obliged.
Thanks for giving the feedback
“Really appreciate you sharing this blog article.Really thank you! Keep writing.”