Skip to content

Latest commit

 

History

History
929 lines (716 loc) · 17.2 KB

File metadata and controls

929 lines (716 loc) · 17.2 KB

Core Concepts

1. Difference Between a Class and an Object

Class

  • A blueprint or template for creating objects.
  • Defines properties (fields) and behaviors (methods).

Object

  • An instance of a class.
  • Represents a specific entity with actual data.

Real-World Analogy

  • Class: A blueprint of a house.
  • Object: A specific house built using the blueprint.

Example

class Car {
    String brand;
    void drive() {
        System.out.println("Driving...");
    }
}

Car myCar = new Car();
myCar.brand = "Toyota";
myCar.drive(); // Output: Driving...

2. Polymorphism

Definition

Polymorphism allows methods or objects to take multiple forms.

Compile-Time Polymorphism (Method Overloading)

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

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

Runtime Polymorphism (Method Overriding)

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

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

3. Encapsulation

Definition

Encapsulation is wrapping data and methods into a single unit, ensuring controlled access.

Example

class Account {
    private double balance;

    public double getBalance() {
        return balance;
    }

    public void deposit(double amount) {
        if (amount > 0) {
            balance += amount;
        }
    }
}

4. Access Modifiers

Types in Java

Modifier Visibility
public Accessible from anywhere.
private Accessible only within the defining class.
protected Accessible within the same package and subclasses.
default Accessible within the same package.

Example

class Example {
    public int publicField;
    private int privateField;
    protected int protectedField;
    int defaultField; // Package-private
}

5. Abstraction

Definition

Abstraction hides implementation details and exposes only essential features.

Abstract Class

abstract class Vehicle {
    abstract void start();
}

Interface

interface Driveable {
    void drive();
}

Can a Class Implement Multiple Interfaces? Yes, a class can implement multiple interfaces to provide a contract for different behaviors.


6. Interface vs Abstract Class

Key Differences

Feature Abstract Class Interface
Methods Abstract and concrete methods Abstract methods only (before Java 8).
Variables Instance variables allowed Only static final constants

Scenario An abstract class is preferred when some methods have shared code to be inherited by subclasses.


7. Inheritance vs Composition

Inheritance

  • Models an "is-a" relationship.
  • Tight coupling between parent and child classes.

Composition

  • Models a "has-a" relationship.
  • Promotes loose coupling.

Example

class Engine {}

class Car {
    private Engine engine;
    public Car(Engine engine) {
        this.engine = engine;
    }
}

8. Multiple Inheritance

Definition

When a class inherits from more than one class.

Java

  • Not Supported to avoid ambiguity (Diamond Problem).
  • Achieved using interfaces.

Example

interface A {
    void methodA();
}

interface B {
    void methodB();
}

class C implements A, B {
    public void methodA() {
        System.out.println("Method A");
    }

    public void methodB() {
        System.out.println("Method B");
    }
}

Python

  • Supports multiple inheritance using MRO (Method Resolution Order).

9. this Keyword

Definition

Refers to the current instance of a class.

Example

class Example {
    private int value;

    Example(int value) {
        this.value = value; // Refers to instance variable
    }

    void display() {
        System.out.println("Value: " + this.value);
    }
}

10. Method Overriding

Definition

A subclass provides a specific implementation for a method in the parent class.

Rules

  1. Same method name, return type, and parameters.
  2. Cannot have a stricter access modifier in the subclass.
  3. Must use @Override annotation for clarity.

Example

class Parent {
    void display() {
        System.out.println("Parent Display");
    }
}

class Child extends Parent {
    @Override
    void display() {
        System.out.println("Child Display");
    }
}

public class Main {
    public static void main(String[] args) {
        Parent obj = new Child(); // Upcasting
        obj.display();            // Output: Child Display
    }
}

Advanced OOP Concepts

11. Static Methods and Static Variables

Static Methods

  • Belong to the class, not an instance.
  • Can be called without creating an object.

Static Variables

  • Shared among all instances of a class.
  • Hold a single copy of data.

When to Use?

  • Utility methods (e.g., Math.sqrt()).
  • Constants or shared data across objects.

Limitations

  • Cannot access non-static fields or methods directly.
  • Static methods cannot be overridden (hidden instead).

Example

class Example {
    static int count = 0; // Static variable

    static void incrementCount() { // Static method
        count++;
    }
}

12. Constructors

What is a Constructor?

  • A special method to initialize objects.
  • Same name as the class, no return type.

Default Constructor vs Copy Constructor

  • Default Constructor: No arguments.
  • Copy Constructor: Creates a new object by copying another object.

Example

class Example {
    int value;

