- The access modifiers in java specifies accessibility (scope) of a data member, method, constructor or class.
- There are 3 types of java access modifiers:
- private
- protected
- public
- Private:-The private access modifier is accessible only within class.
- Protected:-The protected access modifier is accessible within package and outside the package but through inheritance only.The protected access modifier can be applied on the data member, method and constructor. It can’t be applied on the class.
- Public:-The public access modifier is accessible everywhere. It has the widest scope among all other modifiers.Let’s understand the access modifiers by a simple table.
Access Modifier | within class | within package | outside package by subclass only | outside package |
---|---|---|---|---|
Private | Y | N | N | N |
Default | Y | Y | N | N |
Protected | Y | Y | Y | N |
Public | Y | Y | Y | Y |
0 Comments:
Post a Comment