October 9, 2011

Access Modifiers for Class members in Java

There are 3 access modifiers for class members: public, private, protected
and 4 access levels: public, private, protected, default (none of the modifiers present)

public access level
A public member (variable or method) can be accessed from another class regardless of the package in which the other class is present.
public members can be inherited by a subclass regardless of the package in which the subclass is present.

package info.icontraining.p1;
public class Test {
   public int i;           // public member variable
}

package info.icontraining.p2;
import info.icontraining.p1.Test;
public class A {
   public void m1() {
      Test t = new Test();
      t.i = 3;       // public members can be accessed 
                     // by code in another class that is
   }                 // present in a different package 
}

package info.icontraining.p2;

import info.icontraining.p1.Test;
public class B extends Test {
   public void m1() {
      i = 3;         // public members can be inherited
   }                 // by another class that is present
}                    // in a different package


private access level
A private member cannot be accessed by code in any class except by code in the class in which the private member is present.
private members cannot be inherited by any subclass.

package info.icontraining.p1;
public class Test {
   private int i;           // private member variable
   public void m1() {
      i = 3;           // private members can be accessed
   }                   // only by code within the class
}                      // in which it is declared

package info.icontraining.p1;
public class A {
   public void method1() {
      Test t = new Test();
      t.i = 3;           // this line will not compile
   }
}

package info.icontraining.p1;
public class B extends Test {
   public void method1() {
      i = 3;            // this line will not compile
   }
}

default access level
A default member can be accessed from another class if both classes are present in the same package.
default members can be inherited by a subclass is both classes are present in the same package.

package info.icontraining.p1;
public class Test {
   int i;              // default member variable
}

package info.icontraining.p1;
public class A {
   public void m1() {
      Test t = new Test();
      t.i = 3;         // can be accessed by a class
   }                   // in same package
}

package info.icontraining.p1;
public class B extends Test {
   public void m2 {
      i = 3;       // can be inherited by a class
   }               // in same package
}

package info.icontraining.p2;
import info.icontraining.p1.Test;
public class C {
   public void m1() {
      Test t = new Test();
      t.i = 3;         // will not compile, cannot be 
   }                   // accessed in another package
}

package info.icontraining.p2;
import info.icontraining.p1.Test;
public class D extends Test {
   public void m2 {
      i = 3;       // will not compile, cannot be
   }               // inherited in another package
}

protected access level
A protected member can be accessed by another class in the same package only.
However, protected members can be inherited by a subclass present in a different package also.


package info.icontraining.p1;
public class Test {
   protected int i;              // protected member variable
}

package info.icontraining.p1;
public class A {
   public void m1() {
      Test t = new Test();
      t.i = 3;         // can be accessed by a class
   }                   // in same package
}

package info.icontraining.p1;
public class B extends Test {
   public void m2 {
      i = 3;       // can be inherited by a class
   }               // in same package
}

package info.icontraining.p2;
import info.icontraining.p1.Test;
public class C {
   public void m1() {
      Test t = new Test();
      t.i = 3;         // will not compile, cannot be
   }                   // accessed in another package
}

package info.icontraining.p2;
import info.icontraining.p1.Test;
public class D extends Test {
   public void m2 {
      i = 3;       // can be inherited by a class
   }               // in another package
}

No comments:

Post a Comment