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
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?
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