Saturday 14 January 2012

Access Modifiers In Java

There are four access modifiers to help you set the level of access you want for classes as well as the fields, methods and constructors in your classes. A member has package or default accessibility when no access modifier is specified.
Access Modifiers
1. private
2. protected
3. default (friendly or package-private)
4. public
Usage of these access modifiers is restricted to two levels. The two levels are class level access modifiers and member level access modifiers.

Class level access modifiers (java classes only)


It is used to control the visibility of classes between packages.We can use the following access modifiers for class level access.
  • default (friendly or package-private)
  • public
A class may be declared with the modifier public, in which case that class is visible to all classes everywhere. If a class has no modifier (the default, also known as package-private), it is visible only within its own package (packages are named groups of related classes).
Example:
class with default access modifier.

package p1;
class A{
public void display(){
System.out.println(“default -class level access”);
}
}
class B{
public static void main(String args[]){
A obj=new A(); //legal because class A is visible within its package.
obj.display();
}
}
//---------------------------------End of Package p1------------------------------------
package p2;
import p1.*;
class C{
public static void main(String args[]){
A obj=new A();//Illegal ,because class A is visible only with in package p1.
obj.display();
}
}
//---------------------------------End of Package p2------------------------------------
 
Class with 'public' access modifier.

package p1;
public class A{
public void display(){
System.out.println(“public -class level access”);
}
}
class B{
public static void main(String args[]){
A obj=new A(); //legal because class A is visible within its package.
obj.display();
}
}
//---------------------------------End of Package p1------------------------------------
package p2;
import p1.*;
class C{
public static void main(String args[]){
A obj=new A();//legal ,because class A is visible outside its package p1.
obj.display();
}
}
//---------------------------------End of Package p2------------------------------------
 
Member level access modifiers (java variables and java methods)

 

It is used to control the visibility of variables and java methods between classes (with in the same or different packages).
We can use the following access modifiers for it.
  • Private -Accessible only within the declared class.
  • protected -CAN be accessed from ‘same package’ and a subclass existing in any  package can access.
  • default (friendly /package-private) -CAN be accessed from ‘same package’.
  • public -CAN be accessed from anywhere. 
Example :

package p1;
public class A{
private int w;
int x;
protected int y;
public int z;
public void display(){
System.out.println(“w=”+w+”x=”+x+”y=”+y+”z=”+z);
}
}
class B{
public static void main(String args[]){
A obj=new A();
S.o.p(“w=”+obj.w);// illegal ,private can be accessed only within declared class.
S.o.p(“x=”+obj.x);//legal ,default can be accessed within its package.
S.o.p(“y=”+obj.y);/legal,protected can be accessed within its package.
S.o.p(“z=”+obj.z);//legal ,public can be accessed anywhere.
}
}
//---------------------------------End of Package p1------------------------------------
package p2;
import p1.A;
class C{
public static void main(String args[]){
A obj=new A();
S.o.p(“w=”+obj.w);// illegal ,private can be accessed only within declared class.
S.o.p(“x=”+obj.x);//illegal ,default can be accessed only within its package.
S.o.p(“y=”+obj.y);//illegal,protected can be accessed only within its package &
                                                    //sub class of other packages.
S.o.p(“z=”+obj.z);//legal ,public can be accessed anywhere.
}
}
class D extends A{
public static void main(String args[]){
A obj=new A();
S.o.p(“w=”+obj.w);// illegal ,private can be accessed only within declared class.
S.o.p(“x=”+obj.x);//illegal ,default can be accessed only within its package.
S.o.p(“y=”+obj.y);//illegal,protected can be accessed only within its package &
                                                    // sub class of other packages.
S.o.p(“z=”+obj.z);//legal ,public can be accessed anywhere.
D obj1=new D();
S.o.p(“w=”+obj.w);// illegal ,private can be accessed only within declared class.
S.o.p(“x=”+obj.x);//illegal ,default can be accessed only within its package.
S.o.p(“y=”+obj.y);//legal,protected can be accessed within subclass of other 
                                                    //packages.
S.o.p(“z=”+obj.z);//legal ,public can be accessed anywhere.
}
}
//---------------------------------End of Package p2------------------------------------

I think you are a little confused about 'protected' access specifier.Don't worry we will discuss it in detail.
Protected members can be accessed outside its package only through inheritance. i.e. You can't use parent class object with '.' operator to access it.

Example :
package p1;
public Class Parent{
protected int x;
}
//---------------------------------End of Package p1------------------------------------
package p2;
import p1.*;
class Child extends Parent{
public display(){
System.out.println(x);//Legal,getting protected 'x' through inheritance.
Parent p=new Parent();
System.out.println(p.x);//Illegal,'x' can be accessed only through inheritance.
}
}
//---------------------------------End of Package p2------------------------------------

But there's still one more issue we haven't looked at...what does a protected member look like to other classes trying to use the subclass-outside-the-package to get to the subclass' inherited protected superclass member? For example, using our previous Parent/Child classes, what happens if some other class—Neighbor, say—in the same package as the Child (subclass), has a reference to a Child instance and wants to access the member variable x ? In other words, how does that protected member behave once the subclass has inherited it? Does it maintain its protected status, such that classes in the Child's package can see it? .No! Once the subclass-outside-the-package inherits the protected member,that member (as inherited by the subclass) becomes private to any code outside the subclass, with the exception of subclasses of the subclass. So if class Neighbor instantiates a Child object, then even if class Neighbor is in the same package as class Child, class Neighbor won't have access to the Child's inherited (but protected) variable x.

package p2;
import p1.*;
class Neighbor extends Child{
{
public void display(){
System.out.println(x);//Legal,getting protected 'x' through inheritance.
}
public static void main(String args[]){
Child obj=new Child();
System.out.println(obj.x);//Illegal,'x' can be accessed only through inheritance.
}
}

Summary
There are two layers of data security in Java. i.e. Class level(public and default) and member level(private,default ,protected and public).Net visibility of a member outside its package depends on both of it.
Class Level Visibility




Public Default
Same package Yes Yes
Different Package Yes No

Member Level Visibility


Private
default
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

Net Visibility outside of the current package.

Class Access Level Member Access Level Net Access Level outside its package
public private private
public default default
public protected protected
public public public
default private private
default default default
default protected default
default public default



1 comment: