{"id":4909,"date":"2020-08-25T07:43:28","date_gmt":"2020-08-25T07:43:28","guid":{"rendered":"https:\/\/www.testingdocs.com\/questions\/?p=4909"},"modified":"2024-08-09T06:15:30","modified_gmt":"2024-08-09T06:15:30","slug":"what-are-sealed-classes-in-java","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/what-are-sealed-classes-in-java\/","title":{"rendered":"What are Sealed Classes in Java"},"content":{"rendered":"<h2>Sealed Class<\/h2>\n<p>Sealed classes in Java allow inheritance from the known sub classes that are specified by the permits keyword or defined in the same class file. If we mark a class as sealed it should have some sub classes specified using the permits clause or sub classes defined in the file.\u00a0 For example<\/p>\n<p><strong>public abstract sealed class Ship permits CargoShip, PassengerShip {<\/strong><\/p>\n<p>CargoShip and PassengerShip are the only child classes that can inherit Ship class.<\/p>\n<p>CargoShip and PassengerShip child classes can be<\/p>\n<ul>\n<li><strong>final<\/strong><\/li>\n<li><strong>sealed<\/strong><\/li>\n<li><strong>non-sealed<\/strong><\/li>\n<\/ul>\n<h3>Enable Preview<\/h3>\n<p>To enable Preview features in IntelliJ IDEA. Set Preview mode under the Project SDK.<\/p>\n<p>Running a simple program or Java class using IntelliJ IDEA IDE. (<a href=\"https:\/\/www.testingdocs.com\/simple-hello-world-program-using-intellij-ide\/\"><strong>https:\/\/www.testingdocs.com\/simple-hello-world-program-using-intellij-ide\/ )<\/strong><\/a><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4920\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Project-SDK.png\" alt=\"Project SDK\" width=\"1912\" height=\"1103\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Project-SDK.png 1912w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Project-SDK-300x173.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Project-SDK-1024x591.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Project-SDK-768x443.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Project-SDK-1536x886.png 1536w\" sizes=\"auto, (max-width: 1912px) 100vw, 1912px\" \/><\/p>\n<p>or use command line switch:\u00a0 &#8211;enable-preview<\/p>\n<p>Let&#8217;s say by law <strong>NavelShip<\/strong> are not allowed as regular ships in the sea. So sealing the\u00a0 class Ship to cargo and passenger ships. When we try to extend <strong>NavelShip<\/strong> as Ship class would result in error in the sealed class hierarchy.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4927\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Sealed-classes-in-Java.png\" alt=\"Sealed classes in Java\" width=\"1710\" height=\"875\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Sealed-classes-in-Java.png 1710w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Sealed-classes-in-Java-300x154.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Sealed-classes-in-Java-1024x524.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Sealed-classes-in-Java-768x393.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Sealed-classes-in-Java-1536x786.png 1536w\" sizes=\"auto, (max-width: 1710px) 100vw, 1710px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4914\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/NavelShip.png\" alt=\"NavelShip sealed class\" width=\"1858\" height=\"795\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/NavelShip.png 1858w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/NavelShip-300x128.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/NavelShip-1024x438.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/NavelShip-768x329.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/NavelShip-1536x657.png 1536w\" sizes=\"auto, (max-width: 1858px) 100vw, 1858px\" \/><\/p>\n<h3>permits<\/h3>\n<p>permits clause allows us to specify the child classes for the sealed classes. The child class can be specified in separate files.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\/\/ www.TestingDocs.com\r\npublic abstract sealed class Ship permits CargoShip,PassengerShip {\r\n    protected String name;\r\n\r\n    public Ship(String name) {\r\n        this.name = name;\r\n    }\r\n\r\n}\r\n<\/pre>\n<p><strong>PassengerShip<\/strong> and <strong>CargoShip<\/strong> can inherit the ship class.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public final class PassengerShip extends Ship{\r\n    private int noOfPassengers;\r\n\r\n    public PassengerShip(String name,int p) {\r\n        super(name);\r\n        this.noOfPassengers=p;\r\n    }\r\n\r\n    public int getNoOfPassengers() {\r\n        return noOfPassengers;\r\n    }\r\n\r\n    public void setNoOfPassengers(int noOfPassengers) {\r\n        this.noOfPassengers = noOfPassengers;\r\n    }\r\n\r\n    @Override\r\n    public String toString() {\r\n        return \"Passenger Ship{\" +\r\n                \"Cargo Weight in Tons=\" + getNoOfPassengers()  +\r\n                \", name='\" + name + '\\'' +\r\n                '}';\r\n    }\r\n}\r\n<\/pre>\n<p>Note that Passenger ship is final class. So other classes cannot inherit from the final class <strong>PassengerShip<\/strong>. Sealed classes can extend other sealed classes. For example, due to the fewer number of Passenger ships government allows warships to carry passengers due to emergency.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>public sealed class PassengerShip extends Ship permits WarShip{<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public sealed class PassengerShip extends Ship permits WarShip{\r\n    private int noOfPassengers;\r\n\r\n    public PassengerShip(String name,int p) {\r\n        super(name);\r\n        this.noOfPassengers=p;\r\n    }\r\n\r\n    public int getNoOfPassengers() {\r\n        return noOfPassengers;\r\n    }\r\n\r\n    public void setNoOfPassengers(int noOfPassengers) {\r\n        this.noOfPassengers = noOfPassengers;\r\n    }\r\n\r\n    @Override\r\n    public String toString() {\r\n        return \"Passenger Ship{\" +\r\n                \"Number of passengers=\" + getNoOfPassengers()  +\r\n                \", name='\" + name + '\\'' +\r\n                '}';\r\n    }\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h3>non-sealed<\/h3>\n<p>non-sealed opens up the inheritance to unknown child classes. This is like the normal inheritance in Java without specifying the keyword. or example, a non-sealed class would open the sealed hierarchy by allowing unknown classes to inherit the class.<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\/\/www.TestingDocs.com\r\npublic non-sealed class CargoShip extends Ship {\r\n    private double cargoWeight; \/\/ kgs\r\n\r\n    public CargoShip(String name, double w) {\r\n        super(name);\r\n        this.cargoWeight=w;\r\n    }\r\n\r\n    protected double convertToTons() {\r\n        return this.cargoWeight\/1000;\r\n    }\r\n\r\n    @Override\r\n    public String toString() {\r\n        return \"Cargo Ship{\" +\r\n                \"Cargo Weight in Tons=\" + convertToTons()  +\r\n                \", name='\" + name + '\\'' +\r\n                '}';\r\n    }\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sealed Class Sealed classes in Java allow inheritance from the known sub classes that are specified by the permits keyword or defined in the same class file. If we mark a class as sealed it should have some sub classes specified using the permits clause or sub classes defined in the file.\u00a0 For example public [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[40],"tags":[],"class_list":["post-4909","post","type-post","status-publish","format-standard","hentry","category-java","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/4909","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=4909"}],"version-history":[{"count":9,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/4909\/revisions"}],"predecessor-version":[{"id":23645,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/4909\/revisions\/23645"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=4909"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=4909"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=4909"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}