I am playing with the preview sealed classes in java 15, and I was wondering why the keywords sealed
and non-sealed
only apply to classes and interfaces, but not methods (like other modifiers do). I imagine it might be useful to decide specifically, which of the methods can be overridden by permitted subclasses.
An Example: I have a class Unit
that has two subclasses Metric
and Imperial
, which both finally implement a basic functionality, here kind()
.
public abstract sealed class Unit permits Imperial, Metric {
public abstract String kind ();
}
public abstract non-sealed class Imperial extends Unit {
@Override
public final String kind() { return "imperial"; }
}
public abstract non-sealed class Metric extends Unit {
@Override
public final String kind() { return "metric"; }
}
This works. However, now I don't want to implement kind()
in all subclasses, but provide an implementation that is final to all subclasses except the ones where overriding is permitted. In my mind this would look like this:
public abstract sealed class Unit permits Imperial, Metric {
// this is not supported
public sealed String kind () permits Imperial {return "easy"; }
}
public abstract non-sealed class Imperial extends Unit {
@Override
public final String kind() { return "oh my god"; }
}
public abstract non-sealed class Metric extends Unit {
// should not be possible to override kind() here or in any subclass
}
Is there any way that I could achieve this with the new features or is there any other way I am missing?
You can do this with package-access classes.
Place all three classes in their own package, and delegate
kind()
to a package-private method—that is, a method which is neither public nor protected nor private. This will allow only classes in that package to override it:kindImpl()
is package-private, so only classes in the same package are able to override it.This works in any version of Java, regardless of whether sealed classes are in use.