OOPs

OOPs

Top Interview Questions

About OOPs

 

Object-Oriented Programming (OOPs)

Introduction

Object-Oriented Programming (OOP) is a programming paradigm that organizes software design around data, or objects, rather than functions and logic. An object can be anything with a defined state (attributes or properties) and behavior (methods or functions). OOP is widely used in modern programming because it mirrors real-world entities and relationships, making code easier to maintain, reuse, and extend. Languages like Java, C++, Python, C#, and Ruby are heavily based on OOP principles.


Core Concepts of OOP

OOP revolves around four fundamental principles: Encapsulation, Inheritance, Polymorphism, and Abstraction. Let’s discuss each in detail.

1. Encapsulation

Encapsulation is the process of wrapping data (variables) and methods (functions) that manipulate the data into a single unit called a class. It is primarily aimed at restricting direct access to some of an object’s components, which is achieved through access modifiers like private, protected, and public.

Example:

class Employee {
    private String name;
    private double salary;

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return this.name;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public double getSalary() {
        return this.salary;
    }
}

In the above example, the name and salary fields are private and cannot be accessed directly outside the class. Access is provided via getter and setter methods. Encapsulation improves security, reduces complexity, and ensures controlled access to the data.


2. Inheritance

Inheritance is a mechanism in which one class (child or subclass) acquires the properties and behaviors of another class (parent or superclass). It promotes code reusability and establishes a natural hierarchy between classes.

Example:

class Animal {
    void eat() {
        System.out.println("Animal eats food");
    }
}

class Dog extends Animal {
    void bark() {
        System.out.println("Dog barks");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog d = new Dog();
        d.eat(); // inherited method
        d.bark();
    }
}

Here, Dog inherits the eat method from Animal. This avoids code duplication and makes the system easier to manage. Inheritance can be single, multiple (supported via interfaces in Java), multilevel, and hierarchical.


3. Polymorphism

Polymorphism means “many forms” and allows objects to be treated as instances of their parent class rather than their actual class. Polymorphism is of two types:

  • Compile-time (Method Overloading): Same method name with different parameters in the same class.

  • Runtime (Method Overriding): Subclass provides a specific implementation of a method already defined in its superclass.

Example of Method Overloading:

class Calculator {
    int add(int a, int b) {
        return a + b;
    }

    double add(double a, double b) {
        return a + b;
    }
}

Example of Method Overriding:

class Animal {
    void sound() {
        System.out.println("Animal makes a sound");
    }
}

class Cat extends Animal {
    @Override
    void sound() {
        System.out.println("Cat meows");
    }
}

Polymorphism enables flexibility, scalability, and easy maintenance in software design.


4. Abstraction

Abstraction is the concept of hiding unnecessary details from the user and showing only essential features of an object. This helps in reducing complexity and isolating impact of changes. In Java, abstraction is achieved using abstract classes and interfaces.

Example:

abstract class Vehicle {
    abstract void start();
}

class Car extends Vehicle {
    void start() {
        System.out.println("Car starts with key");
    }
}

Here, the user only knows that the Car can start; the internal mechanism is hidden.


Classes and Objects

At the heart of OOP are classes and objects.

  • Class: A blueprint or template for creating objects. It defines the attributes and behaviors.

  • Object: A real-world instance of a class.

Example:

class Student {
    String name;
    int age;

    void display() {
        System.out.println("Name: " + name + ", Age: " + age);
    }
}

public class Main {
    public static void main(String[] args) {
        Student s1 = new Student();
        s1.name = "Yash";
        s1.age = 25;
        s1.display();
    }
}

In this example, Student is a class, and s1 is an object of that class.


Advantages of OOP

  1. Modularity: Each object is independent, making code easier to manage.

  2. Reusability: Inheritance allows code reuse, reducing redundancy.

  3. Scalability: Polymorphism enables systems to handle new requirements easily.

  4. Maintainability: Encapsulation and abstraction make updates less error-prone.

  5. Security: Access modifiers ensure controlled access to data.

  6. Real-world modeling: OOP mimics real-world objects and relationships, improving understanding and design.


OOP Design Principles

