Access control in java

0




Access Control

Variables and methods can be at one of four access levels: public,protected, default, or private. Classes can be at the public or defaultv level. Table 1 shows the access levels.

Access Control

A variable or method marked private is accessible only by methods that are members of the same class as the private variable or method.

A variable, method, or class has default accessibility if it does not have an explicit access modifier as part of its declaration. Such accessibility means that access is permitted from any method in classes that are members of the same package as the target. This is often called package-friendly or package-private.

A variable or method marked with the modifier protected is actually more accessible than one with default access control. A protected method or variable is accessible from methods in classes that are members of the same package and from any method in any subclass. You should use the protected access when it is appropriate for a class’s subclass, but not unrelated classes.

A variable or method marked with the modifier public is accessible universally.

Note – Protected access is provided to subclasses that reside in a different package from the class that owns the protected feature.