{"id":5241,"date":"2021-03-23T15:45:11","date_gmt":"2021-03-23T15:45:11","guid":{"rendered":"https:\/\/ded9.com\/?p=5241"},"modified":"2025-11-05T10:28:57","modified_gmt":"2025-11-05T10:28:57","slug":"interface-training-in-java-in-quite-simple-language","status":"publish","type":"post","link":"https:\/\/ded9.com\/de\/interface-training-in-java-in-quite-simple-language\/","title":{"rendered":"Interface Training in Java \u2014 Explained in Simple Language"},"content":{"rendered":"<p><span style=\"font-size: 18pt;\"><span style=\"font-size: 12pt;\">In this tutorial, we will learn about Java interfaces, how to implement them, and when to use them.<\/span><span id=\"more-50170\"><\/span><\/span><\/p>\n<p><span style=\"font-size: 12pt;\">In Java, an interface defines a set of attributes that other <a href=\"https:\/\/ded9.com\/nested-and-inner-classes-in-java\/\">classes<\/a> must implement.<\/span><\/p>\n<p>For example,<\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">Polygon interface {<\/li>\n<li dir=\"ltr\">public void getArea ();<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<p>Here, Polygon is an interface.\u00a0We used the keyword interface to define the interface.<\/p>\n<p><iframe title=\"Interface In Java | Java Interface Tutorial | Java Interface Example | Java Tutorial | Simplilearn\" width=\"800\" height=\"450\" src=\"https:\/\/www.youtube.com\/embed\/K8P_1Urq9uY?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<p>The getArea () method is a property specified in the <a href=\"https:\/\/www.polygon.com\/\" target=\"_blank\" rel=\"noopener\">Polygon<\/a> interface.\u00a0All classes that use this interface must implement the getArea () method.<\/p>\n<p>The interface can include abstract and fixed methods.\u00a0For example,<\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">Polygon interface {<\/li>\n<li dir=\"ltr\">public static final String color = \u201cblue\u201d;<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">public void getArea ();<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<p>In the example above, we have created a Polygon interface that consists of the color constant variable and the abstract method getArea ().<\/p>\n<p>It is important to note that all methods within an interface are public by default, and all fields are public static by default. Therefore, it is unnecessary to specify the interfaces&#8217; access level. For example, we can write the above code as follows:<\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">Polygon interface {<\/li>\n<li dir=\"ltr\">String color = \u201cblue\u201d;<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">void getArea ();<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<h2><span style=\"font-size: 18pt;\">Keyword implementation in the interface<\/span><\/h2>\n<h1><span style=\"font-size: 18pt;\"><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-258006 size-full\" title=\"Interface \" src=\"https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/2022_06_Interface-in-Java.jpg\" alt=\"Keyword implementation in the interface\" width=\"1200\" height=\"630\" srcset=\"https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/2022_06_Interface-in-Java.jpg 1200w, https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/2022_06_Interface-in-Java-300x158.jpg 300w, https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/2022_06_Interface-in-Java-1024x538.jpg 1024w, https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/2022_06_Interface-in-Java-768x403.jpg 768w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/span><\/h1>\n<p>Like abstract classes, we cannot create objects from the interface. However, we can implement interfaces in other courses. In Java, we use the keyword implements to implement interfaces. For example,<\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">Polygon interface {<\/li>\n<li dir=\"ltr\">void getArea ();<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">class Rectangle implements Polygon {<\/li>\n<li dir=\"ltr\">public void getArea (int length 5, int breadth = 6) {<\/li>\n<li dir=\"ltr\">int length = 5;<\/li>\n<li dir=\"ltr\">int breadth = 6;<\/li>\n<li dir=\"ltr\">System.out.println (\u201cThe area of \u200b\u200bthe rectangle is\u201d + (length * breadth));<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">class Main {<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\">Rectangle r1 = new Rectangle ();<\/li>\n<li dir=\"ltr\">r1.getArea (5, 6);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<p>Output<\/p>\n<p dir=\"ltr\">The area of \u200b\u200bthe Rectangle is 30<\/p>\n<p>In the above program, we have created a Polygon interface with an abstract method, getArea ().<\/p>\n<p>This means that each class that implements Polygon must provide an execution for the getArea () method.<\/p>\n<p>Note that the Rectangle class (which implements the Polygon interface) implements the getArea () method.<\/p>\n<h2><span style=\"font-size: 18pt;\">Why do we use interfaces?<\/span><\/h2>\n<h1><span style=\"font-size: 18pt;\"><img decoding=\"async\" class=\"aligncenter wp-image-258011 size-full\" src=\"https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/interface-inventory-67580fcc.jpg\" alt=\"Why do we use interfaces?\" width=\"1000\" height=\"628\" srcset=\"https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/interface-inventory-67580fcc.jpg 1000w, https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/interface-inventory-67580fcc-300x188.jpg 300w, https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/interface-inventory-67580fcc-768x482.jpg 768w\" sizes=\"(max-width: 1000px) 100vw, 1000px\" \/><\/span><\/h1>\n<p>Now that we know what interfaces are, let&#8217;s learn why they are used in Java.<\/p>\n<p>Interfaces provide properties that a class (which uses them) must follow.<\/p>\n<p>In the example above, we used getArea () as a property inside the Polygon interface. This is like setting a Rule that we should be able to get the area of \u200b\u200ba polygon. Therefore, any class that implements the Polygon interface must provide an execution for the getArea () method.<\/p>\n<p>Similar to abstract classes, interfaces help us achieve abstraction in Java. Here, we know that getArea () calculates the area of \u200b\u200bpolygons, but the method for calculating the area for different polygons is different. Hence, the getArea ()implementations are independent of each other.<\/p>\n<p>Interfaces are also used to achieve multiple inheritance in Java. If a subclass inherits from two or more classes, it is a multiple inheritance.<\/p>\n<p>In Java, multiple inheritance is not possible. However, a class can implement multiple interfaces to achieve various inheritance functionality in Java. For example,<\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">interface Line {<\/li>\n<li dir=\"ltr\">\u2026<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">Polygon interface {<\/li>\n<li dir=\"ltr\">\u2026<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">class Rectangle implements Line, Polygon {<\/li>\n<li dir=\"ltr\">\u2026<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<p>Here, Rectangle must implement all Line and Polygon methods.<\/p>\n<h2><span style=\"font-size: 18pt;\">Private and static methods in the interface<\/span><\/h2>\n<p>With the release of Java 8, interfaces can now include static methods.<\/p>\n<p>Like a class, we can access an interface using references to static methods.\u00a0For example,<\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\" style=\"text-align: left;\">Polygon.staticMethod ();<\/li>\n<\/ol>\n<\/blockquote>\n<p>Interfaces also support private methods with the release of Java 9.\u00a0You can now use private methods and private static methods in interfaces.<\/p>\n<p>Because interfaces cannot be defined, private methods are used as auxiliary methods that provide support for other interface methods.<\/p>\n<h2>Default methods in interfaces<\/h2>\n<p>With the release of Java 8, implementation methods (default methods) were introduced within an interface.\u00a0Before that, all methods in Java were abstract.<\/p>\n<p>We use the default keyword to define the default methods in the interfaces. For example,<\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">public default void getSides () {<\/li>\n<li dir=\"ltr\">\/\/ body of getSides ()<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<h2><span style=\"font-size: 18pt;\">Why default methods?<\/span><\/h2>\n<p>Let&#8217;s find out why the default methods are introduced in Java using a scenario.<\/p>\n<p>Suppose we need to add a new method to an interface.<\/p>\n<p>We can easily add the method in the user interface without execution. However, this is not the end of the story. All classes that implement that interface must provide method implementation.<\/p>\n<p>If a large number of classes were running this interface, we would have to follow all of them and make changes to them. This is not only tedious but also error-prone.<\/p>\n<p>To solve this problem, Java introduced the default methods.\u00a0Default methods are inherited like regular methods.<\/p>\n<p>Let&#8217;s take an example to understand the default methods better.<\/p>\n<p><strong>Example 2: Default method<\/strong><\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">Polygon interface {<\/li>\n<li dir=\"ltr\">void getArea ();<\/li>\n<li dir=\"ltr\">default void getSides () {<\/li>\n<li dir=\"ltr\">System.out.println (\u201cI can get sides of polygon.\u201d);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">class Rectangle implements Polygon {<\/li>\n<li dir=\"ltr\">public void getArea () {<\/li>\n<li dir=\"ltr\">int length = 6;<\/li>\n<li dir=\"ltr\">int breadth = 5;<\/li>\n<li dir=\"ltr\">int area = length * breadth;<\/li>\n<li dir=\"ltr\">System.out.println (&#8220;The area of \u200b\u200bthe rectangle is&#8221; + area);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">public void getSides () {<\/li>\n<li dir=\"ltr\">System.out.println (\u201cI have 4 sides.\u201d);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">class Square implements Polygon {<\/li>\n<li dir=\"ltr\">public void getArea () {<\/li>\n<li dir=\"ltr\">int length = 5;<\/li>\n<li dir=\"ltr\">int area = length * length;<\/li>\n<li dir=\"ltr\">System.out.println (&#8220;The area of \u200b\u200bthe square is&#8221; + area);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">class Main {<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\">Rectangle r1 = new Rectangle ();<\/li>\n<li dir=\"ltr\">r1.getArea ();<\/li>\n<li dir=\"ltr\">r1.getSides ();<\/li>\n<li dir=\"ltr\">Square s1 = new Square ();<\/li>\n<li dir=\"ltr\">s1.getArea ();<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<p>Output<\/p>\n<blockquote>\n<p dir=\"ltr\">The area of \u200b\u200bthe rectangle is 30<\/p>\n<p dir=\"ltr\">I have 4 sides<\/p>\n<p dir=\"ltr\">The area of \u200b\u200bthe square is 25<\/p>\n<\/blockquote>\n<p>In the example above, we created the Polygon () interface. A polygon has a default getSides () method and an abstract getArea () method.<\/p>\n<p>The Rectangle class then implements the Polygon. The Rectangle implements the abstract getArea () method and ignores the default getSide () method.<\/p>\n<p>We have created another Square class that also implements Polygon.\u00a0Here, Square only offers the execution of the getArea () abstract method.<\/p>\n<h2><strong>Practical interface example<\/strong><\/h2>\n<p>Let&#8217;s look at a more practical example of a Java interface.<\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">\/\/ To use the sqrt function<\/li>\n<li dir=\"ltr\">import java.lang.Math;<\/li>\n<li dir=\"ltr\">Polygon interface {<\/li>\n<li dir=\"ltr\">void getArea ();<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">\/\/ calculate the perimeter of a Polygon<\/li>\n<li dir=\"ltr\">default void getPerimeter (int\u2026 sides) {<\/li>\n<li dir=\"ltr\">int perimeter = 0;<\/li>\n<li dir=\"ltr\">for (int side: sides) {<\/li>\n<li dir=\"ltr\">perimeter + = side;<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">System.out.println (&#8220;Perimeter:&#8221; + perimeter);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">class Triangle implements Polygon {<\/li>\n<li dir=\"ltr\">private int a, b, c;<\/li>\n<li dir=\"ltr\">private double s, area;<\/li>\n<li dir=\"ltr\">\/\/ initializing sides of a triangle<\/li>\n<li dir=\"ltr\">Triangle (int a, int b, int c) {<\/li>\n<li dir=\"ltr\">this.a = a;<\/li>\n<li dir=\"ltr\">this.b = b;<\/li>\n<li dir=\"ltr\">this.c = c;<\/li>\n<li dir=\"ltr\">s = 0;<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">\/\/ calculate the area of \u200b\u200ba triangle<\/li>\n<li dir=\"ltr\">public void getArea () {<\/li>\n<li dir=\"ltr\">s = (double) (a + b + c) \/ 2;<\/li>\n<li dir=\"ltr\">area = Math.sqrt (s * (sa) * (sb) * (sc));<\/li>\n<li dir=\"ltr\">System.out.println (\u201cArea:\u201d + area);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">class Main {<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\">Triangle t1 = new Triangle (2, 3, 4);<\/li>\n<li dir=\"ltr\">\/\/ calls the method of the Triangle class<\/li>\n<li dir=\"ltr\">t1.getArea ();<\/li>\n<li dir=\"ltr\">\/\/ calls the method of Polygon<\/li>\n<li dir=\"ltr\">t1.getPerimeter (2, 3, 4);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<p>Output<\/p>\n<blockquote>\n<p dir=\"ltr\">Area: 2.9047375096555625<\/p>\n<p dir=\"ltr\">Perimeter: 9<\/p>\n<\/blockquote>\n<p>In the above program, we have created a Polygon interface that includes a default getParameter() method and an abstract getArea() method.<\/p>\n<p>We can calculate the circumference of all polygons similarly, so we implemented the getPerimeter () method in Polygon. All polygons that implement Polygon can use getPerimeter () to calculate the perimeter.<\/p>\n<p>However, the environment is calculated differently for different polygons because the Rule for calculating the environment differs. Hence, getArea () is included in Polygon without execution. Any class that implements the Polygon interface must implement getArea ().<\/p>\n<h2><span style=\"font-size: 18pt;\">Keyword extends in the interface.<\/span><\/h2>\n<p>Like classes, interfaces can develop other interfaces.\u00a0The extends keyword is used to extend interfaces.\u00a0For example,<\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">interface Line {<\/li>\n<li dir=\"ltr\">\/\/ members of Line interface<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">interface Polygon extends Line {<\/li>\n<li dir=\"ltr\">\/\/ members of Polygon interface and Line interface<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<p>In the example above, the Polygon interface extends the Line interface. If a class implements a Polygon, it must execute all the abstract Line and Polygon classes.<\/p>\n<p>Note that an interface can extend multiple interfaces, similar to a class that implements various interfaces. For example,<\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">interface A {<\/li>\n<li dir=\"ltr\">\u2026<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">interface B {<\/li>\n<li dir=\"ltr\">\u2026<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">Interface C extends A, B {<\/li>\n<li dir=\"ltr\">\u2026<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<h2>FAQ<\/h2>\n<div id=\"rank-math-rich-snippet-wrapper\"><div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-1\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">What is an interface in Java?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>An interface defines a contract (methods) that classes agree to implement without specifying how those methods work.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-2\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">How do you implement an interface?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Use the implements keyword in the class definition and then provide concrete method bodies for all interface methods.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-3\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">Can a class implement multiple interfaces?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Yes \u2014 in Java, a class can implement more than one interface, allowing it to combine different behaviors.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will learn about Java interfaces, how to implement them, and when to use them. In Java, an interface defines a set of attributes that other classes must implement. For example, Polygon interface { public void getArea (); } Here, Polygon is an interface.\u00a0We used the keyword interface to define the interface. [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":5242,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[11513],"tags":[840],"class_list":["post-5241","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-2","tag-java"],"acf":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/posts\/5241","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/comments?post=5241"}],"version-history":[{"count":8,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/posts\/5241\/revisions"}],"predecessor-version":[{"id":265019,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/posts\/5241\/revisions\/265019"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/media\/5242"}],"wp:attachment":[{"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/media?parent=5241"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/categories?post=5241"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/tags?post=5241"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}