Back to Java
2026-03-046 min read

interface

Learn interface step by step with clear examples and exercises.

Why This Matters

In Java programming, interfaces play a crucial role in designing flexible and modular code. They allow for multiple inheritance (through implementation) and provide a way to achieve abstraction and polymorphism - two key concepts that make our programs more efficient and easier to maintain. Understanding interfaces is essential for excelling in Java programming, whether you're preparing for an interview or working on real-world projects.

Interfaces help promote good software design principles such as:

  1. Loose Coupling: Interfaces allow classes to interact with each other without knowing their internal implementation details, promoting a more modular and maintainable codebase.
  2. Separation of Concerns: By defining specific methods that a class must implement, interfaces help separate the responsibilities of different components in a program.
  3. Polymorphism: Interfaces enable polymorphism by allowing objects of different classes to be treated as if they were instances of a single class (the interface). This makes code more flexible and easier to manage.

Prerequisites

Before diving into the core concept of interfaces, it is important to have a solid understanding of the following:

  1. Basic Java syntax (variables, methods, classes)
  2. Inheritance and class hierarchy
  3. Abstract classes and abstract methods
  4. Polymorphism and method overriding
  5. Exception handling
  6. Intermediate-level concepts like constructors, packages, and static members
  7. Understanding of Object-Oriented Programming (OOP) principles

Core Concept

An interface in Java is a collection of abstract methods (methods without a body) and constants (variables with final, public, and static modifiers). An interface cannot be instantiated; instead, it is implemented by classes. A class that implements an interface must provide the implementation for all its methods.

Here's the basic structure of an interface:

public interface InterfaceName {
// constants (variables with final, public, and static modifiers)
int CONSTANT_NAME = value;

// abstract methods (methods without a body)
void methodName();
}

A class can implement multiple interfaces:

public class ClassName implements Interface1, Interface2 {
// implementation of the methods from both interfaces
}

When an object is created from a class that implements an interface, it inherits all the abstract methods and constants defined in the interface. This provides a contract or blueprint for what the implementing class should be able to do.

Interface Methods

An interface can have:

  1. Abstract methods (methods without a body)
  2. Default methods (methods with a body) introduced in Java 8
  3. Static methods (methods that belong to the interface itself and are not inherited by implementing classes)

Abstract Methods

Abstract methods are defined using the abstract keyword, and they do not have a method body. Implementing classes must provide an implementation for these methods.

public abstract void myMethod(); // An example of an abstract method

Default Methods

Default methods were introduced in Java 8 to allow interfaces to contain methods with implementations that can be optionally overridden by implementing classes. They are defined using the default keyword and have a method body.

public default void myMethod() { // An example of a default method }

Static Methods

Static methods belong to the interface itself and do not need to be implemented by any class. They can be called directly on the interface using the interfaceName.methodName() syntax.

public static void myMethod() { // An example of a static method }

Worked Example

Let's consider an example where we have two interfaces, Shape and Printable, and a class Circle that implements both:

// Interface for shapes
public interface Shape {
double getArea();
}

// Interface for printable objects
public interface Printable {
void print();
}

// Class implementing both interfaces
public class Circle implements Shape, Printable {
private final double radius;

public Circle(double radius) {
this.radius = radius;
}

@Override
public double getArea() {
return Math.PI * Math.pow(radius, 2);
}

@Override
public void print() {
System.out.println("Printing circle with radius: " + radius);
}
}

In this example, the Circle class provides implementations for both the getArea() method (defined in the Shape interface) and the print() method (defined in the Printable interface). Now we can create a Circle object that has the behavior of both a shape and a printable object:

public static void main(String[] args) {
Circle circle = new Circle(5);
System.out.println("Area of the circle: " + circle.getArea());
circle.print();
}

Common Mistakes

  1. Forgetting to define an interface method as abstract: If you define a method in an interface without using the abstract keyword, it will be treated as a regular method and not an abstract one. This will cause compilation errors when implementing classes don't provide an implementation for that method.
// Incorrect definition of an interface method
public int myMethod(); // Should be public abstract int myMethod();
  1. Implementing a concrete method in an interface: An interface should only contain abstract methods and constants. If you implement a concrete (non-abstract) method, it will be treated as static and cannot be overridden by implementing classes.
// Incorrect implementation of a concrete method in an interface
public void myMethod() { // Should be public abstract void myMethod(); }
  1. Creating an instance of an interface: Since interfaces cannot be instantiated, attempting to create an instance will result in a compilation error.
// Compilation error: Interface Name cannot be instantiated
InterfaceName obj = new InterfaceName();

Common Mistakes (Continued)

  1. Not providing an implementation for all abstract methods: When implementing an interface, a class must provide an implementation for all its abstract methods, or the class itself should also be declared as abstract.
// Compilation error: Class Name does not implement abstract method getArea() in interface Shape
public class Circle implements Printable { // Should be public abstract class Circle implements Shape, Printable
// ...
}
  1. Using the final keyword with interface methods: Interface methods are already implicitly final and cannot be overridden by implementing classes, so using the final keyword is unnecessary and will result in a compilation error.
// Compilation error: Method print() in interface Printable cannot have the final modifier
public interface Printable {
public final void print(); // Should be public void print(); }

Practice Questions

  1. Create an interface Comparable with a single abstract method int compareTo(Comparable other). Then create a class Person that implements this interface and provides the implementation of the compareTo() method based on age.
  2. Given the following interfaces, implement the Runnable interface using an anonymous inner class:
public interface Moveable {
void move();
}

public interface Talkable {
void talk();
}
  1. Create a Shape interface with methods for calculating area and perimeter. Then create classes Circle, Rectangle, and Triangle that implement the Shape interface and provide their respective calculations.

FAQ

  1. Can an interface extend another interface?

Yes, Java supports multiple inheritance for interfaces. An interface can extend multiple interfaces, but a class can only extend one superclass.

  1. How does method hiding work in interfaces?

Method hiding is not supported in Java when implementing interfaces. If a class implements an interface and provides a method with the same signature as a method from the interface, it will override the interface method, not hide it.

  1. What happens if an interface has a default or static method?

Starting from Java 8, interfaces can contain default methods (methods with a body) and static methods. Implementing classes are not required to provide an implementation for these methods but can override them if needed. Static methods belong to the interface itself and do not need to be implemented by any class.

  1. Can a class implement two interfaces with the same method signature?

Yes, a class can implement multiple interfaces that have the same method signature. The class must provide a unique implementation for each method in every interface it implements.

  1. What is the purpose of default methods in interfaces?

Default methods were introduced in Java 8 to allow interfaces to contain methods with implementations that can be optionally overridden by implementing classes. This provides backward compatibility with existing code that uses interfaces without breaking them, while also allowing for new functionality to be added.

interface | Java | XQA Learn