4 Pillars of OOP
Published: 2023-11-30 04:46:26

Java, is known for its robustness and cross-platform capabilities, stands as a pillar in the world of programming languages. Important to its design is Object-Oriented Programming (OOP), something that structures software design around data, or objects, rather than functions and logic. Understanding the four fundamental pillars of OOP—Inheritance, Encapsulation, Abstraction, and Polymorphism—is crucial for anyone looking to master Java.

1. Inheritance:

Inheritance is a mechanism that allows one class, known as a subclass, to inherit the attributes and methods of another class, referred to as a superclass. This feature fosters reusability and a hierarchical classification. For instance, consider a vehicle class that includes general attributes like speed and color. A car class can inherit these attributes while introducing specific features like numberOfDoors

```

class Vehicle {
    String color;
    int speed;
    // Vehicle class methods
}

class Car extends Vehicle {
    int numberOfDoors;
    // Car class methods
}


```

In this example, car inherits color and speed from vehicle and introduces its unique attribute numberOfDoors.

2. Encapsulation:

Encapsulation, often described as "data hiding," is the bundling of data and the methods that operate on that data. It restricts direct access to some of an object's components, which is a protective barrier that prevents the data from being randomly accessed. In Java, this is achieved through access modifiers like private, public, and protected.

```
class Account {
    private double balance;

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

    public double getBalance() {
        return balance;
    }
}

```

Here, the balance variable is encapsulated within the account class. It's not accessible directly from outside the class and can only be modified through the deposit method.

3. Abstraction:

Abstraction simplifies complex reality by modeling classes appropriate to the problem, and working at the most relevant level of inheritance for a particular aspect of the problem. This concept focuses on hiding the internal implementation and only showing the necessary features of an object.

Imagine we are modeling a payment system. In this system, we want to abstract the concept of a payment without worrying about the specifics of each payment type.

```
abstract class Payment {
    abstract void makePayment(double amount);
    // Other common payment methods and properties
}

class CreditCardPayment extends Payment {
    
    void makePayment(double amount) {
        // Implementation for credit card payment
        System.out.println("Paying " + amount + " using Credit Card.");
    }
}

class PayPalPayment extends Payment {
    
    void makePayment(double amount) {
        // Implementation for PayPal payment
        System.out.println("Paying " + amount + " using PayPal.");
    }
}

```
In this example, Payment is an abstract class that defines a method makePayment(). Different subclasses like CreditCardPayment and PayPalPayment provide their specific implementations of the makePayment() method. The user of these classes doesn't need to know the details of how each payment method works; they just need to know that they can call makePayment() on any payment object.


4. Polymorphism:

Polymorphism allows objects to be treated as instances of their parent class rather than their actual class. The two primary forms of polymorphism in Java are compile-time (method overloading) and runtime (method overriding).

```
class Bird {
    void sing() {
        System.out.println("Bird is singing");
    }
}

class Sparrow extends Bird {
    void sing() {
        System.out.println("Sparrow is singing");
    }
}

class Main {
    public static void main(String[] args) {
        Bird myBird = new Sparrow();
        myBird.sing();  // Output: Sparrow is singing
    }
}

```
In this scenario, a Sparrow object is treated as a Bird object. The sing method of Sparrow is invoked, demonstrating runtime polymorphism.


The four pillars of OOP in Java - Inheritance, Encapsulation, Abstraction, and Polymorphism - are very important in Java. They not only enhance the usability and flexibility of code but also create the way for a more structured and maintainable codebase. As you dig deeper into Java, these concepts will become integral to your understanding and application of this programming language.