Java 15: Sealed Classes, Hidden Classes, and garbage collector updates

Refining JVM inheritance. We explore sealed class directives, hidden classes, and G1 GC performance.

VP
SHIVAM ITCS
·17 September 2020·10 min read·1 views

Technical Overview & Strategic Context

While Java 11 stabilized modular long-term releases, Java 15, released in late 2020, addressed class hierarchy management: class inheritance was unrestricted by default, allowing public classes to be extended by any code. Java 15 resolved this by introducing a preview of Sealed Classes (JEP 360), which allow class declarations to specify exactly which subclasses can inherit from them, improving encapsulation.

Architectural Principle: Use sealed classes to manage class hierarchies. Declaring permitted subclasses explicitly protects internal implementations from unwanted extension.

Core Concepts & Architectural Blueprint

Sealed classes use the sealed and permits keywords. The compiler verifies that only listed subclasses extend the sealed class, preventing developer overrides. This release also introduced Hidden Classes, which are loaded dynamically to execute code safely without naming conflicts.

Performance & Capability Comparison

OOP ParadigmClassic Java ClassJava 15 Sealed ClassDeveloper Benefit
InheritanceCan be extended by any subclassExtended only by permitted subclassesProtects API class designs
Class LoadingStandard JVM class path loaderHidden Classes loaded dynamicallyPrevents runtime naming conflicts
Garbage CollectorZGC and Shenandoah experimentalZGC and Shenandoah production-readyReduces GC pause times significantly

Implementation & Code Pattern

To write sealed class hierarchies in Java 15, developers should follow these guidelines:

  • Enable preview features in compiler options to use sealed classes.
  • Use the sealed modifier on class declarations to restrict inheritance.
  • Declare permitted subclasses using permits statements.
  • Verify that permitted subclasses are declared final, sealed, or non-sealed.
javacode
// Java 15 Sealed Class hierarchy preview (2020)
package in.shivamitcs.portal.model;

// Declare sealed class and specify permitted subclasses
public abstract sealed class UserRecord permits StudentProfile, StaffProfile {
    public abstract String getRole();
}

// Permitted subclass must be declared final, sealed, or non-sealed
public final class StudentProfile extends UserRecord {
    @Override
    public String getRole() { return "STUDENT"; }
}

public final class StaffProfile extends UserRecord {
    @Override
    public String getRole() { return "STAFF"; }
}

// Compiler blocks subclassing below:
// public class GuestProfile extends UserRecord {} // Compiler error!

Operational Governance & Future Outlook

Java 15's introduction of sealed classes and hidden classes simplified class hierarchy management. Adding production-ready garbage collectors like ZGC helps optimize heap performance on server nodes.

VP
Vijay Paliwal
Founder, SHIVAM ITCS · 18+ years enterprise & AI engineering
MCA · Ex-HiveGPT USA · Ex-Social27 Seattle
Java 15: Sealed Classes, Hidden Classes, and garbage collector updates | SHIVAM ITCS Blog | SHIVAM ITCS