πŸ“– Object-Oriented Programming (OOP) Study Guide

1️⃣ What is OOP?

Object-Oriented Programming (OOP) is a way of designing and structuring code by grouping related data (attributes) and functions (methods) inside objects. It makes code easier to manage, reuse, and scale.

βœ… **Real-life Example:** Think of a **Car** as an object. It has:

  • Attributes (Data): Color, Model, Speed.
  • Methods (Functions): Start(), Stop(), Accelerate().

In OOP, we create objects based on **blueprints (Classes)**.

2️⃣ Core Principles of OOP

OOP is based on 4 main principles:

πŸ”Ή 1. Encapsulation (Data Protection)

Encapsulation means **hiding internal details** and allowing controlled access to data using methods.

βœ… **Example:** A **Bank Account** hides the balance, but you can access it using a method.


            class BankAccount {
                private double balance; // Hidden from direct access
                
                public void deposit(double amount) {
                    balance += amount;
                }
                
                public double getBalance() {
                    return balance; // Controlled access
                }
            }
                    

πŸ”Ή 2. Abstraction (Hiding Complexity)

Abstraction means **hiding unnecessary details** and showing only essential features.

βœ… **Example:** When you drive a **car**, you just press the acceleratorβ€” you don't need to know how the engine works internally.


            abstract class Animal {
                abstract void makeSound(); // Hides internal details
            }
            
            class Dog extends Animal {
                void makeSound() {
                    System.out.println("Bark!"); // Implementation shown to the user
                }
            }
                    

πŸ”Ή 3. Inheritance (Code Reusability)

Inheritance allows a new class (**child class**) to use properties and methods of an existing class (**parent class**).

βœ… **Example:** A **Car** inherits features from **Vehicle**.


            class Vehicle {
                String brand = "Toyota";
            }
            
            class Car extends Vehicle {
                String model = "Corolla";
            }
                    

πŸ”Ή 4. Polymorphism (One Name, Many Forms)

Polymorphism allows the same function to perform different tasks.

βœ… **Example:** A **remote control** has one button, but it can operate a **TV, AC, or Fan**.


            // Method Overriding (Different behavior for same method)
            class Animal {
                void makeSound() {
                    System.out.println("Some sound...");
                }
            }
            
            class Cat extends Animal {
                void makeSound() {
                    System.out.println("Meow!");
                }
            }
                    

3️⃣ More OOP Concepts

πŸ”Ή Method Overloading (Multiple Functions with the Same Name)

Allows a method to have different versions.


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

πŸ”Ή Method Overriding (Changing Behavior of a Parent Class Method)

The child class provides a new version of the parent class method.


            class Parent {
                void show() {
                    System.out.println("Parent class");
                }
            }
            
            class Child extends Parent {
                void show() {
                    System.out.println("Child class");
                }
            }
                    

πŸ”Ή Interfaces and Abstract Classes

Interfaces define a contract for classes, and abstract classes provide partial implementations.


            interface Vehicle {
                void start();
            }
            
            class Bike implements Vehicle {
                public void start() {
                    System.out.println("Bike is starting...");
                }
            }
                    

4️⃣ Common OOP Design Patterns

πŸ”Ή Singleton Pattern (Only One Instance of a Class)

Ensures a class has **only one object** throughout the program.


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

πŸ”Ή Factory Pattern (Creating Objects without Exposing the Logic)

Creates objects based on input without revealing the actual class used.


            class ShapeFactory {
                static Shape getShape(String type) {
                    if (type.equals("Circle")) return new Circle();
                    if (type.equals("Square")) return new Square();
                    return null;
                }
            }
                    

5️⃣ SOLID Principles (Good OOP Practices)

πŸ”Ή Single Responsibility Principle (SRP)

A class should have **only one reason to change**.


            class Invoice {
                void calculateTotal() {}
            }
            
            class InvoicePrinter {
                void printInvoice(Invoice invoice) {}
            }
                    

πŸ”Ή Open-Closed Principle (OCP)

A class should be **open for extension but closed for modification**.


            interface Payment {
                void pay();
            }
            
            class CreditCard implements Payment {
                public void pay() {
                    System.out.println("Paid with Credit Card");
                }
            }
                    

6️⃣ Conclusion

OOP makes programming **easier to understand, maintain, and reuse**. Mastering OOP principles helps build scalable and efficient applications.

πŸ”— Next Topics