Interfaces and Their Types in Java

Interfaces in Java are a crucial part of object-oriented programming. They define a contract for classes, ensuring that they adhere to a specific structure without dictating how the behavior is implemented. This enables abstraction, multiple inheritance, and polymorphism, making Java applications more modular and flexible.

What is an Interface in Java?

An interface in Java is a collection of abstract methods and constant variables. It is similar to a class but lacks instance variables and method implementations. Instead, it provides a blueprint that implementing classes must follow.

Key Characteristics of Interfaces:

  • Cannot be instantiated.

  • May contain abstract methods (implicitly public and abstract).

  • May contain default and static methods (since Java 8).

  • May contain private methods (since Java 9).

  • Supports multiple inheritance.

  • Can be used as a reference type.

Multiple Inheritance in Java

Multiple inheritance is the ability of a class to inherit behavior from more than one parent class. Java does not support multiple inheritance with classes to avoid ambiguity, but interfaces allow it. A class can implement multiple interfaces, thereby inheriting multiple sets of behavior.

Example:

interface Flyable {
    void fly();
}

interface Swimmable {
    void swim();
}

class Duck implements Flyable, Swimmable {
    public void fly() {
        System.out.println("Duck is flying");
    }
    public void swim() {
        System.out.println("Duck is swimming");
    }
}

Abstract Methods in Java

Abstract methods are methods that are declared without an implementation. They must be implemented by subclasses or classes that implement an interface.

Example:

interface Vehicle {
    void start(); // Abstract method
    void stop();  // Abstract method
}

class Car implements Vehicle {
    public void start() {
        System.out.println("Car is starting");
    }

    public void stop() {
        System.out.println("Car is stopping");
    }
}

Reference Type in Java

A reference type in Java refers to a variable that stores a reference (memory address) to an object rather than the actual object itself. Interfaces can be used as reference types, meaning an interface variable can hold an object of any class that implements the interface.

Example:

Vehicle myCar = new Car(); // Vehicle is a reference type
myCar.start();

Types of Interfaces in Java

Java provides several types of interfaces, each serving different purposes in application design.

1. Normal Interfaces (Pure Abstract Interfaces)

These are interfaces that contain only abstract methods (before Java 8). They are used as a blueprint for classes.

Example:

interface Animal {
    void makeSound();
}

class Dog implements Animal {
    public void makeSound() {
        System.out.println("Woof Woof!");
    }
}

2. Functional Interfaces

Functional interfaces contain only a single abstract method (SAM). They enable the use of lambda expressions and method references.

Example:

@FunctionalInterface
interface Calculator {
    int add(int a, int b);
}

public class LambdaExample {
    public static void main(String[] args) {
        Calculator sum = (a, b) -> a + b;
        System.out.println("Sum: " + sum.add(10, 5));
    }
}

Common functional interfaces in Java include Runnable, Callable, Supplier, Consumer, and Predicate.

3. Marker Interfaces

Marker interfaces do not contain any methods or fields. They serve as metadata to indicate that a class possesses some specific behavior.

Example:

interface Serializable {} // Java built-in marker interface

class User implements Serializable {
    String name;
}

Examples of built-in marker interfaces: Serializable, Cloneable, and Remote.

4. Tagging Interfaces

Tagging interfaces are similar to marker interfaces but are used to categorize classes into groups. Unlike marker interfaces, tagging interfaces may also include additional methods to provide specific functionality.

Example:

interface Archivable {
    void archive();
}

class Document implements Archivable {
    public void archive() {
        System.out.println("Document has been archived");
    }
}

In this example, the Archivable interface categorizes certain objects as being archivable while also providing a method for implementation. Tagging interfaces are similar to marker interfaces but are used to categorize classes into groups.

5. Comparable and Cloneable Interfaces

These are standard interfaces provided by Java that serve special purposes.

  • Comparable<T>: Used for natural ordering.

  • Cloneable: Allows object cloning.

Example of Comparable:

class Student implements Comparable<Student> {
    int id;
    Student(int id) { this.id = id; }

    public int compareTo(Student s) {
        return Integer.compare(this.id, s.id);
    }
}

Conclusion

Interfaces are a powerful tool in Java that promote flexibility, modularity, and maintainability. They help implement abstraction, multiple inheritance, and functional programming paradigms. Understanding the different types of interfaces allows developers to design more scalable and efficient Java applications.

0
Subscribe to my newsletter

Read articles from Ali Rıza Şahin directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ali Rıza Şahin
Ali Rıza Şahin

Product-oriented Software Engineer with a solid understanding of web programming fundamentals and software development methodologies such as agile and scrum.