Abstraction means showing only what matters and hiding the rest. When you drive a car, you use the steering wheel and pedals โ you don't need to understand the engine. The car abstracts the messy machinery behind simple controls.
In OOP, abstraction means exposing a simple, meaningful interface while hiding complicated implementation details. Your code can say player.play() without caring whether the player streams, reads a file, or plays from memory.
Both help you define abstraction, but they're different tools:
| Feature | Abstract Class | Interface |
|---|---|---|
| Can hold state (fields)? | Yes | No (mostly constants) |
| Can provide method code? | Yes, full and abstract methods | Yes, with default methods |
| Multiple inheritance? | No โ one parent only | Yes โ implement many |
| Best forโฆ | Shared code + shared state | Shared behaviour / contract |
An interface is a contract: "any class that implements me promises to provide these methods." It says what can be done, not how.
Good interfaces are small and focused. A Flyable interface with just fly() is better than a Vehicle interface with 20 methods. Small contracts are easier to implement, test, and swap.
Modern languages like Java and C# allow interfaces to include default methods โ methods with code that implementing classes inherit automatically. This lets you add features to an interface without breaking every class that already implements it.
Static methods in interfaces are helper methods that belong to the interface itself, not to any implementing class. Use them for utility operations, like Flyable.canReach(altitude).
This is the magic: you can hold any implementing object in a variable of the interface type. Call a method, and the correct implementation runs โ automatically.
Example: a List<Flyable> can hold a Bird, an Airplane, and a Drone. Loop through them and call fly() โ each one flies its own way. This is the heart of flexible, extensible design.
Inheritance creates a strong parentโchild bond. Sometimes that's exactly what you want โ but often, an interface is a better fit because it's looser.
Think of it this way: inheritance says "is a" (a Dog is an Animal). An interface says "can do" (a Dog can swim). If a Robot can swim too, it shouldn't need to inherit from Animal โ it should just implement Swimmable.
Abstraction in real life: Pick three objects around you (a phone, a notebook, a door). Write down what you actually interact with and what you don't see. That's abstraction in action.
Shape builder: Create an abstract class Shape with an abstract method area() and a concrete method describe(). Add Circle and Rectangle subclasses. Show that describe() is shared but area() is not.
Flying club: Create an interface Flyable with fly(). Implement it in Bird, Airplane, and Drone. Put all three in a list and loop through them. Watch polymorphism happen.
Default method magic: Add a default method describeFlight() to the Flyable interface. Notice how every implementing class gets it for free โ without changing any of them.
Interface vs. inheritance: Make a Penguin that implements Swimmable but not Flyable. Then make a Duck that implements both. Explain why an interface is better than a deep inheritance tree here.
Contract design: Design a tiny interface (2โ3 methods) for a real scenario: Playable, Rechargeable, or Searchable. Implement it in two classes. Keep it small!
By the end of this module, you'll be able to: explain abstraction, choose between abstract classes and interfaces, design small focused contracts, use default and static interface methods, and swap implementations polymorphically like a pro.