What Is an Interface?


An interface is a special type in programming that only declares what methods a class should have — not how those methods work.

Think of it as:

  • A toy blueprint — shows what parts the toy must have, but not the colors or materials.
  • A remote control — has buttons (methods) you can press, but it doesn’t say what happens inside the TV.
  • A promise/contract — “If you say you implement me, you must give me all these methods.”

Why?

Interfaces help us write flexible code where different classes can be used in the same way, even if they work differently inside.


How Interfaces Differ from Classes and Abstract Classes

Example 1 — Single Interface Implemented by a Class

interface Flyable {
    void fly();
}

class Bird implements Flyable {
    public void fly() {
        System.out.println("Bird is flying");
    }
}

public class Main {
    public static void main(String[] args) {
        Bird b = new Bird();
        b.fly();
    }
}
  • interface Flyable {...} — declares a rule: every Flyable must have fly().
  • class Bird implements Flyable — says Bird follows the Flyable rules.
  • public void fly() — gives the actual flying action.
  • main — creates a Bird and makes it fly.

Analogy: A “flyable” toy could be a paper plane or a drone — the rule is “must fly,” but how it flies is up to the maker.

Example 2 — Multiple Interfaces in One Class

interface Flyable {
    void fly();
}

interface Swimmable {
    void swim();
}

class Duck implements Flyable, Swimmable {
    public void fly() {
        System.out.println("Duck is flying low");
    }
    public void swim() {
        System.out.println("Duck is swimming");
    }
}

public class Main {
    public static void main(String[] args) {
        Duck d = new Duck();
        d.fly();
        d.swim();
    }
}
  • Duck promises to follow two sets of rules: Flyable and Swimmable.
  • It must give code for both fly() and swim().

Analogy: A duck is like a toy that both flies and floats.

Example 3 — Using Interface as a Type

interface Playable {
    void play();
}

class Guitar implements Playable {
    public void play() {
        System.out.println("Strumming the guitar");
    }
}

class Piano implements Playable {
    public void play() {
        System.out.println("Playing the piano");
    }
}

public class Main {
    public static void main(String[] args) {
        Playable p1 = new Guitar();
        Playable p2 = new Piano();
        p1.play();
        p2.play();
    }
}
  • Both Guitar and Piano follow the Playable rule.
  • We can store them in Playable variables.
  • We can call play() without caring if it’s a guitar or piano.

Analogy: A music teacher can say “Play your instrument” without needing to know which instrument you have.

Bonus — Default Methods in Interfaces (Java)

interface Greetable {
    void greet();
    default void sayBye() {
        System.out.println("Goodbye!");
    }
}

class Person implements Greetable {
    public void greet() {
        System.out.println("Hello!");
    }
}

public class Main {
    public static void main(String[] args) {
        Person p = new Person();
        p.greet();
        p.sayBye();
    }
}

When to use: Default methods let you add new methods to interfaces without breaking old code.


Common Mistakes (and How to Avoid Them)

  1. Forgetting to implement all methods — The compiler will complain; double-check the interface rules.
  2. Trying to make variables inside an interface — You can only have constants (final static).
  3. Mixing interface and class keywordsinterface is for rules, class for objects.
  4. Expecting to use new InterfaceName() — Interfaces can’t be directly created.
  5. Overcomplicating interfaces — Keep them short and focused.

When to Use Interfaces — Quick Checklist

  • ✅ Ensure different classes share the same method names.
  • ✅ Flexibility to swap objects without changing code.
  • ✅ Multiple inheritance of behavior rules.
  • ✅ Defining a plugin or API where other people will build the classes.

Comments

Popular posts from this blog

Inheritance [ class – abstract class (extends) ]

OOP

OOPC - Inheritance (class – class)