Difference between abstract class and interface
๐ก Concept: Abstract Class vs Interface in Java
Abstract classes and interfaces define contracts and partial implementations but have distinct purposes and capabilities.
๐ Quick Intro
Abstract classes can have method implementations and state, while interfaces define method signatures (and default methods in Java 8+).
๐ง Analogy
Think of an abstract class as a partially built car frame, while an interface is the contract that guarantees the car has wheels, brakes, and lights.
๐ง Technical Explanation
- Abstract class: Can have fields, constructors, concrete and abstract methods.
- Interface: Only method signatures and static/default methods (Java 8+).
- Classes can extend one abstract class but implement multiple interfaces.
- Abstract classes support access modifiers; interfaces are implicitly public.
- Abstract classes used for shared code; interfaces define capabilities.
๐ฏ Use Cases
- โ Use abstract classes to share code among closely related classes.
- โ Use interfaces to define capabilities for unrelated classes.
- โ Use interfaces to achieve multiple inheritance.
๐ป Java Code Example
abstract class Vehicle {
abstract void move();
void start() {
System.out.println("Vehicle started");
}
}
interface Drivable {
void drive();
}
class Car extends Vehicle implements Drivable {
void move() {
System.out.println("Car moves");
}
public void drive() {
System.out.println("Car driving");
}
}

โ Interview Q&A
Q1: Can a class extend multiple abstract classes?
A: No, Java allows only single inheritance for classes.
Q2: Can a class implement multiple interfaces?
A: Yes, without limitation.
Q3: What can abstract classes have?
A: Fields, constructors, and concrete methods.
Q4: What can interfaces contain?
A: Method signatures and default/static methods.
Q5: Are interface methods public?
A: Yes, implicitly.
Q6: Why use abstract classes?
A: To share common code among related classes.
Q7: Why use interfaces?
A: To define contracts and support multiple inheritance.
Q8: Can abstract classes have constructors?
A: Yes.
Q9: Can interfaces have fields?
A: No, only constants (static final).
Q10: Can interface methods have implementation?
A: Yes, default and static methods (Java 8+).
๐ MCQs
Q1. Can a class extend multiple abstract classes?
- Yes
- No
- Sometimes
- Only with interfaces
Q2. Can a class implement multiple interfaces?
- No
- Yes
- Sometimes
- Depends
Q3. What can abstract classes have?
- Only methods
- Fields, constructors, methods
- Only constructors
- Only fields
Q4. What can interfaces contain?
- Fields
- Method signatures and default/static methods
- Constructors
- Instance methods
Q5. Are interface methods public?
- No
- Yes
- Sometimes
- Never
Q6. Why use abstract classes?
- To restrict inheritance
- To share common code
- For interfaces
- For data hiding
Q7. Why use interfaces?
- For implementation
- To define contracts
- To share code
- For performance
Q8. Can abstract classes have constructors?
- No
- Yes
- Sometimes
- Never
Q9. Can interfaces have fields?
- Yes
- No, only constants
- Sometimes
- No
Q10. Can interface methods have implementation?
- No
- Yes
- Sometimes
- Only abstract methods
๐ก Bonus Insight
Choosing between abstract classes and interfaces depends on whether you want to share code or define capabilities; understanding this distinction is key in Java design.
๐ PDF Download
Need a handy summary for your notes? Download this topic as a PDF!