Try calling finalize:
new Object().finalize();
It won't compile: subclasses only have access to protected methods on other objects of the same type.
If equals, toString, wait and notify were protected, we could not access them freely.
In JLS terms...
Let C be the class in which a protected member is declared. Access is permitted only within the body of a subclass S of C.
In addition, if Id denotes an instance field or instance method, then:
- If the access is by a field access expression
E.Id, or a method invocation expression E.Id(...), or a method reference expression E::Id, where E is a Primary expression, then the access is permitted if and only if the type of E is S or a subclass of S.
In simpler terms, protected members are accessible on...
...itself:
package ex1;
public class Example1 {
protected void m() {}
}
package ex2;
import ex1.Example1;
public class Example2 extends Example1 {{
m(); // m is accessible
}}
...other instances of the same type (or a subclass of it):
package ex3;
import ex1.Example1;
public class Example3 extends Example1 {{
new Example3().m(); // m is accessible
new Example1().m(); // m is NOT accessible
}}