OOP principles guide software engineers to write robust, maintainable, and efficient code. Some key principles include:

  1. SOLID Principles:

    • S: Single Responsibility Principle – A class should have only one reason to change.

    • O: Open/Closed Principle – Classes should be open for extension, closed for modification.

    • L: Liskov Substitution Principle – Objects of a superclass should be replaceable by subclass objects.

    • I: Interface Segregation Principle – Use multiple specific interfaces instead of one general-purpose interface.

    • D: Dependency Inversion Principle – Depend on abstractions, not concrete implementations.

  2. DRY Principle (Don’t Repeat Yourself): Avoid code duplication.

  3. KISS Principle (Keep It Simple, Stupid): Simplicity improves readability and maintainability.


Common OOP Terminology

  • Constructor: Special method to initialize objects.

  • Destructor: Method to clean up resources before object destruction.

  • Method: Function defined inside a class.

  • Attribute/Property: Variables that hold object data.

  • Interface: Defines a contract without implementation.

  • Overloading: Same method name, different parameters.

  • Overriding: Subclass method replaces superclass method.

  • Static members: Belong to class rather than instance.


OOP in Modern Programming Languages

  1. Java: Fully object-oriented; supports encapsulation, inheritance, polymorphism, and abstraction. Widely used for enterprise applications.

  2. C++: Supports both procedural and object-oriented programming; allows multiple inheritance.

  3. Python: OOP is easy to use; supports dynamic typing and multiple inheritance.

  4. C#: Similar to Java, used for .NET applications.

  5. Ruby: Pure OOP; everything is an object.


Real-World Examples of OOP

  1. Banking System:

    • Objects: Account, Customer, Transaction.

    • Methods: deposit(), withdraw(), transfer().

    • Encapsulation protects account balances.

    • Polymorphism handles different account types (Savings, Checking).

  2. E-commerce System:

    • Objects: Product, Order, Cart, User.

    • Inheritance: DigitalProduct and PhysicalProduct inherit Product class.

    • Abstraction hides internal payment processing details.

  3. Transportation System:

    • Objects: Vehicle, Bus, Car, Bike.

    • Inheritance and polymorphism simplify vehicle management.


Challenges in OOP

  • Complexity: Overuse of objects may make code difficult to understand.

  • Performance: Object creation and method calls may be slower than procedural approaches.

  • Design Overhead: Proper class design requires planning and understanding of requirements.

Fresher Interview Questions

 

1. What is OOPs?

Answer:
OOPs stands for Object-Oriented Programming. It is a programming paradigm that uses objects and classes to design programs. OOPs focuses on organizing code based on real-world entities, making programs more modular, reusable, and easier to maintain.

Key Concepts of OOPs:

  • Class: A blueprint for creating objects.

  • Object: An instance of a class.

  • Encapsulation: Wrapping data and methods together.

  • Inheritance: Acquiring properties of one class into another.

  • Polymorphism: Ability to take multiple forms (method overloading/overriding).

  • Abstraction: Hiding implementation details and showing only functionality.


2. What is a Class and an Object?

Answer:

  • Class: A class is a blueprint or template for creating objects. It defines the properties (attributes) and behaviors (methods).
    Example:

    class Car {
        String color;
        int speed;
    
        void start() {
            System.out.println("Car started");
        }
    }
    
  • Object: An object is an instance of a class.
    Example:

    Car myCar = new Car(); // myCar is an object
    myCar.color = "Red";
    myCar.speed = 100;
    myCar.start(); // Output: Car started
    

3. What is Encapsulation?

Answer:
Encapsulation is the process of wrapping data (variables) and methods into a single unit (class). It also provides data hiding, restricting direct access to some class components.

Example in Java:

class Person {
    private String name; // private variable

    public void setName(String name) { // setter method
        this.name = name;
    }

    public String getName() { // getter method
        return name;
    }
}
  • Here, name is hidden from outside the class and accessed through getters and setters.

Benefits:

  • Protects data from unauthorized access.

  • Makes code more maintainable.


4. What is Inheritance?

Answer:
Inheritance allows a class (child/subclass) to acquire the properties and methods of another class (parent/superclass).

Types of Inheritance in Java:

  1. Single Inheritance

  2. Multilevel Inheritance

  3. Hierarchical Inheritance

  4. Multiple Inheritance (via interfaces)

  5. Hybrid Inheritance (combination)

Example:

