Is it possible to refine sealed classes with "sealed methods"?

432 views Asked by At

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?

1

There are 1 answers

4
VGR On

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:

package com.example.units;

public abstract sealed class Unit permits Imperial, Metric {
    public final String kind () {
        return kindImpl();
    }

    String kindImpl() { return "easy"; }
}

package com.example.units;

public abstract class Imperial extends Unit {
    @Override
    String kindImpl() { return "imperial"; }
}

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.