    // Default Constructor
    Example() {
        value = 0;
    }

    // Copy Constructor
    Example(Example obj) {
        this.value = obj.value;
    }
}

13. Deep Copy vs Shallow Copy

Shallow Copy

  • Copies references, not actual objects.

Deep Copy

  • Creates new instances for all objects.

Example

class Address {
    String city;

    public Address(String city) {
        this.city = city;
    }
}

class Person implements Cloneable {
    String name;
    Address address;

    public Person(String name, Address address) {
        this.name = name;
        this.address = address;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone(); // Shallow Copy
    }

    public Person deepClone() {
        return new Person(this.name, new Address(this.address.city)); // Deep Copy
    }
}

14. Final Classes and Methods in Java

Final Classes

  • Cannot be extended.
  • Use for immutable classes or to prevent inheritance.

Final Methods

  • Cannot be overridden.
  • Ensures critical methods are not modified.

Example

final class Example {
    final void display() {
        System.out.println("Final Method");
    }
}

15. == Operator vs .equals() Method

Key Differences

  • ==: Compares memory references.
  • .equals(): Compares content of objects.

Example

String str1 = "Hello";
String str2 = new String("Hello");

System.out.println(str1 == str2);       // false
System.out.println(str1.equals(str2)); // true

16. Garbage Collection in Java

Purpose

  • Reclaims memory for unreferenced objects.
  • Reduces memory leaks.

How It Works

  • JVM runs garbage collection automatically using algorithms like Mark-and-Sweep.

Explicit Call

System.gc();

17. Dynamic Method Dispatch

Definition

  • Resolves method calls at runtime based on the object type.

Example

class Parent {
    void display() {
        System.out.println("Parent Display");
    }
}

class Child extends Parent {
    @Override
    void display() {
        System.out.println("Child Display");
    }
}

public class Main {
    public static void main(String[] args) {
        Parent obj = new Child(); // Upcasting
        obj.display();            // Output: Child Display
    }
}

18. super Keyword

Usage

  1. Access parent class constructors.
  2. Access parent class methods or fields.

Example

class Parent {
    Parent() {
        System.out.println("Parent Constructor");
    }
}

class Child extends Parent {
    Child() {
        super(); // Calls Parent constructor
        System.out.println("Child Constructor");
    }
}

19. Dependency Injection (DI)

Definition

  • A design pattern where dependencies are provided externally, not created within a class.

Example

interface Service {
    void execute();
}

class MyService implements Service {
    public void execute() {
        System.out.println("Service Executed");
    }
}

class Client {
    private Service service;

    Client(Service service) {
        this.service = service; // Dependency Injected
    }

    void performTask() {
        service.execute();
    }
}

public class Main {
    public static void main(String[] args) {
        Service service = new MyService();
        Client client = new Client(service);
        client.performTask();
    }
}

20. Aggregation vs Composition

Aggregation

  • A "has-a" relationship.
  • Child objects can exist independently of the parent.

Example

class Engine {}

class Car {
    private Engine engine;
}

Composition

  • A "part-of" relationship.
  • Child objects' lifecycle depends on the parent.

Example

class Engine {}

class Car {
    private final Engine engine = new Engine();
}

Key Difference

Feature Aggregation Composition
Lifespan Independent of parent Dependent on parent
Coupling Loosely coupled Strongly coupled

Design-Oriented Questions

21. Transportation System Class Hierarchy

Classes and Methods

  1. Vehicle: Base class with methods start() and stop().
  2. Car, Bike, Truck: Inherit from Vehicle and add specific behaviors.

Code Example

abstract class Vehicle {
    void start() {
        System.out.println("Vehicle started");
    }
    void stop() {
        System.out.println("Vehicle stopped");
    }
}

class Car extends Vehicle {
    void playMusic() {
        System.out.println("Playing music in the car");
    }
}

class Bike extends Vehicle {
    void doWheelie() {
        System.out.println("Doing a wheelie");
    }
}

class Truck extends Vehicle {
    void loadCargo() {
        System.out.println("Loading cargo");
    }
}

22. Basic ATM System

Classes and Relationships

  1. Account: Manages balance and transactions.
  2. Customer: Holds account details.
  3. Bank: Contains multiple customers and handles transactions.
  4. Transaction: Represents deposits and withdrawals.

Code Example

class Account {
    private double balance;

    public void deposit(double amount) {
        balance += amount;
    }

    public void withdraw(double amount) {
        if (balance >= amount) balance -= amount;
    }

    public double getBalance() {
        return balance;
    }
}

class Customer {
    private String name;
    private Account account;

    public Customer(String name, Account account) {
        this.name = name;
        this.account = account;
    }