class Animal {
    void eat() {
        System.out.println("Animal is eating");
    }
}

class Dog extends Animal {
    void bark() {
        System.out.println("Dog is barking");
    }
}

Dog d = new Dog();
d.eat();  // Inherited method
d.bark(); // Child method

5. What is Polymorphism?

Answer:
Polymorphism means many forms. It allows objects to behave differently based on their data types or context.

Types:

  1. Compile-time Polymorphism (Method Overloading)
    Same method name with different parameters.

    class Math {
        int add(int a, int b) { return a + b; }
        double add(double a, double b) { return a + b; }
    }
    
  2. Runtime Polymorphism (Method Overriding)
    Subclass provides its own implementation of a method.

    class Animal {
        void sound() { System.out.println("Animal sound"); }
    }
    class Dog extends Animal {
        void sound() { System.out.println("Dog barking"); }
    }
    Animal a = new Dog();
    a.sound(); // Output: Dog barking
    

6. What is Abstraction?

Answer:
Abstraction is hiding the implementation details and showing only the essential features to the user.

In Java:

  • Achieved using abstract classes or interfaces.

Example:

abstract class Vehicle {
    abstract void start(); // abstract method
}
class Car extends Vehicle {
    void start() {
        System.out.println("Car started");
    }
}
  • Here, the user knows start() will start the vehicle but does not know the internal details.


7. What is the difference between Abstraction and Encapsulation?

Feature Abstraction Encapsulation
Concept Hides implementation details Hides data (information)
How achieved Abstract classes, Interfaces Access modifiers (private/protected)
Focus What a system does How data is accessed
Example Vehicle class with start() Person class with private name

8. What are Constructors?

Answer:
A constructor is a special method used to initialize objects. It has:

  • Same name as the class

  • No return type

  • Can be parameterized or default

Example:

class Car {
    String color;
    Car(String c) {
        color = c;
    }
}
Car c1 = new Car("Red");
System.out.println(c1.color); // Red

9. What is the difference between Class and Object?

Feature Class Object
Definition Blueprint/template Instance of a class
Memory No memory allocated Memory is allocated
Example Car myCar = new Car()

10. What is the difference between Overloading and Overriding?

Feature Method Overloading Method Overriding
Compile/Run time Compile-time (polymorphism) Runtime (polymorphism)
Parameters Different parameters Same parameters
Return type Can differ Should be same (covariant allowed)
Inheritance Not mandatory Must have inheritance

11. What is a Static Keyword in OOPs?

Answer:
The static keyword belongs to the class rather than an instance. It can be used for:

  • Static variables

  • Static methods

  • Static blocks

Example:

class Test {
    static int count = 0; // shared by all objects
    Test() { count++; }
}
Test t1 = new Test();
Test t2 = new Test();
System.out.println(Test.count); // Output: 2

12. What is this keyword?

Answer:
this refers to the current object of a class. It is used to:

  • Access class variables

  • Call other constructors

  • Pass the current object

Example:

class Person {
    String name;
    Person(String name) { this.name = name; }
}

13. What is the difference between Interface and Abstract Class?

Feature Interface Abstract Class
Methods Only abstract (before Java 8) Abstract and non-abstract methods
Multiple inheritance Yes (implements multiple interfaces) No
Variables public static final by default Can have instance variables
Constructor Not allowed Allowed

14. What is a Final Keyword?

Answer:
The final keyword is used to restrict the user in OOPs:

  • final class → cannot be inherited

  • final method → cannot be overridden

  • final variable → value cannot be changed

Example:

final int x = 10;
x = 20; // Error

15. Can you explain “Object-Oriented Principles” in simple words?

Answer:

  • Encapsulation → Keep data safe inside class

  • Abstraction → Hide unnecessary details

  • Inheritance → Reuse code from parent

  • Polymorphism → Same method behaves differently


16. What is a “Constructor Overloading”?

Answer:
Constructor overloading occurs when a class has more than one constructor with different parameter lists. It helps in creating objects in multiple ways.

Example:

class Car {
    String color;
    int speed;

    Car() { // Default constructor
        color = "White";
        speed = 0;
    }

    Car(String color) { // Parameterized constructor
        this.color = color;
        speed = 0;
    }

    Car(String color, int speed) {
        this.color = color;
        this.speed = speed;
    }
}

