Is it possible to limit inheritance to a package without using the "default"?

I have some classes that I need to be able to propagate in a single package. But I do not want anyone in my package to extend my classes. Classes in other packages should be able to call my classes, so I cannot use "default".

Is there any way (possibly via an interface) that I can achieve?

+7
source share
3 answers

If you create a local package of constructors, it can be expanded only in the same package, however, access to public members can be obtained in any class, if it is an open class.

+7
source

I would do this by creating a base class with the default visibility, and then extend it with the public final class, which external classes can call but not extend. For example:

 class MyBase { public void doSomething() { ... } } public final class PublicBase extends MyBase { } class ExtendedBase { @Override public void doSomething() { ... } } 
+6
source

You can create an interface to use the class and the factory class, which will return specific implementations, both of which will be publicly accessible and therefore visible outside the package. Then create your specific classes with default visibility.

0
source

All Articles