22 January 2026

In 2009, Final Interfaces were a concept, and many years later Java got Sealed Types

When working on submissions for Projects coin - Controlled Inheritance was one element that I wanted to have.
Sadly, we where told to not push forward any heavy changes into Java - this way idea of limiting inheritance was reduced to making inheritance scope of interfaces final.
Then it turned out that there was 0% chance that any change requiring extended consulations would make it - basically already prepared/aproved projects will be selected.
I have to admit that final is not the best word for this purpose and sealed is definetelly better pick.
I still have my draft of the: Final interfaces... proposal so it can be compared:
interface final Interface allow ClassI
vs
sealed interface Service permits Car, Truck

Earlier, consideration across language was between final and scope limitation - not really secure enough solution for me.
I'm really curious whether Java’s sealed types are a direct evolution of the ideas behind "final interfaces", or if they emerged independently.



WORKAROUNDS
If you still use older Java, this is most simple runtime workaround:
public abstract class AccessCore{ protected AccessCore(){ if (this instanceof FirstAllowedClass){ ... } else if (this instanceof SecondAllowedClass){ ... } else{ throw new RuntimException­(this.getClass().getSimpleName()+" was not supposed to extend "­+AccessCore.class.getSimpleName()); } } }
If you want to have access protocol start with:
public abstract class AccessCore{ private static final AccessKey accessKey; static { accessKey = new AccessKey(); Conditions.registerAccessKey(accessKey); try { { final Class<?> forName = Class.forName( "core.data.Tests4Conditions" ); final Method method = forName.getMethod("registerAccessKey", AccessKey.class); method.invoke(null, accessKey); } } catch (ClassNotFoundException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) { } protected AccessCore(AccessKey accessKey) { if (AccessCore.accessKey != accessKey) {// throw new IllegalArgumentException­(AccessKey.class.getSimpleName() + ": " + accessKey); } } }

0 comments / komentarz(y):

Post a Comment