Junior
What is the difference between an interface and an abstract class?
sobes.tech AI
Answer from AI
An interface defines a contract for classes, specifying a set of methods that must be implemented. It can contain only method signatures (before Java 8) and constants. Multiple inheritance of interfaces is allowed.
An abstract class can contain abstract and concrete methods, fields, and constructors. It cannot be instantiated directly. It can have only one direct parent (restriction on multiple inheritance of classes).
Main differences:
| Characteristic | Interface | Abstract class |
|---|---|---|
| Multiple inheritance | Yes (for implementation) | No (for extension) |
| Methods | Only abstract (before Java 8), can be default and static (from Java 8) | Abstract and concrete |
| Fields | Static and final constants | Any (static, final, primitive, reference) |
| Constructors | No | Yes |
| Implementation | Implemented (implements) |
Extended (extends) |