Difference between protected and default/package access in Java

Updated:

Summary of the difference between protected and default access:

  • the protected access has a modifier (keyword): protected
  • the default access doesn't have any modifier.
  • the default access doesn't allow access outside the package

Protected vs default access control

Here a schema about the difference between the 2 access controls.

Red regions are not accessibles, green regions are accessible.

Differences between the protected and the default/package-private access.

Main difference between protected and default access modifiers

The main difference is that protected members can be accessed within the same package, as well as by subclasses of the same package.
Default members can only be accessed within the same package.

Here's a brief explanation of each access modifier:

protected: A protected member can be accessed within the same package, as well as by subclasses of the same package, even if they are in a different package from the superclass.
BUT it cannot be accessed by classes that are not in the same package and are not subclasses of the same package.

default: A default member (also known as package-private) can only be accessed within the same package. It cannot be accessed by classes that are not in the same package, even if they are subclasses of the class.

Code example

package com.example; 
 
// Parent class with protected and default members 
public class Parent { 
    protected int protectedVar = 42; 
    int defaultVar = 24; 
} 
 
// Subclass of Parent in the same package 
public class ChildSamePackage extends Parent { 
    public void accessParentMembers() { 
        // Accessing protected and default members of Parent 
        System.out.println(protectedVar); // legal 
        System.out.println(defaultVar); // legal 
    } 
} 
 
// Subclass of Parent in a different package 
package com.anotherexample; 
 
public class ChildDiffPackage extends Parent { 
    public void accessParentMembers() { 
        // Accessing protected member of Parent 
        System.out.println(protectedVar); // legal 
        // Accessing default member of Parent (not legal) 
        System.out.println(defaultVar); // compile error 
    } 
} 

In this example, we have a Parent class with both protected and default members.

We also have two subclasses of Parent: ChildSamePackage in the same package as Parent, and ChildDiffPackage in a different package.

In ChildSamePackage, we can access both protectedVar and defaultVar because it is in the same package as Parent.

In ChildDiffPackage, we can access protectedVar because it is a protected member, which allows subclasses to access it even if they are in a different package.

However, we cannot access defaultVar because it is a default member, which can only be accessed within the same package.

The complete details (Java Language Specification 17)

JLS: Access Control

JLS:protected


WebApp built by Marco using SpringBoot 3.2.4 and Java 21, in a Server in Switzerland