Loading…
Loading…
Charting your lesson…
Opening your lesson…
Abstraction: Abstract Classes and Interfaces
Lesson 2 of 4
6 steps•about 12 min•12 flashcards
Step 1/ 61 min
Abstraction is "show what, hide how". In code it takes two forms — abstract classes and interfaces — and choosing between them is one of the most practical OOP design decisions, so interviewers ask about it constantly.
Core idea: abstraction exposes essential behaviour while hiding implementation detail. An abstract class is a partially-implemented base you extend (an "is-a" template with shared code); an interface is a pure contract of method signatures a class promises to fulfil ("can-do" capability).
Step 2/ 61 min
abstract class Shape { // abstract class: some shared code
String name;
abstract double area(); // no body: subclasses MUST implement
void describe() { System.out.println(name + " area=" + area()); } // shared
}
interface Drawable { // interface: pure contract
void draw();
}
You cannot instantiate either directly. An abstract class can hold fields and concrete methods; a (classic) interface only declares behaviour.
| Aspect | Abstract class | Interface |
|---|---|---|
| Instantiable | No | No |
| Method bodies | Yes (mix of abstract + concrete) | Signatures (Java 8+ allows default) |
| Fields/state | Yes (instance fields) | Constants only |
| Inheritance | Single (one base) | Multiple (implement many) |
| Models | "is-a" with shared code | "can-do" capability |
Why both exist: a class can extend only one abstract class but implement many interfaces — so interfaces give a form of multiple inheritance of type without the "diamond" state problems.
The Dependency Inversion Principle says: depend on abstractions, not concretions. Code against an interface and you can swap implementations freely:
interface PaymentGateway { boolean charge(int cents); }
class StripeGateway implements PaymentGateway { ... }
class Checkout {
private final PaymentGateway gw; // depends on the abstraction
Checkout(PaymentGateway gw) { this.gw = gw; } // inject any implementation
}
Now Checkout works with Stripe, PayPal, or a test mock — none of its code changes. This is the backbone of testability and plugin architectures.
Step 3/ 61 min
Suppose you model animals. All animals are a kind of Animal and share eat() logic → abstract class Animal with a concrete eat() and abstract makeSound(). But only some animals can swim or fly — capabilities cutting across the hierarchy → interfaces Swimmer, Flyer.
abstract class Animal { void eat(){...} abstract void makeSound(); }
interface Swimmer { void swim(); }
class Duck extends Animal implements Swimmer, Flyer { ... }
A Duck is an Animal (single base, shared code) and can swim and fly (multiple capabilities). Trying to force "swimmer" into the class hierarchy would fail because fish and ducks share swimming but not a common concrete ancestor.
Step 4/ 61 min
The perennial question: "abstract class vs interface — when do you use which?" Answer with: shared state/code and a strict "is-a" → abstract class; a capability usable across unrelated types, or the need to implement multiple contracts → interface. Note Java 8 default methods blurred the line but not the single-vs-multiple-inheritance rule.
Step 5/ 61 min
default/static methods — useful, but they still can't store per-object fields.Choosing an abstract class purely to "share a couple of methods" when the types are not truly related by "is-a". This forces an artificial single-inheritance hierarchy and blocks the class from extending anything else. If you only need shared behaviour contracts or cross-cutting capabilities, an interface (with default methods if needed) is the better tool.
Step 6/ 6· Worked examples2 min
Problem
Model shapes so that area() must be defined by each shape but describe() is shared, and separately let some shapes be "drawable" and "serializable" independently. Decide where an abstract class fits and where interfaces fit.
Try it on paper first.
Recall
Keep this
Abstraction is an OOP pillar and the source of a hugely common interview question: "abstract class vs interface — when do you use each?". Getting the distinction right (especially in Java) is a strong signal.
Abstraction means exposing only essential features and hiding implementation complexity — the "what", not the "how". A driver uses a steering wheel without knowing the mechanism. In code, you program to an abstract type and let concrete classes fill in details.
An abstract class can have both abstract methods (no body) and concrete methods + state (fields); a class can extend only one. An interface is a pure contract of method signatures (Java 8+ allows default/static methods); a class can implement many.
| Abstract class | Interface | |
|---|---|---|
| Instantiable | No | No |
| State (fields) | Yes | Constants only |
| Multiple inheritance | No (single) | Yes (many) |
| Use | shared base + partial impl | capability/contract |
default methods (bodies) — the old "interfaces have no implementation" rule is outdated."Abstract class vs interface", "when to choose each?", "can you instantiate an abstract class?", "abstraction vs encapsulation", and "can Java classes extend multiple classes?".
Abstraction exposes essentials, hides complexity. Abstract class: single inheritance, can hold state + partial implementation. Interface: multiple implementation, a contract (default methods since Java 8). Neither is instantiable. Interface = capability; abstract class = shared base.
You've read 0 of 6 steps
Now lock it in — questions straight after reading are what make it stick.