
Abstract classes allow partial implementation, meaning they can have both defined and abstract methods. They support properties (variables) and can have constructors. However, a class can extend only one abstract class.
Interfaces, on the other hand, only declare method signatures without implementation. They do not support properties or constructors but allow multiple inheritance through implementation by multiple classes.
Key Difference: Use Abstract Class when some default functionality is needed. Use Interface when multiple inheritance and method structure enforcement are required.
Feature | Abstract Class | Interface |
---|---|---|
Object Creation | ❌ Cannot create an object | ❌ Cannot create an object |
Method Implementation | ✅ Can have method implementations | ❌ Only method declarations (no body) |
Properties (Variables) | ✅ Can have properties (variables) | ❌ Cannot have properties |
Constructor | ✅ Can have a constructor | ❌ Cannot have a constructor |
Access Modifiers | ✅ Supports public , protected , and private methods | ❌ Only public methods are allowed |
Multiple Inheritance | ❌ Cannot extend multiple classes | ✅ Can implement multiple interfaces |
Use Case | Used when some default functionality is required along with some abstract methods | Used when only method signatures are needed and multiple inheritance is required |
Short Interview-Friendly Answer:
Abstract Class: Allows partial implementation, can have properties, and supports single inheritance.
Interface: Only defines method signatures, does not allow properties, and supports multiple inheritance.
Comments (0)
No comments yet. Be the first to comment!