1. Home
  2. Interview Questions
  3. Java
  4. How do abstract classes differ from interfaces in Java?

Try answering first

In a real interview nobody shows you the answer. Think it through (or type it), then reveal the answers and rate yourself.

1 Answer

0 0
InterviewQuestions Team ✔ Expert answer ✔ Best answer ·

Short answer: Abstract classes can have both abstract and concrete methods, while interfaces can only declare methods without implementing them.

Detailed explanation

In Java, both abstract classes and interfaces are tools for abstraction, but they serve different purposes and have distinct characteristics. An abstract class is a class that cannot be instantiated on its own and may contain both abstract methods (without implementation) and concrete methods (with implementation). This flexibility allows an abstract class to provide default behavior while still declaring methods that the subclasses must implement.

On the other hand, an interface is a reference type in Java that can only contain abstract method declarations and static final variables (constants). From Java 8 onwards, interfaces can also include default and static methods with implementations, but they still do not maintain state like classes do. Interfaces are primarily used to define a contract for what methods a class must implement, promoting polymorphism.

The choice between using an abstract class versus an interface typically depends on the design needs. When there is a shared behavior among a group of related classes, using an abstract class is suitable. But if a class is expected to have varied implementations across different hierarchies, interfaces are preferred for their flexibility.

Example

abstract class Shape { abstract void draw(); void paint() { System.out.println("Painting shape"); } } interface Drawable { void draw(); }

Common mistakes

  • Using an abstract class when an interface would be more appropriate and vice versa.
  • Not realizing that an abstract class can have state (instance variables), while interfaces cannot.
  • Overusing interfaces by defining abstract methods that can be encapsulated within a common base class.

Follow-up questions

  • Can an abstract class implement an interface?
  • What are default methods in interfaces and how do they function?
  • Is it possible to extend multiple abstract classes in Java?

Interview tip: Clarify when to use abstract classes versus interfaces based on the design requirements of your application.

Reviewed by the Interview Questions editorial team · Last updated

How well did you know this?

Rate yourself to track progress, build your streak and get “didn't know” questions in your revision list.

Your Answer

More hard Java questions

All hard questions →

Related Questions

‹
Q9 of 18 · Java
Next ›

Interview Questions

Free interview questions and answers, MCQ practice, online tests, mock interviews and group discussions — everything you need to crack your next interview.
📍 Dasardot LLC
1309 Coffeen Avenue STE 1200
Sheridan, Wyoming 82801
USA