{"id":1874,"date":"2018-12-19T12:00:00","date_gmt":"2018-12-19T11:00:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/interfaces-in-java-basics-of-java-interface\/"},"modified":"2026-07-05T03:21:36","modified_gmt":"2026-07-05T01:21:36","slug":"interfaces-in-java-basics-of-java-interface","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/interfaces-in-java-basics-of-java-interface\/","title":{"rendered":"Interfaces in Java (Basics of Java Interface)"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-234 aligncenter\" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/Java-Interfaces-300x134.jpg\" alt=\"\" width=\"300\" height=\"134\" \/><\/p>\n<p>We would cover the following:<\/p>\n<ol>\n<li><a href=\"#t1\">What are Interfaces in Java<\/a><\/li>\n<li><a href=\"#t2\">What are Differences between a Class and an Interface<\/a><\/li>\n<li><a href=\"#t3\">How to Declare an Interface<\/a><\/li>\n<li><a href=\"#t4\">Why Interfaces?<\/a><\/li>\n<li><a href=\"#t5\">Implementing an Interface<\/a><\/li>\n<li><a href=\"#t6\">Extending an Interface<\/a><\/li>\n<li><a href=\"#t7\">Implementing an Extended Interface<\/a><\/li>\n<li><a href=\"#t8\">Some Quiz on Java Interfaces<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p><strong id=\"t1\">1. What are Interfaces in Java?<\/strong><\/p>\n<p>An interface in Java is similar to a class. It is created the same way as as a class. However, an interface is a reference type(this means that it is not a primitive type). An interface can be seen as a collection of abstract methods. An abstract method is a method that is declared with no implementation.<\/p>\n<p>An interface must be inherited(implemented) for it to be used. So a class that implements an interface, would inherit all the abstract methods and must then provide definition for these methods.<\/p>\n<p>&nbsp;<\/p>\n<p><strong id=\"t2\">2. Difference Between a Class and an Interface<\/strong><\/p>\n<p>And interface in Java is similar to a class but take note of the following differences:<\/p>\n<ul>\n<li>An interface cannot be instantiated. This means that you cannot create an object of an interface type.<\/li>\n<li>An interface cannot contain any constructors<\/li>\n<li>All the methods of an interface are implicitly abstract(declared with no implementation)<\/li>\n<li>Any property(field) in an interface must be declared as both static and final. No instance fields in an interface.<\/li>\n<li>A class implements an interface (not extends it)<\/li>\n<li>An interface can extend more than one interfaces. This is a way to achieve multiple inheritance in Java.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><strong id=\"t3\">3. How to Declare and Interface in Java<\/strong><\/p>\n<p>The interface keyword is used to declare an interface in java. In the code below, an interface Shape is declared with two abstract methods<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/*<\/span>\n<span style=\"color: #888888;\"> * Written by: Kindson The Genius<\/span>\n<span style=\"color: #888888;\"> * Date: 19th Dec. 2018<\/span>\n<span style=\"color: #888888;\"> *\/<\/span>\n\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">interface<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Shape<\/span> <span style=\"color: #333333;\">{<\/span>\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">Draw<\/span><span style=\"color: #333333;\">();<\/span>\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #333399; font-weight: bold;\">double<\/span> <span style=\"color: #0066bb; font-weight: bold;\">CalculateArea<\/span><span style=\"color: #333333;\">();<\/span>\n<span style=\"color: #333333;\">}<\/span>\n<\/pre>\n<\/div>\n<p>Listing 1.0: Interface shape<\/p>\n<p>&nbsp;<\/p>\n<p><strong id=\"t4\">4. Why Interfaces?<\/strong><\/p>\n<p>Why do we need to have an interface with only definitions with no implementations? Take a look at the program of Listing 1.0. We have a class Shape which represents shape objects. How to draw a shape would depend on the type of shape object. For example, how to draw a triangle would be different from how to draw a circle. In the same way, the calculation of the area of a triangle would be different from the formula for area of a circle. Therefore it makes sense to put the implementations of this method in the particular shape class that implements shape.<\/p>\n<p>&nbsp;<\/p>\n<p><strong id=\"t5\">5. Implementing an Interface<\/strong><\/p>\n<p>An interface is implemented by a class using the implements keyword followed by the name of the interface. Listing 1.1 shows the classes triangle and circle that inherits from the class shape.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/*<\/span>\n<span style=\"color: #888888;\"> * Written by: Kindson The Genius<\/span>\n<span style=\"color: #888888;\"> *\/<\/span>\n\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Triangle<\/span> <span style=\"color: #008800; font-weight: bold;\">implements<\/span> Shape <span style=\"color: #333333;\">{<\/span>\n\n\t<span style=\"color: #333399; font-weight: bold;\">double<\/span> base<span style=\"color: #333333;\">;<\/span>\n\t<span style=\"color: #333399; font-weight: bold;\">double<\/span> height<span style=\"color: #333333;\">;<\/span>\n\t\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">Draw<\/span><span style=\"color: #333333;\">()<\/span> <span style=\"color: #333333;\">{<\/span>\n\t     <span style=\"color: #888888;\">\/\/implementation of the draw method<\/span>\n\t\t\n\t<span style=\"color: #333333;\">}<\/span>\n\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #333399; font-weight: bold;\">double<\/span> <span style=\"color: #0066bb; font-weight: bold;\">CalculateArea<\/span><span style=\"color: #333333;\">()<\/span> <span style=\"color: #333333;\">{<\/span>\n\t\t<span style=\"color: #333399; font-weight: bold;\">double<\/span> area <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">\/<\/span><span style=\"color: #0000dd; font-weight: bold;\">2<\/span> <span style=\"color: #333333;\">*<\/span> base <span style=\"color: #333333;\">*<\/span> height<span style=\"color: #333333;\">;<\/span>\n\t\t<span style=\"color: #008800; font-weight: bold;\">return<\/span> area<span style=\"color: #333333;\">;<\/span>\n\t<span style=\"color: #333333;\">}<\/span>\t\n<span style=\"color: #333333;\">}<\/span>\n<\/pre>\n<\/div>\n<p>Listing 1.1: Class Triangle implements Shape<\/p>\n<p>We can see that the class Triangle, implements that interface Shape. This means that Triangle must provide the implementation for all the methods defined in Shape. In this case, Draw() and CalculateArea().<\/p>\n<p>If a class that implements an interface fails the provide an implementation for the methods in the interface, then there would be compile time\u00a0 error.<\/p>\n<p>&nbsp;<\/p>\n<p><strong id=\"t6\">6. Extending Interfaces<\/strong><\/p>\n<p>An interface in Java can extend another interface. This is the same way a class in java can inherit another class using the extends keyword. An interface extends another interface. But remember that a class cannot extent an interface.<\/p>\n<p>An interface extends another interface using the extends keyword.\u00a0 This is illustrated in Listing 1.2 where the two classes Truck and Bike extends the interface Vehicle<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/Filename: Vehicle.java<\/span>\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">interface<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Vehicle<\/span> <span style=\"color: #333333;\">{<\/span>\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">setLocation<\/span><span style=\"color: #333333;\">(<\/span>String location<span style=\"color: #333333;\">);<\/span>\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> String <span style=\"color: #0066bb; font-weight: bold;\">getOwner<\/span><span style=\"color: #333333;\">();<\/span>\n<span style=\"color: #333333;\">}<\/span>\n<\/pre>\n<\/div>\n<p>Listing 1.2: Class Vehicle<\/p>\n<p>(Remember that the name of the class or interface in java is also the same as the filename)<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/Filename : Truck<\/span>\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">interface<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Truck<\/span> <span style=\"color: #008800; font-weight: bold;\">extends<\/span> Vehicle <span style=\"color: #333333;\">{<\/span>\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">setDriver<\/span><span style=\"color: #333333;\">(<\/span>String DriverName<span style=\"color: #333333;\">);<\/span>\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">Status<\/span><span style=\"color: #333333;\">();<\/span>\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">getNumberOfWheels<\/span><span style=\"color: #333333;\">();<\/span>\n<span style=\"color: #333333;\">}<\/span>\n<\/pre>\n<\/div>\n<p>Listing 1.3: Truck interface extends Vehicle<\/p>\n<p>&nbsp;<\/p>\n<p><strong id=\"t7\">7. Implementing Extended Interfaces<\/strong><\/p>\n<p>What would happen when a class is created that implements an interface that is extended from another interface? For example the Truck interface.<\/p>\n<p>So if a class is created that implements Truck, then it has to provide definition for all the three abstract methods setDriver, Status() and getNumberOfWheels(). In addition, it also has to implement the methods from parent interface. In this case vehicle. This is given in Listing 1.4 where we create a new class Mack that implements Truck.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-style: italic;\">\/* A class that implements an extended interface<\/span>\n<span style=\"color: #008800; font-style: italic;\"> * By Kindson The Genius<\/span>\n<span style=\"color: #008800; font-style: italic;\"> *\/<\/span>\n<span style=\"color: #000080; font-weight: bold;\">public<\/span> <span style=\"color: #000080; font-weight: bold;\">class<\/span> Mack <span style=\"color: #000080; font-weight: bold;\">implements<\/span> Truck {\n\n\t<span style=\"color: #000080; font-weight: bold;\">public<\/span> <span style=\"color: #000080; font-weight: bold;\">void<\/span> setLocation(String location) { <span style=\"color: #008800; font-style: italic;\">\/\/From parent Interface<\/span>\n\t\t<span style=\"color: #008800; font-style: italic;\">\/\/ TODO Auto-generated method stub<\/span>\n\n\t}\n\n\t<span style=\"color: #000080; font-weight: bold;\">public<\/span> String getOwner() { \t\t<span style=\"color: #008800; font-style: italic;\">\/\/From parent Interface<\/span>\n\t\t<span style=\"color: #008800; font-style: italic;\">\/\/ TODO Auto-generated method stub<\/span>\n\t\t<span style=\"color: #000080; font-weight: bold;\">return<\/span> <span style=\"color: #000080; font-weight: bold;\">null<\/span>;\n\t}\n\n\t<span style=\"color: #000080; font-weight: bold;\">public<\/span> <span style=\"color: #000080; font-weight: bold;\">void<\/span> setDriver(String DriverName) {<span style=\"color: #008800; font-style: italic;\">\/\/From child Interface<\/span>\n\t\t<span style=\"color: #008800; font-style: italic;\">\/\/ TODO Auto-generated method stub<\/span>\n\n\t}\n\n\t<span style=\"color: #000080; font-weight: bold;\">public<\/span> <span style=\"color: #000080; font-weight: bold;\">void<\/span> Status() {\t\t\t<span style=\"color: #008800; font-style: italic;\">\/\/From child Interface<\/span>\n\t\t<span style=\"color: #008800; font-style: italic;\">\/\/ TODO Auto-generated method stub<\/span>\n\n\t}\n\n\t<span style=\"color: #000080; font-weight: bold;\">public<\/span> <span style=\"color: #000080; font-weight: bold;\">void<\/span> getNumberOfWheels() {\t<span style=\"color: #008800; font-style: italic;\">\/\/From child Interface<\/span>\n\t\t<span style=\"color: #008800; font-style: italic;\">\/\/ TODO Auto-generated method stub<\/span>\n\n\t}\n}\n<\/pre>\n<\/div>\n<p>Listing 1.4: A class that implements an extended interface<br \/>\n<strong id=\"t8\">8. Questions and Answers on Interfaces<\/strong><\/p>\n<p>Try the following questions and answers on Interfaces:<\/p>\n<p>Q1:\u00a0 \u00a0Can an Interface implement another Interface?<\/p>\n<p>A1: No. An interface only extends another interface<\/p>\n<p>Q2:\u00a0 Can an class extend an Interface?<\/p>\n<p>A2: No. A class can only implement a Interface<\/p>\n<p>Q3: Can you create an object from an interface(can you instantiate an interface)?<\/p>\n<p>A3: No. Interfaces cannot be instantiated. They are meant only to be implemented by another class.<\/p>\n<p>Q4: Can you have other fields aside from methods in an interface?<\/p>\n<p>A4: Yes, but they must be static and final<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Other Java Topics<\/strong><\/p>\n<ul>\n<li><a href=\"https:\/\/kindsonthegenius.com\/tempsite\/interfaces-in-java-basics-of-java-interface\/\">Interfaces in Java<\/a><\/li>\n<li><a href=\"https:\/\/kindsonthegenius.com\/tempsite\/threading-and-multithreading-in-java\/\">Threading and Multithreading in Java<\/a><\/li>\n<li><a href=\"https:\/\/kindsonthegenius.com\/tempsite\/generics-in-java-generic-classes-and-methods-in-java\/\">Introduction to Generics in Java<\/a><\/li>\n<li><a href=\"https:\/\/kindsonthegenius.com\/tempsite\/more-on-java-generics-bounded-generics\/\">Bounded Generics in Java<\/a><\/li>\n<li><a href=\"https:\/\/kindsonthegenius.com\/tempsite\/generic-classes-in-java\/\">Generic Classes in Java<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>We would cover the following: What are Interfaces in Java What are Differences between a Class and an Interface How to Declare an Interface Why &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pagelayer_contact_templates":[],"_pagelayer_content":"","footnotes":""},"categories":[85],"tags":[],"class_list":["post-1874","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1874","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=1874"}],"version-history":[{"count":1,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1874\/revisions"}],"predecessor-version":[{"id":2042,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1874\/revisions\/2042"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=1874"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=1874"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=1874"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}