📑

On this page — Quick navigation

👇 Click any topic to scroll directly

🔹Interface abstract class difference

1. Basic Concept

  • Abstract Class
    • A class that cannot be instantiated and may contain both abstract (no body) and concrete (with body) methods.
  • Interface
    • A completely abstract contract (before Java 8) that defines what a class must do, not how.

🔹 2. Methods

  • Abstract Class
    • Can have:
      • Abstract methods
      • Concrete methods
  • Interface
    • Traditionally only abstract methods
    • Since Java 8:
      • default methods (with body)
      • static methods
    • Since Java 9:
      • private methods (for internal use)

🔹 3. Variables

  • Abstract Class
    • Can have instance variables (fields)
  • Interface
    • Variables are:
      • public
      • static
      • final (constants only)

🔹 4. Constructors

  • Abstract Class
    • Can have constructors
  • Interface
    • Cannot have constructors

🔹 5. Inheritance

  • Abstract Class
    • A class can extend only one abstract (or any) class
      → supports single inheritance
  • Interface
    • A class can implement multiple interfaces
      → supports multiple inheritance

🔹 6. Access Modifiers

  • Abstract Class
    • Methods and variables can have any access modifier (private, protected, public)
  • Interface
    • Methods are implicitly public (unless private helper methods)
    • Fields are always public static final

🔹 7. When to Use

  • Abstract Class
    • Use when:
      • You want to share code (implementation) among related classes
      • There is an “is-a” relationship (e.g., Animal → Dog)
  • Interface
    • Use when:
      • You want to define a contract
      • You need multiple inheritance
      • Classes are unrelated but share behavior (e.g., Flyable, Runnable)

🔹 8. Example

Abstract Class

abstract class Animal {
    abstract void sound();

    void sleep() {
        System.out.println("Sleeping...");
    }
}

Interface

interface Animal {
    void sound();  // implicitly public and abstract
}

🔹Difference between Abstract Class and Interface

The abstract class achieves partial abstraction (0 to 100%), whereas the interface achieves full abstraction (100%). The following table shows the key differences between difference between abstract class and interface:

Abstract Class Interface
Abstract classes can have abstract and non-abstract methods. An interface can have only abstract methods. Since Java 8, it can have default and static methods also.
Abstract class does not support multiple inheritance. Interface supports multiple inheritance.
Abstract class can have final, non-final, static and non-static variables. An interface has only static and final variables.
Abstract class can provide the implementation of the interface. An interface cannot provide the implementation of an abstract class.
The abstract keyword is used to declare an abstract class. The interface keyword is used to declare an interface.
An abstract class can extend another Java class and implement multiple Java interfaces. An interface can extend another Java interface only.
An abstract class can be extended using the keyword “extends”. An interface can be implemented using the keyword “implements”.
A Java abstract class can have class members like private, protected, etc. Members of a Java interface are public by default.
Example:
public abstract class Shape{
public abstract void draw();
}
Example:
public interface Drawable{
void draw();
}