<?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 Dhaval Joshi on Medium]]></title>
        <description><![CDATA[Stories by Dhaval Joshi on Medium]]></description>
        <link>https://medium.com/@dhavaljoshi007?source=rss-6b3d949872d6------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*Gy6NsxHCgitJWn3T.</url>
            <title>Stories by Dhaval Joshi on Medium</title>
            <link>https://medium.com/@dhavaljoshi007?source=rss-6b3d949872d6------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 18:30:16 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@dhavaljoshi007/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[Unleashing the Limitless Potential of Sealed Classes and Interfaces in Java 17]]></title>
            <link>https://medium.com/@dhavaljoshi007/exploring-the-power-of-sealed-classes-and-interfaces-in-java-17-b6390a5cd005?source=rss-6b3d949872d6------2</link>
            <guid isPermaLink="false">https://medium.com/p/b6390a5cd005</guid>
            <dc:creator><![CDATA[Dhaval Joshi]]></dc:creator>
            <pubDate>Fri, 07 Apr 2023 11:00:54 GMT</pubDate>
            <atom:updated>2023-04-07T11:05:18.648Z</atom:updated>
            <content:encoded><![CDATA[<p>Java 17 introduced a powerful feature called sealed classes and interfaces that provides enhanced control over the class hierarchy. Sealed classes and interfaces provide a new way of restricting the hierarchy of classes and interfaces, allowing only a specific set of classes and interfaces to extend or implement them. In this blog post, we will explore the benefits of using sealed classes and interfaces over final classes, examples of use cases where sealed classes and interfaces can be advantageous, limitations and best practices for implementation, how to implement sealed classes and interfaces in existing codebases, and differences between sealed classes and interfaces and other access modifiers such as private and protected.</p><p>Benefits of Using Sealed Classes and Interfaces over Final Classes</p><p>Final classes are classes that cannot be extended, which means that their behavior remains consistent across all instances. Sealed classes and interfaces provide a similar level of control over the class hierarchy but with more flexibility. By using sealed classes and interfaces, you can specify which classes and interfaces are allowed to extend or implement them, providing a level of control that cannot be achieved with final classes alone.</p><p>Examples of Use Cases Where Sealed Classes and Interfaces can be Advantageous</p><p>One example of where sealed classes and interfaces can be useful is in APIs where developers need to ensure that the class hierarchy is closed and cannot be extended by other classes outside of the intended hierarchy. For instance, consider the following example:</p><pre>public sealed interface Vehicle permits Car, Bus, Bike {<br>    void start();<br>}</pre><pre>public final class Car implements Vehicle {<br>    public void start() {<br>        System.out.println(&quot;Car is starting.&quot;);<br>    }<br>}</pre><pre>public final class Bus implements Vehicle {<br>    public void start() {<br>        System.out.println(&quot;Bus is starting.&quot;);<br>    }<br>}</pre><pre>public final class Bike implements Vehicle {<br>    public void start() {<br>        System.out.println(&quot;Bike is starting.&quot;);<br>    }<br>}</pre><p>Here, the Vehicle interface is sealed, which means that only the Car, Bus, and Bike classes can implement it. This ensures that the class hierarchy is closed, preventing any other classes from implementing the Vehicle interface.</p><p>Another example is in security-sensitive applications where it is important to prevent unauthorized access to sensitive code.</p><p>Limitations and Best Practices for Implementation</p><p>While sealed classes and interfaces provide a powerful tool for controlling class hierarchies, there are some limitations and best practices to consider when implementing them. One limitation is that sealed classes and interfaces cannot be used in anonymous classes, which may limit their usefulness in certain scenarios. Best practices include carefully considering which classes and interfaces should be sealed and which should be non-sealed, as well as carefully managing the permitted classes and interfaces to avoid unexpected behavior.</p><p>How to Implement Sealed Classes and Interfaces in Existing Codebases</p><p>Implementing sealed classes and interfaces in an existing codebase involves adding the “sealed” keyword to class and interface declarations and specifying which classes and interfaces are allowed to extend or implement them using the “permits” keyword. Developers should also carefully consider the impact on existing code and make any necessary changes to ensure that the code continues to function as intended.</p><p>Differences Between Sealed Classes and Interfaces and Other Access Modifiers</p><p>Sealed classes and interfaces are similar in some ways to other access modifiers, such as private and protected, but they provide a different level of control over the class hierarchy. Private and protected access modifiers control access to fields and methods within a class, while sealed classes and interfaces control which classes and interfaces are allowed to extend or implement them.</p><p>In conclusion, sealed classes and interfaces provide a powerful tool for controlling class hierarchies in Java 17. By carefully considering the benefits, use cases, limitations, and best practices, developers can take advantage of this feature to build more secure and maintain</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b6390a5cd005" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>