I have a stateless abstract base class that should not be inherited from outside of its package:
package foo;
public abstract class Foo
{
// some abstract methods
// one concrete method
// no state
// Prevent classes outside of package foo from inheriting
Foo()
{
}
}
Now that Java 8 supports default methods in interfaces, I would like to convert the abstract class to an interface. With interfaces, is it also possible to prevent inheritance outside of the current package?
From the java tutorial:
.. All abstract, default, and static methods in an interface are implicitly public, so you can omit the public modifier.
It means that you can't unless you restrict the interface visibility to package. But i guess that you can't.
package foo;
interface Foo
{
}
I suppose that you could write a custom annotation (somelink like @InstanciableOnlyInMyPackage
) and put it on the interface. And then using raise a compiler error using Annotation Processing Tool.