    public Account getAccount() {
        return account;
    }
}

class Transaction {
    private Customer customer;
    private String type;
    private double amount;

    public Transaction(Customer customer, String type, double amount) {
        this.customer = customer;
        this.type = type;
        this.amount = amount;
    }

    public void process() {
        if (type.equals("deposit")) customer.getAccount().deposit(amount);
        else if (type.equals("withdraw")) customer.getAccount().withdraw(amount);
    }
}

23. Simple E-Commerce System

Classes and Relationships

  1. Product: Represents items.
  2. User: Customer of the platform.
  3. Cart: Aggregates products.
  4. Order: Composed of cart and user details.

Code Example

class Product {
    private String name;
    private double price;

    public Product(String name, double price) {
        this.name = name;
        this.price = price;
    }

    public double getPrice() {
        return price;
    }
}

class User {
    private String username;
    public User(String username) {
        this.username = username;
    }
}

class Cart {
    private List products = new ArrayList<>();

    public void addProduct(Product product) {
        products.add(product);
    }

    public List getProducts() {
        return products;
    }
}

class Order {
    private User user;
    private Cart cart;

    public Order(User user, Cart cart) {
        this.user = user;
        this.cart = cart;
    }
}

24. Thread-Safe Singleton Pattern

Code Example

class Singleton {
    private static volatile Singleton instance;

    private Singleton() {}

    public static Singleton getInstance() {
        if (instance == null) {
            synchronized (Singleton.class) {
                if (instance == null) {
                    instance = new Singleton();
                }
            }
        }
        return instance;
    }
}

25. Factory Design Pattern

Code Example

interface Shape {
    void draw();
}

class Circle implements Shape {
    public void draw() {
        System.out.println("Drawing Circle");
    }
}

class Square implements Shape {
    public void draw() {
        System.out.println("Drawing Square");
    }
}

class ShapeFactory {
    public static Shape createShape(String type) {
        switch (type) {
            case "Circle": return new Circle();
            case "Square": return new Square();
            default: throw new IllegalArgumentException("Unknown shape type");
        }
    }
}

26. Observer Pattern

Code Example

interface Subscriber {
    void update(String news);
}

class NewsPublisher {
    private List subscribers = new ArrayList<>();

    void subscribe(Subscriber sub) {
        subscribers.add(sub);
    }

    void notifySubscribers(String news) {
        for (Subscriber sub : subscribers) {
            sub.update(news);
        }
    }
}

class EmailSubscriber implements Subscriber {
    public void update(String news) {
        System.out.println("Email received: " + news);
    }
}

27. Zoo Management System

Code Example

abstract class Animal {
    abstract void makeSound();
}

class Mammal extends Animal {
    void makeSound() {
        System.out.println("Mammal sound");
    }
}

class Bird extends Animal {
    void makeSound() {
        System.out.println("Bird chirp");
    }
}

class ZooKeeper {
    void feedAnimal(Animal animal) {
        System.out.println("Feeding animal");
    }
}

28. Builder Pattern

Code Example

class Car {
    private String color;
    private boolean hasSunroof;

    public static class Builder {
        private String color;
        private boolean hasSunroof;

        public Builder setColor(String color) {
            this.color = color;
            return this;
        }

        public Builder setSunroof(boolean hasSunroof) {
            this.hasSunroof = hasSunroof;
            return this;
        }

        public Car build() {
            return new Car(this);
        }
    }

    private Car(Builder builder) {
        this.color = builder.color;
        this.hasSunroof = builder.hasSunroof;
    }
}

29. Parking Lot System

Code Example

class ParkingSpot {
    private boolean isOccupied;

    public void occupy() {
        isOccupied = true;
    }

    public void vacate() {
        isOccupied = false;
    }
}

class Vehicle {
    private String licensePlate;
}

class ParkingLot {
    private List spots;

    public ParkingLot(int capacity) {
        spots = new ArrayList<>();
        for (int i = 0; i < capacity; i++) {
            spots.add(new ParkingSpot());
        }
    }

    public ParkingSpot findSpot() {
        for (ParkingSpot spot : spots) {
            if (!spot.isOccupied()) return spot;
        }
        return null;
    }
}

30. Scalable Notification System

Code Example

abstract class Notification {
    abstract void send(String message);
}

class EmailNotification extends Notification {
    void send(String message) {
        System.out.println("Email sent: " + message);
    }
}

class SMSNotification extends Notification {
    void send(String message) {
        System.out.println("SMS sent: " + message);
    }
}

class NotificationService {
    public void notifyUser(Notification notification, String message) {
        notification.send(message);
    }
}