In Object-Oriented Programming (OOP), an interface is a crucial concept that defines a contract for classes to follow. It allows multiple classes to share a common structure while enforcing certain behaviors. Interfaces are widely used in Java and other OOP languages to achieve abstraction, polymorphism, and loose coupling.
An interface in Java is a collection of abstract methods (methods without implementation) that a class can implement. It defines a contract that the implementing classes must adhere to.
- Defines a contract that implementing classes must follow.
- Cannot have instance variables (only
public static final
constants). - All methods are implicitly public and abstract (unless they have a default or static implementation).
- Supports multiple inheritance, unlike classes.
- Improves code flexibility and testability.
To define an interface, use the interface
keyword.
// Defining an interface
public interface Vehicle {
void start(); // Abstract method (no implementation)
void stop(); // Abstract method (no implementation)
}
A class implements an interface using the implements
keyword.
// Implementing the Vehicle interface in a Car class
public class Car implements Vehicle {
@Override
public void start() {
System.out.println("Car is starting...");
}
@Override
public void stop() {
System.out.println("Car is stopping...");
}
}
Now, let's create objects and call the methods.
public class Main {
public static void main(String[] args) {
Vehicle myCar = new Car(); // Polymorphism: Interface reference
myCar.start();
myCar.stop();
}
}
Car is starting...
Car is stopping...
Unlike classes, Java does not support multiple inheritance with classes, but it does support multiple inheritance with interfaces.
// First interface
interface Flyable {
void fly();
}
// Second interface
interface Drivable {
void drive();
}
// Implementing multiple interfaces
public class FlyingCar implements Flyable, Drivable {
@Override
public void fly() {
System.out.println("FlyingCar is flying...");
}
@Override
public void drive() {
System.out.println("FlyingCar is driving...");
}
}
public class Main {
public static void main(String[] args) {
FlyingCar myVehicle = new FlyingCar();
myVehicle.fly();
myVehicle.drive();
}
}
FlyingCar is flying...
FlyingCar is driving...
Java 8 introduced default methods in interfaces, allowing methods with a body.
interface Animal {
void sound();
// Default method with implementation
default void sleep() {
System.out.println("Sleeping...");
}
}
class Dog implements Animal {
@Override
public void sound() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.sound();
myDog.sleep(); // Calling default method
}
}
Dog barks
Sleeping...
Interfaces can also have static methods.
interface MathOperations {
static int add(int a, int b) {
return a + b;
}
}
public class Main {
public static void main(String[] args) {
int result = MathOperations.add(5, 10);
System.out.println("Sum: " + result);
}
}
Sum: 15
interface Payment {
void pay(double amount);
}
class CreditCardPayment implements Payment {
@Override
public void pay(double amount) {
System.out.println("Paid " + amount + " using Credit Card");
}
}
class PayPalPayment implements Payment {
@Override
public void pay(double amount) {
System.out.println("Paid " + amount + " using PayPal");
}
}
public class Main {
public static void main(String[] args) {
Payment payment1 = new CreditCardPayment();
payment1.pay(100.50);
Payment payment2 = new PayPalPayment();
payment2.pay(200.75);
}
}
Paid 100.5 using Credit Card
Paid 200.75 using PayPal