Access Specifier
- Sometimes it is necessary to restrict the access to certain variables and method from outside the class.
- We don’t want that object of a class directly alter the value of a variable or method.
- We can specify visibility control.
1)Private
2)Public
3)protected
Private:
- Private Field has the highest degree of protection and they are accessible only within their own class.
- Private members can not be inherited into the subclass.
Public:
- When any member is declared as public, it is visible to entire class in which it is defined.
- If you want to make it visible to all the class or entire for the program then declare that member as public.
Protected:
- The visibility level of protected field lies in between the public and private access.
- The protected applies only when the inheritance is involved.
- Members declared as protected only accessible to that class and the sub class of that class.
Default access/friendly access
- When no access modifier is specified then it is default access, which has limited version of public accessibility known as friendly level of access.
- The difference between the public access and friendly access is that the public modifier makes fields visible in all classes.
No comments:
Post a Comment