Syntax
interface InterfaceName {\n returnType methodName(parameters);\n}Examples
Basic Interface
Defining a contract and implementing it in a class.
public interface Shape {
double area();
double perimeter();
}
public class Rectangle implements Shape {
private double width, height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
@Override
public double area() {
return width * height;
}
@Override
public double perimeter() {
return 2 * (width + height);
}
}
// Usage
Shape rect = new Rectangle(5, 3);
System.out.println(rect.area()); // 15.0Implementing Multiple Interfaces
A class can implement more than one interface, gaining the contract of each.
interface Swimmer {
void swim();
}
interface Runner {
void run();
}
public class Triathlete implements Swimmer, Runner {
@Override
public void swim() {
System.out.println("Swimming");
}
@Override
public void run() {
System.out.println("Running");
}
}
Triathlete athlete = new Triathlete();
athlete.swim();
athlete.run();Default and Static Methods
Since Java 8, interfaces can include default methods with a body, and static utility methods.
interface Vehicle {
void drive();
default void honk() { // has a body, implementing classes can use it as-is
System.out.println("Beep beep!");
}
static Vehicle createDefault() { // static factory-like helper
return () -> System.out.println("Driving a default vehicle");
}
}
public class Car implements Vehicle {
@Override
public void drive() {
System.out.println("Driving a car");
}
}
Car car = new Car();
car.drive();
car.honk(); // uses the default implementationInterface as a Type Reference
Using an interface as the declared type lets you write flexible code that works with any implementation.
interface PaymentMethod {
void pay(double amount);
}
class CreditCard implements PaymentMethod {
public void pay(double amount) {
System.out.println("Paid $" + amount + " with credit card");
}
}
class PayPal implements PaymentMethod {
public void pay(double amount) {
System.out.println("Paid $" + amount + " with PayPal");
}
}
public class Checkout {
public static void processPayment(PaymentMethod method, double amount) {
method.pay(amount); // works with ANY implementation
}
public static void main(String[] args) {
processPayment(new CreditCard(), 50.0);
processPayment(new PayPal(), 30.0);
}
}Best practices
- Program to an interface rather than a concrete class (declare variables as 'Shape', not 'Rectangle') for more flexible, decoupled code
- Use interfaces to define capabilities that unrelated classes might share, rather than forcing an inheritance hierarchy
- Keep interfaces small and focused (Interface Segregation Principle) - many specific interfaces beat one large, do-everything interface
- Use default methods sparingly, mainly for adding new functionality to an interface without breaking existing implementing classes
At a glance
- Purpose
- General-purpose application development
- File extension
- .java
- Runs in
- Java Virtual Machine
- Usually used with
- JDK and Java libraries
Specifications & further reading
Related Java documentation
Classes in Java are blueprints for creating objects, defining their properties (fields) and behaviors (methods). Java is a strictly object-oriented language where everything is encapsulated within classes. A class serves as a template that specifies what data an object will store and what operations it can perform.Abstract Classes
An abstract class is a class that cannot be instantiated directly and may contain both fully implemented methods and abstract methods (declared without a body, which subclasses must implement). Abstract classes sit between interfaces and regular classes: like an interface, they define a contract; like a regular class, they can hold state (fields) and provide shared, already-implemented behavior.Constructors
A constructor is a special method that runs automatically when an object is created with 'new', typically used to initialize the object's fields. A constructor shares its name with the class and has no return type, not even void. Java supports constructor overloading, letting a class offer multiple ways to construct an object, and constructor chaining with this(...), where one constructor calls another in the same class.Access Modifiers
Access modifiers control the visibility of classes, fields, and methods from other parts of a program. Java has four levels: public (accessible from anywhere), protected (accessible within the same package and by subclasses), default/package-private (accessible only within the same package, used when no modifier is written), and private (accessible only within the declaring class itself).