Bright Java Tutorial

is brought to you by DoITBright LLC

Java

Access Modifiers

To be able to limit the access of other classes to the fields and methods of a class, access modifiers are used as part of the declaration. Here are the four access modifiers that are available in Java and their accessibility.
Modifier Within Class Within Package Within Subclass Everywhere
public Yes Yes Yes Yes
protected Yes Yes Yes No
default (no modifier) Yes Yes No No
private Yes No No No

Let us look at the example codes below.

  package com.example.core.accessmod;

  /**
   * A class to demonstrate the access modifiers.
   * 
   * @author Rolan Liwanag
   *
   */
  public class Parent {

    /*
     * A private variable or method can only be accessed 
     * within the declaring class.
     */
    private String aPrivateAttribute;
    /*
     * A variable or method declared as default access 
     * modifier, this means not explicitly declaring the 
     * access modifier, is available to any other class
     * in the same package. In this case, all classed that 
     * belong to com.example.core.accessmod will have 
     * access to aDefaultAttribute.
     */
	String aDefaultAttribute;
	/*
	 * A protected variable or method can only be accessed 
	 * by any class within the same package or any class 
	 * in other package that extends the class where the 
	 * variable or method is declared.
	 */
	protected String aProtectedAttribute;
	/*
	 * A public variable or method is accessible to all.
	 */
	public String aPublicAttribute;
	
	public static void main(String[] args) {
		Parent obj = new Parent();
		obj.aPrivateAttribute = "private";
		obj.aDefaultAttribute = "default";
		obj.aProtectedAttribute = "protected";
		obj.aPublicAttribute = "public";
		/*
		 * as you may have seen, all variables are 
		 * accessible here since all of these variables 
		 * are declared in the same class.
		 */
    }
  }
                            

There is nothing unusual and complicated in the 'Parent' class. All its fields are accessible within the class itself no matter what access modifier each may have. It will also apply to any method declared within the class itself.
Within the same package, let us extend the 'Parent' class.
  package com.example.core.accessmod;

  /**
   * @author Rolan Liwanag
   */
  public class Child extends Parent{

    public static void main(String[] args) {
      Child obj = new Child();
      obj.aDefaultAttribute = "Parent Default Attribute";
      obj.aProtectedAttribute = "Parent Protected " 
        + "Attribute";
      obj.aPublicAttribute = "Parent Public Attribute";
      /*
       * As you may have seen, the private attribute of 
       * the Parent is no longer available here.
       */
    }
  }
                            

Since 'Child' is a subclass or child class of 'Parent', all of the 'Parent' attributes except for those declared as private are accessible within the 'Child'. This same concept applies to methods.
Within the same package, create a class that does not have any relationship with 'Parent' nor 'Child'. Let us look at the example code as shown below.
  package com.example.core.accessmod;

  /**
   * @author Rolan Liwanag
   */
  public class NoRelationship {

    public static void main(String[] args) {
      NoRelationship noRel = new NoRelationship();
      /*
       * There are no inherited methods or attributes in 
       * this class.
       */
      Parent parentObj = new Parent();
      parentObj.aDefaultAttribute = "Default is " 
        + "accessible";
      parentObj.aProtectedAttribute = "Protected is " 
        + "accessible";
      parentObj.aPublicAttribute = "Public is accessible";
      /*
       * As you may have seen, We can definitely create an 
       * instance of Parent. We can also access the 
       * variables declared as default, protected or 
       * public. However, private is not accessible.
       * Remember, private can only be accessed inside the 
       * declaring class.
       */
    }
  }
                            

The instance of 'NoRelationship' wont inherit any method nor attributes from 'Parent' nor 'Child'. Here is something to take note of. There is nothing that stop us from creating an instance of 'Parent' or 'Child' inside the 'NoRelationship' class. Both 'Parent' and 'Child' classes are declared as public. The above example also illustrates that the instance of 'Parent' will only expose its methods and properties according to each access modifier.
Let us create a class in a different package. However, this class will be extending the 'Parent' class.
  package com.example.core.accessmod.anotherpkg;

  import com.example.core.accessmod.Parent;

  /**
   * @author Rolan Liwanag
   */
  public class OutsiderButExtends extends Parent {

    public static void main(String[] args) {
      OutsiderButExtends outsiderExtObj 
        = new OutsiderButExtends();
      outsiderExtObj.aProtectedAttribute 
        = "Protected is accessible via Parent " 
          + "subclass instance.";
      outsiderExtObj.aPublicAttribute 
        = "Public is accessible via Parent " 
          + "subclass instance.";
      /*
       * The Parent protected and public variables are 
       * inherited by OutsiderButExtends instance.
       */
      Parent parentObj = new Parent();
      parentObj.aPublicAttribute 
        = "Public is accessible via Parent instance";
      /*
       * Protected is no longer accessible here since 
       * parentObj is only an instance in 
       * OutsiderButExtends. Only public is accessible 
       * here.
       */
    }
  }
                            

Since 'OutsiderButExtends' is a subclass of 'Parent', it inherited the public and protected attributes of 'Parent'. The same will apply to methods. Why did the default attributes of parent was not inherited? It is because 'OutsiderButExtends' and 'Parent' are in a different packages.
The instance of 'Parent' class only exposed the public attribute. The reason is, the instance of parent is declared outside its own package.
Let us now create a class in the same package as 'OutsiderButExtends' but does not extend 'Parent' class.
  package com.example.core.accessmod.anotherpkg;

  import com.example.core.accessmod.Parent;

  /**
   * @author Rolan Liwanag
   */
  public class Outsider {

    public static void main(String[] args) {
      Outsider outsiderObj = new Outsider();
      /*
       * As you may have seen, none of the Parent 
       * attributes are accessible in the outsiderObj 
       * instance.
       */
      Parent parentObj = new Parent();
      //yes we can declare an instance of Parent by doing 
      //an import.
      parentObj.aPublicAttribute 
        = "Public is the only accessible here";
      /*
       * private can no longer be accessed here since it 
       * is not accessed inside the Parent class itself.
       * protected cannot be accessed here since Outsider 
       * class is outside the package 
       * com.example.core.accessmod and it is not 
       * extending the Parent class.
       * default cannot be accessed here since Outsider 
       * is outside the package com.example.core.accessmod.
       */
    }
  }
                            



Back    Next