Explaining Access Modifiers with Example Code

We have discussed the theory about access modifiers in the previous note. This note will give you example code that will show some of the valid and invalid usages for members belonging to different access levels from same class, subclass in same package, subclass in another package and a non-subclass in another package.  

You may want to revise object oriented programming from http://javajee.com/beginning-object-oriented-programming-in-java, for understanding certain things within this note.

 

Accessing Members

There are two different ways through which we can access members of another class: through an object reference (normal usage) or through inheritance. Static members can be accessed also using the type (class) name.

Accessing through an object reference

You can create an object of the class and then access the members through the reference variable as:

MyParent p= new MyParent();  

System.out.println(p.myVar); 

Accessing through inheritance

Child class inherits the members of the parent class. The inherited members can then accessed just like any other member of the child class.

For instance if  myVar is an inherited variable, then I can access it within my class as:

System.out.println(myVar);

or create an object of the child class and access it as:

MyChild c = new MyChild(); 

System.out.println(c.myVar);

 

Example Code with Scenarios

Commented lines in red are not legal.

Base class: MyParent.java in package1

package package1;

 

public class MyParent {

  private int myPrivateVar = 1;

  int myDefaultVar = 2;

  protected int myProtectedVar = 3;

  public int myPublicVar = 4;

}

 

 

Case 1: Subclass in the same package

package package1;

 

public class MyChild extends MyParent {

 

  //Accessing through inheritance

  public void printInheritedVariables() {

    System.out.println(myPublicVar);

    System.out.println(myProtectedVar);

    System.out.println(myDefaultVar);

    // System.out.println(myPrivateVariable);

  }

 

  public static void main(String[] args) {

 

    MyParent p = new MyParent();

    //Accessing through object reference

    System.out.println(

          p.myPublicVar + "," 

        + p.myProtectedVar + ","

        + p.myDefaultVar

    // +","+p.myPrivateVariable

        );

 

    //Accessing inherited vars 

    //through child reference

    MyChild c = new MyChild();

    System.out.println(

          c.myPublicVar + "," 

        + c.myProtectedVar + ","

        + c.myDefaultVar

    // +","+c.myPrivateVariable

        );

  }

}

 

Case 2: Subclass in another package.

package package2;

 

import package1.MyParent;

 

public class MyChildAnother extends MyParent {

 

  // Accessing through inheritance

  public void printInheritedVariables() {

    System.out.println(myPublicVar);

    System.out.println(myProtectedVar);

    // System.out.println(myDefaultvariable);

    // System.out.println(myPrivateVariable);

  }

 

  public static void main(String[] args) {

 

    // Accessing through object reference

    MyParent p = new MyParent();

    System.out.println(

        p.myPublicVar

    // +","+p.myProtectedVar // work for static

    // +","+p.myDefaultVar

    // +","+p.myPrivateVar

        );

 

  // Accessing through object reference

    //Parent reference, child object

    MyParent p1 = new MyChildAnother();

    System.out.println(

        p1.myPublicVar

     //+","+p.myProtectedVar // work for static

    // +","+p.myDefaultVar

    // +","+p.myPrivateVar

        );

 

    //Accessing inherited vars 

    //through child reference

    MyChildAnother c = new MyChildAnother();

    System.out.println(c.myPublicVar + "," 

        + c.myProtectedVar

    // +","+c.myDefaultVar

    // +","+c..myPrivateVar

        );

  }

 

}

Inherited protected members in the child class cannot be accessed using as parent reference (even if referring to child object). 

Static protected members can be accessed in subclasses through:

  • class name

  • object reference (Parent or Child)

 

Case 3: Non subclass in another package

package package2;

 

import package1.MyParent;

 

class AnotherClass {

  public static void main(String[] args) {

 

    MyParent p = new MyParent();

    System.out.println(p.myPublicVar

    // +","+p.myProtectedVar

    // +","+p.myDefaultVar

    // +","+p.myPrivateVar

        );

 

    MyChildAnother c = new MyChildAnother();

    System.out.println(c.myPublicVar

    // +","+c.myProtectedVar

    // +","+c.myDefaultVar

    // +","+c.myPrivateVar

        );

 

  }

}

Protected members cannot be accessed from a non-subclass in another package. 

 

Summary

  1. Inherited protected members in the child class cannot be accessed using as parent reference (even if referring to child object). 

  2. Static protected members can be accessed in subclasses through:

    • class name

    • object reference (Parent or Child)

  3. Protected members cannot be accessed from a non-subclass in another package. 

Search the Web

Custom Search

Searches whole web. Use the search in the right sidebar to search only within javajee.com!!!