{"id":1210,"date":"2022-02-25T16:52:13","date_gmt":"2022-02-25T16:52:13","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1210"},"modified":"2022-02-25T16:52:13","modified_gmt":"2022-02-25T16:52:13","slug":"java-interface","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/25\/java-interface\/","title":{"rendered":"Java Interface"},"content":{"rendered":"\n<p>In this tutorial, we will learn about Java interfaces. We will learn how to implement interfaces and when to use them in detail with the help of examples.<\/p>\n\n\n\n<p id=\"introduction\">An interface is a fully abstract class. It includes a group of abstract methods (methods without a body).<\/p>\n\n\n\n<p>We use the&nbsp;<code>interface<\/code>&nbsp;keyword to create an interface in Java. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>interface Language {\n  public void getType();\n\n  public void getVersion();\n}<\/code><\/pre>\n\n\n\n<p>Here,<\/p>\n\n\n\n<ul><li><var>Language<\/var>&nbsp;is an interface.<\/li><li>It includes abstract methods:&nbsp;<code>getType()<\/code>&nbsp;and&nbsp;<code>getVersion()<\/code>.<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"implement\">Implementing an Interface<\/h2>\n\n\n\n<p>Like abstract classes, we cannot create objects of interfaces.<\/p>\n\n\n\n<p>To use an interface, other classes must implement it. We use the&nbsp;<code>implements<\/code>&nbsp;keyword to implement an interface.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 1: Java Interface<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>interface Polygon {\n  void getArea(int length, int breadth);\n}\n\n\/\/ implement the Polygon interface\nclass Rectangle implements Polygon {\n\n  \/\/ implementation of abstract method\n  public void getArea(int length, int breadth) {\n    System.out.println(\"The area of the rectangle is \" + (length * breadth));\n  }\n}\n\nclass Main {\n  public static void main(String&#91;] args) {\n    Rectangle r1 = new Rectangle();\n    r1.getArea(5, 6);\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>The area of the rectangle is 30<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we have created an interface named&nbsp;<var>Polygon<\/var>. The interface contains an abstract method&nbsp;<code>getArea()<\/code>.<\/p>\n\n\n\n<p>Here, the&nbsp;<var>Rectangle<\/var>&nbsp;class implements&nbsp;<var>Polygon<\/var>. And, provides the implementation of the&nbsp;<code>getArea()<\/code>&nbsp;method.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example\">Example 2: Java Interface<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ create an interface\ninterface Language {\n  void getName(String name);\n}\n\n\/\/ class implements interface\nclass ProgrammingLanguage implements Language {\n\n  \/\/ implementation of abstract method\n  public void getName(String name) {\n    System.out.println(\"Programming Language: \" + name);\n  }\n}\n\nclass Main {\n  public static void main(String&#91;] args) {\n    ProgrammingLanguage language = new ProgrammingLanguage();\n    language.getName(\"Java\");\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Programming Language: Java<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we have created an interface named&nbsp;<var>Language<\/var>. The interface includes an abstract method&nbsp;<code>getName()<\/code>.<\/p>\n\n\n\n<p>Here, the&nbsp;<var>ProgrammingLanguage<\/var>&nbsp;class implements the interface and provides the implementation for the method.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Implementing Multiple Interfaces<\/h3>\n\n\n\n<p>In Java, a class can also implement multiple interfaces. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>interface A {\n  \/\/ members of A\n}\n\ninterface B {\n  \/\/ members of B\n}\n\nclass C implements A, B {\n  \/\/ abstract members of A\n  \/\/ abstract members of B\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"extend\">Extending an Interface<\/h2>\n\n\n\n<p>Similar to classes, interfaces can extend other interfaces. The&nbsp;<code>extends<\/code>&nbsp;keyword is used for extending interfaces. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>interface Line {\n  \/\/ members of Line interface\n}\n\n\/\/ extending interface\ninterface Polygon extends Line {\n  \/\/ members of Polygon interface\n  \/\/ members of Line interface\n}<\/code><\/pre>\n\n\n\n<p>Here, the&nbsp;<var>Polygon<\/var>&nbsp;interface extends the&nbsp;<var>Line<\/var>&nbsp;interface. Now, if any class implements&nbsp;<var>Polygon<\/var>, it should provide implementations for all the abstract methods of both&nbsp;<var>Line<\/var>&nbsp;and&nbsp;<var>Polygon<\/var>.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Extending Multiple Interfaces<\/h3>\n\n\n\n<p>An interface can extend multiple interfaces. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>interface A {\n   ...\n}\ninterface B {\n   ... \n}\n\ninterface C extends A, B {\n   ...\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"advantage\">Advantages of Interface in Java<\/h2>\n\n\n\n<p>Now that we know what interfaces are, let&#8217;s learn about why interfaces are used in Java.<\/p>\n\n\n\n<ul><li>Similar to abstract classes, interfaces help us to achieve&nbsp;<strong>abstraction in Java<\/strong>.<br><br>Here, we know&nbsp;<code>getArea()<\/code>&nbsp;calculates the area of polygons but the way area is calculated is different for different polygons. Hence, the implementation of&nbsp;<code>getArea()<\/code>&nbsp;is independent of one another.<\/li><li>Interfaces&nbsp;<strong>provide specifications<\/strong>&nbsp;that a class (which implements it) must follow.<br><br>In our previous example, we have used&nbsp;<code>getArea()<\/code>&nbsp;as a specification inside the interface&nbsp;<var>Polygon<\/var>. This is like setting a rule that we should be able to get the area of every polygon.<br><br>Now any class that implements the&nbsp;<var>Polygon<\/var>&nbsp;interface must provide an implementation for the&nbsp;<code>getArea()<\/code>&nbsp;method.<\/li><li>Interfaces are also used to achieve multiple inheritance in Java. For example,<br>&nbsp;<code>interface Line { \u2026 } interface Polygon { \u2026 } class Rectangle implements Line, Polygon { \u2026 }<\/code><br><br>Here, the class&nbsp;<var>Rectangle<\/var>&nbsp;is implementing two different interfaces. This is how we achieve multiple inheritance in Java.<\/li><\/ul>\n\n\n\n<p><strong>Note<\/strong>: All the methods inside an interface are implicitly&nbsp;<code>public<\/code>&nbsp;and all fields are implicitly&nbsp;<code>public static final<\/code>. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>interface Language {\n  \n  \/\/ by default public static final\n  String type = \"programming language\";\n\n  \/\/ by default public\n  void getName();\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"default-methods\">default methods in Java Interfaces<\/h2>\n\n\n\n<p>With the release of Java 8, we can now add methods with implementation inside an interface. These methods are called default methods.<\/p>\n\n\n\n<p>To declare default methods inside interfaces, we use the&nbsp;<code>default<\/code>&nbsp;keyword. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public default void getSides() {\n   \/\/ body of getSides()\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Why default methods?<\/h3>\n\n\n\n<p>Let&#8217;s take a scenario to understand why default methods are introduced in Java.<\/p>\n\n\n\n<p>Suppose, we need to add a new method in an interface.<\/p>\n\n\n\n<p>We can add the method in our interface easily without implementation. However, that&#8217;s not the end of the story. All our classes that implement that interface must provide an implementation for the method.<\/p>\n\n\n\n<p>If a large number of classes were implementing this interface, we need to track all these classes and make changes to them. This is not only tedious but error-prone as well.<\/p>\n\n\n\n<p>To resolve this, Java introduced default methods. Default methods are inherited like ordinary methods.<\/p>\n\n\n\n<p>Let&#8217;s take an example to have a better understanding of default methods.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Example: Default Method in Java Interface<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>interface Polygon {\n  void getArea();\n\n  \/\/ default method \n  default void getSides() {\n    System.out.println(\"I can get sides of a polygon.\");\n  }\n}\n\n\/\/ implements the interface\nclass Rectangle implements Polygon {\n  public void getArea() {\n    int length = 6;\n    int breadth = 5;\n    int area = length * breadth;\n    System.out.println(\"The area of the rectangle is \" + area);\n  }\n\n  \/\/ overrides the getSides()\n  public void getSides() {\n    System.out.println(\"I have 4 sides.\");\n  }\n}\n\n\/\/ implements the interface\nclass Square implements Polygon {\n  public void getArea() {\n    int length = 5;\n    int area = length * length;\n    System.out.println(\"The area of the square is \" + area);\n  }\n}\n\nclass Main {\n  public static void main(String&#91;] args) {\n\n    \/\/ create an object of Rectangle\n    Rectangle r1 = new Rectangle();\n    r1.getArea();\n    r1.getSides();\n\n    \/\/ create an object of Square\n    Square s1 = new Square();\n    s1.getArea();\n    s1.getSides();\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>The area of the rectangle is 30\nI have 4 sides.\nThe area of the square is 25\nI can get sides of a polygon.<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we have created an interface named&nbsp;<var>Polygon<\/var>. It has a default method&nbsp;<code>getSides()<\/code>&nbsp;and an abstract method&nbsp;<code>getArea()<\/code>.<\/p>\n\n\n\n<p>Here, we have created two classes&nbsp;<var>Rectangle<\/var>&nbsp;and&nbsp;<var>Square<\/var>&nbsp;that implement&nbsp;<var>Polygon<\/var>.<\/p>\n\n\n\n<p>The&nbsp;<var>Rectangle<\/var>&nbsp;class provides the implementation of the&nbsp;<code>getArea()<\/code>&nbsp;method and overrides the&nbsp;<code>getSides()<\/code>&nbsp;method. However, the&nbsp;<var>Square<\/var>&nbsp;class only provides the implementation of the&nbsp;<code>getArea()<\/code>&nbsp;method.<\/p>\n\n\n\n<p>Now, while calling the&nbsp;<code>getSides()<\/code>&nbsp;method using the&nbsp;<var>Rectangle<\/var>&nbsp;object, the overridden method is called. However, in the case of the&nbsp;<var>Square<\/var>&nbsp;object, the default method is called.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"private-static-methods\">private and static Methods in Interface<\/h2>\n\n\n\n<p>The Java 8 also added another feature to include static methods inside an interface.<\/p>\n\n\n\n<p>Similar to a class, we can access static methods of an interface using its references. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ create an interface\ninterface Polygon {\n  staticMethod(){..}\n}\n\n\/\/ access static method\nPolygon.staticMethod();<\/code><\/pre>\n\n\n\n<p><strong>Note<\/strong>: With the release of Java 9, private methods are also supported in interfaces.<\/p>\n\n\n\n<p>We cannot create objects of an interface. Hence, private methods are used as helper methods that provide support to other methods in interfaces.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"practical-example\">Practical Example of Interface<\/h3>\n\n\n\n<p>Let&#8217;s see a more practical example of Java Interface.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ To use the sqrt function\nimport java.lang.Math;\n\ninterface  Polygon {\n   void getArea();\n  \n \/\/ calculate the perimeter of a Polygon\n   default void getPerimeter(int... sides) {\n      int perimeter = 0;\n      for (int side: sides) {\n         perimeter += side;\n      }\n\n   System.out.println(\"Perimeter: \" + perimeter);\n   }\n}\n\nclass Triangle implements Polygon {\n   private int a, b, c;\n   private double s, area;\n\n\/\/ initializing sides of a triangle\n   Triangle(int a, int b, int c) {\n      this.a = a;\n      this.b = b;\n      this.c = c;\n      s = 0;\n   }\n\n\/\/ calculate the area of a triangle\n   public void getArea() {\n      s = (double) (a + b + c)\/2;\n      area = Math.sqrt(s*(s-a)*(s-b)*(s-c));\n      System.out.println(\"Area: \" + area);\n   }\n}\n\nclass Main {\n   public static void main(String&#91;] args) {\n      Triangle t1 = new Triangle(2, 3, 4);\n\n\/\/ calls the method of the Triangle class\n      t1.getArea();\n\n\/\/ calls the method of Polygon\n      t1.getPerimeter(2, 3, 4);\n   }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Area: 2.9047375096555625\nPerimeter: 9<\/samp><\/code><\/pre>\n\n\n\n<p>In the above program, we have created an interface named&nbsp;<var>Polygon<\/var>. It includes a default method&nbsp;<code>getPerimeter()<\/code>&nbsp;and an abstract method&nbsp;<code>getArea()<\/code>.<\/p>\n\n\n\n<p>We can calculate the perimeter of all polygons in the same manner so we implemented the body of&nbsp;<code>getPerimeter()<\/code>&nbsp;in&nbsp;<var>Polygon<\/var>.<\/p>\n\n\n\n<p>Now, all polygons that implement&nbsp;<var>Polygon<\/var>&nbsp;can use&nbsp;<code>getPerimeter()<\/code>&nbsp;to calculate perimeter.<\/p>\n\n\n\n<p>However, the rule for calculating the area is different for different polygons. Hence,&nbsp;<code>getArea()<\/code>&nbsp;is included without implementation.<\/p>\n\n\n\n<p>Any class that implements&nbsp;<var>Polygon<\/var>&nbsp;must provide an implementation of&nbsp;<code>getArea()<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will learn about Java interfaces. We will learn how to implement interfaces and when to use them in detail with the help of examples. An interface is a fully abstract class. It includes a group of abstract methods (methods without a body). We use the&nbsp;interface&nbsp;keyword to create an interface in Java. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[531],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1210"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=1210"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1210\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1210"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1210"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1210"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}