{"id":3370,"date":"2025-12-24T10:01:01","date_gmt":"2025-12-24T15:01:01","guid":{"rendered":"https:\/\/www.mymiller.name\/wordpress\/?p=3370"},"modified":"2025-12-24T10:01:01","modified_gmt":"2025-12-24T15:01:01","slug":"sealed-classes","status":"publish","type":"post","link":"https:\/\/www.mymiller.name\/wordpress\/java_new_features\/sealed-classes\/","title":{"rendered":"Sealed Classes"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Java 15 introduced a new feature called Sealed Classes, which allows developers to restrict the types that can extend a class or implement an interface. This feature is designed to improve code safety and maintainability by enforcing a set of rules that limit the hierarchy of classes and interfaces.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this blog article, we will explore what Sealed Classes are, how they work, and how to use them in your Java applications.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">What are Sealed Classes?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Sealed Classes are a new feature in Java 15 that allow developers to restrict the types that can extend a class or implement an interface. This is done by adding a new keyword <code>sealed<\/code> to the class or interface declaration, followed by a list of permitted subclasses or implementors.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Sealed Classes enforce a set of rules that limit the hierarchy of classes and interfaces, making it easier to maintain and modify the code over time. This feature is especially useful when working with complex systems that require a high level of abstraction and modularity.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">How Sealed Classes Work<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Sealed Classes work by allowing the developer to specify a set of rules that limit the types that can extend a class or implement an interface. This is done using the <code>sealed<\/code> keyword in the class or interface declaration, followed by a list of permitted subclasses or implementors.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s an example of how a sealed class might look:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public sealed class Shape     permits Circle, Rectangle, Triangle {          \n\t\/\/ Class definition goes here... \n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In this example, <code>Shape<\/code> is declared as a sealed class, which means that it can only be extended by the classes listed in the <code>permits<\/code> clause. In this case, <code>Circle<\/code>, <code>Rectangle<\/code>, and <code>Triangle<\/code> are the only permitted subclasses of <code>Shape<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To create a subclass of a sealed class, the subclass must meet one of the following criteria:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The subclass is listed in the <code>permits<\/code> clause of the sealed class<\/li>\n\n\n\n<li>The subclass is a final class<\/li>\n\n\n\n<li>The subclass is a non-sealed class that extends a permitted subclass of the sealed class<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s an example of how a subclass of a sealed class might look:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public final class Circle extends Shape {     \n\t\/\/ Class definition goes here... \n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In this example, <code>Circle<\/code> is a final class that extends <code>Shape<\/code>, which is a permitted subclass of the sealed class.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Benefits of Sealed Classes<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Sealed Classes provide several benefits over traditional class hierarchies. These include:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Improved code safety: Sealed Classes allow developers to restrict the types that can extend a class or implement an interface, which can reduce the risk of errors and bugs in the code.<\/li>\n\n\n\n<li>Easier maintenance: Sealed Classes limit the hierarchy of classes and interfaces, making it easier to maintain and modify the code over time.<\/li>\n\n\n\n<li>Increased abstraction: Sealed Classes provide a high level of abstraction, which can make it easier to reason about the code and understand its structure.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Conclusion<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Sealed Classes are a new feature in Java 15 that allow developers to restrict the types that can extend a class or implement an interface. This feature improves code safety and maintainability by enforcing a set of rules that limit the hierarchy of classes and interfaces.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">With Sealed Classes, developers can create more modular and maintainable code, which can reduce the risk of errors and bugs in the code. By providing a high level of abstraction and limiting the hierarchy of classes and interfaces, Sealed Classes make it easier to reason about the code and understand its structure.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java 15 introduced a new feature called Sealed Classes, which allows developers to restrict the types that can extend a class or implement an interface. This feature is designed to improve code safety and maintainability by enforcing a set of rules that limit the hierarchy of classes and interfaces. In this blog article, we will [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3372,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[458],"tags":[],"series":[],"class_list":["post-3370","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java_new_features"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/06\/letter-g26b7a9ac7_640.jpg?fit=640%2C427&ssl=1","jetpack-related-posts":[{"id":3362,"url":"https:\/\/www.mymiller.name\/wordpress\/java_new_features\/pattern-matching-for-instanceof\/","url_meta":{"origin":3370,"position":0},"title":"Pattern Matching for instanceof","author":"Jeffery Miller","date":"December 24, 2025","format":false,"excerpt":"Java 16 introduced a new feature called \"Pattern Matching for instanceof\" which simplifies the process of casting objects in Java. This feature improves the readability and safety of code that uses instanceof and casting by allowing developers to combine the two operations into a single step. In this blog article,\u2026","rel":"","context":"In &quot;Java New Features&quot;","block_context":{"text":"Java New Features","link":"https:\/\/www.mymiller.name\/wordpress\/category\/java_new_features\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/06\/jigsaw-gcae0977ae_640.png?fit=640%2C455&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/06\/jigsaw-gcae0977ae_640.png?fit=640%2C455&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/06\/jigsaw-gcae0977ae_640.png?fit=640%2C455&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":3374,"url":"https:\/\/www.mymiller.name\/wordpress\/spring_databases\/spring-data-with-java-records\/","url_meta":{"origin":3370,"position":1},"title":"Spring Data with Java Records","author":"Jeffery Miller","date":"December 24, 2025","format":false,"excerpt":"Java records are a new feature introduced in Java 14 that provides a concise way to declare classes that are meant to hold immutable data. Spring Data is a popular framework for building data access layers in Java applications. In this answer, we will explain how to use Java records\u2026","rel":"","context":"In &quot;Spring Databases&quot;","block_context":{"text":"Spring Databases","link":"https:\/\/www.mymiller.name\/wordpress\/category\/spring_databases\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/06\/office-work-gc63fa3774_640.jpg?fit=640%2C430&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/06\/office-work-gc63fa3774_640.jpg?fit=640%2C430&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/06\/office-work-gc63fa3774_640.jpg?fit=640%2C430&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":3848,"url":"https:\/\/www.mymiller.name\/wordpress\/java\/level-up-your-testing-structuring-unit-tests-with-subclasses\/","url_meta":{"origin":3370,"position":2},"title":"Level Up Your Testing: Structuring Unit Tests with Subclasses","author":"Jeffery Miller","date":"December 24, 2025","format":false,"excerpt":"When your project grows, unit test classes can become repetitive. You often find yourself duplicating setup code, utility methods, or common assertions across multiple test suites. Subclassing provides a powerful way to eliminate this redundancy, promote code reuse, and create a more organized and maintainable testing structure. Why Use Subclasses\u2026","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.mymiller.name\/wordpress\/category\/java\/"},"img":{"alt_text":"","src":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/10\/ai-generated-8686283_1280-jpg.avif","width":350,"height":200,"srcset":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/10\/ai-generated-8686283_1280-jpg.avif 1x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/10\/ai-generated-8686283_1280-jpg.avif 1.5x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/10\/ai-generated-8686283_1280-jpg.avif 2x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/10\/ai-generated-8686283_1280-jpg.avif 3x"},"classes":[]},{"id":3737,"url":"https:\/\/www.mymiller.name\/wordpress\/java_new_features\/java-23-is-here-exploring-the-full-release-and-incubator-features\/","url_meta":{"origin":3370,"position":3},"title":"Java 23 is Here: Exploring the Full Release and Incubator Features","author":"Jeffery Miller","date":"April 20, 2026","format":false,"excerpt":"Java 23 arrived in September 2023 with a range of new features and improvements. While it may not be a Long-Term Support (LTS) release, it offers some exciting additions worth exploring. In this blog post, we\u2019ll dive into the full release features of Java 23, providing clear explanations and practical\u2026","rel":"","context":"In &quot;Java New Features&quot;","block_context":{"text":"Java New Features","link":"https:\/\/www.mymiller.name\/wordpress\/category\/java_new_features\/"},"img":{"alt_text":"","src":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/10\/immune-defense-1359197_1280-jpg.avif","width":350,"height":200,"srcset":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/10\/immune-defense-1359197_1280-jpg.avif 1x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/10\/immune-defense-1359197_1280-jpg.avif 1.5x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/10\/immune-defense-1359197_1280-jpg.avif 2x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/10\/immune-defense-1359197_1280-jpg.avif 3x"},"classes":[]},{"id":3600,"url":"https:\/\/www.mymiller.name\/wordpress\/java_new_features\/record-patterns-in-java-21-simplifying-data-extraction\/","url_meta":{"origin":3370,"position":4},"title":"Record Patterns in Java 21: Simplifying Data Extraction","author":"Jeffery Miller","date":"December 24, 2025","format":false,"excerpt":"Java 21 introduces record patterns, a powerful addition to the pattern matching arsenal. This feature streamlines the extraction of components from record classes, making code more concise and readable. What are Record Patterns? Record patterns leverage the structure of record classes to deconstruct them into their constituent parts. By specifying\u2026","rel":"","context":"In &quot;Java New Features&quot;","block_context":{"text":"Java New Features","link":"https:\/\/www.mymiller.name\/wordpress\/category\/java_new_features\/"},"img":{"alt_text":"","src":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/07\/collector-3930337_1280-jpg.avif","width":350,"height":200,"srcset":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/07\/collector-3930337_1280-jpg.avif 1x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/07\/collector-3930337_1280-jpg.avif 1.5x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/07\/collector-3930337_1280-jpg.avif 2x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/07\/collector-3930337_1280-jpg.avif 3x"},"classes":[]},{"id":3388,"url":"https:\/\/www.mymiller.name\/wordpress\/java_new_features\/vector-api-for-computations\/","url_meta":{"origin":3370,"position":5},"title":"Vector API for computations","author":"Jeffery Miller","date":"December 24, 2025","format":false,"excerpt":"Java 16 introduced a new feature called the Vector API, which provides a set of low-level vector operations for performing mathematical calculations on large sets of data. The Vector API is designed to take advantage of the hardware capabilities of modern CPUs, such as SIMD (Single Instruction Multiple Data) instructions,\u2026","rel":"","context":"In &quot;Java New Features&quot;","block_context":{"text":"Java New Features","link":"https:\/\/www.mymiller.name\/wordpress\/category\/java_new_features\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/06\/board-g12f4be736_640.jpg?fit=640%2C424&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/06\/board-g12f4be736_640.jpg?fit=640%2C424&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/06\/board-g12f4be736_640.jpg?fit=640%2C424&ssl=1&resize=525%2C300 1.5x"},"classes":[]}],"jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts\/3370","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/comments?post=3370"}],"version-history":[{"count":2,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts\/3370\/revisions"}],"predecessor-version":[{"id":3373,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts\/3370\/revisions\/3373"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/media\/3372"}],"wp:attachment":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/media?parent=3370"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/categories?post=3370"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/tags?post=3370"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/series?post=3370"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}