Java.Core.What is the difference between an interface and an abstract class?

This is another core Java interview favorite, so let me break down the differences between an interface and an abstract class in a way that’s easy to remember — and with some practical tips on when to use which.


💥 Interface vs Abstract Class — The Key Differences

FeatureInterfaceAbstract Class
PurposeDefines a contract (what a class must do)Provides a partial implementation (how something works)
MethodsOnly abstract methods (until Java 8+) — but can have default & static methods nowCan have both abstract and concrete methods
FieldsOnly public static final constants (no normal fields)Can have instance fields (normal class variables)
InheritanceA class can implement multiple interfaces (multiple inheritance of type)A class can extend only one abstract class (single inheritance)
ConstructorNo constructor (interfaces can’t be instantiated)Can have a constructor (runs when subclass is created)
Access ModifiersMethods are public by defaultMethods can have any access modifier (public, protected, package-private, private)
Type of RelationshipDefines what a class can do (capability)Defines what a class is (inheritance)
Speed EvolutionHarder to evolve (changing an interface breaks all implementations)Easier to evolve (you can add new non-abstract methods without breaking subclasses)

📚 Example — Interface





public interface Animal {
    void makeSound();   // By default: public and abstract
}

📚 Example — Abstract Class

public abstract class Animal {
    private String name;  // Can have fields

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

    abstract void makeSound();  // Must be implemented by subclasses

    void eat() {                 // Concrete method (already implemented)
        System.out.println(name + " is eating");
    }
}

🔗 Key Rule — Inheritance vs Capability

ConceptMeaning
Abstract Class“This is a base class for other classes to inherit from” (is-a relationship)
Interface“This defines a capability that any class can have” (can-do relationship)

⚡ Quick Example — Flying Animal

Abstract Class (Base type: Animal)





abstract class Animal {
    abstract void eat();
}

Interface (Extra capability: Flyable)

interface Flyable {
    void fly();
}

A bird can inherit Animal and implement Flyable:

class Bird extends Animal implements Flyable {
    @Override
    void eat() {
        System.out.println("Bird is eating");
    }

    @Override
    public void fly() {
        System.out.println("Bird is flying");
    }
}

✅ This shows:

  • extends for abstract class.
  • implements for interface.

🔥 Modern Twist (Java 8+)

Java 8+ added:

  • default methods in interfaces (interfaces can now have concrete methods).
  • static methods in interfaces.
interface Animal {
    void makeSound();

    default void breathe() {  // Concrete method in interface
        System.out.println("Breathing...");
    }
}

⚠️ This makes interfaces look a bit like abstract classes, but interfaces still cannot have fields (except constants) or constructors.


🏎️ When to Use What?

Use CaseUse
Shared behavior across related classesAbstract Class
Unrelated classes need the same ability (like Serializable, Comparable)Interface
You need multiple inheritance of typesInterface
You need to define a base class with fields and helper methodsAbstract Class

🔥 Summary Table

FeatureInterfaceAbstract Class
FieldsOnly public static finalAny type (private, protected, etc.)
Multiple Inheritance✔️ Yes (can implement many interfaces)❌ No (can extend only one abstract class)
Concrete Methods✔️ From Java 8+ (default, static)✔️ Always allowed
Constructors❌ No✔️ Yes
Best forCapabilities (can-do)Base behavior (is-a)

🎯 Interview Pro Tip

✅ Say “interface = behavior/capability contract, abstract class = base class for inheritance.”
✅ Always mention “Java allows multiple interfaces but only one parent class.”
✅ Mention “Java 8 made interfaces more powerful, but they still can’t have fields like abstract classes.”

This entry was posted in Без рубрики. Bookmark the permalink.