Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

396
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda