1. Why Do We Need Interfaces & Abstract Classes?
Inheritance works great when you have a clear is-a relationship. But what if:
- You want common behavior but not full implementation?
- You need to force multiple classes to follow a contract?
- You want to simulate multiple inheritance (which Java does not allow for classes)?
👉 That’s where abstract classes and interfaces come in.
2. Abstract Classes – Deep Dive
2.1 Definition
-
A class declared with the
abstractkeyword. Can have:
- Abstract methods (declared, no body).
- Concrete methods (fully implemented).
- Cannot be instantiated directly.
2.2 Example
abstract class Shape {
abstract double area(); // abstract method
void display() { System.out.println("Shape details"); }
}
class Circle extends Shape {
double radius;
Circle(double r) { radius = r; }
@Override
double area() { return Math.PI * radius * radius; }
}
👉 Why abstract? Because Shape is too generic; it makes no sense to create a raw “Shape” object.
2.3 Key Points
- Constructors can exist in abstract classes → useful for common initialization.
- Can have fields, methods (abstract + non-abstract), static, final, etc.
- Child class must implement all abstract methods, else it too becomes abstract.
📌 Interview Trap: Can abstract classes have a main() method? ✅ Yes, but you can’t instantiate them directly.
3. Interfaces – Deep Dive
3.1 Definition
- A pure contract (before Java 8).
By default, methods are:
public abstract(no body).- Variables are
public static final(constants).
3.2 Example
interface Animal {
void sound(); // implicitly public abstract
}
class Dog implements Animal {
public void sound() { System.out.println("Bark"); }
}
Here, Dog must implement sound() or else it won’t compile.
3.3 Interfaces vs Abstract Classes
| Feature | Abstract Class | Interface |
|---|---|---|
| Methods | Abstract + Concrete | Abstract (till Java 7), Default/Static (Java 8+), Private (Java 9+) |
| Variables | Instance + static | Only public static final |
| Constructors | ✅ Allowed | ❌ Not allowed |
| Multiple Inheritance | ❌ Only single | ✅ Supports multiple interfaces |
| Usage | “is-a” with shared implementation | “can-do” capability / contract |
📌 Interview Line: Use abstract class when you want shared code, use interface when you only want to define a contract.
4. Java 8+ Superpowers of Interfaces
Earlier interfaces were “pure abstract,” but Java 8 changed the game:
4.1 Default Methods
- Allow methods with implementation inside interface.
- Used for backward compatibility.
interface Vehicle {
default void start() { System.out.println("Vehicle starts"); }
}
4.2 Static Methods
- Can be called via interface name directly.
interface MathUtils {
static int add(int a, int b) { return a + b; }
}
4.3 Functional Interfaces (Java 8)
- Interface with only one abstract method.
- Annotated with
@FunctionalInterface. - Supports lambda expressions.
@FunctionalInterface
interface Calculator {
int operate(int a, int b);
}
public class Main {
public static void main(String[] args) {
Calculator add = (a, b) -> a + b;
System.out.println(add.operate(5, 3)); // 8
}
}
📌 Used in Java Streams, Lambdas, concurrency APIs.
5. Real-World Use Cases
5.1 Abstract Class Example
HttpServlet(abstract) → subclasses likeLoginServlet,RegisterServletimplement request handling.
5.2 Interface Example
Comparable<T> → used for sorting objects (Collections.sort()).Runnable → thread tasks.List, Set, Map → defined as interfaces, implemented by ArrayList, HashSet, etc.
6. Multiple Inheritance via Interfaces
Java avoids multiple inheritance of classes but allows multiple interfaces:
interface Flyable { void fly(); }
interface Swimmable { void swim(); }
class Duck implements Flyable, Swimmable {
public void fly() { System.out.println("Duck flying"); }
public void swim() { System.out.println("Duck swimming"); }
}
📌 Interview Answer: Java uses interfaces to achieve multiple inheritance safely, avoiding diamond problem of classes.
7. Common Confusions
| Question | Answer |
|---|---|
| Can interfaces have constructors? | ❌ No. |
| Can abstract classes have constructors? | ✅ Yes. |
Can interface methods be private? |
✅ From Java 9 (helper methods). |
| Can abstract classes implement interfaces? | ✅ Yes, but must implement/leave abstract methods. |
| Can we create object of interface? | ❌ No, only reference type allowed. |
8. Interview Mindset Answers
- Q: Why interface if abstract class exists?
- Interfaces allow multiple inheritance and better abstraction of capability (
can-do). - Q: Why abstract class if interface exists?
- Abstract classes allow shared implementation, fields, constructors, which interfaces can’t fully do.
- Q: Real-life analogy of interface?
- A driving license – you must implement (learn driving) before you can use it.
✅ Final Summary
- Abstract Class → For partial abstraction + code reuse.
- Interface → For contracts, multiple inheritance, functional programming.
- Java 8+ gave interfaces default, static, and functional interface powers.
- Both are cornerstones of OOP + design patterns + interview prep.
Next Chapter 12 – Exception Handling