Car c1 = new Car();          // White, 0
Car c2 = new Car("Red");     // Red, 0
Car c3 = new Car("Blue", 100); // Blue, 100

17. What is “Dynamic Method Dispatch”?

Answer:
Dynamic method dispatch is the process in which a call to an overridden method is resolved at runtime rather than compile time.

Example:

class Animal {
    void sound() { System.out.println("Animal sound"); }
}
class Dog extends Animal {
    void sound() { System.out.println("Dog barking"); }
}

Animal a = new Dog();
a.sound(); // Output: Dog barking
  • Here, the actual method is decided at runtime depending on the object type.


18. What is the difference between Object and Class?

Feature Class Object
Definition Blueprint/template Instance of a class
Memory Not allocated Memory allocated
Behavior Defines behavior Exhibits behavior
Example Car myCar = new Car()

19. What is a “Static Block”?

Answer:

  • A static block is used to initialize static variables when a class is loaded.

  • It executes only once at the time of class loading.

Example:

class Test {
    static int count;
    static {
        count = 100;
        System.out.println("Static block executed");
    }
}
  • Output: "Static block executed" is printed when the class is loaded.


20. Difference between “Overriding” and “Overloading”

Feature Overriding Overloading
Compile/Runtime Runtime Compile-time
Parameters Must be same Must be different
Return Type Same (covariant allowed) Can be different
Inheritance Required Not required

21. What is a “Package” in OOP?

Answer:
A package is a collection of related classes and interfaces. It helps in code modularity, reusability, and avoiding naming conflicts.

Example:

package mypackage;
public class Car {
    void start() { System.out.println("Car started"); }
}

22. What is the difference between “Abstract Class” and “Interface”?

Feature Abstract Class Interface
Methods Abstract + concrete allowed Only abstract (before Java 8)
Multiple inheritance Not allowed Allowed
Constructor Allowed Not allowed
Variables Instance variables allowed Only public static final

23. What is “Aggregation” and “Composition”?

Answer:

  • Aggregation: “Has-a” relationship where the child can exist without the parent.

  • Composition: “Part-of” relationship where the child cannot exist without the parent.

Example:

// Aggregation
class Engine { }
class Car {
    Engine engine; // Car has-a Engine
}

// Composition
class House {
    Room room; // Room cannot exist without House
    House() { room = new Room(); }
}

24. Difference between “Association”, “Aggregation”, and “Composition”

Relationship Description Life of Child
Association General relationship between classes Independent
Aggregation “Has-a” relationship Independent
Composition Strong “Part-of” relationship Dependent on parent

25. What is the difference between “final”, “finally”, and “finalize”?

Keyword Description
final Used for variable, method, class to restrict changes/inheritance
finally Block used in exception handling, always executes
finalize Method called by GC before destroying an object

26. What is “Object Cloning”?

Answer:
Cloning is creating an exact copy of an object using the clone() method from the Cloneable interface.

Example:

class Person implements Cloneable {
    String name;
    int age;

    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

Person p1 = new Person();
Person p2 = (Person) p1.clone();

27. What is the difference between “Shallow Copy” and “Deep Copy”?

Feature Shallow Copy Deep Copy
Copies object Copies object reference only Copies object + its references
Changes in copy Affects original object Original object not affected
Method clone() Custom method or serialization

28. What are “Access Modifiers” in OOP?

Answer:
Access modifiers control visibility of class members.

Modifier Description
private Accessible only within class
default Accessible within package
protected Accessible within package + subclass
public Accessible from anywhere

29. What is “Interface vs Abstract Class vs Class”?

Feature Class Abstract Class Interface
Methods Concrete Abstract + concrete Abstract only (Java 8+ default/static)
Variables Yes Yes public static final
Inheritance Single/multiple Single only Multiple
Instantiation Yes No No

30. What are the main benefits of OOPs?

Answer:

  1. Code Reusability: Classes can be reused via inheritance.

  2. Modularity: Objects are self-contained modules.

  3. Maintainability: Easy to modify and extend code.

  4. Flexibility: Polymorphism allows flexibility in method usage.

  5. Security: Encapsulation provides data hiding.

Experienced Interview Questions

 

1. What are the main principles of OOP and how have you applied them in real projects?

Answer:
The main OOP principles are:

