Access Modifier in java defines the scope of accessibility of methods, constructors, variables etc. So to access the different members of the class/interfaces with different scope we have three access modifiers :
1. Public --> Defines visibility everywhere
2. Private --> Defines visibility within the current class only
3. Protected --> Defines visibility within the same package and outside the package with
the object of the sub-class extending the class in another package.
the object of the sub-class extending the class in another package.
4. default --> Defines the visibility within the package only.
Note: If we are not using any access modifier then by default the modifier will be " default " and explicitly we cannot use default as default is already a keyword in java.
Difference between access modifiers and modifiers
Access modifier
- It defines the scope of visibility or accessibility of the members of the class.
- Public, protected, private and default are the access modifiers used in java.
- Except public and by default "default " modifier is used with the class .
- We cannot use private and protected with the class.
- It cab be at class or package level.
Modifiers
- It provides the restriction to the methods and variables about its accessibility at the class level.
- Static,final,abstract,synchronized, volatile are the modifiers used in java.
- We cannot use static with the main class and hence can be used with inner class.
- Final can be used with the class indicates that it cannot be extended by other classes.
0 comments:
Post a Comment