beginnerOOP Recap & UML

What is the difference between an abstract class and an interface in Java, and when would you choose one over the other?

An abstract class can hold shared state (fields) and partially implemented behavior, and is meant for classes that are genuinely related and share a common identity, like Card being the shared parent of CreditCard and DebitCard. An interface holds no state at all — only method signatures every implementer must fulfill — and is meant for a capability that unrelated classes can all satisfy, like PaymentMethod being implemented by both a Card subclass and an unrelated UPI class. A class can extend only one abstract class but can implement any number of interfaces, so interfaces are also the right tool when a class needs to satisfy multiple independent contracts at once. As a rule of thumb: reach for an abstract class when subclasses share real data and some default logic, and reach for an interface when the only thing shared is a promise about behavior.

Ready to master this question?

Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.

Sign in to generate a response

Next Step

Continue to Why must a shared flyweight object be immutable once it has been constructed?← Back to all Low-Level Design & Design Patterns questions