  1. Encapsulation: Hiding data using private/protected modifiers.

    • Example: Using getters and setters to access private fields.

  2. Abstraction: Exposing only relevant methods, hiding implementation.

    • Example: Creating abstract classes for Payment processing (PayPal, CreditCard) and implementing only necessary methods.

  3. Inheritance: Reusing code through parent-child relationships.

    • Example: Base class Employee with Manager and Developer subclasses.

  4. Polymorphism: Allowing objects to take multiple forms.

    • Example: Method overriding for calculateSalary() in different employee types.

Real-world scenario:
In a billing system, I used abstraction to create a Payment interface. Different payment types (CreditCardPayment, UPIPayment) implemented the interface, allowing easy extension without changing core logic.


2. Can you explain method overloading and overriding with real examples?

Answer:

  • Method Overloading: Same method name with different parameters in the same class.

    class Calculator {
        int add(int a, int b) { return a + b; }
        double add(double a, double b) { return a + b; }
    }
    
  • Method Overriding: Subclass provides a specific implementation of a method defined in parent class.

    class Employee {
        void work() { System.out.println("Employee working"); }
    }
    class Developer extends Employee {
        void work() { System.out.println("Developer coding"); }
    }
    

Real-world scenario:
In an HR application, Employee.calculateBonus() was overridden in Manager and Developer classes because bonus calculation logic differed.


3. Explain the difference between abstract class and interface. When would you use each?

Feature Abstract Class Interface
Methods Abstract + concrete Abstract (Java 7); default/static allowed in Java 8+
Variables Instance variables allowed Only public static final
Multiple inheritance Not allowed Allowed
Constructor Allowed Not allowed

Use case:

  • Use abstract class when you have shared code and want some default behavior.

  • Use interface for defining capabilities that multiple unrelated classes can implement (Serializable, Cloneable).


4. What is the difference between composition and aggregation? Provide examples.

Answer:

  • Aggregation: Weak “has-a” relationship. The child can exist independently.

    class Department { }
    class Employee {
        Department dept; // Employee has-a Department
    }
    
  • Composition: Strong “part-of” relationship. Child cannot exist without parent.

    class House {
        Room room;
        House() { room = new Room(); }
    }
    

Scenario:
In a project management system, a Project contains multiple Tasks. If the project is deleted, tasks are deleted too → composition.


5. How do you handle multiple inheritance issues in Java?

Answer:
Java does not support multiple inheritance with classes to avoid ambiguity (“diamond problem”).

  • Instead, interfaces are used.

  • If two interfaces have default methods with the same signature, the implementing class must override the method.

Example:

interface A { default void show() { System.out.println("A"); } }
interface B { default void show() { System.out.println("B"); } }
class C implements A, B {
    public void show() { System.out.println("C"); } // resolve conflict
}

6. Explain object cloning and difference between shallow and deep copy.

  • Shallow Copy: Copies object reference only; changes affect original.

  • Deep Copy: Creates a new object with cloned values; original remains unaffected.

Example:

class Employee implements Cloneable {
    Address address;
    protected Object clone() throws CloneNotSupportedException {
        return super.clone(); // shallow copy
    }
}

For deep copy, you manually clone Address inside Employee.clone().

Scenario:
In caching systems, deep copy ensures cached objects are isolated from changes to original objects.


7. How do you implement design patterns using OOP principles?

Answer:

  • Singleton Pattern: Restrict class to a single instance.

    public class Logger {
        private static Logger instance;
        private Logger() { }
        public static Logger getInstance() {
            if(instance == null) instance = new Logger();
            return instance;
        }
    }
    
  • Factory Pattern: Create objects without exposing instantiation logic.

  • Observer Pattern: Notify multiple objects on state changes.

Scenario:
In a real-time notification system, I used the Observer Pattern to update dashboards whenever a new order was placed.


8. What is the difference between final, finally, and finalize?

Keyword Description
final Prevent inheritance/overriding/changes to variables
finally Block in try-catch that always executes
finalize Called by GC before destroying an object

Scenario:
finally was used in DB operations to ensure connection closure, regardless of exceptions.


9. How do you prevent memory leaks in OOP projects?

Answer:

  1. Avoid circular references in objects.

  2. Use weak references for caches.

