Interface vs Abstract Class in Java
Compare interface vs abstract class in Java: inheritance, state, constructors, Java 8 default methods, when to use each, code examples and interview questions.
Expected Interview Answer
An interface is a pure contract of method signatures that a class promises to implement, while an abstract class is a partially built base class that can mix abstract methods with concrete methods and state.
A class can implement many interfaces but extend only one abstract class, so interfaces model capabilities across unrelated types while abstract classes model an 'is-a' hierarchy sharing code and fields. Interface members are implicitly public and its variables are public static final constants; abstract classes allow instance fields, constructors, and any access modifier. Since Java 8 interfaces can carry default and static methods, and since Java 9 private methods, narrowing but not erasing the difference.
- Interfaces enable multiple inheritance of type
- Abstract classes share common code and state
- Interfaces decouple capability from hierarchy
- Abstract classes provide partial implementation and constructors
- Choosing correctly keeps designs flexible and DRY
AI Mentor Explanation
An interface is like the laws of cricket every player agrees to follow — a contract of what must be done — while an abstract class is like a coaching academy that already teaches shared drills but leaves each player to develop their own signature shot. One is a pure agreement; the other is a partly built foundation you extend.
Step-by-Step Explanation
Step 1
Check the relationship
Use an abstract class for a true is-a hierarchy; use an interface for a capability shared across unrelated types.
Step 2
Decide on shared code
If subclasses need common implemented methods or fields, an abstract class fits; a contract-only need suits an interface.
Step 3
Consider multiple inheritance
A class extends one abstract class but can implement many interfaces, so interfaces allow mixing capabilities.
Step 4
Account for state and constructors
Abstract classes can hold instance state and constructors; interfaces cannot hold instance fields.
Step 5
Leverage default methods carefully
Since Java 8, interfaces can add default methods for evolution without breaking implementers.
What Interviewer Expects
- Single vs multiple inheritance distinction
- Awareness of state, constructors, and access modifiers differences
- Knowledge of Java 8+ default and static interface methods
- When to pick each in real design
- A correct is-a vs can-do framing
Common Mistakes
- Saying interfaces can never have method bodies (false since Java 8 default methods)
- Claiming a class can extend multiple abstract classes
- Forgetting interface fields are public static final constants
- Assuming interfaces can declare instance variables
Best Answer (HR Friendly)
“An interface is a promise about what a class can do, and a class can sign many such promises. An abstract class is a half-finished parent class that shares real code and data with its children, and a class can only have one parent, so you pick based on whether you need a shared foundation or a shared capability.”
Code Example
interface Drivable {
void drive(); // contract only
default void honk() { System.out.println("Beep"); } // Java 8+
}
abstract class Vehicle {
private final String name; // state allowed
Vehicle(String name) { this.name = name; } // constructor allowed
abstract void start();
String name() { return name; } // shared concrete method
}
class Car extends Vehicle implements Drivable {
Car(String name) { super(name); }
@Override void start() { System.out.println(name() + " started"); }
@Override public void drive() { System.out.println(name() + " driving"); }
}
public class Demo {
public static void main(String[] args) {
Car c = new Car("Sedan");
c.start();
c.drive();
c.honk();
}
}Follow-up Questions
- Can a Java class extend a class and implement an interface at the same time?
- What changed for interfaces in Java 8 and Java 9?
- Why does Java forbid multiple class inheritance but allow multiple interfaces?
- Can an abstract class implement an interface without defining all its methods?
- How do default methods cause the diamond problem and how is it resolved?
MCQ Practice
1. How many abstract classes can a Java class extend directly?
Java supports single class inheritance, so a class may extend only one abstract (or concrete) class.
2. Since Java 8, an interface can contain which kind of method with a body?
Java 8 added default (and static) methods, letting interfaces carry method bodies while staying contracts.
3. Variables declared in a Java interface are implicitly what?
Interface fields are implicitly public, static, and final — that is, constants shared by all implementers.
Flash Cards
Interface vs abstract class in one line? — Interface is a contract (can-do); abstract class is a partly built parent (is-a with shared code).
How many of each can a class have? — Many interfaces, but only one (abstract) superclass.
Can interfaces hold state? — No instance fields; only public static final constants (plus default/static methods since Java 8).
When choose an abstract class? — When subclasses share code, state, or constructors within a real is-a hierarchy.