Interface in Java Explained

Interface in Java Explained

6 mins read2K Views Comment
Updated on Mar 15, 2023 18:53 IST

The below Java Tutorial Article explains the interface in Java with examples. Moreover it covers multiple inheritance through interfaces using examples.

2022_06_Interface-in-Java.jpg

Contents

Recommended online courses

Best-suited Java courses for you

Learn Java with these high-rated online courses

– / –
350 hours
Free
6 months
– / –
4 months
– / –
– / –
– / –
1 month
50 K
7 weeks
7.8 K
3 months
8.47 K
2 months
7 K
6 months
4 K
2 months

Introduction

Interface in Java is the most significant and beneficial topic used extensively today. To begin with, first, let’s understand Java Interfaces in simple terms. Implementing Interfaces in Java is one of the ways to achieve Data Abstraction in Object-Oriented Programming (OOP). FYI, the other way is using Abstract Class in Java.

Also Read: OOPs concepts in Java

Interface Explained (with Example)

The world has been into smartphones since their very inception, which contain plenty of applications to keep our lives busy and entertained. Let’s take an example of the simplest feature on our mobiles which is the dialler which provides us functionalities like picking up a call, putting it on hold, disconnecting the call, or recording it. All this is possible with the interface to interact between you and your mobile. The interface depending on the interaction, can completely change its outputs. Hence, the interface is like a structure which can be changed given any shape or implementation. Coming back to the example, several mobile manufacturers use such interfaces to execute different implementations using the same interface on the calling/dialler feature. Moreover, they can be extended or enhanced further, such as the conference call feature on the dialler application.

Java Methods Explained (with Code)
Java Methods Explained (with Code)
The below java tutorial covers java methods with examples and explanations. It also goes through the declaration, implementation, types, and its uses.The below java tutorial covers java methods with examples...read more
Understanding ArrayList in Java
Understanding ArrayList in Java
An ArrayList in Java is an array that resizes itself as needed. It is also popularly known as a resizable array. The article below explains ArrayList in Java with suitable...read more
Multithreading in Java
Multithreading in Java
The below article goes through explaining the concepts of Multithreading in Java and its implementation. Let’s begin the tutorial.

What is Interface in Java? 

In Java, an interface is a reference type similar to classes. However, it implicitly consists of interface variables (public, final and static) and abstract methods. FYI, implicitly here means that even if we don’t use keywords to declare variables (as public, final and static) and methods (as abstract), they are so, by default, inside the interface. 

Moreover, interfaces can’t have any regular or concrete methods (or functions) as they can’t be instantiated (which means we can’t create an object to call those methods). 

In addition, different classes can implement interfaces and provide implementations (or bodies) to the abstract classes. Simply put, the implementing class is responsible for providing definitions for all abstract methods available inside the interface.

Also read: Method Overriding in Java

Why do we use Interface in Java?

Why interface is used in Java because, as mentioned above, Interfaces in Java are one of the ways to achieve data abstraction, which means showing up only essential information by hiding unnecessary details. Secondly, we have interfaces because we can’t implement multiple inheritances using classes. It helps to implement multiple inheritances in Java. Moreover, interfaces work as a stage that lets other classes rebuild its method as per their requirement.

Also, Read: Data Types in Java – Primitive and Non-Primitive Data Types Explained

Implementing Interface in Java

Syntax to declare an interface:

 
interface Name
{
Data_Type Var1; //implicit public final static variables
Data_Type Var1;
Return_Type Method_Name(Arguments); //Abstract method
}
Copy code

The following example shows the implementation to execute interfaces in Java:

Example 1:

 
interface Phone
{
void dial_number(String number); //abstract method 1
void disconnectCall(); //abstract method 2
}
//Implementing class 1
class MobilePhone implements Phone
{
public void dial_number(String number)
{
System.out.println("Connecting to mobile number: " +number);
}
public void disconnectCall()
{
System.out.println("Disconnected the call on mobile!");
}
}
//Implementing class 2
class Landline implements Phone
{
public void dial_number(String number)
{
System.out.println("\nConnecting to Landline number: " +number);
}
public void disconnectCall()
{
System.out.println("Disconnected the call on Landline!");
}
}
//Main Class
class Main
{
public static void main(String[] args)
{
//Phone p = new Phone();
//Error! cannot instatiate an interface
Phone p; //reference type variable
p = new MobilePhone();
//calling methods of class MobilePhone using reference
p.dial_number("+91 7042176752");
p.disconnectCall();
//OR (Another Way)- create object for the implementing class
Landline l = new Landline();
//calling methods of class Landline using class object
l.dial_number("011 2042176752");
l.disconnectCall();
}
}
Copy code

