Java Interface

What is a Java Interface?

A Java Interface is a blueprint for a class that includes a set of abstract methods. It defines the methods that a class must implement if it follows the interface. These methods don't have any implementation in the interface itself, only their signatures. An interface acts as a contract, ensuring that any class implementing it provides the required functionality. Java interfaces support multiple inheritance, as a class can implement multiple interfaces, facilitating loose coupling by allowing classes to interact with each other through interfaces.

Why Use Interfaces in Java?

Total Abstraction

Interfaces in Java enable total abstraction, allowing developers to define a set of methods without any implementation details. This abstraction hides the internal workings of classes, focusing on the behavior that needs to be provided to other parts of the program.

Support for Multiple Inheritance

Java interfaces allow a class to implement multiple interfaces, inheriting behaviors from different sources. This flexibility enhances code reuse.

Loose Coupling

Interfaces help achieve loose coupling in software development. By relying on interfaces instead of concrete classes, components are not dependent on specific implementations, promoting modularity and maintainability.

Implementing Abstraction

Interfaces provide a way to define a common set of methods that classes must implement, enforcing a certain level of abstraction.

Abstract Methods and Method Signatures

Defining Abstract Methods in Interfaces

Abstract methods in interfaces are defined by their method signature without any implementation. These methods must be implemented by any class that chooses to implement the interface. Abstract methods in interfaces are implicitly public, meaning any class implementing the interface must also declare the method as public.

Syntax for Method Signatures in Interfaces

In Java, method signatures in interfaces include the method name, parameters, and return type, without any code implementation. All methods in an interface are automatically public, ensuring they can be accessed from anywhere in the program.

Implementing Abstract Methods in Classes

Implementing in Concrete Classes

When a class implements an interface, it must provide an implementation for all abstract methods defined in the interface. Failure to do so results in a compilation error. For example:

public abstract class Vehicle {
    public abstract void transform();
}

public class Car extends Vehicle {
    public void transform() {
        // Implement the transform method for the Car class
    }
}

Static Methods and Constants

Adding Static Methods to Interfaces

Since Java 8, interfaces can include static methods, which are utility methods that are not tied to any specific instance of a class. These methods can be called using the interface name itself:

public interface MyInterface {
    static void myStaticMethod() {
        // method implementation
    }
}

Declaring Static Constants in Interfaces

Interfaces can contain static constants, which are variables that are implicitly public, static, and final. These constants provide a set of values that can be accessed by any class implementing the interface.

Default Methods and Default Implementation

Understanding Default Methods in Interfaces

Default methods in interfaces allow methods to have a default implementation. Classes implementing the interface can inherit this implementation or choose to override it. For example:

public interface Animal {
    void sound();
    
    default void eat() {
        System.out.println("Animal is eating.");
    }
}

Providing Default Implementations

Default methods help solve the interface evolution problem by allowing new methods to be added without breaking existing implementations. Implementing classes automatically inherit the default method unless they choose to override it.

Private and Helper Methods

Using Private Methods in Interfaces

Private methods in interfaces help factor out common code used by default and static methods, avoiding code duplication. These methods are accessible only within the interface itself, ensuring encapsulation.

Creating Helper Methods within Interfaces

Helper methods in interfaces provide common functionality that can be shared across multiple classes implementing the interface. These methods are typically static and can be accessed using the interface name.

public interface MathUtils {
    static int square(int a) {
        return a * a;
    }
}

Create a free account to access the full topic

“It has all the necessary theory, lots of practice, and projects of different levels. I haven't skipped any of the 3000+ coding exercises.”
Andrei Maftei
Hyperskill Graduate