{"id":10476,"date":"2020-09-19T14:19:44","date_gmt":"2020-09-19T08:49:44","guid":{"rendered":"https:\/\/java2blog.com\/?p=10476"},"modified":"2021-01-11T18:29:34","modified_gmt":"2021-01-11T12:59:34","slug":"java-9-private-methods-interface","status":"publish","type":"post","link":"https:\/\/java2blog.com\/java-9-private-methods-interface\/","title":{"rendered":"Java 9 &#8211; Private methods in interface"},"content":{"rendered":"<p>In this topic, we will discuss the <code>private<\/code> and <code>private static methods<\/code> inside an interface.<\/p>\n<div id=\"toc_container\" class=\"toc_light_blue no_bullets\"><p class=\"toc_title\">Table of Contents<\/p><ul class=\"toc_list\"><li><a href=\"#Interface_Private_Method\">Interface Private Method<\/a><\/li><li><a href=\"#Interface_Private_Static_Methods\">Interface Private Static Methods<\/a><\/li><li><a href=\"#Key-points_to_remember\">Key-points to remember<\/a><\/li><li><a href=\"#Conclusion\">Conclusion<\/a><\/li><\/ul><\/div>\n\n<p><a href=\"https:\/\/java2blog.com\/interface-in-java-with-example\" target=\"_blank\" rel=\"noopener noreferrer\">interface<\/a> in Java is a concept which is used to achieve abstraction. In early versions of Java, an <code>interface<\/code> can contain only abstract methods and constants until <a href=\"https:\/\/java2blog.com\/java-8-tutorial\/\" target=\"_blank\" rel=\"noopener noreferrer\">Java 8<\/a> which allows to add <a href=\"https:\/\/java2blog.com\/interface-default-methods-in-java-8\" target=\"_blank\" rel=\"noopener noreferrer\">default and static methods<\/a> as well.<\/p>\n<p>In Java 9, <code>interface<\/code> allows creating <code>private and private static methods<\/code>. It means now an interface can have the following:<\/p>\n<ol start=\"2\">\n<li>Abstract methods<\/li>\n<li>Constant variables<\/li>\n<li>Default methods (added in Java 8)<\/li>\n<li>Static methods (added in Java 8)<\/li>\n<li>Private methods (added in Java 9)<\/li>\n<li>Private static methods (added in Java 9)<\/li>\n<\/ol>\n<p>Now, let&#8217;s understand what is private method and why it is important.<\/p>\n<h2><span id=\"Interface_Private_Method\">Interface Private Method<\/span><\/h2>\n<p>A <code>private method<\/code> declared inside an interface is similar to declared inside a class. It is declared using private <a href=\"https:\/\/java2blog.com\/access-modifiers-java\/\" target=\"_blank\" rel=\"noopener noreferrer\">access modifier<\/a> to keep it limited to the interface. These methods can&#8217;t be accessible outside the <code>interface<\/code> and don&#8217;t inherit to the interface or implementing class.<\/p>\n<p>The primary purpose behind to add private method was to share common code between non-abstract methods within the interface. Let&#8217;s see an example.<\/p>\n<pre code=\"java\">\ninterface Drinkable{\n    default void breakTime() {\n        coffee();\n    }\n    private void coffee() {\n        System.out.println(\"Let's have a Coffee\");\n    }\n}\n\npublic class Main implements Drinkable{\n    public static void main(String[] args){\n        Main main = new Main();\n        main.breakTime();\n    }\n}\n<\/pre>\n<p>Output:<\/p>\n<div class=\"content-box-green\">\nLet&#8217;s have a Coffee\n<\/div>\n<h2><span id=\"Interface_Private_Static_Methods\">Interface Private Static Methods<\/span><\/h2>\n<p>Like private instance methods, private methods can be <a href=\"https:\/\/java2blog.com\/static-keyword-in-java\/\" target=\"_blank\" rel=\"noopener noreferrer\">static<\/a> as well. A private static method can not be accessible from outside the interface and can only be accessible inside the default or static methods. <\/p>\n<pre code=\"java\">\ninterface Drinkable{\n    default void breakTime() {\n        coffee();\n        tea();\n    }\n    private void coffee() {\n        System.out.println(\"Let's have a Coffee\");\n    }\n    private static void tea() {\n        System.out.println(\"Let's have a Tea\");\n    }\n}\n\npublic class Main implements Drinkable{\n    public static void main(String[] args){\n        Main main = new Main();\n        main.breakTime();\n    }\n}\n<\/pre>\n<p>Output:<\/p>\n<div class=\"content-box-green\">\nLet&#8217;s have a Coffee<br \/>\nLet&#8217;s have a Tea\n<\/div>\n<p>If we want to share code between <code>instance methods<\/code>, private instance methods and private static methods both can be used. But If we want to share code between <code>static methods<\/code>, use private static methods only. See, what happens, if we try to access a <code>private method<\/code> inside a <code>static method<\/code>.<\/p>\n<pre code=\"java\">\ninterface Drinkable{\n    static void breakTime() {\n        coffee(); \/\/ Error : Cannot make a static reference to the non-static method\n    }\n\n    private void coffee() {\n        System.out.println(\"Let's have a Coffee\");\n    }\n}\n\npublic class Main implements Drinkable{\n    public static void main(String[] args){\n        Main main = new Main();\n        \/\/main.breakTime();\n    }\n}\n<\/pre>\n<p>Output:<\/p>\n<div class=\"content-box-green\">\nError : Cannot make a static reference to the non-static method\n<\/div>\n<h2><span id=\"Key-points_to_remember\">Key-points to remember<\/span><\/h2>\n<ol>\n<li><code>Interface private methods<\/code> must have implementation body; they can&#8217;t be declared as abstract methods.<\/li>\n<li>A <code>static method<\/code> can call only private static method, but a default method can call both the static and non-static(instance) private methods.<\/li>\n<li>The <code>interface private methods<\/code> support sharing common code between non-abstract methods(default, static and private).<\/li>\n<\/ol>\n<h2><span id=\"Conclusion\">Conclusion<\/span><\/h2>\n<p>This topic is all about the private methods in the <code>interface<\/code>. This feature makes the interface more powerful and advance. Now, interface supports both <code>private and private static<\/code> methods within an interface.<\/p>\n<p>That&#8217;s all about Java 9 Private methods in interface.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Table of ContentsInterface Private MethodInterface Private Static MethodsKey-points to rememberConclusion In this topic, we will discuss the private and private static methods inside an interface. interface in Java is a concept which is used to achieve abstraction. In early versions of Java, an interface can contain only abstract methods and constants until Java 8 which [&hellip;]<\/p>\n","protected":false},"author":12,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_mi_skip_tracking":false},"categories":[201],"tags":[],"_links":{"self":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/10476"}],"collection":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/comments?post=10476"}],"version-history":[{"count":0,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/10476\/revisions"}],"wp:attachment":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media?parent=10476"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/categories?post=10476"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/tags?post=10476"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}