🪞 Abstraction in Java — Abstract Classes vs Interfaces
Master abstraction in Java — abstract classes vs interfaces, default and static methods, functional interfaces, and when to use which. Updated for modern Java with examples.
Abstraction means exposing what something does while hiding how. Java offers two tools: abstract classes and interfaces.
🧱 Abstract classes
A class marked abstract can't be instantiated and may contain abstract methods (no body) plus concrete methods and state.
abstract class Shape {
abstract double area(); // must be implemented
void describe() { System.out.println("area=" + area()); }
}🔌 Interfaces
An interface is a pure contract. A class implements it. Since Java 8, interfaces can have default and static methods with bodies:
interface Drawable {
void draw(); // abstract
default void clear() { System.out.println("clearing"); } // default
static Drawable empty() { return () -> {}; } // static
}⚖️ Abstract class vs interface
| Abstract class | Interface | |
|---|---|---|
| State (fields) | Yes | Constants only |
| Constructors | Yes | No |
| Multiple inheritance | No (one superclass) | Yes (implement many) |
| Use when | Shared base + state | Capability/contract |
💡 Rule of thumb
Use an interface to define a capability multiple unrelated classes can have (Comparable, Runnable). Use an abstract class when subtypes share state and a partial implementation. A single-method interface is a functional interface — the basis of lambdas, covered later.
💻 Code Examples
Abstract class with template method
abstract class Payment {
abstract void charge(double amt);
void process(double amt) {
System.out.println("validating...");
charge(amt);
}
}
class Card extends Payment {
void charge(double amt){ System.out.println("charged $" + amt); }
}
new Card().process(50);validating... charged $50.0
Implementing multiple interfaces
interface Flyer { void fly(); }
interface Swimmer { void swim(); }
class Duck implements Flyer, Swimmer {
public void fly() { System.out.println("flap"); }
public void swim() { System.out.println("paddle"); }
}
Duck d = new Duck(); d.fly(); d.swim();flap paddle
Default method
interface Greeter {
String name();
default void greet() { System.out.println("Hi " + name()); }
}
Greeter g = () -> "Sam";
g.greet();Hi Sam
⚠️ Common Mistakes
- Trying to instantiate an abstract class — `new Shape()` is a compile error.
- Forgetting interface methods are public — implementing methods must be public.
- Adding state to an interface — only public static final constants are allowed.
- Reaching for an abstract class when a simple interface (capability) would be cleaner.
🎯 Interview Questions
Real questions asked at top product and service-based companies.
Q1.What is the difference between an abstract class and an interface?Beginner
Q2.When would you use an abstract class over an interface?Intermediate
Q3.What are default methods and why were they added?Intermediate
Q4.Can a class implement two interfaces with the same default method?Advanced
Q5.What is a functional interface?Intermediate
🧠 Quick Summary
- Abstraction exposes what, hides how.
- Abstract class: state + constructors + partial implementation, single inheritance.
- Interface: contract, multiple implementation, default/static methods (Java 8+).
- Interface for capabilities; abstract class for shared base + state.
- A single-abstract-method interface is a functional interface (lambda-ready).