Interface vs.
Abstract Class
Understand when to use an interface and when to reach for an abstract class in Java: the conceptual distinctions that guide real design decisions.
What Is an Interface?
An interface is a contract: it declares what a class can do without specifying how. Any class that implements an interface promises to provide concrete behavior for every method the interface defines.
What Is an Abstract Class?
An abstract class is a partially complete blueprint. It can contain both abstract methods (no body) and concrete methods (with a body). A subclass extends it and fills in the blanks.
Why Does This Distinction Matter?
Choosing the wrong abstraction leads to rigid designs that are hard to extend. Java enforces different rules for each, so the choice affects everything from inheritance to state management.
The Core Rules at a Glance
Single vs. Multiple Inheritance
Contract vs. Blueprint
How to Decide: Step by Step
Example: Interface in Practice
A Sortable interface lets completely unrelated classes be sorted by a common algorithm: students by GPA, files by size, songs by duration.
Example: Abstract Class in Practice
Every Vehicle has an engine type and can start, but each subclass starts differently. The abstract class captures the shared identity and state.
Combining Both Together
Default Methods in Interfaces
Since Java 8, interfaces can include default methods with a body. This blurs the line slightly, but the conceptual distinction remains: interfaces still cannot hold instance state.
Decision Flowchart
Common Mistakes
Summary Comparison
| Feature | Interface | Abstract Class |
|---|---|---|
| Inheritance | Multiple | Single |
| Instance Fields | ✕ | ✓ |
| Constructors | ✕ | ✓ |
| Concrete Methods | default / static only | ✓ any |
| Access Modifiers | public, private (Java 9+) | Any (public, protected, etc.) |
| Keyword | implements | extends |
| Best For | "Can do" capabilities | "Is a" family hierarchy |
Question 1 of 5
A Dog, a Robot, and a Submarine all need a swim() method. What is the best abstraction?
Question 2 of 5
Which of the following is true about Java interfaces?
Question 3 of 5
You are designing a hierarchy where Cat, Dog, and Parrot all share a name field and a feed() method, but each has a unique speak() method. What should you use?
Question 4 of 5
Why can't a Java class extend two abstract classes simultaneously?
Question 5 of 5
A MusicPlayer class extends ElectronicDevice (abstract) and needs to be both Bluetooth-enabled and WiFi-enabled. How should you model the wireless capabilities?