01 / 20
?

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.

Java

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.

public interface Drawable { void draw(); void resize(double factor); }

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.

public abstract class Shape { String color; abstract double area(); void describe() { System.out.println("A " + color + " shape"); } }

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.

Interface
Defines a capability. A class can implement many interfaces. No shared state.
Abstract Class
Defines a family. A class can extend only one. Shared fields and methods.

The Core Rules at a Glance

Interface ✓ Multiple inheritance ✓ Abstract methods ✓ Default methods (Java 8+) ✓ Static methods ✕ Instance fields ✕ Constructors ✓ Constants only implements keyword Methods public by default Abstract Class ✓ Single inheritance only ✓ Abstract methods ✓ Concrete methods ✓ Static methods ✓ Instance fields ✓ Constructors ✓ Any access modifier extends keyword Mixed visibility allowed

Single vs. Multiple Inheritance

EXTENDS (single) Animal Dog IMPLEMENTS (multiple) Drawable Resizable Circle BOTH TOGETHER Shape Colorable Rectangle

Contract vs. Blueprint

Interface = Contract
Specifies what behaviors a class must support. No shared state, no construction logic. Perfect when unrelated classes share a capability.
Abstract Class = Blueprint
Provides partial implementation with shared fields and methods. Subclasses complete the design. Perfect for a family of related classes.

How to Decide: Step by Step

1
Do unrelated classes need this behavior? If yes, use an interface. A Dog and a Printer can both be Serializable.
2
Do the classes share common fields or helper methods? If yes, lean toward an abstract class to avoid duplicating code.
3
Will a class need multiple "types" of behavior? Java allows only one superclass but many interfaces, so use interfaces for cross-cutting capabilities.
4
Is construction logic needed? Only abstract classes can have constructors. If initialization matters, choose an abstract class.
5
Combine both: extend an abstract class for shared identity AND implement interfaces for extra capabilities.

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.

// Using generics for type safety, like Java's Comparable<T> public interface Sortable<T> { int compareTo(T other); } public class Student implements Sortable<Student> { double gpa; @Override public int compareTo(Student other) { return Double.compare(this.gpa, other.gpa); } } public class Song implements Sortable<Song> { int durationSecs; @Override public int compareTo(Song other) { // Use Integer.compare to avoid overflow bugs return Integer.compare(this.durationSecs, other.durationSecs); } }

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.

public abstract class Vehicle { String engineType; int year; public Vehicle(String engine, int y) { this.engineType = engine; this.year = y; } abstract void start(); void describe() { System.out.println(year + " " + engineType + " vehicle"); } } public class Car extends Vehicle { public Car(int y) { super("Gasoline", y); } void start() { /* turn key */ } }

Combining Both Together

Animal (abstract) Swimmable Trainable Dog Duck Fish extends Animal implements Trainable extends Animal implements Swimmable extends Animal implements Swimmable extends (solid) implements (dashed)

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.

public interface Logger { void log(String msg); default void warn(String msg) { log("WARNING: " + msg); } }
✓ Can
Provide shared logic via default and static methods.
✕ Cannot
Store instance fields, define constructors, or use non-public access.

Decision Flowchart

Need shared instance state? Yes No Need constructors? Yes Abstract Class Multiple classes need this behavior? Yes Interface Often: Use BOTH extend + implement

Common Mistakes

✕ Using Abstract Class for a Capability
Making unrelated classes extend the same abstract class just to share one method. Use an interface instead: a Robot and a Car can both be Drivable.
✕ Forgetting Single Inheritance Limit
Trying to extend two abstract classes at once. Java forbids this. Refactor one into an interface to avoid the conflict.
✕ Putting State in an Interface
Attempting to declare instance fields in an interface. Interface fields are implicitly public static final (constants), not mutable state.

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?