Interface in Java
Interface in Java
A Java interface is like a blueprint of a class. It is an abstract type that specifies the behaviour
of a class. Java interfaces contain static constants and abstract methods. In an interface in
Java programming language, there can only be abstract methods and no method body. It is a
mechanism used to achieve abstraction and multiple inheritance.
In simple words, we can say that an interface program in Java can have variables and abstract
methods, but it can’t have a method body.
An interface mentions what a class must do rather than how. Hence, it is known as a
blueprint of the behaviour of a class.
We identify a type of entity by its behaviour and not its attribute; we define it as an
interface.
Behaviour is represented as an interface unless each sub-type of a class has that
behaviour.
It represents the Is-A relationship.
It doesn’t have a constructor.
Similar to a class, an interface can have variables and methods. However, methods
declared in an interface are abstract by default.
Since Java 8, an interface has default and static methods.
If a class implements an interface without providing method bodies for all specified
functions, it is declared as an abstract.
Since Java 9, an interface can have private methods.
// Example interface
interface Shape {
double calculateArea();
double calculatePerimeter();
}
// Implementing the interface
class Rectangle implements Shape {
private double length;\
private double width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
@Override\
public double calculateArea() {
return length * width;
}
@Override
public double calculatePerimeter() {
return 2 * (length + width);
}
}
// Using the interface
public class Main {
public static void main(String[] args) {
Rectangle rectangle = new Rectangle(5, 3);
System.out.println("Area: " + rectangle.calculateArea());
System.out.println("Perimeter: " + rectangle.calculatePerimeter());
}
}
Explanation:
In this example, the Shape interface declares two methods: calculateArea() and
calculatePerimeter(). The Rectangle class implements the Shape interface and
provides the implementation for these methods.
In the Main class, we create an instance of Rectangle and call the calculateArea() and
calculatePerimeter() methods to calculate and display the area and perimeter of the
rectangle.
1. Functional Interface
The functional interface has exactly one abstract method but can contain any number of static
and default methods. It represents a single unit of the behaviour of a class that is used as a
method reference or lambda expression.
Functional interfaces are also known as SAM interfaces. To make an interface with a single
abstract method as a functional interface, annotate it with @FunctionalInterface.
2. Marker Interface
A marker interface doesn’t contain any methods, abstract methods, fields, or constants. It is
an empty interface that indicates a certain behaviour a class implements.
Example:
interface Flyable {
void fly();
}
interface Swimmable {
void swim();
}
class Bird implements Flyable, Swimmable {
@Override
public void fly() {
System.out.println("Bird is flying.");
}
@Override
public void swim() {
System.out.println("Bird is swimming.");
}
}
public class Main {
public static void main(String[] args) {
Bird bird = new Bird();
bird.fly();
bird.swim();
}
}
Explanation:
In this example, we have two interfaces: Flyable and Swimmable. The Bird class implements
both interfaces, which means it needs to provide an implementation for the fly() method from
the Flyable interface and the swim() method from the Swimmable interface.
In the main() method, we create an instance of Bird and call both the fly() and swim()
methods. Since Bird implements both interfaces, it can perform both flying and swimming
actions.
Class Interface
Here, you can create objects and instantiate Here, you can create objects but can’t
variables. instantiate variables.
It stores the entire method definition. It stores just the signature of a method.
In a class, access specifiers are essential and In an Interface, access specifiers are not
include private, protected, and public required and only a public specifier is
specifiers. used.