Friday, February 27, 2015

Access Modifiers


Use the code below to practice the access modifiers effects on fields and methods.  Save this code  as ClassA.

Copy HelperA code three times and rename two of the copies to HelperB and HelperC. ( don't forget to adjust the name of the constructors )
Add package package1; to all three newly created classes.

Make two more classes HelperD and HelperE by copying this code again and adding package package2; to the beginning of the classes.

That way, you will have three classes in one package and two classes in another package, so you can "play" with access to fields and methods.

Try to compile the classes and see the effects.  As you are getting error messages, just comment out the statement that causes the error message.  Make a note of the error message, comment no access, and continue until you have no more compile errors. 

As you are making modification to classes, keep track of the errors and verify this table's values with fields and methods.  You can also extend this table with other test values if needed.


public class ClassA {
   
    private int value;
    public static int static_ident;
    public static final String  CONSTANT= "STATIC CONSTANT STRING";

    public ClassA(int value ){  value=value; static_ident=2015; }  //value is a shadow, avoid it

    public int getValue(){return value;}
   
    public static int getStatValue(){return static_ident;}   //can not use this for static
   
    public static void main(String args[]) {

        ClassA me=new ClassA(20);
        String myName=new String("Richland College");
       
        System.out.println("Hello My Non-Static Value!!!"+me.getValue());
        System.out.println("Hello My Static Method!!!"+me.getStatValue());
        System.out.println("Hello Constant Field!!!"+CONSTANT);   //can not refer to it as this.CONSTANT
       
        HelperA helper=new HelperA();
       
        //System.out.println("Hello Helper Private Method!!!"+helper.getPriName());  //no access
        System.out.println("Hello Helper Public Method!!!"+helper.getPubName());
        System.out.println("Hello Helper No Method!!!"+helper.getNoName());
        System.out.println("Hello Helper Protected Method!!!"+helper.getProName());
       
        //System.out.println("Hello Helper Private Field!!!"+helper.pri_name);  //no access
        System.out.println("Hello Helper Public Field!!!"+helper.pro_name);
        System.out.println("Hello Helper No Field!!!"+helper.no_name);
        System.out.println("Hello Helper Protected Field!!!"+helper.pub_name);
    }

}

class HelperA{
    private String pri_name;
    public String pub_name;
    String no_name;
    protected String pro_name;
   
    public HelperA(){
       pri_name="pri_Anonymous";
       pro_name="pro_Anonymous";
       no_name="no_Anonymous";
       pub_name="pub_Anonymous";
    }
   
    private String getPriName(){ return pri_name; }
   
    public String getPubName(){ return pub_name; }
   
    String getNoName(){  return no_name;  }
   
    protected String getProName(){ return pro_name; }
}

No comments:

Post a Comment