AccentureObject-Oriented ProgrammingMedium

What is the difference between abstract class and interface in Java?

JavaOOPAbstract ClassInterface

Question

What is the difference between abstract class and interface in Java? Explain with examples.

Interview Answer

Use an abstract class when you want shared state/behavior and a common base implementation. Use an interface when you want to define a contract that multiple unrelated classes can implement.

Explanation

Abstract classes allow constructors, state, and concrete methods, while interfaces are primarily contracts and support multiple implementation inheritance. In Java 8+, interfaces can also have default and static methods, but they still should model capabilities rather than shared object state.

Key Points

  • Abstract class: shared base behavior + state
  • Interface: capability contract across multiple classes
  • A class can implement multiple interfaces but extend only one class
  • Prefer interface-first design for extensibility

Common Mistakes

  • Assuming interfaces cannot contain any implementation in modern Java
  • Using abstract class when only behavior contract is needed
  • Confusing method overloading with interface implementation details

Likely Follow-Up Questions

  • When would you choose composition over inheritance?
  • Can abstract classes and interfaces be used together?
  • How do default methods change interface design?

Interview Timing

Target speaking time: about 3 minutes.

Related Questions