An interface is a type that defines a contract of operations without specifying a concrete implementation.
In Java, a class can only extend one other class, but it can implement multiple interfaces.
Imagine we have the Coche class.
- A
Cocheis aVehiculo. - But a
Cochecan also be anActivoFinanciero(it can be bought/sold). - And it can also be
Localizable(it has GPS coordinates).
How do we make Coche all three things at once if we can only extend one class?
The answer is Interfaces.
What is an Interface?
An Interface is a contract. It does not define how things are done, only WHAT must be done. It’s a list of requirements that a class must fulfill to be considered “compatible” with that interface.
Syntax: interface and implements
They are defined with the keyword interface.
Method declarations without a body are implicitly public and abstract. Modern interfaces can also include default, static, and private methods with an implementation.
// Define the contract "Can be located by GPS"
public interface Localizable {
// No need to write 'public abstract', it's already implicit.
void obtenerCoordenadas();
}
// Another independent contract
public interface ActivoFinanciero {
double getValorMercado();
}Now, we make our Coche sign those contracts using implements.
// Inherits from Vehiculo and implements multiple interfaces
public class Coche extends Vehiculo implements Localizable, ActivoFinanciero {
// ... constructor and methods of Coche ...
// MANDATORY: Fulfill the Localizable contract
@Override
public void obtenerCoordenadas() {
System.out.println("GPS: 40.416, -3.703");
}
// MANDATORY: Fulfill the ActivoFinanciero contract
@Override
public double getValorMercado() {
return 25000.00;
}
}Thanks to this, we have achieved Multiple Inheritance of Types. Our object is now polymorphic in 4 directions: it is an Object, a Vehiculo, a Localizable, and an ActivoFinanciero.
Coche miCoche = new Coche();
Localizable gps = miCoche; // Valid
gps.obtenerCoordenadas();
ActivoFinanciero cartera = miCoche; // Valid
System.out.println(cartera.getValorMercado());Interfaces vs Abstract Classes
This is the eternal question. When do I use one and when the other?
| Feature | Abstract Class | Interface |
|---|---|---|
| Relationship | “Is a” (Identity). | “Is capable of” (Capability / Behavior). |
| State | Can have attributes (fields) with state. | Can only have constants (public static final). |
| Inheritance | Single (extends). | Multiple (implements). |
| Constructors | Yes, it has them. | No, it does not. |
- Use an Abstract Class if you want to share code and state among very related classes (e.g., Dog and Cat share the
nombreattribute). - Use an Interface if you want to define common behavior for classes that are unrelated (e.g., a
Cocheand aMovilareLocalizable, but they are not related).
default Methods
A default method is a method in an interface that DOES have a body (code).
public interface Localizable {
void obtenerCoordenadas(); // Abstract (Mandatory)
// DEFAULT method (Optional)
// If the class does not override it, this code is used by default.
default void imprimirUbicacion() {
System.out.println("Printing coordinates to console...");
obtenerCoordenadas();
}
}Now, if you update the interface by adding a default method, old classes still work because they use the default implementation.
The Multiple Inheritance Problem (Solved)
If you implement two interfaces and both have an identical default method (same name), which one wins?
Java will give you a compilation error and force you to override it in your class to manually decide which one to call (using Interfaz.super.metodo()).
static and private Methods
With modern versions, interfaces have become very powerful:
staticMethods (Java 8): Utility methods associated with the interface (e.g.,Localizable.calcularDistancia(p1, p2)).privateMethods (Java 9): Used to avoid code repetition within the interface itself, sharing logic between twodefaultmethods.