The key thing here is which package the classes belongs to.
This is explained in JLS paragraph 6.6.2:
6.6.2 Details on protected Access
A protected member or constructor of an object may be accessed from outside the package in which it is declared only by code that is responsible for the implementation of that object.
Examples:
This does not compile:
FILE pkg1/A.java (corresponds to the Object class in your question)
package pkg1;
public class A {
protected void method() {};
}
FILE pkg2/B.java (corresponds to storeDate in your question)
package pkg2;
import pkg1.A;
public class B extends A {
public static void main(String args[]) {
new A().method();
}
}
javac
outputs the following:
pkg2/B.java:5: method() has protected access in pkg1.A
new A().method();
^
(which is similar to what you have: clone() has protected access in java.lang.Object kkk.clone();)
Simply moving B
to the pkg1
package solves it.
That is, this does compile:
FILE pkg1/A.java (unchanged)
package pkg1;
public class A {
protected void method() {};
}
FILE pkg1/B.java (moved from pkg2 to pkg1)
package pkg1; // Changed from pkg2
//import pkg1.A; // Not necessary anymore.
public class B extends A {
public static void main(String args[]) {
new A().method();
}
}
So, what would have been required for you to be able to do something like new Object().clone()
? Well, you would have to belong to the java.lang
package (which in turn, however results in a SecurityException: Prohibited package name: java.lang
).