Difference between two control visibilities

Protected and default access modifiers are two control visibility in java and  are identical, but have one single difference. Default member can be access only from the class that belongs to same package, but protected members can be accessed even from outside the package but only through inheritance. Here in below table I have tried to show the access level of this two access modifier.

Default

Protected

Same Class

Yes

Yes

Same Package Subclass

Yes

Yes

Same Package Non-Subclass

Yes

Yes

Different Package Subclass

No

Yes

Different Package Non-Subclass

No

No

To be more clear, let’s take a example of four classes, which is in a package javaprac.

ClassA.java:

package javaprac.collection;
public class ClassA {
protected int temp1 = 10;
int temp2 = 20;
void A_X()
{
System.out.println(“Displayed from method X of class A”);
}
void A_Y()
{
System.out.println(“Displayed from method Y of class A”);
}
protected void A_Z()
{
System.out.println(“Displayed from method Z of class A”);
}
}

ClassB.java:

package javaprac.collection;
public class ClassB {
ClassA A =new ClassA();
void B_X()
{
A.A_X();
}
public void B_Y()
{
A.A_Y();
}
public void displayTemp_B()
{
System.out.println(“The Value of temp is ” + A.temp1);
System.out.println(“The Value of temp is ” + A.temp2);
}
}

ClassC.java:

package javaprac;
import javaprac.collection.*;
public class ClassC extends ClassA {
public void C_Z()
{
//A_X(); Not accessible
//A_Y(); Not accessible
A_Z();
}
void displayTemp_C()
{
System.out.println(“The Value of temp is ” + temp1);
//temp2 is not accessible
}
}

Main.java:

package javaprac;
import javaprac.collection.*;
public class Main {
public static void main(String[] args) {
ClassB objB=new ClassB();
//objB.B_X();   Can’t access because B_X() is default access
objB.B_Y();
objB.displayTemp_B();
System.out.println(“~~~~~~~~~~~~~~~~~~~~~~”);
ClassC objC=new ClassC();
objC.C_Z();
objC.displayTemp_C();
}
}

Output:
Displayed from method Y of class A
Displayed from method Z of class A
The Value of temp is 10
The Value of temp is 20
~~~~~~~~~~~~~~~~~~~~~~
Displayed from method Z of class A
The Value of temp is 10

In above example package javaprac contains
– collection (package)
– ClassC.java
– Main.java

And collection package contains
– ClassA.java
– ClassB.java

ClassA contains two member variables and three member methods. From ClassB all of the member methods and member variables are accessible, because of being in same class. But from ClassC, which is outside the package collection and inherits ClassA, can access only those members which are protected.

package javaprac;

import javaprac.collection.*;

public class ClassC extends ClassA {

public void C_Z()

{

//A_X(); Not accessible

//A_Y(); Not accessible

A_Z();

}

void displayTemp_C()

{

System.out.println(“The Value of temp is ” + temp1);

//temp2 is not accessible

}

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: