I am not sure why in a context like the following
class Parent {
private void method1() {
System.out.println("Parent's method1()");
}
public void method2() {
System.out.println("Parent's method2()");
}
}
public class Child extends Parent {
public void method1() {
System.out.println("Child's method1()");
}
public static void main(String args[]) {
Parent p = new Child();
p.method2();
}
}
Parent may be package private, but not protected.
Specifically the error generated is
modifier protected not allowed here
Which would seem to indicate that it is an issue of access privilege - but I am suspicious that this may be a red herring. Private
modifier also generates an error (naturally).
In Java, Top level classes can have only package private and public modifiers.
Making a class private
doesn't make any sense. If no one use that class for any reason, then why we need that class?
protected
access modifier means, only visible within the enclosing class and any subclasses, and also anywhere in the enclosing class's package. No body knows which classes going to be subclass of a protected class. So, it also doesn't make sense
But both private and protected can be (and frequently are) applied to nested classes and interfaces, just never top-level classes and interfaces