  3. Close resources (DB connections, files) in finally blocks or use try-with-resources.

  4. Remove objects from collections when no longer needed.

Scenario:
In a Spring Boot project, failing to remove listeners led to memory leaks. Refactoring with weak references resolved it.


10. What is SOLID principle in OOP and how have you applied it?

Answer:
SOLID principles guide OOP design:

  1. S - Single Responsibility Principle: Each class has one responsibility.

  2. O - Open/Closed Principle: Classes open for extension, closed for modification.

  3. L - Liskov Substitution Principle: Subtypes should replace base types without errors.

  4. I - Interface Segregation Principle: Use multiple specific interfaces instead of one general interface.

  5. D - Dependency Inversion Principle: High-level modules should not depend on low-level modules; depend on abstractions.

Scenario:
In a billing system:

  • Separate classes for InvoiceGenerator and PaymentProcessor (SRP).

  • PaymentProcessor implements Payment interface (OCP & DIP).


11. Difference between Object-Oriented and Procedural Programming

Feature Procedural Programming Object-Oriented Programming
Approach Function-based Object-based
Reusability Low High
Data Handling Separate from functions Encapsulated in objects
Example C Java, C++, C#

Scenario:
In legacy systems, converting procedural billing code to OOP improved maintainability and reduced code duplication.


12. Explain the use of this and super keywords.

  • this: Refers to the current object. Used to avoid naming conflicts, call constructors, or pass object reference.

  • super: Refers to parent class. Used to call parent constructors or methods.

Scenario:
In a multi-level inheritance scenario for Employee -> Manager, super was used to call the base constructor to initialize common fields.


13. How do you implement exception handling in OOP projects?

Answer:

  • Use try-catch-finally blocks.

  • Prefer custom exceptions for project-specific errors.

  • Use checked exceptions for recoverable and unchecked for programming errors.

Scenario:
In an e-commerce project, OutOfStockException (custom exception) was thrown when stock was insufficient, maintaining clean error handling.


14. Can you explain runtime polymorphism in your projects?

Answer:

  • Runtime polymorphism allows behavior to be decided at runtime via method overriding.

Scenario:
In a payroll system, calculateSalary() was overridden in multiple subclasses (FullTimeEmployee, ContractEmployee). Depending on the object type, the correct method was invoked at runtime.


15. What is your approach to designing maintainable OOP systems?

Answer:

  1. Apply SOLID principles.

  2. Use design patterns (Factory, Observer, Singleton, Strategy).

  3. Encapsulate data and minimize coupling.

  4. Write unit tests for critical classes.

  5. Maintain modular code structure with packages.

Scenario:
In a banking application, we used modular packages for Customer, Account, and Transaction with interfaces and abstract classes to ensure scalability.


16. What is the difference between “Has-a” and “Is-a” relationships? Give examples.

Answer:

  • Is-a relationship: Represents inheritance. One class is a subtype of another.

    • Example: Dog is-a Animalclass Dog extends Animal.

  • Has-a relationship: Represents composition or aggregation. One class contains another.

    • Example: Car has-a Engineclass Car { Engine engine; }.

Scenario:
In an e-commerce system, Order has-a Customer, and Manager is-a Employee.


17. What is Liskov Substitution Principle (LSP) and how did you implement it?

Answer:

  • Definition: Subtypes must be substitutable for their base types without affecting correctness.

  • Scenario:
    In a payment system:

    class Payment {
        double process(double amount) { return amount; }
    }
    class CreditCardPayment extends Payment {
        double process(double amount) { return amount * 0.98; } // discount applied
    }
    

    Any Payment reference can hold CreditCardPayment without breaking functionality.


18. Explain Dependency Injection and how it relates to OOP.

Answer:

  • Dependency Injection (DI): Technique to inject dependencies rather than creating them inside a class.

