Login    New User    Forgot Password    Help      

Access Modifiers

Java Tutorials

Access Modifiers are one of the most important topics when it comes to taking the SCJP exam. You can expect a number of questions from these access modifiers. Some questions test several concepts of access control at the same time, so not knowing one small part of access control could blow an entire question. Let us get started... 

An Access Modifier is a key word in java that determines what level of access or visibility a particular java variable/method or class has. There are 4 basic access modifiers in java. They are: 
1. Public
2. Protected
3. Default and
4. Private

Private is the most restrictive access modifier whereas public is the least restrictive. Default is the access protection you get when you do not specifically mention an access modifier to be used for a java object. 

Java programming does not run by just a single piece of class that has the whole functionality. You have hundreds of classes that interact with one another, passing data between them and returning output to the user of the system. So it is very important for members of one class to access members of another. Here members may refer to variables, methods and even classes. So, this is where the access modifiers come into picture. The modifier associated with every member of the class determines what level of visibility that member has. 

Let us say you have a Class A and you are trying to access a method from inside Class B. If the modifier for the method inside class B is restricted (say private) then the compiler will punch you in the face and tell you that what you are doing is wrong. Because, class A is not even supposed to know about the private members of class B and you cant possibly use them.

You need to understand two different access issues:

• Whether method code in one class can access a member of another class
• Whether a subclass can inherit a member of its superclass

The first type of access is when a method in one class tries to access a method or a variable of another class, using the dot operator (.) to invoke a method or retrieve a variable

Access Modifiers – Access to Class Members
Visibility Public Protected Default Private
From Within the Same Class Yes Yes Yes Yes
From any class in the Same Package Yes Yes Yes No
From a sub-class inside the Same Package Yes Yes Yes No
From a sub-class outside the Package Yes Yes (Only through Inheritance) No No
From a non sub-class outside the Package Yes No No No

 

 

Example

// Inheritance
// Member Access
class base
{
private int a;
int b;

        void getdata()
        {
        a=10;
        b=20;
        }

        void showdata()
        {
        System.out.println("Value of a is "+a);
        System.out.println("Value of b is "+b);
        }

}
// Inheritance
class drive extends base
{

}
class herit2
{
public static void main(String args[])
{
base b1=new base();
b1.getdata();
b1.showdata();
drive d1=new drive();
d1.getdata();
d1.showdata();

drive d2=new drive();
//a is private hence is not accesable here
//d2.a=10;
d2.b=20;
System.out.println("A is 0 because a is private member and not acceable in main class");

d2.showdata();
}
}

Output

Java Tutorials