In this write-up, we will learn the below-listed concepts of Java interfaces:
- Interface in Java
- Syntax
- implements Keyword in Java
- extends Keyword in Java
- What is the Need For an Interface in Java
- Examples
So, let’s get started!
Interface in Java
It is a blueprint/template of a java class that contains only abstract methods and constant attributes. This means an interface includes only the declaration of methods, and these methods will be defined in the implemented class. In java, interfaces can’t be instantiated.
Syntax
The below code block will show the basic syntax of a java interface:
// constant fields/variable;
// abstract methods;
}
Here, “interface” is a keyword used to create an interface, while “Example” is a user-defined interface name.
Implements Keyword in Java
It is used to implement a java interface. Let’s have a look at the below snippet for a profound understanding of this concept:
//methods definition;
//code;
}
In the above snippet, a class named “ExampleClass” will inherit/implement the “ExampleInterface” using the “implements” keyword.
Similarly, a class can implement multiple interfaces using the below-given syntax:
//methods definition;
//code;
}
extends Keyword in Java
One interface can extend another interface using the “extends” keyword. Consider the below code block to understand the basic concept of the “extends” keyword:
//code
}
interface SecondInterface extends FirstInterface {
// code
}
Similarly, using the extends keyword an interface can extend multiple interfaces.
What is the purpose of using an Interface in Java
The below-given characteristics show the purpose of Java interfaces:
- In java, interfaces are used to achieve multiple inheritance.
- Interfaces in java provide Abstraction.
- Loose coupling(class independence) can be achieved using Java interfaces.
- In java, interfaces are public so that any other class can implement the interfaces.
Example1
In the below snippet, we created an interface named “Employee” and a class named “DemoClass”. The “DemoClass” class will implement the “Employee” interface:
void getData(int empId, String empName);
}
class DemoClass implements Employee {
public void getData(int empId, String empName) {
System.out.println("Employee ID: " + empId);
System.out.println("Employee Name: " + empName);
}
public static void main(String[] args) {
DemoClass object = new DemoClass();
object.getData(12, "Joe");
}
}
The Employee interface has an abstract method getData(int empId, String empName). The DemoClass implemented that method and defined it:
The output shows that the DemoClass successfully implemented the Employee interface.
Example2
This example will explain how an interface can extend another interface:
void showData();
}
interface SecondInterface extends FirstInterface{
void printData();
}
public class MainClass implements SecondInterface{
public void showData(){
System.out.println("Linuxhint.com");
}
public void printData(){
System.out.println("Welcome to Linuxhint");
}
public static void main(String[] args) {
MainClass object = new MainClass();
object.showData();
object.printData();
}
}
In this example, we performed the below-listed functionalities:
- Initially, we created two interfaces: FirstInterface and SecondInterface.
- Both interfaces have an abstract method.
- SecondInterface extended the FirstInterface.
- After that, we created a class named MainClass.
- The MainClass implemented the SecondInterface.
- The MainClass defined the abstract methods.
- Afterward, we created an object of the MainClass and accessed the methods of both the interfaces using that object.
- Note that the MainClass didn’t implement the FirstInterface. Still, it defined the abstract method of FirstInterface and accessed it using the object of MainClass.
The MainClass implemented the SecondInterface while the SecondInterface extended the FirstInterface. Using SecondInterface, the MainClass can also access the FirstInterface. In this way, interfaces provide the functionality of multiple inheritance in java.
Conclusion
An interface in java is a blueprint or template of a class that contains only abstract methods and constant attributes. In java, interfaces provide numerous features, e.g., multiple inheritance, Abstraction, loose coupling(class independence), etc. This post presents a comprehensive overview of java interfaces with the help of relevant examples.