What is protected in Java?

protected on a method or a field in Java means, that this method or a field can be accessed only by either a child class or another class in the same package.

So if this is your class with protected method:

package com.example;

public class ProtectedExample {

  protected void iAmProtected() {
    System.out.println("I am protected!");
  }
}

Then another class in com.example package can access it:

package com.example;

public class AnotherClass {

  protected void callExample() {
    var example = new com.example.ProtectedExample();
    // this will work!
    example.iAmProtected();
  }
}

And a child class in another package com.example2 can access it:

package com.example2;

public class AnotherClass extends com.example.ProtectedExample {

  public void callProtected() {
    // this will work!
    iAmProtected();
  }
}

But just any other class in com.example2 package cannot access it:

package com.example2;

public class AnotherClass {

  protected void callExample() {
    var example = new com.example.ProtectedExample();
    // this will NOT work!
    // Error: 'iAmProtected()' has protected access in 'com.example.ProtectedExample'
    example.iAmProtected();
  }
}