✏️ Explanatory Question
| Feature | Abstract Class | Interface |
|---|---|---|
| Definition | A class declared with abstract keyword. |
A contract defined with interface keyword. |
| Implementation | Can have both abstract methods (no body) and concrete methods (with body). | Can only declare method signatures (no method body). |
| Variables | Can have fields (member variables). | Cannot have fields, only methods. |
| Access Modifiers | Methods can have different modifiers (public, protected, private). |
All methods are implicitly public. |
| Inheritance | A class can inherit (extend) only one abstract class. | A class can implement multiple interfaces. |
| Constructors | Abstract classes can have constructors. | Interfaces cannot have constructors. |
| Use Case | Use when you want to provide base functionality + enforce certain methods. | Use when you want to enforce a contract only, with no default implementation. |
This means:
Provide base functionality → You can write some methods with actual logic inside the abstract class. All child classes will inherit this logic (so you don’t repeat code everywhere).
Enforce certain methods → You can also declare abstract methods (no logic, just a declaration). All child classes must implement these methods in their own way.
When we say “contract”, it means:
👉 You are forcing any class that implements the interface to provide those methods.
👉 But the interface itself does not contain the logic (it only declares the methods).