Interfaces in Java are a fundamental feature that allows you to define a contract that classes can implement. They are a way of achieving abstraction in Java, and are crucial for software design, especially when it comes to large systems and frameworks. When learning Java, understanding interfaces is essential to advance to intermediate and advanced level programming.

Interface Concept

An interface is a collection of abstract methods and static constants. Abstract methods are those that are declared without an implementation. When a class implements an interface, it must provide concrete implementations for all abstract methods declared in the interface. This ensures that the class follows the "contract" established by the interface.

Interface Declaration

To declare an interface in Java, you use the interface keyword. Here is a simple example:

public interface Vehicle {
    int MAX_SPEED = 120; // static constant
    void start(); // abstract method
    void stop(); // abstract method
    void accelerate(int speed); // abstract method
}

Note that methods within an interface are implicitly public and abstract, so you do not need to specify these modifiers. Constants are public, static and final by definition.

Implementing Interfaces

When a class implements an interface, it uses the implements keyword. Here is an example of a class that implements the Vehicle interface:

public class Car implements Vehicle {
    public void start() {
        // implementation of the start method
    }
    
    public void stop() {
        // implementation of the stop method
    }
    
    public void accelerate(int speed) {
        // implementation of the accelerate method
    }
}

If a class does not provide implementations for all of the abstract methods of the interface, it must be declared abstract.

Interfaces and Inheritance

One of the great advantages of interfaces is that they can be used to simulate multiple inheritance, something that is not possible with classes in Java. A class can implement several interfaces, separating them with commas:

public class ElectricCar implements Vehicle, EcoFriendly {
    // implementation of Vehicle and EcoFriendly methods
}

This allows the ElectricCar class to have behaviors defined by multiple interfaces.

Default Methods

Starting in Java 8, interfaces can have default methods, which have a default implementation. This allows new methods to be added to interfaces without breaking the classes that implement them. For example:

public interface Vehicle {
    // abstract methods
    
    default void turnAlarmOn() {
        // default implementation of the turnAlarmOn method
    }
    
    default void turnAlarmOff() {
        // default implementation of the turnAlarmOff method
    }
}

Classes that implement the Vehicle interface can choose to use these default implementations or provide their own.

Static Methods

Interfaces can also have static methods. These are methods that can be called in the name of the interface, without the need for an instance:

public interface Vehicle {
    // abstract and default methods
    
    static String getVehicleType() {
        return "Unknown Vehicle Type";
    }
}

This method can be called like this: String type = Vehicle.getVehicleType();

Functional Interfaces

A functional interface is an interface that contains exactly one abstract method. They are used as the basis for lambda expressions and method references. Java 8 introduced the @FunctionalInterface annotation to indicate that an interface is intended to be a functional interface:

@FunctionalInterface
public interface SimpleFunctionalInterface {
    void execute();
}

This annotation is not mandatory, but it helps ensure that the interface maintains only one abstract method.

Conclusion

Interfaces are a fundamental piece in Java programming, allowing the creation of robust contracts, promoting abstraction and facilitating code maintenance and expansion. By implementing interfaces, classes can become more modular and interchangeable, which is a significant advantage in software design. Learning to use interfaces correctly is an essential step toward becoming a competent Java programmer and is at the heart of many advanced design patterns in object-oriented programming.

Now answer the exercise about the content:

_Which of the following statements about interfaces in Java is correct?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Basic Java syntax: Exception Handling (try, catch, finally, throw, throws)

Next page of the Free Ebook:

17Basic Java syntax: Exception Handling (try, catch, finally, throw, throws)

5 minutes

Obtenez votre certificat pour ce cours gratuitement ! en téléchargeant lapplication Cursa et en lisant lebook qui sy trouve. Disponible sur Google Play ou App Store !

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text