Loading…
Loading…
Charting your lesson…
Opening your lesson…
Abstraction
Lesson 2 of 5
Exposing only essential behavior while hiding implementation detail — modeling "what" not "how" via interfaces/abstract classes.
6 steps•about 10 min•8 flashcards
Step 1/ 61 min
You drive a car with a steering wheel and pedals, not by managing pistons. That's abstraction — and it's the OOP pillar interviewers use to test whether you can design clean interfaces that hide complexity.
Step 2/ 61 min
Abstraction means exposing only the essential features of something while hiding the complex implementation details. It models the what, not the how. In code you define a contract — the operations available — and let concrete classes fill in the details. Callers program to the abstraction and never depend on the messy internals.
Two main tools:
Either way, callers depend on the abstract type, not a specific concrete class.
Use an interface to define a capability many unrelated classes can share ("Comparable", "Serializable"), and when you need multiple such capabilities (a class can implement many interfaces). Use an abstract class when related classes share common code/state and you want to provide a partial base. Rule of thumb: interface = can-do capability; abstract class = is-a base with shared implementation.
Abstraction underpins the Dependency Inversion Principle: high-level modules depend on abstractions, not concretions. A PaymentProcessor interface lets you swap Stripe for PayPal without changing business logic; a Repository interface lets you swap a database for an in-memory fake in tests. The risk is over-abstraction — layers of interfaces with a single implementation add indirection without benefit (YAGNI). Good abstraction is chosen at the seams where implementations genuinely vary.
Step 3/ 61 min
Define a Shape abstraction with an area() operation. Circle and Rectangle implement it differently, but drawing code calls shape.area() without knowing the concrete type. Add a Triangle later and the drawing code needs zero changes — it only ever depended on the abstraction. The "how" (formula per shape) is hidden; the "what" (every shape has an area) is exposed.
Step 4/ 61 min
Introduce an abstraction where the implementation genuinely varies or may change: external services, storage, algorithms, notification channels. Depend on the abstraction so alternatives (and test doubles) plug in cleanly. Don't abstract things that will only ever have one form.
Step 5/ 61 min
Remember "Abstraction = the dashboard, not the engine." Interface = a capability contract (can-do); abstract class = a shared base (is-a).
Don't confuse abstraction with encapsulation. Encapsulation hides internal state within one class; abstraction exposes a simplified model and hides implementation across a boundary. And beware over-abstraction: an interface with exactly one implementation and no foreseeable second one is usually needless indirection.
Step 6/ 6· Worked examples1 min
Problem
A checkout flow must support Credit Card today and UPI tomorrow without rewriting checkout logic. Show how abstraction achieves this.
Try it on paper first.
Recall
Keep this
Fast revision of abstraction.
You've read 0 of 6 steps
Now lock it in — questions straight after reading are what make it stick.