Advanced Java OOPs (Design Patterns + Scenarios)
--------------------------------------------
Advanced Java OOPs Interview Questions (Part 2)
--------------------------------------------
1. Object Class and Its Methods
--------------------------------------------
- Why Object class is the parent of all classes?
- Common methods:
toString(), equals(), hashCode(), clone(), getClass()
- How to override equals() and hashCode()? Example:
@Override
public boolean equals(Object obj) {
if(this == obj) return true;
if(!(obj instanceof Employee)) return false;
Employee e = (Employee) obj;
return [Link] == [Link];
}
@Override
public int hashCode() {
return [Link](id);
}
--------------------------------------------
2. Constructors and Initialization
--------------------------------------------
- Constructor overloading example.
- Constructor chaining using this() and super().
- Order of initialization: static -> instance -> constructor.
--------------------------------------------
3. Deep Dive into Inheritance and Polymorphism
--------------------------------------------
- What is covariant return type?
- Can constructors be overridden? No, but can be chained.
- Can we call a subclass method from superclass constructor? Dangerous practice (object not fully
constructed).
--------------------------------------------
4. Composition vs Inheritance
--------------------------------------------
- Inheritance: IS-A relationship.
- Composition: HAS-A relationship.
Example:
class Engine {}
class Car {
private Engine engine;
public Car(Engine engine){ [Link] = engine; }
}
--------------------------------------------
5. Aggregation and Association
--------------------------------------------
- Association: relationship between two classes.
- Aggregation: "Has-A" relationship (weak).
- Composition: "Part-of" relationship (strong).
--------------------------------------------
6. Real-World OOP Design Scenarios
--------------------------------------------
1. Design a Library Management System:
- Classes: Book, Member, Librarian, Library.
- Use inheritance (User -> Librarian, Member).
- Use polymorphism for fine calculation or return behavior.
2. Design a Ride-Sharing App (like Ola/Uber):
- Classes: Vehicle, Driver, Ride, Passenger.
- Interfaces for PaymentMethod, NotificationService.
3. Design an E-commerce System:
- Classes: Product, Cart, Order, Customer.
- Use abstraction for Payment Gateway integration.
--------------------------------------------
7. Java Design Patterns (Core OOPs-Based)
--------------------------------------------
1. Singleton Pattern
- Ensures a single instance of a class.
Example:
public class Singleton {
private static Singleton instance;
private Singleton(){}
public static Singleton getInstance(){
if(instance == null){ instance = new Singleton(); }
return instance;
}
}
2. Factory Pattern
- Creates objects without exposing creation logic.
Example:
class ShapeFactory {
public Shape getShape(String type){
if([Link]("circle")) return new Circle();
else if([Link]("square")) return new Square();
return null;
}
}
3. Strategy Pattern
- Behavior changes dynamically at runtime.
Example:
interface PaymentStrategy { void pay(); }
class CardPayment implements PaymentStrategy { public void pay(){ [Link]("Card
Payment"); }}
class UpiPayment implements PaymentStrategy { public void pay(){ [Link]("UPI
Payment"); }}
4. Observer Pattern
- Notifies all observers when state changes.
Used in event-driven systems.
--------------------------------------------
8. SOLID Principles (Core of OOP Design)
--------------------------------------------
S - Single Responsibility Principle
O - Open/Closed Principle
L - Liskov Substitution Principle
I - Interface Segregation Principle
D - Dependency Inversion Principle
--------------------------------------------
9. Common OOP Mistakes in Interviews
--------------------------------------------
- Misusing inheritance instead of composition.
- Forgetting to override equals/hashCode together.
- Ignoring access modifiers (encapsulation leaks).
- Using static everywhere (breaks OOP structure).
--------------------------------------------
10. Final Tips
--------------------------------------------
- Always use real-life examples in answers.
- Explain how OOP makes maintenance easier.
- Relate OOPs to actual project experience (e.g., MVC design in Spring Boot).
--------------------------------------------