vendredi 7 juin 2019

Prevent dependency on a class in Java / Kotlin, but allow extending

I wish to create a useful base Java class with a few protected methods and hooks so subclasses can be easily implemented.

However, I wish this class to ONLY be available for deriving a subclass, but not available as a dependency. The reason is to prevent some junior/careless developers from coupling their code to this base class.

For example, if my base class is called BaseActivity.java, anyone can create their own

public class MyNewActivity extends BaseActivity

But no one can refer directly to BaseActivity with a field or method signature, for example this should not be allowed:

public void doSomethingOnBaseActivity(BaseActivity activity);

private BaseActivity someField;

public BaseActivity getActivity();

Is there any way to accomplish such a restriction in Java? Maybe in Kotlin this would be possible?

Aucun commentaire:

Enregistrer un commentaire