Explanation:

example1

In the above example, the class MobilePhone and Landline implements the interface Phone. (FYI, implements keyword means that the class is using the elements of a Java Interface in itself.) The classes redefined the abstract methods dial_number() and disconnectCall() as per their requirements. Moreover, We can’t create an object for the interface. So, we can access the redefined methods in implemented classes. There are two ways to access the redefined abstract methods:- 1. to create a reference variable for interface. 2. To call using objects of classes that implement the interface.

Output:

output1

Read: Implementing Array in Java

Example 2:

 
interface Phone
{
int lock = 1; //public static final by default
int unlock = 0;
void checkStatus(int s); //abstract class
}
//class 1
class Nokia implements Phone
{
public void checkStatus(int sn)
{
if(sn == lock)
{
System.out.println("Phone Locked!");
}
else
{
System.out.println("Phone Unocked!");
}
}
}
//class 2
class Samsung implements Phone
{
public void checkStatus(int ss)
{
if(ss == lock)
{
System.out.println("Draw Pattern to Unlock..");
}
else if(ss == unlock)
{
System.out.println("Phone Unocked!");
}
}
}
//Main Class
public class Main
{
public static void main (String[] args)
{
Phone p; //reference type p
p = new Nokia();
p.checkStatus(0);
p = new Samsung();
p.checkStatus(1);
}
}
Copy code

Read: Conditional Statements in Java

Output:

output2

Implementing Multiple Interfaces

The below example shows multiple inheritance using interfaces in Java.

Example:

 
interface Rectangle
{
void area_rectangle(int l, int b);
}
interface Parallelogram extends Rectangle
{
void area_parallelogram(int l, int h);
}
class Figure implements Parallelogram
{
public void area_rectangle(int l, int b)
{
System.out.println("I'm from Rectangle Interface");
System.out.println("Area of Rectangle: " +(l*b));
}
public void area_parallelogram(int l, int h)
{
System.out.println("I'm from Parallelogram Interface");
System.out.println("Area of Parallelogram: " +(l*h));
}
}
//main class
class Main
{
public static void main(String args[])
{
Parallelogram p;
p = new Figure();
p.area_rectangle(12, 14);
p.area_parallelogram(5,6);
}
}
Copy code

Explanation:

example2

Output:

output3

Read: Exception Handling in Java.

How to Add Two Numbers in Java
How to Add Two Numbers in Java
In our earlier education days, we had learned addition, subtraction, division, and multiplication. We use the ‘+’ operator to add two integers. Similarly, we use the ‘+’ operator to add...read more
Java Lambda Expression
Java Lambda Expression
Java Lambda Expression is a way to define an anonymous function or method that can be passed around as a value. It allows you to write code that is more...read more
Understanding Java Scanner Class
Understanding Java Scanner Class
In this article, you will learn about Java Scanner class. We have also covered Java user input, input types, etc., relevant topics. Let’s begin!

Extending Interface in Java

The below example shows extending an interface (Rectangle) using another interface (Parallelogram) and implementing it via a class (Figure).

Note: Here, the implements keyword depicts that you are using the elements of a Java Interface in your class. On the other hand, extends keyword means that you are creating a subclass of the base class or interface.

