# OOP Concepts  (Using Getters and Setters)

In **Object-Oriented Programming (OOP)**, concepts such as **encapsulation**, **abstraction**, and **data hiding** are essential for designing maintainable and scalable systems. Let's expand these ideas using a simple e-commerce platform example.

#### 1\. **Encapsulation**

**Encapsulation** refers to bundling data and methods that operate on that data into a single unit, usually a class. In an e-commerce platform, a class representing a **Product** would encapsulate details like product name, price, and stock, and provide methods to access and modify those details safely.

##### Example: Product Class

```java
public class Product {
    private String name;
    private double price;
    private int stock;

    // Getter for product name
    public String getName() {
        return name;
    }

    // Setter for product name
    public void setName(String name) {
        this.name = name;
    }

    // Getter for product price
    public double getPrice() {
        return price;
    }

    // Setter for product price
    public void setPrice(double price) {
        if (price > 0) {
            this.price = price; // Price should be positive
        }
    }

    // Getter for stock
    public int getStock() {
        return stock;
    }

    // Setter for stock
    public void setStock(int stock) {
        if (stock >= 0) {
            this.stock = stock; // Stock cannot be negative
        }
    }
}
```

**Encapsulation** ensures that:

* Data like product price or stock is kept **private** and cannot be directly modified from outside the class.
    
* Only public methods (getters and setters) can access or change this data. This ensures safe, controlled changes. For example, a product price cannot be negative because the setter method checks for it before updating.
    

#### 2\. **Abstraction**

**Abstraction** means hiding the complex details of how things work and showing only the necessary parts. In an e-commerce system, users don’t need to know how the system calculates taxes or discounts—they just need to see the final price.

For instance, when we have a method to apply a discount on the product price, users don’t need to know how the discount is calculated. The **Product** class can handle that internally using its methods, hiding the complexity from the user.

##### Example: Applying a Discount

```java
public void applyDiscount(double discountPercentage) {
    if (discountPercentage > 0 && discountPercentage <= 100) {
        double discountAmount = (price * discountPercentage) / 100;
        price -= discountAmount;
    }
}
```

Here, the user can call `applyDiscount(10)` to give a 10% discount on the product, but they don’t need to understand how the discount is calculated. This keeps the internal working hidden, promoting **abstraction**.

#### 3\. **Data Hiding**

**Data Hiding** is a principle where data is kept private and protected from direct access. This prevents users or other classes from accidentally or intentionally altering sensitive data. In our example, the `price` and `stock` are private fields. These fields can only be modified through controlled methods like `setPrice()` and `setStock()`.

By hiding the internal data, you:

* **Prevent Invalid Changes**: For example, setting a negative price is disallowed in the `setPrice()` method.
    
* **Ensure Data Integrity**: The product's `stock` cannot go below zero because the setter checks this.
    

This is crucial in e-commerce, where incorrect data could result in customers paying the wrong amount or products going out of stock while still being listed for sale.

##### Example: Stock Management

```python
public void reduceStock(int quantity) {
    if (quantity > 0 && stock >= quantity) {
        stock -= quantity; // Reduce stock if there is enough
    } else {
        System.out.println("Not enough stock");
    }
}
```

This method hides the complex logic of how stock is managed and ensures that the internal state of `stock` remains consistent.

#### 4\. **Modularity**

**Modularity** refers to dividing a program into smaller, independent modules (or classes). In an e-commerce system, you would have separate classes for different parts of the platform like **Product**, **Customer**, **Order**, etc. Each class is responsible for a specific part of the system, making it easier to develop, test, and maintain.

For example, the `Product` class handles everything related to products, and the `Customer` class handles customer-related data. These independent modules interact with each other but remain self-contained. If you need to update the product-related functionality, you can do so without affecting the customer-related functionality.

---

**Getters** and **setters** in Java help enforce important OOP concepts in e-commerce systems:

* **Encapsulation**: Keeps product data secure and manageable by bundling it within a class.
    
* **Abstraction**: Simplifies complex operations like discount calculation for users.
    
* **Data Hiding**: Ensures sensitive product details are not directly accessible, protecting the integrity of the data.
    
* **Modularity**: Allows for a clear, organized structure, making the system easier to maintain.
    

These OOP concepts make software systems like e-commerce platforms more reliable, scalable, and easier to maintain over time​([W3Schools.com](https://www.w3schools.com/java/java_encapsulation.asp))​([FavTutor](https://favtutor.com/blogs/java-getters-and-setters))​([Tutorials Freak](https://www.tutorialsfreak.com/java-tutorial/java-encapsulation)).
