2. Introducing Access Control
Encapsulation links data with the code that manipulates it.
Through encapsulation, we can control what part of a program can access the members of a
class.
By controlling access, we can prevent misuse.
How a member can be accessed is determined by access specifiers that modifies its
declaration.
3. What is Access specifiers?
Access specifiers (or access modifiers) are keywords in object-oriented languages that set the
accessibility of classes, methods, and other members.
Access modifiers in object oriented programming language:
C++ uses only three access modifiers called public, protected and private.
C# extends that number to six. It’s modifiers are public, protected, internal, private, protected internal
and private protected.
While Java has four access modifiers i.e. public, protected, private and package.
The access modifier package is the default and used, if any other modifier keyword is missing.
The meaning of these access specifiers may differ from one language to another.
4. Access control in JAVA
Java provides many levels of protection to allow fine-grained control over the visibility of
variables and methods within classes, subclasses and packages.
Classes and packages are both means of encapsulating and containing the name space and
scope of variables and methods.
Because of the interplay between classes and package, Java addresses four categories of
visibility for class members.
Subclasses in the same package
Non-subclasses in the same package
Subclasses in different packages
Classes that are neither in the same package nor subclasses
The three specifiers public, private and protected, provide a variety of ways to produce the
many levels of access required by these category.
5. Access modifiers in JAVA
Let’s have a look on these access modifiers.
Anything declared public can be accessed from anywhere.
Anything declared private cannot be seen outside of its class.
If we want to allow an element to be seen outside current package, but only to classes that
subclass our class directly, then declare that element protected.
When a member does not have an explicit access specification, it is visible to subclasses as well
as to other classes in the same package. This is the default access.
6. Class Member Access Table
Private No Modifier Protected Public
Same Class Yes Yes Yes Yes
Same package
subclass
No Yes Yes Yes
Same package
non-subclass
No Yes Yes Yes
Different package
subclass
No No Yes Yes
Different package
non-subclass
No No No Yes
7. Examples
The following example shows all combinations of the access modifiers.
This example uses two packages(p1 and p2) and five classes.
8. This is file Protection.java:
package p1;
public class Protection {
int n = 1;
private int n_pri = 2;
protected int n_pro = 3;
public int n_pub = 4;
public Protection()
{
System.out.println("base constructor");
System.out.println("n = " + n);
System.out.println("private variable = " + n_pri);
System.out.println("protected variable = " + n_pro);
System.out.println("public variable = " + n_pub);
}
}
9. This is file Derived.java:
package p1;
public class Derived extends Protection{
public Derived()
{
System.out.println("derived constructor");
System.out.println("n = " + n);
// Private variable - class only
// System.out.println("private variable = " + n_pri);
System.out.println("protected variable = " + n_pro);
System.out.println("public variable = " + n_pub);
}
}
10. This is file SamePackage.java:
package p1;
public class SamePackage {
public SamePackage()
{
Protection p = new Protection();
System.out.println("SamePackage NonSubclass constructor");
System.out.println("n = " + p.n);
// private variable - class only
// System.out.println("private variable = " + p.n_pri);
System.out.println("protected variable = " + p.n_pro);
System.out.println("public variable = " + p.n_pub);
}
}
11. Following is the source code for other package, p2.
This is file Protection2.java:
package p2;
public class Protection2 extends p1.Protection{
public Protection2()
{
System.out.println("Different Package subclass");
// No modifier - class or same package only
// System.out.println("n = " + n);
// private variable - class only
// System.out.println("private variable = " + n_pri);
System.out.println("protected vaiable = " + n_pro);
System.out.println("public variabl = " + n_pub);
}
}
12. This is file OtherPackage.java:
package p2;
public class OtherPackage {
public OtherPackage()
{
p1.Protection p = new p1.Protection();
System.out.println("Other Package non sublcass");
// no modifier -class or same package only
// System.out.println("n = " + p.n);
// private variable - class only
// System.out.println("private variable = " + p.n_pri);
// protected variable - class, subclass or same package only
// System.out.println("protected variable = " + p.n_pro);
System.out.println("public variable = " + p.n_pub);
}
}
13. We have two test files. The one is for package p1 is shown here:
package p1;
public class DemoTest1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Protection obj = new Protection();
Derived obj1 = new Derived();
SamePackage obj2 = new SamePackage();
}
}
14. The test file for p2 is shown next:
package p2;
public class DemoTest2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Protection2 obj3 = new Protection2();
OtherPackage obj4 = new OtherPackage();
}
}