Abstraction vs Interfaces

πŸ” What is Abstraction?

Abstraction is the process of hiding implementation details and showing only essential features of an object. It helps in managing complexity and enables focus on what an object does rather than how it does it.

In Java, abstraction is achieved in two ways:

  • Abstract classes

  • Interfaces

πŸ”Έ Abstract Class

An abstract class is a class that cannot be instantiated and may contain abstract methods (methods without implementation), as well as concrete methods (with implementation), fields, and constructors.

βœ… Example:

abstract class Animal {
    String name;

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

    abstract void makeSound();

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

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

    void makeSound() {
        System.out.println("Woof!");
    }
}

πŸ”— What is an Interface?

An interface is a blueprint for a class. It contains abstract methods (implicitly public and abstract), and from Java 8 onwards, it can also include default and static methods.

Interfaces represent capabilities or contracts. A class can implement multiple interfaces β€” which helps achieve multiple inheritance of type.

βœ… Example:

interface Flyable {
    void fly();
}

interface Swimmable {
    void swim();
}

class Duck implements Flyable, Swimmable {
    public void fly() {
        System.out.println("Duck is flying");
    }

    public void swim() {
        System.out.println("Duck is swimming");
    }
}

πŸ†š Differences Between Abstract Class and Interface

Feature
Abstract Class
Interface

Inheritance

Single

Multiple (a class can implement many)

Method Type

Can have abstract and concrete methods

Only abstract (until Java 7), plus default & static from Java 8

Fields

Can have instance variables

Only constants (public static final)

Constructors

Can have constructors

Cannot have constructors

Access Modifiers

Can use any access modifier

All methods are implicitly public

Use Case

Used for β€œis-a” relationships (shared base class)

Used for β€œcan-do” relationships (capabilities)


🎯 When to Use What?

Scenario
Recommendation

Need shared code across related classes

Use abstract class

Need to define a contract across unrelated classes

Use interface

Need to extend only one base class

Use abstract class

Need to add capabilities to many classes

Use interface

Want to ensure backward compatibility while evolving APIs

Use interface with default methods (Java 8+)


πŸ†• Java 8 and Beyond β€” What Changed?

Java 8 introduced major enhancements to interfaces:

βœ… Default Methods

You can now provide a default implementation for methods in interfaces.

interface Logger {
    default void log(String message) {
        System.out.println("Log: " + message);
    }
}

This allows you to add new methods to interfaces without breaking existing implementations.


βœ… Static Methods in Interfaces

You can define static utility methods directly in the interface.

interface MathUtil {
    static int square(int x) {
        return x * x;
    }
}

Usage:

int result = MathUtil.square(5);

βœ… Private Methods (Java 9+)

Interfaces can now have private methods to avoid code duplication between default and static methods.

interface Helper {
    private static void printCommon() {
        System.out.println("Shared logic");
    }

    static void show() {
        printCommon();
        System.out.println("From show()");
    }
}

βœ… Final Thoughts β€” How to Choose?

Ask yourself:

  • Do I need to share code between multiple closely related classes? β†’ Use abstract class.

  • Do I want to define a set of behaviors that can be applied to any class, regardless of where it sits in the hierarchy? β†’ Use interface.

  • Do I need multiple inheritance? β†’ Only possible with interfaces.

  • Am I evolving a public API? β†’ Prefer interfaces with default methods for backward compatibility.


πŸ’¬ Conclusion

Both abstract classes and interfaces are tools for abstraction in Java. With Java 8 and above, interfaces have become more powerful and flexible β€” blurring the lines between them. Still, each has its place in clean, maintainable software design.

Remember:

Use interfaces to define what an object can do, and abstract classes to define what an object is.

Last updated