0
0
JavaComparisonBeginner · 4 min read

Abstract Class vs Interface in Java: Key Differences and Usage

Use a abstract class in Java when you want to share common code among related classes and provide some method implementations. Use an interface when you want to define a contract for unrelated classes to implement, focusing only on method signatures without code sharing.
⚖️

Quick Comparison

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

FactorAbstract ClassInterface
PurposeShare common code and define base behaviorDefine a contract with method signatures
Method ImplementationCan have both abstract and concrete methodsBefore Java 8: only abstract methods; Java 8+: default and static methods allowed
Multiple InheritanceNot allowed (only single inheritance)Allowed (a class can implement multiple interfaces)
FieldsCan have instance variables with any access modifierOnly static final constants
ConstructorCan have constructorsCannot have constructors
Use CaseWhen classes are closely relatedWhen unrelated classes need to follow the same contract
⚖️

Key Differences

An abstract class in Java is like a partially built blueprint. It can have both methods with code and methods without code (abstract methods). This allows you to share common behavior among related classes while forcing subclasses to implement specific methods. It can also have instance variables and constructors.

On the other hand, an interface is a pure contract that specifies what methods a class must have, without dictating how they work. Before Java 8, interfaces could only declare abstract methods, but now they can also have default and static methods with implementations. Interfaces cannot have instance variables or constructors.

Another big difference is inheritance: a class can extend only one abstract class but can implement many interfaces. This makes interfaces very flexible for defining capabilities that many unrelated classes can share.

⚖️

Code Comparison

java
abstract class Animal {
    String name;

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

    void eat() {
        System.out.println(name + " is eating.");
    }

    abstract void sound();
}

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

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

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

Interface Equivalent

java
interface Animal {
    void eat();
    void sound();
}

class Dog implements Animal {
    String name;

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

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

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

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

When to Use Which

Choose an abstract class when:

  • You have closely related classes that share common code or state.
  • You want to provide some default behavior and force subclasses to implement others.
  • You need to define non-static or non-final fields.

Choose an interface when:

  • You want to define a contract for unrelated classes to implement.
  • You need to support multiple inheritance of type.
  • You want to specify capabilities or behaviors without enforcing code sharing.

In short, use abstract classes for code reuse in a class family, and interfaces for flexible, multiple-type contracts.

Key Takeaways

Use abstract classes to share code and state among related classes.
Use interfaces to define contracts for unrelated classes with no code sharing.
A class can extend one abstract class but implement multiple interfaces.
Abstract classes can have constructors and instance variables; interfaces cannot.
Choose based on whether you need code reuse (abstract class) or multiple type inheritance (interface).