In Java, an interface is a reference type, similar to a class, that can contain only abstract methods, default methods, static methods, and constant variables (final fields). Interfaces are used to define a contract or blueprint for classes that implement them, specifying what methods should be provided, without dictating how they should be implemented.
public
, static
, and final
. They are constant values that cannot be modified.An interface is defined using the interface
keyword. By convention, interface names are usually adjectives or descriptive nouns.
// Define an interface named Vehicle
public interface Vehicle {
// Abstract method (no body)
void start();
// Abstract method (no body)
void stop();
}
In this example, the Vehicle
interface declares two abstract methods: start()
and stop()
.
A class that implements an interface must provide implementations for all of the interface’s abstract methods. The implements
keyword is used to specify that a class implements an interface.
// Implementing the Vehicle interface in the Car class
public class Car implements Vehicle {
// Provide implementation for the start method
@Override
public void start() {
System.out.println("Car is starting.");
}
// Provide implementation for the stop method
@Override
public void stop() {
System.out.println("Car is stopping.");
}
public static void main(String[] args) {
Car myCar = new Car();
myCar.start(); // Output: Car is starting.
myCar.stop(); // Output: Car is stopping.
}
}
In this example:
Car
class implements the Vehicle
interface, providing concrete implementations for the start()
and stop()
methods.@Override
annotation indicates that these methods are overriding the interface’s abstract methods.A class can implement multiple interfaces, allowing for more flexible code structure.
// Define another interface named Electric
interface Electric {
void charge();
}
// Implementing both Vehicle and Electric interfaces in the ElectricCar class
public class ElectricCar implements Vehicle, Electric {
@Override
public void start() {
System.out.println("Electric car is starting.");
}
@Override
public void stop() {
System.out.println("Electric car is stopping.");
}
@Override
public void charge() {
System.out.println("Electric car is charging.");
}
public static void main(String[] args) {
ElectricCar myElectricCar = new ElectricCar();
myElectricCar.start(); // Output: Electric car is starting.
myElectricCar.charge(); // Output: Electric car is charging.
myElectricCar.stop(); // Output: Electric car is stopping.
}
}
In this example:
ElectricCar
class implements both the Vehicle
and Electric
interfaces, providing implementations for the methods in both interfaces.Java 8 introduced default methods in interfaces, which are methods with a body. This feature allows interface designers to add new methods to existing interfaces without breaking existing code.
public interface Vehicle {
void start();
void stop();
// Default method with a body
default void honk() {
System.out.println("Honking!");
}
}
// Implementing the Vehicle interface
public class Bike implements Vehicle {
@Override
public void start() {
System.out.println("Bike is starting.");
}
@Override
public void stop() {
System.out.println("Bike is stopping.");
}
public static void main(String[] args) {
Bike myBike = new Bike();
myBike.start(); // Output: Bike is starting.
myBike.honk(); // Output: Honking!
myBike.stop(); // Output: Bike is stopping.
}
}
In this example:
Vehicle
interface has a default method honk()
, which provides a default implementation.Bike
class implements the Vehicle
interface and gains access to the honk()
method without explicitly overriding it.Interfaces can also have static methods, which belong to the interface itself and cannot be overridden by implementing classes.
public interface Utility {
static void printMessage(String message) {
System.out.println("Message: " + message);
}
}
public class UtilityDemo {
public static void main(String[] args) {
// Call the static method from the interface
Utility.printMessage("Hello, World!"); // Output: Message: Hello, World!
}
}
In this example:
Utility
interface defines a static method printMessage()
, which can be called using the interface name.Fields in an interface are implicitly public
, static
, and final
, meaning they are constants that must be initialized when declared.
public interface Constants {
// Constant variable
int MAX_SPEED = 120;
String VEHICLE_TYPE = "Automobile";
}
public class ConstantsDemo {
public static void main(String[] args) {
// Access constant variables from the interface
System.out.println("Max Speed: " + Constants.MAX_SPEED); // Output: Max Speed: 120
System.out.println("Vehicle Type: " + Constants.VEHICLE_TYPE); // Output: Vehicle Type: Automobile
}
}
In this example:
Constants
interface defines constant variables MAX_SPEED
and VEHICLE_TYPE
, which can be accessed using the interface name.A functional interface is an interface that contains exactly one abstract method. Functional interfaces can be used with lambda expressions to create concise code for implementing the single abstract method.
// Functional interface with a single abstract method
@FunctionalInterface
interface Greeting {
void sayHello(String name);
}
public class LambdaDemo {
public static void main(String[] args) {
// Using a lambda expression to implement the Greeting interface
Greeting greeting = (name) -> System.out.println("Hello, " + name);
greeting.sayHello("Alice"); // Output: Hello, Alice
}
}
In this example:
Greeting
interface is a functional interface with a single abstract method sayHello()
.Aspect | Interface | Abstract Class |
---|---|---|
Method Implementation | Can have abstract, default, and static methods | Can have both abstract and concrete methods |
Multiple Inheritance | A class can implement multiple interfaces | A class can only extend one abstract class |
Access Modifiers | Methods are implicitly public | Methods can have any access modifier |
Fields | Fields are public , static , and final | Can have instance fields (not final ) |
Inheritance Keyword | Implemented using implements | Extended using extends |
Use Cases | Used to define capabilities or behaviors | Used for base classes with common functionality |