{"id":4947,"date":"2020-08-26T03:46:22","date_gmt":"2020-08-26T03:46:22","guid":{"rendered":"https:\/\/www.testingdocs.com\/questions\/?p=4947"},"modified":"2024-08-09T06:13:48","modified_gmt":"2024-08-09T06:13:48","slug":"what-is-a-sealed-interface-in-java","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/what-is-a-sealed-interface-in-java\/","title":{"rendered":"What is a Sealed Interface in Java"},"content":{"rendered":"<h2>What is a Sealed Interface in Java<\/h2>\n<p>Sealed Interface allows only the permitted classes to implement the interface. Unknown classes are not allowed to implement the interface. The sealed interface specifies the permitted classes using the permits clause. We can specify the interface as sealed using the keyword sealed.<\/p>\n<h3>Sealed Interface<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\/\/www.TestingDocs.com\r\npublic sealed interface Thinkable permits Person  {\r\n    public void think();\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>Only the <strong>Person<\/strong> class is allowed to implement the <strong>Thinkable<\/strong> interface. Person class can be final, or sealed or non-sealed.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4950\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Sealed-Interface-Java.png\" alt=\"Sealed Interface Java\" width=\"1791\" height=\"996\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Sealed-Interface-Java.png 1791w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Sealed-Interface-Java-300x167.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Sealed-Interface-Java-1024x569.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Sealed-Interface-Java-768x427.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Sealed-Interface-Java-1536x854.png 1536w\" sizes=\"auto, (max-width: 1791px) 100vw, 1791px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Person class<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">package jdk15;\r\n\/\/www.TestingDocs.com\r\npublic non-sealed class Person implements Thinkable {\r\n    protected String name;\r\n\r\n    public Person(String name) {\r\n        this.name = name;\r\n    }\r\n\r\n    public String getName() {\r\n        return name;\r\n    }\r\n\r\n    @Override\r\n    public void think() {\r\n        System.out.println( getName() + \" is thinking...\");\r\n    }\r\n\r\n    @Override\r\n    public String toString() {\r\n        return \"Person{\" +\r\n                \"name='\" + name + '\\'' +\r\n                '}';\r\n    }\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h3>Interfaces in permits clause<\/h3>\n<p>Till now we only talked about specifying classes in the permits clause. We can even specify interfaces in the permits clause of a sealed interface.<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\/\/www.TestingDocs.com\r\npublic sealed interface Thinkable permits Dreamable, Person  {\r\n    public void think();\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\/\/www.TestingDocs.com\r\npublic non-sealed interface Dreamable extends Thinkable {\r\n    public void dream();\r\n}<\/pre>\n<h3>implements and permits clause<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\/\/www.TestingDocs.com\r\npublic sealed class Person implements Thinkable, Dreamable permits Employee {\r\n    protected String name;\r\n\r\n    public Person(String name) {\r\n        this.name = name;\r\n    }\r\n\r\n    public String getName() {\r\n        return name;\r\n    }\r\n\r\n    @Override\r\n    public void think() {\r\n        System.out.println( getName() + \" is thinking...\");\r\n    }\r\n\r\n    @Override\r\n    public String toString() {\r\n        return \"Person{\" +\r\n                \"name='\" + name + '\\'' +\r\n                '}';\r\n    }\r\n\r\n    @Override\r\n    public void dream() {\r\n        System.out.println( getName() + \" is in REM Sleep...\");\r\n    }\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is a Sealed Interface in Java Sealed Interface allows only the permitted classes to implement the interface. Unknown classes are not allowed to implement the interface. The sealed interface specifies the permitted classes using the permits clause. We can specify the interface as sealed using the keyword sealed. Sealed Interface \/\/www.TestingDocs.com public sealed interface [&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-4947","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\/4947","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=4947"}],"version-history":[{"count":6,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/4947\/revisions"}],"predecessor-version":[{"id":23641,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/4947\/revisions\/23641"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=4947"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=4947"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=4947"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}