DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • How to Convert XLS to XLSX in Java
  • Recurrent Workflows With Cloud Native Dapr Jobs
  • Java Virtual Threads and Scaling
  • Java’s Next Act: Native Speed for a Cloud-Native World

Trending

  • Measuring the Impact of AI on Software Engineering Productivity
  • How to Practice TDD With Kotlin
  • Scalability 101: How to Build, Measure, and Improve It
  • Setting Up Data Pipelines With Snowflake Dynamic Tables
  1. DZone
  2. Coding
  3. Java
  4. Effectively Sealed Classes in Java

Effectively Sealed Classes in Java

Sealed classes are extremely helpful in class implementation in Java. Check out this post on using sealed classes and the Option class in Java.

By 
Grzegorz Piwowarek user avatar
Grzegorz Piwowarek
·
Aug. 16, 18 · Tutorial
Likes (20)
Comment
Save
Tweet
Share
27.7K Views

Join the DZone community and get the full member experience.

Join For Free

Java is missing various “hot” features from languages like Scala or Kotlin, but luckily some of them can be recreated using existing features — sealed hierarchies are one of these.

In this short article, we’ll see how to achieve the “sealed” effect by playing around with Java.

Sealed Classes

One of the most popular sealed hierarchies that exist is Option. The classic implementation involves making Option a sealed abstract class and providing two subclasses: Some  and None .

In Scala, it looks like:

sealed abstract class Option[+A] extends Product {}

case object None extends Option[Nothing] {}
case class Some[+A](value: A) extends Option[A] {}


If we try to further extend Option, it’s no longer possible as long as we can’t add the extension to the file with the base class — and that’s the essence of class sealing.

Furthermore, we could leverage additional compiler support, for example, when working with pattern matching.

You can find more examples in Kotlin, as well.

Sealed Classes in Java

Since we don’t have a dedicated solution, we need to try to construct our own.

In Java, Optional is implemented in a totally different way, because it’s designed to become a value type in the future — but that’s another story for another time.

But, now, let’s try to achieve sealed-class semantics using Vanilla Java. The whole trick relies on a clever usage of visibility restrictions of private constructors and nested classes:

public abstract class Option<T> {
    // ...
    public final static class Some<T> extends Option<T> { ... }
    public final static class None<T> extends Option<T> { ... }
}


Unfortunately, we can’t really forbid the class from being extended. However, we can make every extension outside the file unusable by making the default constructor private:

public abstract class Option<T> {
    private Option() {}
    // ...
    public final static class Some<T> extends Option<T> { ... }
    public final static class None<T> extends Option<T> { ... }
}


And, now, if we try to create an anonymous implementation, we end up with a compilation error:

Option<Integer> o = new Option<Integer>() {}
// 'Option()' has private access in 'com.pivovarit.sealed.Option'



And, if we try to create a standalone extension, it turns out that the default constructor isn’t visible from the outside:

public class SomeNone<T> extends Option<T> {
    private SomeNone() {
    }
}
// There is no default constructor available in 'com.pivovarit.sealed.Option'


And this is how we end up with an effectively sealed class in Java.


A Complete Example

package com.pivovarit.sealed;

import java.util.function.Supplier;

public abstract class Option<T> {

    abstract T getOrElse(Supplier<T> other);

    private Option() {
    }

    public final static class Some<T> extends Option<T> {

        private final T value;

        public Some(T value) {
            this.value = value;
        }

        @Override
        T getOrElse(Supplier<T> other) {
            return value;
        }
    }

    public final static class None<T> extends Option<T> {
        @Override
        T getOrElse(Supplier<T> other) {
            return other.get();
        }
    }
}


The above code snippet can be found on GitHub.

Java (programming language)

Published at DZone with permission of Grzegorz Piwowarek, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • How to Convert XLS to XLSX in Java
  • Recurrent Workflows With Cloud Native Dapr Jobs
  • Java Virtual Threads and Scaling
  • Java’s Next Act: Native Speed for a Cloud-Native World

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!