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. 

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) Arrays (1) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (55) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (5) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (9) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (11) Java Programmer 2 (7) Javascript Frameworks (1) Java SE Professional (1) JPA 1 - Module (6) JPA 1 - Modules (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)