> Markdown version of [/videos/500-java-sealed-classes-vs-kotlin-sealed-classes-slideless](https://www.wearedevelopers.com/videos/500-java-sealed-classes-vs-kotlin-sealed-classes-slideless). Every page supports `.md` or `Accept: text/markdown`. Links point to the HTML versions so they work for humans too. Agent guide: [/agents.md](https://www.wearedevelopers.com/agents.md). --- # Java Sealed Classes vs. Kotlin Sealed Classes #Slideless Tired of unpredictable, null-prone code? Discover how sealed classes in both Java 17 and Kotlin eliminate nullable return types through strict hierarchies and exhaustive pattern matching. - **Speakers:** Lothar Schulz - **Event:** World Congress 2022 - **Published:** June 15, 2022 - **Duration:** 38:07 - **URL:** https://www.wearedevelopers.com/videos/500-java-sealed-classes-vs-kotlin-sealed-classes-slideless ## Summary **Core Concept** This session explores the differences and similarities between sealed classes in Java and Kotlin, demonstrating how developers can leverage them to write safer, null-free code. By explicitly defining a strict type hierarchy of permitted subclasses, sealed classes act as powerful containers for specific application states, functioning effectively as "state-holding super enums." **Kotlin Refactoring** Through live-coding a GitHub API integration, the presentation illustrates the transition from error-prone nullable return types to bounded success and failure states. In Kotlin, this involves nesting data classes and utilizing exhaustive 'when' expressions for type-safe pattern matching. This approach eliminates wildcard fallbacks, ensuring structural safety if new states are added to the hierarchy. **Java Implementation** The Java approach leverages JDK 17 LTS functionality, showcasing how sealed interfaces combined with Java records can drastically reduce boilerplate class generation. The session highlights Java's modern pattern matching capabilities, moving from standard conditional logic to label-bound 'instanceof' checks and updated 'switch' statements for direct data extraction. Ultimately, the core takeaway is that sealed class features are structurally on par across both languages, empowering developers to achieve highly expressive type safety regardless of their chosen ecosystem. **Keywords:** java sealed classes, kotlin sealed classes, exhaustive pattern matching, bounded type hierarchy, java records refactoring, java sealed interfaces, kotlin data classes, null safety improvements, switch statement pattern matching, jdk 17 preview features, instanceof pattern matching, github api integration ## Chapters 1. **Controlling class extension with Java sealed classes** (01:42) — Java 17 introduces sealed classes to restrict and explicitly define permitted subtypes. 1. **Kotlin sealed classes as flexible super enums** (03:42) — Kotlin sealed classes enable fixed type hierarchies that store state in subclasses. 1. **Refactoring API integration using Kotlin sealed classes** (05:06) — Replacing nullable API responses with explicit success and failure data classes. 1. **Applying exhaustive pattern matching in Kotlin** (11:55) — The when expression ensures safe handling of all possible sealed class types. 1. **Structuring API responses with Java sealed classes** (17:34) — Creating explicit success and exception subclasses improves type safety in Java API integrations. 1. **Evaluating object subtypes with Java instanceof** (26:04) — Using traditional type checking allows branching logic based on the API response type. 1. **Reducing boilerplate with Java sealed interfaces and records** (28:35) — Transitioning from standard classes to records simplifies syntax and eliminates redundant code. 1. **Pattern matching with Java switch preview features** (31:46) — Java 17 enables exhaustive type matching to handle sealed class branches safely. 1. **Comparing Java and Kotlin sealed class features** (35:10) — Both languages offer parity in sealed class capabilities and robust type matching. ## Related Moments - [Introduction to Kotlin history and Java ecosystem interoperability](https://www.wearedevelopers.com/videos/384-moving-from-java-to-kotlin) (from "Moving from Java to Kotlin") - [Introduction to Kotlin and Java interoperability](https://www.wearedevelopers.com/videos/512-route-from-java-to-kotlin) (from "Route from Java to Kotlin") - [Introduction to the Java and Kotlin ecosystems](https://www.wearedevelopers.com/videos/661-why-kotlin-is-the-better-java-and-how-you-can-start-using-it) (from "Why Kotlin is the better Java and how you can start using it") - [Configuring runtime Java and Kotlin interoperability protocols](https://www.wearedevelopers.com/videos/661-why-kotlin-is-the-better-java-and-how-you-can-start-using-it) (from "Why Kotlin is the better Java and how you can start using it") - [Summarizing the practical engineering advantages of writing Kotlin](https://www.wearedevelopers.com/videos/661-why-kotlin-is-the-better-java-and-how-you-can-start-using-it) (from "Why Kotlin is the better Java and how you can start using it") - [Significant performance and language features launching in Java 21](https://www.wearedevelopers.com/videos/658-beam-me-up-java-unraveling-the-warp-speed-evolution-a-journey-through-java-lts-versions-11-to-21) (from "Beam Me Up, Java! Unraveling the Warp-Speed Evolution: A Journey through Java LTS Versions 11 to 21") ## Related Articles - [Using Java 17 latest features in real world projects](https://www.wearedevelopers.com/magazine/121-using-java-17-latest-features-in-real-world-projects) - [Modern Java Records](https://www.wearedevelopers.com/magazine/41-modern-java-records) - [Modern Java Switch Expressions](https://www.wearedevelopers.com/magazine/40-modern-java-switch-expressions) - [Top 10 Java Libraries](https://www.wearedevelopers.com/magazine/364-top-10-java-libraries) ## Related Jobs - [Senior Software Engineer, Android (Earn Experience)](https://www.wearedevelopers.com/jobs/ext/1937627-senior-software-engineer-android-earn-experience) at **Bitpanda** - [Senior Software Engineer](https://www.wearedevelopers.com/jobs/ext/15942-senior-software-engineer) at **GitHub** - [Senior Backend Engineer (Java)](https://www.wearedevelopers.com/jobs/ext/19369-senior-backend-engineer-java) at **Bonial International GmbH** - [Senior Software Developer Java (m/w/d)](https://www.wearedevelopers.com/jobs/39654-senior-software-developer-java-m-w-d) at **Sopra Steria Custom Software Solutions GmbH** - [(Senior) Java Software-Entwickler](https://www.wearedevelopers.com/jobs/48140-senior-java-software-entwickler) at **Finanz Informatik** - [Softwareentwickler Java](https://www.wearedevelopers.com/jobs/ext/1245896-softwareentwickler-java) at **Aok Systems Gmbh**