Java Access Modifiers
- Public access: Any variable or method is visible to the entire calss in which it is defined. what if we want to make it visible to all the classes outside this class? this is possible by simply declaring the variable or method as public.
- public int a;
- public void sum()
- Friendly Access: when no access modifier is specified , the member defaults to a limited version of public accessibility known as “friendly” level of acess. The difference between the public acess and the friendly access is that the public modifier makes fields visible in all classes , regardless of their packages while the friendly access makes fields visible only in the same package, butnot in other packages.
- Protected Access: The visibility level of a protected field lies in between the public access and friendly access. that is , the protected modifie makes the fields visible not only to all classed and subclassed in the same package but also to subclasses in other packages. note that non-subclasses in other packages cannot access the protected members.
- Private Access: Private fields enjoy the highest degree of protection, they are accessible only within their own class. they cannot be inherited by subclasses and therefore not accessible in subclasses. A method declared as private begaves like a method declared as final. It prevents the method from being subclassed . Also note that we cannot override a non-private method in a subclass and then make it private.
- Private protected access: A field can be declared with two keywords private and protected together like
- private protected int a;
this gives a visibility level in between the protected access and private access. this modifier makes the fields visible in all subclasses regardless of what package they are in. These fields are not accessible by other classes in the same package.
For
Online Classes
Contact Us: 9885348743
Online Classes
Contact Us: 9885348743