Example:

 
\n \n \n <pre class="java" style="font-family:monospace">\n \n \n <span style="color: #000000;font-weight: bold">\n \n \n interface \n \n \n <span style="color: #003399">\n \n \n Rectangle\n \n \n
\n \n \n <span style="color: #009900">\n \n \n {\n \n \n
\n \n \n <span style="color: #000066;font-weight: bold">\n void area_rectangle \n <span style="color: #009900">\n ( \n <span style="color: #000066;font-weight: bold">\n int l, \n <span style="color: #000066;font-weight: bold">\n int b \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #009900">\n } \n
\n <span style="color: #000000;font-weight: bold">\n interface Parallelogram \n <span style="color: #000000;font-weight: bold">\n extends \n <span style="color: #003399">\n Rectangle \n
\n <span style="color: #009900">\n { \n
\n <span style="color: #000066;font-weight: bold">\n void area_parallelogram \n <span style="color: #009900">\n ( \n <span style="color: #000066;font-weight: bold">\n int l, \n <span style="color: #000066;font-weight: bold">\n int h \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #009900">\n } \n
\n <span style="color: #000000;font-weight: bold">\n class Figure \n <span style="color: #000000;font-weight: bold">\n implements Parallelogram \n
\n <span style="color: #009900">\n { \n
\n <span style="color: #000000;font-weight: bold">\n public \n <span style="color: #000066;font-weight: bold">\n void area_rectangle \n <span style="color: #009900">\n ( \n <span style="color: #000066;font-weight: bold">\n int l, \n <span style="color: #000066;font-weight: bold">\n int b \n <span style="color: #009900">\n ) \n
\n <span style="color: #009900">\n { \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "I'm from Rectangle Interface" \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "Area of Rectangle: " \n <span style="color: #339933">\n + \n <span style="color: #009900">\n (l \n <span style="color: #339933">\n *b \n <span style="color: #009900">\n ) \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #009900">\n } \n
\n
\n <span style="color: #000000;font-weight: bold">\n public \n <span style="color: #000066;font-weight: bold">\n void area_parallelogram \n <span style="color: #009900">\n ( \n <span style="color: #000066;font-weight: bold">\n int l, \n <span style="color: #000066;font-weight: bold">\n int h \n <span style="color: #009900">\n ) \n
\n <span style="color: #009900">\n { \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "I'm from Parallelogram Interface" \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "Area of Parallelogram: " \n <span style="color: #339933">\n + \n <span style="color: #009900">\n (l \n <span style="color: #339933">\n *h \n <span style="color: #009900">\n ) \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #009900">\n } \n
\n <span style="color: #009900">\n } \n
\n <span style="color: #666666;font-style: italic">\n //main class \n
\n <span style="color: #000000;font-weight: bold">\n class Main \n
\n <span style="color: #009900">\n { \n
\n <span style="color: #000000;font-weight: bold">\n public \n <span style="color: #000000;font-weight: bold">\n static \n <span style="color: #000066;font-weight: bold">\n void main \n <span style="color: #009900">\n ( \n <span style="color: #003399">\n String args \n <span style="color: #009900">\n [ \n <span style="color: #009900">\n ] \n <span style="color: #009900">\n ) \n
\n <span style="color: #009900">\n { \n
Parallelogram p \n <span style="color: #339933">\n ; \n
p \n <span style="color: #339933">\n = \n <span style="color: #000000;font-weight: bold">\n new Figure \n <span style="color: #009900">\n ( \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
p. \n <span style="color: #006633">\n area_rectangle \n <span style="color: #009900">\n ( \n <span style="color: #cc66cc">\n 12, \n <span style="color: #cc66cc">\n 14 \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
p. \n <span style="color: #006633">\n area_parallelogram \n <span style="color: #009900">\n ( \n <span style="color: #cc66cc">\n 5, \n <span style="color: #cc66cc">\n 6 \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n
\n <span style="color: #009900">\n } \n
\n <span style="color: #009900">\n } \n <p><strong><span style="text-decoration: underline">Output:</span></strong></p> \n <figure class="wp-block-image is-resized"> \n <img loading="lazy" src="https://lh4.googleusercontent.com/9U5WeOsST-tyVGpGtgunKGYRNgN_-r44NoZeon5QKxuP_5fDPukCAWIIgSz8_rsOHwOAFCNrU9b6vFRntLD_1mA21WatceYbm36Yya2PSLuV8AbtGJelCh-ENuJMZq1UbXC9P0etq7NQBrK-bg" alt="output4" width="381" height="107"> \n </figure> \n <p><strong><em>Read: <a href="https://www.shiksha.com/online-courses/articles/constructors-in-java/" target="_blank" rel="noreferrer noopener">Constructors in Java</a></em></strong></p> \n <h2 id="con"><strong>Conclusion</strong></h2> \n <p>Hope the above article about interfaces helped you understand the concept better. If you face any queries, feel free to reach us at the link below and stay tuned for more content in Java.</p> \n <p><strong><em>For more, Read: <a href="https://www.shiksha.com/online-courses/articles/8-most-important-data-structures/" target="_blank" rel="noreferrer noopener">8 Most Important Data Structures a Programmer Must Know</a></em></strong></p> \n <div id="faqWrapper_last" class="faqWrapper manual" contenteditable="false"> \n <h2 id="">FAQs</h2> \n <p class="fQ"> <strong id="faq_q1">What is Interface in Java?</strong> </p> \n <div id="faq_a1" class="fA"> \n <p>In Java, an interface is a reference type similar to classes. However, it consists of interface variables (which are public, final, static and abstract methods implicitly.</p> \n </div> \n <p class="fQ"> <strong id="faq_q2">What is the difference between classes and interfaces in Java?</strong> </p> \n <div id="faq_a2" class="fA"> \n <p>1. Classes can instantiate data members (variables) and methods using objects. However, Interfaces can't be instantiated. 2. An interface can contain only abstract methods, whereas classes can have concrete methods as well. 3. A class can be public, private or protected, whereas interfaces can only be public.</p> \n </div> \n <p class="fQ"> <strong id="faq_q3">What is the difference between abstract classes and interfaces in Java?</strong> </p> \n <div id="faq_a3" class="fA"> \n <p>1. Abstract classes can have both concrete and abstract methods, whereas interfaces can only contain abstract methods. 2. Abstract classes don't support multiple inheritance. However, interfaces do support multiple inheritance in Java. 3. Interfaces can only extend to other interfaces. Whereas abstract classes can extend classes as well as implement other interfaces.</p> \n </div> \n <p class="fQ"> <strong id="faq_q4">What is the difference between 'implements' and 'extends' keywords in Java?</strong> </p> \n <div id="faq_a4" class="fA"> \n <p>Implements keyword depicts that you are using the elements of a Java Interface in your class. On the other hand, extends keyword means that you are creating a subclass of the base class or interface.</p> \n </div> \n </div> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #cc66cc"> \n </span style="color: #cc66cc"> \n </span style="color: #009900"> \n </span style="color: #006633"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #cc66cc"> \n </span style="color: #cc66cc"> \n </span style="color: #009900"> \n </span style="color: #006633"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #000000;font-weight: bold"> \n </span style="color: #339933"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #003399"> \n </span style="color: #009900"> \n </span style="color: #000066;font-weight: bold"> \n </span style="color: #000000;font-weight: bold"> \n </span style="color: #000000;font-weight: bold"> \n </span style="color: #009900"> \n </span style="color: #000000;font-weight: bold"> \n </span style="color: #666666;font-style: italic"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #339933"> \n </span style="color: #0000ff"> \n </span style="color: #009900"> \n </span style="color: #006633"> \n </span style="color: #006633"> \n </span style="color: #003399"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #0000ff"> \n </span style="color: #009900"> \n </span style="color: #006633"> \n </span style="color: #006633"> \n </span style="color: #003399"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #000066;font-weight: bold"> \n </span style="color: #000066;font-weight: bold"> \n </span style="color: #009900"> \n </span style="color: #000066;font-weight: bold"> \n </span style="color: #000000;font-weight: bold"> \n </span style="color: #009900"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #339933"> \n </span style="color: #0000ff"> \n </span style="color: #009900"> \n </span style="color: #006633"> \n </span style="color: #006633"> \n </span style="color: #003399"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #0000ff"> \n </span style="color: #009900"> \n </span style="color: #006633"> \n </span style="color: #006633"> \n </span style="color: #003399"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #000066;font-weight: bold"> \n </span style="color: #000066;font-weight: bold"> \n </span style="color: #009900"> \n </span style="color: #000066;font-weight: bold"> \n </span style="color: #000000;font-weight: bold"> \n </span style="color: #009900"> \n </span style="color: #000000;font-weight: bold"> \n </span style="color: #000000;font-weight: bold"> \n </span style="color: #009900"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #000066;font-weight: bold"> \n </span style="color: #000066;font-weight: bold"> \n </span style="color: #009900"> \n </span style="color: #000066;font-weight: bold"> \n </span style="color: #009900"> \n </span style="color: #003399"> \n </span style="color: #000000;font-weight: bold"> \n </span style="color: #000000;font-weight: bold"> \n </span style="color: #009900"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #000066;font-weight: bold"> \n </span style="color: #000066;font-weight: bold"> \n </span style="color: #009900"> \n </span style="color: #000066;font-weight: bold">\n \n \n </span style="color: #009900">\n \n \n </span style="color: #003399">\n \n \n </span style="color: #000000;font-weight: bold">\n \n \n </pre class="java" style="font-family:monospace">
Copy code
About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio