Consider the following protected
class containing a public
method.
package my.package;
class MyClass {
public void myMethod() {
// myMethod implementation
}
}
Since MyClass is not marked as public
, it therefore has protected
access and thus is not accessible outside the package. However, its method myMethod
is marked as public
, signifying that it is accessible outside the class.
Since myMethod
presumably cannot be accessed without accessing MyClass
, why is it possible to mark myMethod
as public? Is it because someone might extend MyClass
from within the package, with a custom public
class for use outside the package? For example:
package my.package;
public MyPublicClass extends MyClass {
// MyPublicClass implementation
}
If so, is there any other use for a public
method defined within a protected
class?