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

Abstract class versus interface in Java: state, constructors, multiple inheritance, default and static methods since Java 8, and when to choose each.

0 0
Read Time:1 Minute, 45 Second

Both let you define a type without a full implementation. The difference is what each can hold and how many a class can use.

Side by side

Abstract class            Interface
Instance fields (state)  Yes                       No (only public static final constants)
Constructors             Yes                       No
Method bodies            Yes, any                  default and static (Java 8+), private (Java 9+)
Access modifiers         Any                       Methods are public (except private helpers)
A class can use          Only one (extends)        Many (implements)
Represents               "is-a" with shared code   A capability or contract ("can do")

Example

public interface Notifier {            // a capability
    void send(String to, String msg);
    default void sendAll(List<String> to, String msg) { to.forEach(t -> send(t, msg)); }
}

public abstract class BaseReport {     // shared state and steps
    protected final LocalDate from, to;
    protected BaseReport(LocalDate from, LocalDate to) { this.from = from; this.to = to; }
    public final byte[] generate() { return render(fetchRows()); }   // template method
    protected abstract List<Row> fetchRows();
    protected abstract byte[] render(List<Row> rows);
}

When to use which

  • Interface when unrelated classes should share a contract (EmailNotifier and SmsNotifier), or when a class needs several types at once.
  • Abstract class when related classes share state and part of the algorithm, as in the template-method report above.
  • A common design uses both: an interface for the contract, and an abstract base class that implements part of it.

Java 8 and later

Default methods let interfaces add behaviour without breaking existing implementations; that is how List.forEach and Collection.stream() were added. If two interfaces give the same default method, the class must override it and can call one with A.super.method(). Interfaces still cannot hold instance state, which is the line that remains.

Follow-ups

  • Can an abstract class have no abstract methods? (Yes, to stop direct instantiation.)
  • What is a functional interface?
  • Can an interface extend a class?

This answer was written by Debugger’s Space as practice material. It is asked in most Indian IT services interviews (TCS, Infosys, Wipro, HCLTech, Tech Mahindra, LTIMindtree, Accenture, Cognizant, Capgemini, IBM). See each company’s hiring steps and practice questions on Debugger’s Space.

Previous post Explain the four pillars of OOP with a real example from your project.
Next post What is the difference between checked and unchecked exceptions?
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Close
0
Would love your thoughts, please comment.x
()
x