Back to Java
2026-03-067 min read

Interfaces (Java)

Learn Interfaces (Java) step by step with clear examples and exercises.

Title: Mastering Polymorphism and Abstraction with Java Interfaces

Why This Matters

In this tutorial, we will delve deep into Java interfaces, a powerful tool for achieving polymorphism and abstraction in object-oriented programming. Understanding interfaces is crucial for acing coding interviews, solving real-world programming challenges, and writing clean, maintainable code.

Prerequisites

Before diving into Java interfaces, ensure you have a solid grasp of the following concepts:

  • Java syntax basics (variables, methods, classes)
  • Object-oriented programming principles (inheritance, encapsulation, and polymorphism)
  • Exception handling in Java
  • Basic understanding of abstract classes

Core Concept

What is an Interface?

An interface is a collection of abstract methods and constants that define a set of behaviors for a particular type. An interface does not provide any implementation; it only specifies what the implementing classes should do. In Java, interfaces are declared using the interface keyword.

public interface Shape {
double PI = 3.14; // constant

void area(); // abstract method
}

Implementing an Interface

To implement an interface, a class must provide concrete implementations for all the abstract methods defined in the interface. The implementing class extends the interface using the implements keyword.

public class Circle implements Shape {
private double radius;

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

@Override
public void area() {
System.out.println("The area of the circle is: " + (PI * Math.pow(radius, 2)));
}
}

Interface Inheritance and Multiple Implementation

An interface can extend another interface to inherit its methods. A class can implement multiple interfaces as well.

public interface Drawable extends Shape {
void draw();
}

public class Square implements Shape, Drawable {
private double side;

public Square(double side) {
this.side = side;
}

@Override
public void area() {
System.out.println("The area of the square is: " + (Math.pow(side, 2)));
}

@Override
public void draw() {
// Drawing logic for squares
}
}

Interface Polymorphism

Since all implementing classes must provide an implementation for each abstract method defined in the interface, we can use interfaces to achieve polymorphism. This means that we can create a reference of an interface type and assign it to objects of different implementing classes. At runtime, the appropriate method will be called based on the actual object's class.

public static void main(String[] args) {
Shape circle = new Circle(5);
circle.area();

Shape square = new Square(4);
square.area();
}

Interface Default Methods and Static Methods

Java 8 introduced default methods and static methods in interfaces, allowing for additional functionality without breaking backward compatibility with existing implementations.

Default methods are implemented by the interface itself but can be overridden by implementing classes if needed.

public interface Shape {
double PI = 3.14; // constant

void area(); // abstract method

default void perimeter() {
System.out.println("Perimeter not implemented for this shape.");
}

static void printPI() {
System.out.println("PI is: " + PI);
}
}

public class Circle implements Shape {
private double radius;

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

@Override
public void area() {
System.out.println("The area of the circle is: " + (PI * Math.pow(radius, 2)));
}

@Override
public void perimeter() {
System.out.println("The perimeter of the circle is: " + (2 * PI * radius));
}
}

Static methods in interfaces can be called directly without creating an instance of any class that implements the interface.

public interface Shape {
double PI = 3.14; // constant

void area(); // abstract method

default void perimeter() {
System.out.println("Perimeter not implemented for this shape.");
}

static void printPI() {
System.out.println("PI is: " + PI);
}
}

public class Main {
public static void main(String[] args) {
Shape.printPI(); // Calling a static method directly from the interface
}
}

Worked Example

Let's create an example of using interfaces to model different shapes and their behaviors.

import java.util.Arrays;

public interface Shape {
double PI = 3.14; // constant

void area(); // abstract method

default void perimeter() {
System.out.println("Perimeter not implemented for this shape.");
}

static void printPI() {
System.out.println("PI is: " + PI);
}
}

public class Circle implements Shape {
private double radius;

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

@Override
public void area() {
System.out.println("The area of the circle is: " + (PI * Math.pow(radius, 2)));
}

@Override
public void perimeter() {
System.out.println("The perimeter of the circle is: " + (2 * PI * radius));
}
}

public class Square implements Shape {
private double side;

public Square(double side) {
this.side = side;
}

@Override
public void area() {
System.out.println("The area of the square is: " + (Math.pow(side, 2)));
}

@Override
public void perimeter() {
System.out.println("The perimeter of the square is: " + (4 * side));
}
}

