{"id":1069,"date":"2021-06-19T12:57:35","date_gmt":"2021-06-19T12:57:35","guid":{"rendered":"https:\/\/usemynotes.com\/?p=1069"},"modified":"2023-01-22T13:39:16","modified_gmt":"2023-01-22T13:39:16","slug":"interface-in-java","status":"publish","type":"post","link":"https:\/\/usemynotes.com\/interface-in-java\/","title":{"rendered":"Why should I use Interface in Java?"},"content":{"rendered":"<p>In this tutorial, we will discuss <strong>Why should I use Interface in Java programming<\/strong>. We will understand what they are, how to implement them, why they are used in Java programming, and much more. So, let&#8217;s begin with its introduction.<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_80 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 eztoc-toggle-hide-by-default' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/usemynotes.com\/interface-in-java\/#Interface_in_Java_Programming\" >Interface in Java Programming<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/usemynotes.com\/interface-in-java\/#How_to_implement_Interface_in_Java\" >How to implement Interface in Java?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/usemynotes.com\/interface-in-java\/#Why_should_I_use_Interface_in_Java\" >Why should I use Interface in Java?<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/usemynotes.com\/interface-in-java\/#There_are_a_few_things_to_keep_in_mind_when_using_an_Interface_in_Java\" >There are a few things to keep in mind when using an Interface in Java<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/usemynotes.com\/interface-in-java\/#Extending_an_Interface_in_Java\" >Extending an Interface in Java<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/usemynotes.com\/interface-in-java\/#Extending_Multiple_Interfaces_in_Java_programming\" >Extending Multiple Interfaces in Java programming<\/a><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"Interface_in_Java_Programming\"><\/span>Interface in Java Programming<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>An Interface in Java is a fundamental concept in Java programming. It is similar to a class but with some differences.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-1070 size-full\" src=\"https:\/\/usemynotes.com\/wp-content\/uploads\/2021\/06\/interface-in-java.jpg\" alt=\"Interface in Java\" width=\"1000\" height=\"650\" title=\"\" srcset=\"https:\/\/usemynotes.com\/wp-content\/uploads\/2021\/06\/interface-in-java.jpg 1000w, https:\/\/usemynotes.com\/wp-content\/uploads\/2021\/06\/interface-in-java-300x195.jpg 300w, https:\/\/usemynotes.com\/wp-content\/uploads\/2021\/06\/interface-in-java-768x499.jpg 768w, https:\/\/usemynotes.com\/wp-content\/uploads\/2021\/06\/interface-in-java-150x98.jpg 150w, https:\/\/usemynotes.com\/wp-content\/uploads\/2021\/06\/interface-in-java-696x452.jpg 696w, https:\/\/usemynotes.com\/wp-content\/uploads\/2021\/06\/interface-in-java-646x420.jpg 646w\" sizes=\"(max-width: 1000px) 100vw, 1000px\" \/><\/p>\n<ul>\n<li>An Interface is a group of abstract methods and constants.<\/li>\n<li>An Interface may contain constants, static methods, default methods, and nested types.<\/li>\n<li>Default and static methods have their body in an interface.<\/li>\n<li>An Interface cannot be instantiated.<\/li>\n<\/ul>\n<p>A class mainly describes the behaviours of the object that is instantiated using it. But an interface contains behaviours that a class must implement if it implements it. If the class that implements the interface is not abstract, then all the methods of the interface need to be defined in the class or the program won&#8217;t compile.<\/p>\n<p><strong>Syntax of Interface in Java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\ninterface &lt;name&gt; {\r\n    \/\/ constant 1\r\n    \/\/ constant 2, so on\r\n\r\n    \/\/ abstract method 1\r\n    \/\/ abstract method 2, so on\r\n}\r\n<\/pre>\n<p><strong>Example of Interface in Java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\ninterface MySimpleInterface {\r\n    void hello();\r\n}\r\n<\/pre>\n<h2><span class=\"ez-toc-section\" id=\"How_to_implement_Interface_in_Java\"><\/span>How to implement Interface in Java?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Using an <strong>Interface in Java<\/strong> is just like a signed contract between a class and an Interface. The class that implements an Interface needs to agree to perform specific behaviours of the interface and if that class does not perform all the behaviours, then the class must declare itself as <a href=\"https:\/\/usemynotes.com\/what-is-abstraction-in-java\/\">abstract<\/a>, or again the program won&#8217;t run. This might seem a bit complicated, but it isn&#8217;t.<\/p>\n<p>The &#8216;implements&#8217; keyword is used in Java to implement an interface. This keyword appears in the class declaration after the &#8216;extends&#8217; part of the declaration.<\/p>\n<p><strong>Example Interface in Java programming<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\ninterface Wifi {\r\n    void connect();\r\n    void disconnect();\r\n}\r\n\r\nclass Mobile implements Wifi {\r\n\r\n    public void connect() {\r\n        System.out.println(&quot;Wifi connected&quot;);\r\n    }\r\n\r\n    public void disconnect() {\r\n        System.out.println(&quot;Wifi disconnected&quot;);\r\n    }\r\n\r\n    public static void main(String args&#x5B;]) {\r\n         Mobile mobile = new Mobile();\r\n        mobile.connect();\r\n        mobile.disconnect();\r\n    }\r\n}\r\n\r\n\/\/Output\r\n\/\/Wifi connected\r\n\/\/\/Wifi disconnected\r\n<\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Why_should_I_use_Interface_in_Java\"><\/span>Why should I use Interface in Java?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<ul>\n<li>Abstraction is achieved using an <strong>Interface in Java<\/strong>.<\/li>\n<li>To achieve loose coupling.<\/li>\n<li>Supports the functionality of multiple inheritances in Java.<\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"There_are_a_few_things_to_keep_in_mind_when_using_an_Interface_in_Java\"><\/span>There are a few things to keep in mind when using an Interface in Java<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ul>\n<li>The same signature must be used when overriding interface methods.<\/li>\n<li>Interface <a href=\"https:\/\/usemynotes.com\/what-are-methods-in-java\/\">methods<\/a> must be implemented in classes that are not abstract.<\/li>\n<li>Zero, one, or more interfaces can be implemented by a class at a time.<\/li>\n<li>An interface in Java can extend another interface just like a class extends another class.<\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Extending_an_Interface_in_Java\"><\/span>Extending an Interface in Java<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>An interface can extend another interface just like a class that extends another class. To extend an <strong>Interface in Java<\/strong>, the &#8216;extends&#8217; keyword is used.<\/p>\n<p><strong>Example<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\ninterface HotSpot {\r\n    void enableHotspot();\r\n    void disableHotspot();\r\n}\r\n\r\ninterface WirelessFeatures extends HotSpot {\r\n    void connectBluetooth();\r\n    void disconnectBluetooth();\r\n}\r\n\r\nclass Smartphone implements WirelessFeatures {\r\n\r\n    public void connectBluetooth() {\r\n        System.out.println(&quot;Bluetooth connected&quot;);\r\n    }\r\n\r\n    public void disconnectBluetooth() {\r\n        System.out.println(&quot;Bluetooth disconnected&quot;);\r\n    }\r\n\r\n    public void enableHotspot() {\r\n        System.out.println(&quot;Hotspot enabled&quot;);\r\n    }\r\n\r\n    public void disableHotspot() {\r\n        System.out.println(&quot;Hotspot disabled&quot;);\r\n    }\r\n\r\n    public static void main(String args&#x5B;]) {\r\n        Smartphone smartphone = new Smartphone();\r\n        smartphone.connectBluetooth();\r\n        smartphone.enableHotspot();\r\n        smartphone.disconnectBluetooth();\r\n        smartphone.disableHotspot();\r\n    }\r\n}\r\n\r\n\/\/Output\r\n\/\/ Bluetooth connected\r\n\/\/ Hotspot enabled\r\n\/\/ Bluetooth disconnected\r\n\/\/ Hotspot disabled\r\n<\/pre>\n<p>In the above example, The interface WirelessFeatures is extending the Hotspot <strong>interface in Java programming<\/strong>. Now the WirelessFeatures has a total of 4 abstract methods in it. The class Smartphone implements the WirelessFeatures interface so it must implement all the 4 abstract methods that the interface contains.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Extending_Multiple_Interfaces_in_Java_programming\"><\/span>Extending Multiple Interfaces in Java programming<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>A Java class can only extend one class as its parent because multiple <a href=\"https:\/\/usemynotes.com\/what-is-inheritance-in-java\/\">inheritances<\/a> are not allowed in Java. Interfaces are not classes so an interface can extend more than one interface at a time. Each of the parent interfaces in Java programming must be comma-separated.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-1071 size-full\" src=\"https:\/\/usemynotes.com\/wp-content\/uploads\/2021\/06\/extending-multiple-interfaces.jpg\" alt=\"Extending Multiple Interfaces in Java programming\" width=\"1000\" height=\"650\" title=\"\" srcset=\"https:\/\/usemynotes.com\/wp-content\/uploads\/2021\/06\/extending-multiple-interfaces.jpg 1000w, https:\/\/usemynotes.com\/wp-content\/uploads\/2021\/06\/extending-multiple-interfaces-300x195.jpg 300w, https:\/\/usemynotes.com\/wp-content\/uploads\/2021\/06\/extending-multiple-interfaces-768x499.jpg 768w, https:\/\/usemynotes.com\/wp-content\/uploads\/2021\/06\/extending-multiple-interfaces-150x98.jpg 150w, https:\/\/usemynotes.com\/wp-content\/uploads\/2021\/06\/extending-multiple-interfaces-696x452.jpg 696w, https:\/\/usemynotes.com\/wp-content\/uploads\/2021\/06\/extending-multiple-interfaces-646x420.jpg 646w\" sizes=\"(max-width: 1000px) 100vw, 1000px\" \/><\/p>\n<p><strong>Example<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\ninterface NFC {\r\n    void enableNFC();\r\n    void disableNFC();\r\n}\r\n\r\ninterface Internet {\r\n    void enableInternet();\r\n    void disableInternet();\r\n}\r\n\r\ninterface Wireless extends NFC, Internet {\r\n    boolean isWireless();\r\n}\r\n\r\nclass Smartphone implements Wireless {\r\n\r\n    public void enableNFC() {\r\n        System.out.println(&quot;NFC enabled&quot;);\r\n    }\r\n\r\n    public void disableNFC() {\r\n        System.out.println(&quot;NFC disabled&quot;);\r\n    }\r\n\r\n    public void enableInternet() {\r\n        System.out.println(&quot;Internet enabled&quot;);\r\n    }\r\n\r\n    public void disableInternet() {\r\n        System.out.println(&quot;Internet disabled&quot;);\r\n    }\r\n\r\n    public boolean isWireless() {\r\n        return true;\r\n    }\r\n\r\n    public static void main(String args&#x5B;]) {\r\n        Smartphone smartphone = new Smartphone();\r\n        System.out.println(&quot;Wireless: &quot; + smartphone.isWireless());\r\n        smartphone.enableNFC();\r\n        smartphone.disableNFC();\r\n        smartphone.enableInternet();\r\n        smartphone.disableInternet();\r\n    }\r\n}\r\n\r\n\/\/Output\r\n\/\/ Wireless: true\r\n\/\/ NFC enabled\r\n\/\/ NFC disabled\r\n\/\/ Internet enabled\r\n\/\/ Internet disabled\r\n<\/pre>\n<p>I hope at this point you have a better understanding of Why should I use <strong>Interface in Java<\/strong> programming? To learn more on various topics of Java keep following us. Thank you for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will discuss Why should I use Interface in Java programming. We will understand what they are, how to implement them, why they are used in Java programming, and much more. So, let&#8217;s begin with its introduction. Interface in Java Programming An Interface in Java is a fundamental concept in Java programming. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1070,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":{"0":"post-1069","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-java-programming"},"_links":{"self":[{"href":"https:\/\/usemynotes.com\/wp-json\/wp\/v2\/posts\/1069","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/usemynotes.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/usemynotes.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/usemynotes.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/usemynotes.com\/wp-json\/wp\/v2\/comments?post=1069"}],"version-history":[{"count":0,"href":"https:\/\/usemynotes.com\/wp-json\/wp\/v2\/posts\/1069\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/usemynotes.com\/wp-json\/wp\/v2\/media\/1070"}],"wp:attachment":[{"href":"https:\/\/usemynotes.com\/wp-json\/wp\/v2\/media?parent=1069"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/usemynotes.com\/wp-json\/wp\/v2\/categories?post=1069"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/usemynotes.com\/wp-json\/wp\/v2\/tags?post=1069"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}