Lesson 42 of 50 – Java Interfaces
84%

Java Interfaces

An interface in Java defines a contract that implementing classes agree to follow. Interfaces are useful for abstraction, polymorphism, and writing flexible programs.

1. What Is an Interface?

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().

An interface describes what a class should do, while the implementing class provides the required implementation.
2. Implementing an Interface

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();
    }
}
Output:
Dog barks

Interface abstract methods are implicitly public, so their implementations must be public as well.

3. Interface with Multiple Methods
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();
    }
}
Output:
Car started
Car stopped

A concrete class must implement all inherited abstract interface methods unless the class itself is abstract.

4. Interface Reference

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();
    }
}
Output:
Dog barks
Cat meows

The same interface reference type can be used with different implementations.

5. Implementing Multiple Interfaces

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();
    }
}
Output:
Printing document
Scanning document

This allows one class to provide behavior required by more than one interface.

6. Interface Variables

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);
    }
}
Output:
100

Access the constant using the interface name. You cannot assign a new value to Settings.MAX_USERS.

7. Default Methods in Interfaces

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();
    }
}
Output:
Hello from Java

Student inherits the default implementation without needing to define the method itself.

8. Static Methods in Interfaces

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);
    }
}
Output:
30

Call the method as Calculator.add(10, 20). Interface static methods are not inherited as instance methods by implementing classes.

9. Extending an Interface

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();
    }
}
Output:
Dog is eating
Dog is playing

Dog implements Pet and must also implement the abstract method inherited from Animal.

10. Interface vs Abstract Class
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.
Use an interface to define a contract. Use an abstract class when related classes should share state or common implementation.
11. Real-Life Example: Payment System
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);
    }
}
Output:
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.

12. Advantages of Interfaces
  • Support abstraction and flexible program design.
  • Allow classes to implement multiple contracts.
  • Enable polymorphism through interface references.
  • Help reduce dependencies on specific implementations.
  • Make it easier to introduce new implementations.
Important Point

An interface cannot be instantiated directly. Create an object of a concrete implementing class and, when useful, store it in an interface reference.

13. Practice Assignments
  1. Create an Animal interface with a sound() method. Implement it in Dog and Cat classes.
  2. Create a Vehicle interface with start() and stop() methods. Implement it in Car and Bike.
  3. Create Printable and Scannable interfaces. Implement both in a Machine class.
  4. Create a Calculator interface with an add() method and implement it in a class.
  5. Create a Payment interface and implement it in UpiPayment and CardPayment.
  6. Create a Greeting interface with a default sayHello() method.
  7. Create Parent and Child interfaces where Child extends Parent. Implement Child in a class.
Practice Tip: Use interface references in your programs to understand how different implementations can be used through a common type.

🧠 Quick Quiz

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?