Lesson Completion
Back to course

Implementing Interfaces: Completing the Contract

Intermediate
15 minutes4.7Java

1. The Hook (The "Byte-Sized" Intro)

In a Nutshell: To implement an interface, a class uses the implements keyword and provides concrete implementations for ALL abstract methods in the interface. A class can implement multiple interfaces, enabling flexible, capability-based design.

Think of certifications. When you get a "Java Certified" badge, you've "implemented" the Java certification interface—you've proven you can do everything the certification requires!


2. Conceptual Clarity (The "Simple" Tier)

💡 The Analogy: The Driver's License

To get a driver's license (implement Drivable):

  • You must pass written test (knowRules())
  • You must pass driving test (drive())
  • You must pass vision test (see())

Once you implement all requirements, you're certified!


3. Technical Mastery (The "Deep Dive")

Formal Definition

Implementing Interface:

  1. Use implements keyword after class name
  2. Provide public implementations for ALL abstract methods
  3. Can implement multiple interfaces: class C implements A, B, C
  4. Can extend a class AND implement interfaces: class C extends Parent implements A, B

4. Interactive & Applied Code

java
interface Printable { void print(); } interface Scannable { void scan(); } // Implementing multiple interfaces class AllInOnePrinter implements Printable, Scannable { @Override public void print() { System.out.println("Printing document..."); } @Override public void scan() { System.out.println("Scanning document..."); } } // Extending class + implementing interfaces class Device { String brand; Device(String brand) { this.brand = brand; } } class SmartPrinter extends Device implements Printable, Scannable { SmartPrinter(String brand) { super(brand); } @Override public void print() { System.out.println(brand + " printing..."); } @Override public void scan() { System.out.println(brand + " scanning..."); } } public class Main { public static void main(String[] args) { AllInOnePrinter printer = new AllInOnePrinter(); printer.print(); printer.scan(); // Polymorphism Printable p = printer; p.print(); Scannable s = printer; s.scan(); } }

5. The Comparison & Decision Layer

ScenarioSyntax
Implement oneclass C implements A
Implement multipleclass C implements A, B, C
Extend + Implementclass C extends Parent implements A, B

6. The "Interview Corner" (The Edge)

The "Killer" Interview Question: "What happens if you don't implement all interface methods?" Answer: The class must be declared abstract. You can't create a concrete class that doesn't fulfill the interface contract.

Pro-Tip: Use interfaces for dependency injection:

java
class Service { private Notifier notifier; // Interface, not concrete class Service(Notifier notifier) { this.notifier = notifier; // Inject any implementation! } }

Topics Covered

Object-Oriented ProgrammingAbstraction

Tags

#java#abstraction#interfaces#abstract-classes#contract-programming

Last Updated

2025-02-01