{"id":1521,"date":"2022-02-22T14:30:23","date_gmt":"2022-02-22T01:30:23","guid":{"rendered":"https:\/\/www.ronella.xyz\/?p=1521"},"modified":"2022-02-22T14:30:23","modified_gmt":"2022-02-22T01:30:23","slug":"sealed-classes-and-interfaces","status":"publish","type":"post","link":"https:\/\/www.ronella.xyz\/?p=1521","title":{"rendered":"Sealed Classes and Interfaces"},"content":{"rendered":"<p><strong>Sealed classes<\/strong> and <strong>interfaces<\/strong> restrict which other classes or interfaces may extend or implement them.<\/p>\n<pre><code class=\"language-java\">public sealed interface IAnimal \n    permits AquaticAnimal, LandAnimal {\n    String lifespan();\n}<\/code><\/pre>\n<p>Notice the use of the sealed modifier and permits keyword. The <strong>sealed modifier<\/strong> indicates that the class or interface is restricted. The <strong>permit keyword<\/strong> indicates which classes <em>(or interfaces)<\/em> can implement <em>(or extends)<\/em> it. Thus, <strong>the sealed modifier and the permit keyword must be used together.<\/strong><\/p>\n<pre><code class=\"language-java\">public sealed abstract class AquaticAnimal implements IAnimal permits SeaTurtle {\n}<\/code><\/pre>\n<p>The class <em>(or interface)<\/em> that can implement <em>(or extend)<\/em> a sealed interface <em>(or class)<\/em> must have one of the following modifiers:<\/p>\n<ul>\n<li>sealed<\/li>\n<li>non-sealed<\/li>\n<li>final<\/li>\n<\/ul>\n<pre><code class=\"language-java\">public non-sealed abstract class LandAnimal implements IAnimal {\n}<\/code><\/pre>\n<p>Using the <strong>non-sealed modifier<\/strong> indicates that this class <strong>can be extended without restriction<\/strong>.<\/p>\n<pre><code class=\"language-java\">public sealed class Dog extends LandAnimal permits Boxer {\n    @Override\n    public String lifespan() {\n        return &quot;10 - 13 years&quot;;\n    }\n}<\/code><\/pre>\n<p><strong>Limiting the hierarchy<\/strong> of Dog class to just Boxer class.<\/p>\n<pre><code class=\"language-java\">public final class Boxer extends Dog {\n    @Override\n    public String lifespan() {\n        return &quot;10 - 12 years&quot;;\n    }\n}<\/code><\/pre>\n<p>The <strong>final implementation<\/strong> of the Dog class. <\/p>\n<pre><code class=\"language-java\">public class Cat extends LandAnimal {\n    @Override\n    public String lifespan() {\n        return &quot;12 - 18 years&quot;;\n    }\n}<\/code><\/pre>\n<p>Since LandAnimal is <strong>non-sealed abstract class<\/strong>, it <strong>can be inherited without restriction<\/strong>. <\/p>\n<pre><code class=\"language-java\">public final class SeaTurtle extends AquaticAnimal {\n    @Override\n    public String lifespan() {\n        return &quot;Up to 50 years or more&quot;;\n    }\n}<\/code><\/pre>\n<p>SeaTurtle is the only class that can implement AquaticAnimal. Because this the <strong>only class permitted<\/strong> in the AquaticAnimal definition.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sealed classes and interfaces restrict which other classes or interfaces may extend or implement them. public sealed interface IAnimal permits AquaticAnimal, LandAnimal { String lifespan(); } Notice the use of the sealed modifier and permits keyword. The sealed modifier indicates that the class or interface is restricted. The permit keyword indicates which classes (or interfaces) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[74],"tags":[],"_links":{"self":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts\/1521"}],"collection":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1521"}],"version-history":[{"count":1,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts\/1521\/revisions"}],"predecessor-version":[{"id":1522,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts\/1521\/revisions\/1522"}],"wp:attachment":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1521"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1521"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1521"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}