Interfaces vs Abstract Classes


Interfaces vs Abstract Classes in Java
Feature Interface Abstract Class
Purpose Defines a contract for classes to follow Provides a partial implementation with optional method bodies
Keyword interface abstract
Inheritance Type Multiple inheritance is allowed Only single inheritance
Methods All methods are abstract by default (Java 7); Java 8+ supports default and static methods Can have abstract and concrete (implemented) methods
Variables public static final (constants) only Can have instance variables with any access modifier
Constructors No constructors Yes, can have constructors
Access Modifiers Methods are public by default Methods/fields can be private, protected, public, etc.
Implements vs Extends A class implements an interface A class extends an abstract class
Use Case Define a common behavior without implementation Provide shared code and enforce method implementation
Example: Interface
    
    interface Vehicle {
        void start();  // abstract method
    }

    class Car implements Vehicle {
        public void start() {
            System.out.println("Car started");
        }
    }
    
    
Example: Abstract Class
    
    abstract class Animal {
        abstract void makeSound();  // abstract method

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

    class Dog extends Animal {
        void makeSound() {
            System.out.println("Dog barks");
        }
    }
    
    
When to Use What?
Scenario Use
You need to define common behavior but no implementation Use Interface
You want to share some code and enforce method implementation Use Abstract Class
You want to achieve multiple inheritance Use Interfaces
You want to model real-world hierarchy Use Abstract Class (e.g., Animal → Dog)
Java 8+ Enhancements

Interfaces can now have:

  • default methods (with body)
  • static methods

"Interface = “Tell me what to do, I’ll decide how.”"
"Abstract Class = “Here’s how to do some of it, but you must finish the rest.”"