Difference Between Abstract Class and Interface

Naveen AnamaneniUpdated · 6 min read

"What is the difference between an abstract class and an interface?" may be the most asked Java interview question of all. Since Java 8 gave interfaces default methods, the two look more alike than they used to, so the answer has changed. The short version: an interface defines what a type can do and can be added to any class; an abstract class is a partly built class that shares state and code with a family of subclasses.

Side by Side

Abstract classInterface
Keywordabstract class, used with extendsinterface, used with implements
How many per classOne (single inheritance)Many
Instance fields (state)YesNo, only public static final constants
ConstructorsYesNo
Methods with codeYes, anydefault, static and private methods only (Java 8 and 9)
Abstract methodsOptional; marked abstractMethods without a body are implicitly public abstract
Access of membersAny: private, protected, package, publicpublic (or private helpers)
final methodsAllowedNot allowed
Relationship expressedIs-a, a family: Contractor is an EmployeeCan-do, an ability: Billable, Comparable
Adding a new method laterEasy with a body; subclasses inherit itUse a default method, or every implementer breaks

How to Choose

Start with an interface; add an abstract class when shared state appears
  • Choose an interface when unrelated classes need the same ability, when a class may need several such abilities, or when you're defining a type others will implement.
  • Choose an abstract class when closely related classes share fields, constructors or protected helper methods, or you want to lock steps with final template methods.
  • When in doubt, start with an interface. A class can implement it without giving up its only extends.

Using Both: The Skeletal Implementation

Library designers often offer both: an interface as the public type, and an abstract class that implements it partly, so writing a new implementation is quick. The JDK does this with List and AbstractList, and Map and AbstractMap.

Skeleton.java▶ Try it
public class Skeleton {
    interface PaymentMethod {
        String pay(long amount);
        int count();
    }
 
    abstract static class BasePayment implements PaymentMethod {
        private final String label;
        private int count;
 
        BasePayment(String label) { this.label = label; }
 
        public final String pay(long amount) {
            count++;
            return label + " #" + count + ": " + process(amount);
        }
 
        public int count() { return count; }
 
        protected abstract String process(long amount);
    }
 
    static class Upi extends BasePayment {
        Upi() { super("UPI"); }
        protected String process(long amount) {
            return "Rs. " + amount + " sent";
        }
    }
 
    static class Card extends BasePayment {
        Card() { super("Card"); }
        protected String process(long amount) {
            return "Rs. " + (amount + amount * 2 / 100) + " charged";
        }
    }
 
    public static void main(String[] args) {
        PaymentMethod upi = new Upi();
        PaymentMethod card = new Card();
        System.out.println(upi.pay(500));
        System.out.println(upi.pay(120));
        System.out.println(card.pay(1000));
        System.out.println("UPI payments: " + upi.count());
    }
}
Output
UPI #1: Rs. 500 sent
UPI #2: Rs. 120 sent
Card #1: Rs. 1020 charged
UPI payments: 2

How it works

  1. PaymentMethod is the type everyone uses; main never mentions BasePayment.
  2. BasePayment holds the state an interface can't: a label and a counter, set up by a constructor.
  3. Its final pay method fixes the steps; subclasses only supply process, which is protected, another thing an interface can't offer.
  4. A class that already extends something else could still implement PaymentMethod directly, without the skeleton.

Common Mistakes

Answering with the pre-Java 8 list. "Interfaces can't have method bodies" is no longer true; mention default, static and private methods.

Choosing an abstract class for an ability. abstract class Billable would stop a SchoolFee that already extends something else from being billable.

Putting state in an interface through constants. Interface fields are shared constants, not per-object data.

Interview Corner

What is the difference between an abstract class and an interface?

A class can extend only one abstract class but implement many interfaces. Abstract classes can have instance fields, constructors, methods of any access level, and final methods; interfaces have no instance state or constructors, their abstract methods are public, and since Java 8 and 9 they can have default, static and private methods. Abstract classes model an is-a family that shares state and code; interfaces model a capability that unrelated classes can share.

When would you use an abstract class instead of an interface?

When closely related classes share fields, constructor logic or non-public helper methods, or when you want a final template method that fixes an algorithm's steps. Often the best design uses both: an interface as the public type and an abstract class as a skeletal implementation, like List and AbstractList.

After Java 8, why not use interfaces for everything?

Interfaces still can't hold instance state, can't have constructors, and can't have protected or final methods. Default methods can only build on other interface methods, so behavior that needs fields still belongs in a class.

Practice

  1. Easy: For each, choose abstract class or interface and say why: Shape with a stored color; Printable; Vehicle with a registration number; Comparable.
  2. Harder: Add a NetBanking class to Skeleton.java with a flat fee of Rs. 10, then add a Wallet class that implements PaymentMethod directly without BasePayment.

Quick check: Abstract class vs interface

4 questions
1Which can have instance fields?
2A class can implement how many interfaces and extend how many abstract classes?
3Which can have a constructor?
4Unrelated classes need a common ability. Which fits best?

Key Takeaways

Was this tutorial helpful?

Lesson 139 of 482