0
0
JavaComparisonBeginner · 4 min read

Abstract Class vs Interface in Java: Key Differences and Usage

In Java, a abstract class can have both abstract and concrete methods and can hold state with fields, while an interface defines a contract with abstract methods and default/static methods but no instance state. Abstract classes support single inheritance, whereas interfaces support multiple inheritance of type.
⚖️

Quick Comparison

This table summarizes the main differences between abstract class and interface in Java.

FactorAbstract ClassInterface
PurposeTo share common code and define partial implementationTo define a contract for classes to implement
MethodsCan have abstract and concrete methodsAll methods are abstract by default; can have default and static methods since Java 8
FieldsCan have instance variables with any access modifierCan only have public static final constants
InheritanceSupports single inheritance (extends one class)Supports multiple inheritance (implements multiple interfaces)
ConstructorsCan have constructorsCannot have constructors
Access ModifiersMethods can have any access modifierMethods are implicitly public
⚖️

Key Differences

An abstract class in Java is like a partially built blueprint. It can have both methods with code (concrete) and methods without code (abstract). It can also hold state through instance variables. This allows subclasses to inherit common behavior and override or add specific features.

On the other hand, an interface is a pure contract that classes agree to follow. Before Java 8, interfaces could only declare abstract methods without any implementation. Since Java 8, interfaces can have default methods with code and static methods, but they cannot hold instance state. Interfaces allow a class to promise to provide certain behaviors without forcing a class hierarchy.

Another key difference is inheritance: a class can extend only one abstract class but can implement multiple interfaces. This makes interfaces useful for adding multiple capabilities to a class. Also, abstract classes can have constructors and protected members, while interfaces cannot.

⚖️

Code Comparison

java
abstract class Animal {
    String name;

    Animal(String name) {
        this.name = name;
    }

    abstract void sound();

    void sleep() {
        System.out.println(name + " is sleeping");
    }
}

class Dog extends Animal {
    Dog(String name) {
        super(name);
    }

    @Override
    void sound() {
        System.out.println(name + " says: Woof Woof");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog("Buddy");
        dog.sound();
        dog.sleep();
    }
}
Output
Buddy says: Woof Woof Buddy is sleeping
↔️

Interface Equivalent

java
interface Animal {
    void sound();

    default void sleep() {
        System.out.println("Animal is sleeping");
    }
}

class Dog implements Animal {
    private String name;

    Dog(String name) {
        this.name = name;
    }

    @Override
    public void sound() {
        System.out.println(name + " says: Woof Woof");
    }

    @Override
    public void sleep() {
        System.out.println(name + " is sleeping");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog("Buddy");
        dog.sound();
        dog.sleep();
    }
}
Output
Buddy says: Woof Woof Buddy is sleeping
🎯

When to Use Which

Choose an abstract class when: you want to share common code among closely related classes, need to maintain state, or require controlled access with protected members. It is ideal when classes share a common base and behavior.

Choose an interface when: you want to define a contract for unrelated classes to implement, need multiple inheritance of type, or want to specify capabilities without enforcing a class hierarchy. Interfaces are best for defining roles or abilities that many classes can share.

Key Takeaways

Abstract classes can have state and concrete methods; interfaces define contracts with abstract and default methods.
A class can extend only one abstract class but implement multiple interfaces.
Use abstract classes for shared base behavior; use interfaces to define capabilities across unrelated classes.
Interfaces cannot have constructors or instance fields, while abstract classes can.
Since Java 8, interfaces can have default and static methods with implementation.