Interfaces in Java are a collection of abstract methods and constants, which can be implemented by any class. An interface is a contract that specifies a set of methods that a class must implement, without providing any implementation details. The purpose of Java interfaces is to allow the development of modular and extensible applications, by separating the specification of behavior from its implementation.
Table of Contents
Creating Java Interfaces
To create a Java interface, use the interface keyword followed by the name of the interface and the list of abstract methods.
public interface Animal {
void makeSound();
void eat();
}
Differences between Interfaces and Classes
In Java, classes and interfaces are both used to define types. However, there are some important differences between them:
- A class can have state (fields) and behavior (methods), while an interface only defines behavior (methods).
- A class can implement multiple interfaces, but it can only inherit from one class.
- An interface can only have public static final constants, while a class can have other types of fields and methods.
Abstract Classes vs. Interfaces
- An abstract class can have both abstract and non-abstract methods, while an interface can only have abstract methods.
- An abstract class can have state (fields), while an interface cannot have any state.
- A class can inherit from an abstract class and implement one or more interfaces, while an interface can only extend one or more other interfaces.
Here is an example of an abstract class that implements the Animal interface:
public interface Animal {
public void speak();
}
public abstract class Mammal implements Animal {
private String name;
private int age;
public Mammal(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public abstract void giveBirth();
public void speak() {
System.out.println("The " + this.getClass().getSimpleName() + " speaks.");
}
}
public class Dog extends Mammal {
public Dog(String name, int age) {
super(name, age);
}
public void giveBirth() {
System.out.println("The dog gives birth to puppies.");
}
}
public class Main {
public static void main(String[] args) {
Dog myDog = new Dog("Rufus", 3);
myDog.speak();
myDog.giveBirth();
System.out.println("Name: " + myDog.getName() + ", Age: " + myDog.getAge());
}
}
Output

Polymorphism and Interfaces
One of the main benefits of using interfaces in Java is that they enable polymorphism. Polymorphism allows a single variable to refer to objects of different types, and it is a key concept in object-oriented programming.
public interface Shape {
double getArea();
}
public class Circle implements Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
public double getArea() {
return Math.PI * radius * radius;
}
}
public class Rectangle implements Shape {
private double length;
private double width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
public double getArea() {
return length * width;
}
}
public class Main {
public static void main(String[] args) {
Shape shape1 = new Circle(5);
Shape shape2 = new Rectangle(10, 20);
System.out.println("Area of shape1: " + shape1.getArea());
System.out.println("Area of shape2: " + shape2.getArea());
}
}
Output

Interface Inheritance and Default Methods
Another important feature of Java interfaces is inheritance. An interface can extend another interface to inherit its methods and constants. Here is an example:
// Parent interface
interface Vehicle {
void start();
void stop();
default void honk() {
System.out.println("Beep beep!");
}
}
// Child interface extends parent interface
interface Car extends Vehicle {
void drive();
}
// Class implementing child interface
class Sedan implements Car {
public void start() {
System.out.println("Sedan started.");
}
public void stop() {
System.out.println("Sedan stopped.");
}
public void drive() {
System.out.println("Sedan driving.");
}
}
public class Main {
public static void main(String[] args) {
Sedan sedan = new Sedan();
sedan.start();
sedan.drive();
sedan.stop();
sedan.honk(); // Using default method
}
}
Output

Conclusion
Interfaces in Java provide a way to define contracts for classes to implement, improving modularity and extensibility. They can achieve polymorphism, abstraction, and default implementations. By using interfaces effectively, we can write more modular, extensible, and maintainable code.