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.

Feb 22, 20263 views0 likes0 fires
18px

[!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.

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
2 questions5 min

Continue Learning

19. Packages & Access Modifiers

Beginner
8 min

20. Enums (Enumerations)

Intermediate
7 min

21. Exceptions: Handling Runtime Errors

Intermediate
12 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 Java Programming: From Zero to EnterpriseAll Categories