What is the interface in OOPs?
Interface is quite an important concept in object programming and useful as well. Interface can also have variables and methods like a class but methods declared in interface are by default abstract (only method signature and no body of method)
Interface is like a blueprint of a class and tells what the class must do and not how to do. It specifies a set of methods that the class has to implement. Still, interfaces are quite different from classes like we can’t initiate interfaces. An interface is not extended by class rather than it’s implemented by class.
Example :-
import java.io.*;
// A simple interface
interface printnumber
{
// public, static and final
final int x = 10;
// public and abstract
void printnum();
}
// A class that implements an interface.
class testClass implements printnumber
{
// Implementing the capabilities of
// interface.
public void printnum()
{
System.out.println("Number");
}
// Driver Code
public static void main (String[] args)
{
testClass t = new testClass();
t.printnum();
System.out.println(x);
}
}
Above is a simple example of an interface where we declare an interface and same we have in class to print the number. In the real world if you have some common functionality which needs to be performed by different classes with some added function then we can create an interface and use it in different classes.