Javac · Java Tutorials - Default, aka Package-Private Access Applied to a Class

Default, aka Package-Private Access Applied to a Class

When no access modifier exists in the declaration statement of a standard outer class, the class is implicitly assigned default aka package-private access. With default access the following is granted:

  • Full access is granted to inherit the class from within the same package.
  • Full access is granted to create a new reference to an instance from within the same package.

It is very important to understand that default access does not set or change the access of the members and elements enclosed inside of the class.

Things to think about

  • If you do not explicitly create a constructor for the class, a default constructor will be implicitly created with default access.
  • Members or elements of a default access class can be assigned public access. While this is counter-intuitive, it is possible for a public subclass to expose the public members of a default access superclass. Thereby exposing superclass members to non-package access.


Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md one
C:\Java>cd one
C:\Java\one>Notepad Tester.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


package one;

import two.*;

//class Tester extends Insider { // Inheritance in same package
//class Tester extends OutSider { // Inheritance from different package - FAIL
class Tester {

    public static void main(String args[]) {
        // Reference in same package
        // Insider i = new Insider();
        // i.displayMessage();

        // Inheritance in same package or different package
        // Tester ot = new Tester();
        // ot.displayMessage();
    
        // Reference in different package
        // Outsider o = new Outsider(); // FAIL

        // Reference in different package
        // Oops o = new Oops(); 
        // o.displayMessage();
    }

}

class Insider {
    void displayMessage() {
        System.out.println("I am an insider in package ONE!");
    }    
}

Now switch back to the command prompt (CMD) and type in the following commands.


C:\Java\one>cd ..
C:\Java>md two
C:\Java>cd two
C:\Java\two>notepad Oops.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


package two;

class Outsider {
    public void displayMessage() {
        System.out.println("I am an outsider in package TWO - good luck getting to me!");
    }    
}

public class Oops extends Outsider { 
    // nothing to see here but the public displayMessage() method I just inherited :0
}

Now switch back to the command prompt (CMD) and type in the following commands.


C:\Java\one>cd ..
C:\Java>javac one\Tester.java
C:\Java>java one.Tester // play around with uncommenting and comment various statements.
Varying results


Final thoughts

It is very important to understand that default access does not set or change the access of the members and elements enclosed inside of the class.


Tutorials