You've seen public (anyone can access) and private (only the same class). The protected modifier sits between them. A protected member is accessible to the class itself, any child class, and any class in the same package.
public class Animal {
protected String name;
protected void sleep() {
System.out.println(name + " is sleeping");
}
}
public class Dog extends Animal {
void rest() {
sleep();
System.out.println(name + " is resting");
}
}
Dog can access name and sleep() because it extends Animal. An unrelated class in a different package cannot.
Use protected when you want child classes to access a member but want to keep it hidden from the rest of your code. If you make everything public, any class can depend on those internals, making your code harder to change later.