Fundamentals 6 min read

Differences Between Abstract Classes and Interfaces in Java and When to Use Each

This article explains the characteristics of abstract classes and interfaces in Java, compares them across multiple dimensions, provides code examples, and offers guidance on choosing the appropriate construct based on default implementations, inheritance needs, and future changes.

Java Captain
Java Captain
Java Captain
Differences Between Abstract Classes and Interfaces in Java and When to Use Each

Many interview questions ask about the differences between abstract classes and interfaces and when to use each; this article discusses those topics in detail.

Abstract Class

An abstract class captures common features of subclasses, cannot be instantiated, and serves as a template for inheritance hierarchies. For example, the JDK class

public abstract class GenericServlet implements Servlet, ServletConfig, Serializable {
    // abstract method
    abstract void service(ServletRequest req, ServletResponse res);
    void init() {
        // Its implementation
    }
    // other method related to Servlet
}

provides a base for HttpServlet which implements the service method and adds doGet and doPost implementations.

Interface

An interface is a collection of abstract methods that a class must implement, acting like a contract. For instance, the Externalizable interface

public interface Externalizable extends Serializable {
    void writeExternal(ObjectOutput out) throws IOException;
    void readExternal(ObjectInput in) throws IOException, ClassNotFoundException;
}

requires implementing classes to provide writeExternal and readExternal methods, as shown in the Employee class example.

Comparison

The table below summarizes key differences:

Parameter

Abstract Class

Interface

Default method implementation

Can have default implementations

Fully abstract (no implementations)

Implementation keyword

Uses

extends

Uses

implements

Constructor

Can define constructors

Cannot define constructors

Access modifiers

Methods can be public, protected, or default

Methods are implicitly public

Multiple inheritance

Can extend one class and implement multiple interfaces

Can extend multiple interfaces

Performance

Slightly faster

Slightly slower due to method dispatch

Adding new methods

Can provide default implementations without breaking existing code

Requires all implementing classes to be updated

When to Use

If you need some methods with default implementations, choose an abstract class.

If you need multiple inheritance of type, use interfaces (Java does not support multiple class inheritance).

If core functionality changes frequently, an abstract class may be preferable; adding methods to an interface forces changes in all implementers.

Java 8 Default and Static Methods

Since Java 8, interfaces can contain default and static methods, reducing the gap between abstract classes and interfaces by allowing default implementations directly in interfaces.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Backend Developmentprogramming fundamentalsOOP
Java Captain
Written by

Java Captain

Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.