Skip to content
QuizMaker logoQuizMaker
Activity
Java Programming: From Zero to Enterprise
2. Object-Oriented Programming
1. Getting Started with Java & the JVM
2. Data Types & Variables
3. Control Flow: Ifs & Loops
4. String Manipulation in Depth
5. Methods (Functions) Architecture
6. Arrays & The Enhanced For Loop
7. User Input via Scanner
8. Mathematical Operations & The Math Class
9. Operators in Depth
10. Block Scope & Variable Lifecycles
11. Introduction to Object-Oriented Programming
12. Classes & Instances Deep Dive
13. Constructors
14. Encapsulation & The 'this' Keyword
15. Inheritance: Extending Functionality
16. Polymorphism & Method Overriding
17. Abstraction & Abstract Classes
18. Interfaces: The Ultimate Contract
19. Packages & Access Modifiers
20. Enums (Enumerations)
21. Exceptions: Handling Runtime Errors
22. The 'throw' and 'throws' keywords
23. Dates, Times, and Formatting
24. Enumerable Data Structures
25. LinkedLists: The Alternative
26. HashMaps: Key-Value Architecture
27. HashSets: The Art of Uniqueness
28. Iterator: Safe Collection Traversal
29. Wrapper Classes & Autoboxing
30. Basic File I/O
31. Generics: Type-Safe Templates
32. Lambda Expressions & Functional Interfaces
33. The Stream API: Functional Data Pipelines
34. Optional: Beating the NullPointerException
35. Multithreading & Concurrency Basics
36. JDBC: Connecting to SQL Databases
37. Annotations & Reflection
38. The JVM Garbage Collector
39. Introduction to Spring Boot
40. Unit Testing with JUnit
41. Java Collections for DSA
CONTENTS

18. Interfaces: The Ultimate Contract

Overcoming single-inheritance limits using pure abstract Interfaces.

Java Programming: From Zero to Enterprise
2. Object-Oriented Programming
February 22, 2026
63
A

[!NOTE] We learned earlier that Java enforces strict Single Inheritance. A child class can only extend exactly ONE parent class. But what if your Bird class needs to inherit from Animal, but also needs to inherit a bunch of flying logic from a Flyable class?

We solve this problem using Interfaces.

What is an Interface?

An Interface is an extreme version of an Abstract Class. Historically (prior to Java 8), an interface could only contain pure abstract methods and constant variables. It represents a strict "Contract of Behavior".

Instead of a class extending an interface, a class implements an interface. And unlike strict single inheritance, a class can implement an infinite number of interfaces simultaneously!

// 1. Defining Interfaces (Contracts)
interface Flyable {
    void fly(); // Naturally abstract and public, even if we don't type it!
}

interface Swimable {
    void swim();
}

// 2. A normal Parent Class
class Animal {
    void sleep() { System.out.println("Zz"); }
}

// 3. The Ultimate Child Class
// It extends ONE parent, but implements TWO interfaces!
class Duck extends Animal implements Flyable, Swimable {
    
    // Contract fulfillment: Must provide flight code!
    public void fly() {
        System.out.println("The duck flies awkwardly over the lake.");
    }

    // Contract fulfillment: Must provide swim code!
    public void swim() {
        System.out.println("The duck paddles in the water.");
    }
}

Why Use Interfaces?

Interfaces decouple "what an object is" from "what an object can do".

A Helicopter is absolutely NOT an Animal. It makes zero sense for a Helicopter class to inherit from Animal. However, both a Helicopter and a Duck can fly. They share a behavioral capability.

If you have a video game function that accepts anything that can fly, you can use the Interface as the expected data type!

public class GameEngine {
    // This method accepts ANYTHING that implements the Flyable interface!
    // It doesn't care if it's a Duck, a Helicopter, or a MagicCarpet!
    public void triggerTakeoff(Flyable target) {
        target.fly();
    }
}

[!TIP] Modern Java Updates: Since Java 8, interfaces are actually allowed to have default methods with real body implementations. This was added to allow architects to upgrade massive enterprise interfaces without breaking millions of legacy applications that hadn't written overrides for the new methods.

Interfaces Describe Capabilities

An interface is ideal when unrelated classes share a capability. A Bird, Drone, and Plane are not the same kind of object, but all can be Flyable. That makes interfaces a powerful way to design flexible code.

Programming to an Interface

interface PaymentMethod {
    boolean pay(double amount);
}

class CardPayment implements PaymentMethod {
    public boolean pay(double amount) {
        System.out.println("Paid by card: " + amount);
        return true;
    }
}

class CheckoutService {
    void checkout(PaymentMethod paymentMethod, double amount) {
        paymentMethod.pay(amount);
    }
}

CheckoutService does not care whether payment uses card, UPI, wallet, or net banking. It only cares that the object can pay.

Default Methods

interface Trackable {
    void track();

    default void printTrackingStarted() {
        System.out.println("Tracking started");
    }
}

Default methods are useful, but keep them small. Interfaces should mainly describe behavior, not become large utility classes.

Common Mistakes

  • Naming interfaces too vaguely, such as Manager or Handler, without clear behavior.
  • Adding too many unrelated methods to one interface.
  • Using an abstract class when unrelated classes only need to share a capability.
  • Forgetting that interface methods are public contracts for implementers.

Mini Practice

Create an interface Downloadable with a download() method. Implement it in PdfFile and VideoFile. Write one method that accepts any Downloadable.

Practice Lab: Downloadable Files

Use an interface to model a shared capability across unrelated classes.

  1. Create interface Downloadable with method download().
  2. Create PdfFile, VideoFile, and AudioFile.
  3. Make each class implement Downloadable.
  4. Write one method that accepts Downloadable and calls download().
  5. Pass all three file types to that method.

Goal: Depend on capability, not concrete class names.

Revision Checkpoint

  • Interface: Contract describing capability.
  • implements: Connects a class to an interface.
  • Multiple interfaces: One class can implement many interfaces.
  • Capability design: Interfaces describe what an object can do.
  • Default methods: Interfaces can provide small shared implementations.

Before the quiz: Name three unrelated classes that could implement the same interface.

Share this article

Share on TwitterShare on LinkedInShare on FacebookShare on WhatsAppShare on Email

Test your knowledge

Take a quick quiz based on this chapter.

mediumJava
Quiz: Interfaces
10 questions5 min
Lesson 8 of 10 in 2. Object-Oriented Programming
Previous in 2. Object-Oriented Programming
17. Abstraction & Abstract Classes
Next in 2. Object-Oriented Programming
19. Packages & Access Modifiers
Back to Java Programming: From Zero to Enterprise
Back to moduleCategories