Visibility control
Visibility control
The visibility modifiers are also known as access modifiers. Java provides three types of visibility modifiers : public, private and protected. They provide different levels of protection :
Public access : Any variable or method is visible to entire class in which it is defined. Declaring the variable or method as public makes it visible to all the classes even outside the baseclass. A variable or method declared as public has the widest possible visibility and accessibility.
Example
Public intx ;
Friend function When no access modifier is specified , the member default to a limited version of public accessibility known as friendly level of access. The difference between the public access and the friendly access is that the public modifier makes field visible in all classes regardless of their packages while the friendly access makes fields visible only in the some package but not in other packages. ( A package is a group of related classes stored separately. )
Protected access :
The visibility level of a protected field lies in between the public access and friendly access. That is , the protected modifier makes the fields visible not only to all classes and sub classes in the same package but also to sub classes of other packages. Not that non sub classes n other packages cannot access the protected members.
Private access
Private fields enjoy the highest degree of protection. They are accessible only with their own class. They cannot be inherited by subclasses and therefore cannot be accessed in the sub classes. A method that is declared a private behaves a method declared a final. It prevents methods from being sub classes. We cannot override a non private method in a sub class and then make it private.