Fundamentals 9 min read

Understanding Abstract Classes and Interfaces in Java

This article explains the concepts, syntax, and design differences between Java abstract classes and interfaces, illustrating how each is used for abstraction, inheritance, and behavior contracts with code examples.

Java Captain
Java Captain
Java Captain
Understanding Abstract Classes and Interfaces in Java

In object‑oriented programming, abstraction is a key feature, and in Java it is expressed through abstract classes and interfaces, which have many similarities but also important differences.

Abstract classes are defined using the abstract keyword and may contain abstract methods (declared without implementation) as well as concrete methods, fields, and constructors. An abstract method is declared like abstract void fun(); . A class that contains any abstract method must itself be declared abstract and cannot be instantiated directly. Abstract classes can also exist without any abstract methods; they serve primarily as a base for inheritance.

public abstract class ClassName {
    abstract void fun();
}

Key points about abstract classes: abstract methods must be public or protected ; abstract classes cannot be instantiated; and any subclass must implement all inherited abstract methods unless it is also declared abstract.

Interfaces are declared with the interface keyword. All fields in an interface are implicitly public static final , and all methods are implicitly public abstract (they cannot have a body). A class implements an interface using the implements clause and must provide implementations for all its methods, unless the class is abstract.

public interface InterfaceName {
    // method signatures only
}
class ClassName implements Interface1, Interface2 {
    // implementations
}

Key differences between abstract classes and interfaces include: abstract classes can provide method implementations and static members, while interfaces cannot (prior to Java 8); a class can extend only one abstract class but can implement multiple interfaces; and abstract classes model a “is‑a” relationship whereas interfaces model a “can‑do” capability.

Design‑wise, abstract classes are suited for shared code and common state, while interfaces define contracts for behavior. For example, a Door abstract class might declare open() and close() , and a separate Alarm interface could declare alarm() . A concrete AlarmDoor can extend Door and implement Alarm to combine both behaviors.

interface Alarm {
    void alarm();
}
abstract class Door {
    void open();
    void close();
}
class AlarmDoor extends Door implements Alarm {
    void open() { /* ... */ }
    void close() { /* ... */ }
    void alarm() { /* ... */ }
}
JavaOOPinterface{}inheritanceAbstract Class
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

login 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.