  • Helps reduce tight coupling and improves testability.

Example:

class EmailService { void sendEmail() {} }
class UserService {
    private EmailService emailService;
    public UserService(EmailService emailService) { this.emailService = emailService; }
}
  • Scenario: In a Spring Boot application, DI allowed swapping EmailService with SMSService without changing UserService.


19. What is the difference between shallow copy and deep copy?

Feature Shallow Copy Deep Copy
Copies Only object reference Object + referenced objects
Effect on original Changes in copy affect original Original remains unaffected
Implementation Object.clone() Manual cloning or serialization

Scenario:
In caching objects for a workflow engine, deep copy ensured tasks were independent of original workflow objects.


20. What is the difference between Composition and Inheritance?

Feature Composition Inheritance
Relationship “Has-a” “Is-a”
Coupling Loose Tight
Flexibility High Limited
Example Car has-a Engine Dog is-a Animal

Scenario:
In a project management tool, Project has-a Task (composition) while Manager is-a Employee (inheritance).


21. What are design patterns and which ones have you used in projects?

Answer:

  • Design Patterns: Reusable solutions for common problems in software design.

  • Patterns I’ve used:

    1. Singleton: Logger, DB connection.

    2. Factory: Payment gateway selection.

    3. Observer: Real-time notifications in dashboards.

    4. Strategy: Dynamic discount calculation.

    5. Decorator: Dynamic enhancement of features, e.g., applying multiple tax rules.


22. Explain the Open/Closed Principle (OCP) with an example.

Answer:

  • Definition: Software entities should be open for extension but closed for modification.

  • Example:

interface Notification { void notifyUser(String message); }
class EmailNotification implements Notification { ... }
class SMSNotification implements Notification { ... }
  • Adding a new notification type (PushNotification) doesn’t modify existing code, satisfying OCP.


23. What is Interface Segregation Principle (ISP) and why is it important?

Answer:

  • Definition: Clients should not be forced to implement interfaces they don’t use.

  • Scenario:
    Instead of one large EmployeeActions interface:

    interface Work { void doWork(); }
    interface Report { void generateReport(); }
    

    Developer implements Work only, Manager implements both Work and Report.


24. Explain Single Responsibility Principle (SRP) with example.

Answer:

  • Definition: A class should have only one reason to change.

  • Example:

    class InvoiceGenerator { void generateInvoice() {} }
    class InvoicePrinter { void printInvoice() {} }
    
  • Scenario:
    In a billing system, separating invoice generation and printing reduces bugs and enhances maintainability.


25. What is the difference between Association, Aggregation, and Composition?

Relationship Definition Life of Child
Association General relationship Independent
Aggregation “Has-a” relationship Can exist alone
Composition Strong “part-of” relationship Dependent on parent

Scenario:

  • Employee associated with Project (Association)

  • Car has Engine (Aggregation)

  • House has Room (Composition)


26. How do you implement multiple inheritance in Java?

Answer:

  • Via interfaces only.

  • If multiple interfaces have the same default method, the implementing class must override the method.

Example:

interface A { default void show() {} }
interface B { default void show() {} }
class C implements A, B { public void show() { } } 

27. What is the difference between Abstract Class and Template Method Pattern?

Answer:

  • Abstract Class: Provides partial implementation with abstract methods.

  • Template Method Pattern: Defines skeleton of an algorithm in the abstract class but lets subclasses define specific steps.

Scenario:
In order processing, abstract class defined processOrder(), and subclasses implemented calculateDiscount() differently.


28. How do you implement exception handling and custom exceptions in OOP projects?

Answer:

  • Use try-catch-finally for safe error handling.

  • Create custom exceptions for business-specific rules.

Example:

class InsufficientFundsException extends Exception { }
class BankAccount {
    void withdraw(double amount) throws InsufficientFundsException { ... }
}

Scenario:
In a banking system, InsufficientFundsException handled invalid transactions without crashing the system.


29. How do you optimize object-oriented design for performance?

Answer:

  1. Use composition over inheritance where suitable.

  2. Apply lazy initialization for heavy objects.

  3. Avoid unnecessary object creation (use object pooling).

  4. Minimize deep object graphs to reduce GC overhead.

  5. Use immutable objects for thread safety.

Scenario:
In a reporting engine, object pooling reduced creation overhead of report templates.


30. How do you ensure maintainability and scalability in OOP systems?

Answer:

  1. Follow SOLID principles.

  2. Use design patterns appropriately.

  3. Encapsulate data and minimize tight coupling.

  4. Modularize code with packages and interfaces.

  5. Implement unit testing and code reviews.

Scenario:
In a subscription management system, modular OOP design allowed easy addition of new subscription types without changing existing code.