public class Triangle implements Shape {
private double side1, side2, side3;

public Triangle(double side1, double side2, double side3) {
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}

@Override
public void area() {
double s = (side1 + side2 + side3) / 2;
double result = Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
System.out.println("The area of the triangle is: " + result);
}
}

public class Main {
public static void main(String[] args) {
Shape[] shapes = {new Circle(5), new Square(3), new Triangle(3, 4, 5)};

for (Shape shape : shapes) {
System.out.println("Area:");
shape.area();
System.out.println("Perimeter:");
shape.perimeter();
System.out.println();
}

Shape.printPI();
}
}

Common Mistakes

  1. Forgetting to implement all abstract methods in an interface's implementing class.
  2. Not declaring constants as public static final inside an interface.
  3. Using the new keyword with an interface type, which will result in a compile-time error.
  4. Implementing both default and concrete methods for the same name in an interface and its implementing class, leading to method hiding issues.
  5. Not understanding the difference between abstract classes and interfaces.
  6. Incorrectly using static methods within an instance of a class that implements the interface.
  7. Creating a new instance of an interface directly instead of instantiating a class that implements the interface.
  8. Ignoring the benefits of using interfaces for polymorphism and abstraction, leading to code that is less maintainable and harder to test.

Practice Questions

  1. Create an Animal interface with abstract methods for eating, sleeping, and moving. Implement a Cat and Dog class that implements the Animal interface.
  2. Modify the previous example to include a Rectangle class that implements the Shape interface.
  3. Create an Employee interface with abstract methods for getting name, getting salary, and working. Implement a Manager and Worker class that implements the Employee interface.
  4. Modify the previous example to include a Cylinder class that implements the Shape interface using the radius and height properties.
  5. Create an Automobile interface with abstract methods for starting, accelerating, and braking. Implement a Car, Truck, and Motorcycle class that implements the Automobile interface.
  6. Modify the previous example to include a Bicycle class that implements the Automobile interface using the gear and speed properties.
  7. Create an Account interface with abstract methods for depositing, withdrawing, and checking the balance. Implement a SavingsAccount, CheckingAccount, and CreditCardAccount class that implements the Account interface.
  8. Modify the previous example to include a LoanAccount class that implements the Account interface using the loanAmount, interestRate, and monthlyPayment properties.
  9. Create an EmployeeManager class that manages a list of employees, allowing for adding, removing, and displaying employees. The EmployeeManager should implement the Iterable interface to allow easy iteration over the employees.
  10. Modify the previous example to include a ShapeFactory class that creates different shapes based on user input. The factory should return an instance of the appropriate shape implementing the Shape interface.

FAQ

  1. What is the purpose of interfaces in Java?
  • Interfaces define a contract for a set of behaviors, allowing for polymorphism and abstraction in object-oriented programming. They also provide a way to achieve multiple inheritance without the issues that can arise from using abstract classes.
  1. Can an interface have concrete methods?
  • Yes, starting from Java 8, interfaces can have default methods (methods with a body) and static methods (methods that do not operate on any instance of the class). However, abstract methods are still required to be implemented by the implementing classes.
  1. What happens if a class implements multiple interfaces but they have conflicting method names?
  • If a class implements multiple interfaces with conflicting method names, it will need to override the conflicting methods in the implementing class to resolve the name clash. The order of inheritance (the order in which the interfaces are listed in the implements clause) can affect the resolution of the method call.
  1. Can we create an object of an interface type?
  • No, we cannot create objects of an interface type directly because interfaces only define a contract and do not provide any implementation. Instead, we can create objects of the implementing classes and assign them to interface variables. However, we can call static methods on an interface type directly.
  1. What is the difference between abstract classes and interfaces?
  • Abstract classes can have instance variables, concrete methods, and non-abstract methods, while interfaces only contain abstract methods and constants. Abstract classes serve as a base class for other classes, while interfaces define a contract that classes must adhere to. Inheritance from multiple abstract classes is not allowed due to the diamond problem, but a class can implement multiple interfaces.
  1. What are the benefits of using interfaces?
  • Interfaces provide several benefits, including promoting modular and reusable code, enforcing polymorphism and abstraction, allowing for multiple inheritance without the issues that can arise from using abstract classes, and simplifying testing by allowing easy mocking of dependencies.
  1. Why are static methods included in interfaces?
  • Static methods in interfaces provide a way to include utility or helper methods related to the interface's contract without requiring an instance of a class that implements the interface. They can be called directly from the interface type, making them easily accessible and reusable across multiple classes.
Interfaces (Java) | Java | XQA Learn