An interface in Java defines a contract that implementing classes agree to follow. Interfaces are useful for abstraction, polymorphism, and writing flexible programs.
An interface is declared using the
interface keyword. It can define
abstract methods and may also contain default,
static, and private methods.
interface Animal {
void sound();
}
Here, Animal is an interface with one abstract method,
sound().
A class uses the implements keyword
to implement an interface.
interface Animal {
void sound();
}
class Dog implements Animal {
@Override
public void sound() {
System.out.println("Dog barks");
}
}
class Main {
public static void main(String[] args) {
Dog d1 = new Dog();
d1.sound();
}
}
Dog barks
Interface abstract methods are implicitly public, so their implementations must be public as well.
interface Vehicle {
void start();
void stop();
}
class Car implements Vehicle {
@Override
public void start() {
System.out.println("Car started");
}
@Override
public void stop() {
System.out.println("Car stopped");
}
}
class Main {
public static void main(String[] args) {
Car c1 = new Car();
c1.start();
c1.stop();
}
}
Car started
Car stopped
A concrete class must implement all inherited abstract interface methods unless the class itself is abstract.
An interface reference can refer to an object of a class that implements that interface. This supports runtime polymorphism.
interface Animal {
void sound();
}
class Dog implements Animal {
@Override
public void sound() {
System.out.println("Dog barks");
}
}
class Cat implements Animal {
@Override
public void sound() {
System.out.println("Cat meows");
}
}
class Main {
public static void main(String[] args) {
Animal a1 = new Dog();
Animal a2 = new Cat();
a1.sound();
a2.sound();
}
}
Dog barks
Cat meows
The same interface reference type can be used with different implementations.
A Java class can implement multiple interfaces by separating their names with commas.
interface Printable {
void print();
}
interface Scannable {
void scan();
}
class Machine implements Printable, Scannable {
@Override
public void print() {
System.out.println("Printing document");
}
@Override
public void scan() {
System.out.println("Scanning document");
}
}
class Main {
public static void main(String[] args) {
Machine m1 = new Machine();
m1.print();
m1.scan();
}
}
Printing document
Scanning document
This allows one class to provide behavior required by more than one interface.
Variables declared in an interface are implicitly
public static final. They are constants
and must be initialized when declared.
interface Settings {
int MAX_USERS = 100;
}
class Main {
public static void main(String[] args) {
System.out.println(Settings.MAX_USERS);
}
}
100
Access the constant using the interface name.
You cannot assign a new value to
Settings.MAX_USERS.
A default method has a method body and uses
the default keyword. Implementing
classes inherit it and may override it.
interface Greeting {
default void sayHello() {
System.out.println("Hello from Java");
}
}
class Student implements Greeting {
}
class Main {
public static void main(String[] args) {
Student s1 = new Student();
s1.sayHello();
}
}
Hello from Java
Student inherits the default implementation without needing to define the method itself.
An interface can contain static methods with implementations. They are called using the interface name.
interface Calculator {
static int add(int a, int b) {
return a + b;
}
}
class Main {
public static void main(String[] args) {
int result = Calculator.add(10, 20);
System.out.println(result);
}
}
30
Call the method as
Calculator.add(10, 20).
Interface static methods are not inherited
as instance methods by implementing classes.
An interface can extend another interface. It can also extend multiple interfaces.
interface Animal {
void eat();
}
interface Pet extends Animal {
void play();
}
class Dog implements Pet {
@Override
public void eat() {
System.out.println("Dog is eating");
}
@Override
public void play() {
System.out.println("Dog is playing");
}
}
class Main {
public static void main(String[] args) {
Dog d1 = new Dog();
d1.eat();
d1.play();
}
}
Dog is eating
Dog is playing
Dog implements Pet and must also implement the abstract method inherited from Animal.
| Interface | Abstract Class |
|---|---|
Declared with interface. |
Declared with abstract class. |
| Cannot have constructors. | Can have constructors. |
| Can define abstract, default, static, and private methods. | Can define abstract and concrete methods. |
| Fields are implicitly public, static, and final. | Can have instance fields with different access modifiers. |
| A class can implement multiple interfaces. | A class can extend only one class. |
interface Payment {
void pay(double amount);
}
class UpiPayment implements Payment {
@Override
public void pay(double amount) {
System.out.println("UPI payment: " + amount);
}
}
class CardPayment implements Payment {
@Override
public void pay(double amount) {
System.out.println("Card payment: " + amount);
}
}
class Main {
static void processPayment(Payment payment, double amount) {
payment.pay(amount);
}
public static void main(String[] args) {
Payment p1 = new UpiPayment();
Payment p2 = new CardPayment();
processPayment(p1, 500);
processPayment(p2, 1000);
}
}
UPI payment: 500.0
Card payment: 1000.0
The processPayment() method accepts any Payment implementation, making it easier to add new payment types without rewriting the method.
An interface cannot be instantiated directly. Create an object of a concrete implementing class and, when useful, store it in an interface reference.
1. Which keyword is used to implement an interface?
2. Can a Java class implement multiple interfaces?
3. How do you call a static interface method?