Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

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

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?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!