Sealed Classes and Interfaces in Java 15

Sealed Classes and Interfaces in Java 15

Explore explored sealed classes and interfaces, a preview feature in Java SE 15. Sealing allows classes and interfaces to define their permitted subtypes. A class or an interface can now define which classes can implement or extend it. It is a useful feature for domain modeling and increasing the security of libraries.

1. Overview

The release of Java SE 15 introduces sealed classes (JEP 360) as a preview feature.

This feature is about enabling more fine-grained inheritance control in Java. Sealing allows classes and interfaces to define their permitted subtypes.

In other words, a class or an interface can now define which classes can implement or extend it. It is a useful feature for domain modeling and increasing the security of libraries.

2. Motivation

A class hierarchy enables us to reuse code via inheritance. However, the class hierarchy can also have other purposes. Code reuse is great but is not always our primary goal.

2.1. Modeling Possibilities

An alternative purpose of a class hierarchy can be to model various possibilities that exist in a domain.

As an example, imagine a business domain that only works with cars and trucks, not motorcycles. When creating the Vehicle abstract class in Java, we should be able to allow only Car and Truck classes to extend it. In that way, we want to ensure that there will be no misuse of the Vehicle abstract class within our domain.

In this example, we are more interested in the clarity of code handling known subclasses then defending against all unknown subclasses.

Before version 15, Java assumed that code reuse is always a goal. Every class was extendable by any number of subclasses.

2.2. The Package-Private Approach

In earlier versions, Java provided limited options in the area of inheritance control.

A final class can have no subclasses. A package-private class can only have subclasses in the same package.

Using the package-private approach, users cannot access the abstract class without also allowing them to extend it:

public class Vehicles {

    abstract static class Vehicle {

        private final String registrationNumber;

        public Vehicle(String registrationNumber) {
            this.registrationNumber = registrationNumber;
        }

        public String getRegistrationNumber() {
            return registrationNumber;
        }

    }

    public static final class Car extends Vehicle {

        private final int numberOfSeats;

        public Car(int numberOfSeats, String registrationNumber) {
            super(registrationNumber);
            this.numberOfSeats = numberOfSeats;
        }

        public int getNumberOfSeats() {
            return numberOfSeats;
        }

    }

    public static final class Truck extends Vehicle {

        private final int loadCapacity;

        public Truck(int loadCapacity, String registrationNumber) {
            super(registrationNumber);
            this.loadCapacity = loadCapacity;
        }

        public int getLoadCapacity() {
            return loadCapacity;
        }

    }

}

2.3. Superclass Accessible, Not Extensible

A superclass that is developed with a set of its subclasses should be able to document its intended usage, not constrain its subclasses. Also, having restricted subclasses should not limit the accessibility of its superclass.

Thus, the main motivation behind sealed classes is to have the possibility for a superclass to be widely accessible but not widely extensible.

java programming developer

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How to Install OpenJDK 11 on CentOS 8

What is OpenJDK? OpenJDk or Open Java Development Kit is a free, open-source framework of the Java Platform, Standard Edition (or Java SE).

Top 10+ Java Development Companies in India & Developers Reviews 2020 – TopDevelopers.co

A thoroughly researched list of top 10 Java development companies in India with ratings & reviews to help find the best java developers around India.

Hire Java Developers Team | Java Developer India - Developers.dev

.DEV is a leading java Developer agency. Our offshore Java Developer Staff offers robust cross-platform java development services at an affordable rate. 1000+ Sucessful Projects, 100+ Happy Customers, 650+ Professionals, 60% Saving on Project Cost

How long does it take to develop/build an app?

This article covers A-Z about the mobile and web app development process and answers your question on how long does it take to develop/build an app.

3 Best Programming Languages for Java Developers

In this article, I’ll share 3 JVM Programming languages Java programmers can learn and why you should learn them. Being a Polyglot developer is of good quality and often valued highly in interviews.