<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Jalaj Goyal on Medium]]></title>
        <description><![CDATA[Stories by Jalaj Goyal on Medium]]></description>
        <link>https://medium.com/@jalajgoyal18?source=rss-6eae67f5c81e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*UN2El5V_-KmZHH67.</url>
            <title>Stories by Jalaj Goyal on Medium</title>
            <link>https://medium.com/@jalajgoyal18?source=rss-6eae67f5c81e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 06:33:36 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jalajgoyal18/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Sealed Classes in Java 17 — Features and Rules]]></title>
            <link>https://medium.com/@jalajgoyal18/sealed-classes-in-java-17-features-and-rules-9ab6c539f0ac?source=rss-6eae67f5c81e------2</link>
            <guid isPermaLink="false">https://medium.com/p/9ab6c539f0ac</guid>
            <category><![CDATA[sealed-classes]]></category>
            <category><![CDATA[sealed-interface]]></category>
            <category><![CDATA[java17]]></category>
            <category><![CDATA[record]]></category>
            <category><![CDATA[java-17-features]]></category>
            <dc:creator><![CDATA[Jalaj Goyal]]></dc:creator>
            <pubDate>Sun, 21 Jun 2026 13:14:29 GMT</pubDate>
            <atom:updated>2026-06-21T13:14:29.795Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cN_v8I138iEQfsM_7bgyuA.png" /></figure><p><strong>What do Sealed Classes provide?</strong></p><p>Sealed classes and interfaces restrict which other classes or interfaces may extend or implement them.</p><p><strong>Why were sealed classes needed?</strong></p><p>With inheritance, we were able to reuse the code that was written for a parent class. But, what if you want your parent class to share code only for specific other classes. For example if you are working on class Furniture and want to use the behaviour of Furniture class with other classes like Sofa, DiningTable but not with any other furniture type, then you need to use Sealed Classes. By defining a class as sealed, you can control which classes are permitted to extend the sealed class.</p><p><strong>How the sealed classes are defined?</strong></p><ol><li>We need to add a ‘sealed’ modifier to the class declaration.</li><li>After extends, implementation clauses add ‘permits’ clause.</li></ol><pre>public sealed class Furniture <br>      permits Sofa, DiningTable, CoffeeTable {...}<br><br>final class Sofa extends Furniture {...}<br><br>public non-sealed class DiningTable extends Furniture {...}<br><br>public sealed class CoffeeTable extends Furniture permits RoundCoffeeTable{...}<br><br>final public class RoundCoffeeTable{...}</pre><p><strong>Constraints on permitted sub-classes</strong></p><ol><li>They must be available to sealed class at the time of compilation. It implies in order to compile Furniture class, Sofa, DiningTable and CoffeeTable should be available, but also, RoundCoffeeTable should be available as it is subclass of sealed class CoffeeTable.</li><li>They must directly extend the sealed class.</li><li>All subclasses should have exactly one of the below access modifier, these modifiers indicate how the subclasses are carrying the sealing of superclass:<br>i) final — This prevents the extension of sealing by another subclass.<br>ii) sealed — This ensures that only permitted subclasses can only carry forward the sealing<br>iii) non-sealed — This can have any unknown sub classes, this acts exactly like a normal class, exception being that it needs to be defined as non-sealed explicitly because of the parent class which is sealed.</li><li>All subclasses must be in the same module if it is named(explicitly defined modules) or in the same package if it is in unnamed module(JAR files which are loaded from classpath at runtime)</li><li>You can also define the permitted subclass in the same file as of the sealed class, you then don’t need permits clause.</li></ol><p><strong>Sealed Interfaces</strong></p><p>It is similar to the classes, you need to mark it as sealed using the keyword ‘sealed’ and add a permits clause after extends clause. Permits clause defines which classes can implement the sealed interface and which interfaces can extend this sealed interface.</p><pre>public sealed interface Car extends Vehicle<br>              permits BMW, Audi, Porsche {...}<br><br>non-sealed interface BMW extends Car {...}<br><br>sealed interface Porsche extends Car<br>              permits Cayman, Cayenne {...}<br><br>final class Audi implements Car {...}</pre><p><strong>Records as permitted subclasses</strong></p><p>Record classes can be added to permits clause of a sealed class or interface. Benefit of record is that they are implicitly final.</p><p><strong>Benefits of Sealed Classes and Interfaces</strong></p><ol><li>Classes have a predictable inheritance, developer now knows directly which classes are extending the classes, thus increasing the maintenance of large code bases.</li><li>Modern switch expressions can eliminate default case by using sealed classes as there will be no surprises if all cases are pre-determined.</li><li>API Designs becomes much secure as there is no unauthorized extension of any sealed class.</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9ab6c539f0ac" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>