Object-Oriented Programming Concepts
Published: 2024-05-12 19:04:25

---
title: Object-Oriented Programming Concepts
published: true
date: 2024-04-08 18:13:37 UTC
tags: 
canonical_url: https://blog.saurabhmahajan.com/object-oriented-programming-concepts
---

Object-Oriented Programming (OOP) has emerged as a powerful paradigm in the world of software development, revolutionizing the way we design and build applications. At its core, OOP is a approach that models real-world concepts by creating objects that encapsulate both data and the code that operates on that data.

The beauty of OOP lies in its ability to create modular, reusable, and extensible code. By organizing software into objects, programmers can break down complex problems into smaller, more manageable components, each with its own well-defined responsibilities and behaviors.

OOP concepts have been instrumental in the development of robust, scalable, and maintainable software systems across various domains, from web applications and mobile apps to games and scientific simulations. By embracing OOP principles, programmers can create code that is not only functional but also easier to understand, modify, and extend over time.

In this blog, we'll explore the intricacies of OOP concepts, providing examples and real-world use cases to help you grasp their significance and practical applications. Whether you're a beginner or an experienced programmer, mastering OOP will undoubtedly enhance your ability to write clean, efficient, and future-proof code.

* * *

# Why is OOPs required?

1. **Modularity** : OOPs promotes modular design, where the program is divided into smaller, reusable components (objects), making it easier to develop, maintain, and modify the code.

2. **Code Reusability** : Through inheritance, objects can reuse code from existing classes, reducing redundancy and promoting code reuse.

3. **Data Abstraction** : OOPs allows for data abstraction, which means that the implementation details of an object are hidden from the outside world, providing better security and flexibility.

4. **Encapsulation** : By encapsulating data and methods within objects, OOPs provides better control over data access and modification, improving code organization and security.

5. **Polymorphism** : Polymorphism allows objects of different classes to be treated as objects of a common superclass, enabling more flexible and extensible code.

6. **Maintainability** : OOPs promotes modularity, which makes it easier to identify and fix errors, as well as add new features or modify existing ones, improving code maintainability.

7. **Real-world Modeling** : OOPs provides a way to model real-world objects and their interactions, making it easier to understand and design complex systems.

* * *

# Class

A class is a blueprint or template for creating objects. It defines a set of properties (attributes) and behaviors (methods) that objects of that class will have. Classes are the fundamental building blocks of Object-Oriented Programming (OOP).

A class is a user-defined data type that encapsulates data (attributes) and code (methods) that operate on that data.

Example:-

```
public class BankAccount { // Attributes (Properties) private String accountNumber; private String accountHolderName; private double balance; // Constructor public BankAccount(String accountNumber, String accountHolderName, double initialBalance) { this.accountNumber = accountNumber; this.accountHolderName = accountHolderName; this.balance = initialBalance; } // Methods (Behaviors) public void deposit(double amount) { balance += amount; System.out.println( "Deposited $" + amount + ". New balance: $" + balance); } public void withdraw(double amount) { if (balance >= amount) { balance -= amount; System.out.println( "Withdrew $" + amount + ". New balance: $" + balance); } else { System.out.println("Insufficient funds."); } } // Getter and Setter methods for attributes // ...}
```

In this example, the `BankAccount` class has three attributes (`accountNumber`, `accountHolderName`, and `balance`) that define the state of a bank account object. It also has a constructor that initializes these attributes when creating a new `BankAccount` object. Additionally, the class has two methods (`deposit()` and `withdraw()`) that define the behavior of a bank account object.

**Components of a Class:**

1. **Modifiers** : These specify the access level of the class, such as `public`, `private`, `protected`, or default (no modifier).

2. **Class Name** : The name of the class, following the naming conventions (e.g., `BankAccount`).

3. **Superclass (if any)**: If the class inherits from another class, the name of the superclass is specified using the `extends` keyword.

4. **Interfaces (if any)**: If the class implements one or more interfaces, they are specified using the `implements` keyword.

5. **Body** : The class body is enclosed within curly braces `{ }`, containing the class members (attributes and methods).

* * *

# Object

An object is an instance of a class. It is a real-world entity that has its own state (attributes) and behavior (methods) defined by the class it belongs to.

An object is a single instance of a class that encapsulates data and the code that operates on that data.

Example:-

```
BankAccount account1 = new BankAccount("123456789", "John Doe", 1000.0);BankAccount account2 = new BankAccount("987654321", "Jane Smith", 5000.0);account1.deposit(500.0); // Output: Deposited $500.0. New balance: $1500.0account2.withdraw(1000.0); // Output: Withdrew $1000.0. New balance: $4000.0
```

In this example, `account1` and `account2` are two separate objects of the `BankAccount` class, each with its own set of attribute values (`accountNumber`, `accountHolderName`, and `balance`). We can invoke the `deposit()` and `withdraw()` methods on these objects, and they will behave according to their respective states.

**Components of an Object:**

1. **State** : The state of an object is defined by its attributes or properties (e.g., `accountNumber`, `accountHolderName`, and `balance` in the `BankAccount` class).

2. **Behavior** : The behavior of an object is defined by its methods (e.g., `deposit()` and `withdraw()` in the `BankAccount` class).

3. **Identity** : Each object has a unique identity, which is typically represented by its memory address or reference.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1712599278042/2d950434-cc5e-4e22-bfbd-8bd2edc9fe90.webp)

* * *

# Major Pillars of OOPs

## Abstraction

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1712599469196/64ce0685-ac6c-4bb7-b6b2-fcf7c035beb4.png)

Abstraction is the process of hiding unnecessary details and exposing only the essential features of an object. It allows you to define the interface (how an object should behave) without revealing the implementation details (how the object works internally).

Abstraction is a way of representing complex real-world entities in a simplified manner by focusing on their essential features and ignoring unnecessary details.

Example:-

```
public class Main { public static void main(String[] args) { Animal dog = new Dog("Buddy"); System.out.println("Name: " + dog.getName()); // Output: Name: Buddy dog.makeSound(); // Output: Woof! }}abstract class Animal { private String name; public Animal(String name) { this.name = name; } public String getName() { return name; } public abstract void makeSound();}class Dog extends Animal { public Dog(String name) { super(name); } @Override public void makeSound() { System.out.println("Woof!"); }}
```

In this example, the `Animal` class is an abstract class that defines the common behavior of animals (e.g., having a name) but leaves the implementation of `makeSound()` to its concrete subclasses (e.g., `Dog`). The `Dog` class provides the implementation for the `makeSound()` method.

## Encapsulation

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1712599670417/a4131492-0d34-439b-8fac-a83116b02f24.jpeg)

Encapsulation is the bundling of data (properties) and methods (behaviors) together within a single unit, known as a class. It helps in achieving data abstraction and controlling the access to the object's internal state.

Encapsulation is the mechanism of binding data and the code that operates on that data into a single unit, known as a class. It restricts direct access to the object's internal state and provides controlled access through public methods.

Example:-

```
public class Main { public static void main(String[] args) { BankAccount account = new BankAccount(); System.out.println("Initial balance: " + account.getBalance()); // Output: Initial balance: 0.0 account.deposit(1000.0); System.out.println("Balance after deposit: " + account.getBalance()); // Output: Balance after deposit: 1000.0 account.withdraw(500.0); System.out.println("Balance after withdrawal: " + account.getBalance()); // Output: Balance after withdrawal: 500.0 account.withdraw(600.0); // Output: Insufficient funds. }}class BankAccount { private double balance; public double getBalance() { return balance; } public void deposit(double amount) { balance += amount; } public void withdraw(double amount) { if (balance >= amount) balance -= amount; else System.out.println("Insufficient funds."); }}
```

In this example, the `BankAccount` class encapsulates the `balance` field by making it private and providing public methods (`getBalance`, `deposit`, and `withdraw`) to access and modify the balance in a controlled manner.

## **Inheritance**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1712599682846/978869aa-c11b-470a-a5a0-7a56c1cb063e.png)

Inheritance is a mechanism that allows a new class (child or derived class) to be based on an existing class (parent or base class). The child class inherits properties and methods from the parent class, promoting code reuse and enabling the creation of hierarchical relationships.

Inheritance is a concept in OOP that allows a new class (derived class) to inherit properties and behaviors from an existing class (base class), forming an "is-a" relationship between the classes.

Example:-

```
public class Main { public static void main(String[] args) { Vehicle vehicle = new Vehicle("Toyota", "Corolla"); vehicle.start(); // Output: Vehicle started. Car car = new Car("Honda", "Civic", 4); car.start(); // Output: Vehicle started. car.openTrunk(); // Output: Trunk opened. }}class Vehicle { protected String make; protected String model; public Vehicle(String make, String model) { this.make = make; this.model = model; } public void start() { System.out.println("Vehicle started."); }}class Car extends Vehicle { private int numDoors; public Car(String make, String model, int numDoors) { super(make, model); this.numDoors = numDoors; } public void openTrunk() { System.out.println("Trunk opened."); }}
```

In this example, the `Car` class inherits from the `Vehicle` class. It inherits the `make`, `model`, and `start()` method from the `Vehicle` class and adds its own `numDoors` attribute and `openTrunk()` method.

## Polymorphism

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1712599698185/6939d78c-fb2f-4c39-a17f-1140e16248cc.webp)

Polymorphism is a concept in OOP that allows objects of different classes to be treated as objects of a common superclass, enabling code reusability and flexibility. It can be achieved through method overriding (runtime polymorphism) or method overloading (compile-time polymorphism).

### **Runtime Polymorphism (Method Overriding)**

Runtime polymorphism, also known as method overriding, occurs when a subclass provides its own implementation of a method that is already defined in its superclass.

Example:-

```
class Animal { public void makeSound() { System.out.println("Animal sound"); }}class Dog extends Animal { @Override public void makeSound() { System.out.println("Woof!"); }}class Cat extends Animal { @Override public void makeSound() { System.out.println("Meow!"); }}public class Main { public static void main(String[] args) { Animal animal1 = new Animal(); Animal animal2 = new Dog(); Animal animal3 = new Cat(); animal1.makeSound(); // Output: Animal sound animal2.makeSound(); // Output: Woof! animal3.makeSound(); // Output: Meow! }}
```

In this example, the `Dog` and `Cat` classes override the `makeSound()` method of the `Animal` class, exhibiting runtime polymorphism. The `makeSound()` method behaves differently based on the actual object type (Animal, Dog, or Cat) at runtime.

Rules:-

- Methods of child and parent class must have the same name.

- Methods of child and parent class must have the same parameters.

- IS-A relationship is mandatory (inheritance).

- One cannot override the private methods of a parent class.

- One cannot override Final methods.

- One cannot override static methods.

### **Compile-time Polymorphism (Method Overloading)**

Compile-time polymorphism, also known as method overloading, occurs when a class has multiple methods with the same name but different parameter lists (different number of parameters, different types of parameters, or both).

Example:-

```
public class Main { public static void main(String[] args) { Calculator calculator = new Calculator(); int sum1 = calculator.add(2, 3); // Output: 5 double sum2 = calculator.add(2.5, 3.7); // Output: 6.2 int sum3 = calculator.add(1, 2, 3); // Output: 6 }}class Calculator { public int add(int a, int b) { return a + b; } public double add(double a, double b) { return a + b; } public int add(int a, int b, int c) { return a + b + c; }}
```

In this example, we create an instance of the `Calculator` class and call different overloaded versions of the `add()` method. The compiler determines the correct method to call based on the number and types of arguments provided, demonstrating compile-time polymorphism.

Types:-

- By changing number of parameters

- By changing data type of any parameter

- By changing sequence of parameters

* * *

# Minor Pillars of OOPs

## Typing

In a strongly typed language like Java, every variable and expression must have a well-defined data type. The compiler enforces type safety by ensuring that operations are performed only on compatible data types. This helps catch type-related errors at compile-time, making the code more robust and reliable.

Example:-

```
int age = 25; // Validage = "thirty"; // Compiler error: Incompatible typesString name = "John";int length = name.length(); // Valid
```

In the above example, the variable `age` is declared as an `int`, so you cannot assign a string value like `"thirty"` to it. The compiler will raise an error. However, you can call the `length()` method on the `String` object `name` and assign the result (an `int` value) to the `length` variable.

## Persistance

Persistence refers to the ability of an object to survive beyond the lifetime of the program or process that created it. In other words, persistent objects can be stored and retrieved from non-volatile storage, such as a file system or a database, without losing their state.

Example:-

Consider a `Student` class that represents a student's information:

```
public class Student { private String name; private int age; private double gpa; // Constructors, getters, and setters}
```

To make `Student` objects persistent, you can use serialization, which is the process of converting an object's state to a byte stream that can be saved to a file or sent over a network. Java provides built-in support for serialization through the `Serializable` interface.

```
import java.io.*;public class PersistenceExample { public static void main(String[] args) { Student student = new Student("John", 20, 3.8); // Serialize the object to a file try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("student.dat"))) { oos.writeObject(student); } catch (IOException e) { e.printStackTrace(); } // Later, deserialize the object from the file Student deserializedStudent = null; try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("student.dat"))) { deserializedStudent = (Student) ois.readObject(); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } // The deserializedStudent object has the same state as the original student object System.out.println(deserializedStudent.getName()); // Output: John System.out.println(deserializedStudent.getAge()); // Output: 20 System.out.println(deserializedStudent.getGpa()); // Output: 3.8 }}
```

In this example, a `Student` object is serialized to a file named `student.dat`. Later, the object is deserialized from the same file, and its state is preserved, allowing you to work with the same object even after the program has terminated and restarted.

## Concurrency

Concurrency refers to the ability of multiple computations or threads to execute simultaneously and potentially interact with each other.

In Java, you can create and manage threads to achieve concurrency. However, concurrent access to shared resources (e.g., objects or data structures) can lead to race conditions and other synchronization issues.

Example:-

Consider a `BankAccount` class that represents a bank account with a balance:

```
public class BankAccount { private double balance; public BankAccount(double initialBalance) { this.balance = initialBalance; } public synchronized void deposit(double amount) { double newBalance = balance + amount; // Simulate a delay to illustrate the potential for race conditions try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } balance = newBalance; } public synchronized void withdraw(double amount) { double newBalance = balance - amount; // Simulate a delay to illustrate the potential for race conditions try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } balance = newBalance; } public double getBalance() { return balance; }}
```

Now, let's create two threads that both try to deposit and withdraw money from the same `BankAccount` object:

```
public class ConcurrencyExample { public static void main(String[] args) { BankAccount account = new BankAccount(1000.0); Runnable depositTask = () -> { for (int i = 0; i < 1000; i++) { account.deposit(10.0); } }; Runnable withdrawTask = () -> { for (int i = 0; i < 1000; i++) { account.withdraw(10.0); } }; Thread depositThread = new Thread(depositTask); Thread withdrawThread = new Thread(withdrawTask); depositThread.start(); withdrawThread.start(); try { depositThread.join(); withdrawThread.join(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Final balance: " + account.getBalance()); }}
```

In this example, two threads (`depositThread` and `withdrawThread`) are created to perform 1000 deposit and withdrawal operations, respectively, on the same `BankAccount` object. Without proper synchronization, the balance may not be accurately updated due to race conditions.

To prevent race conditions, the `deposit()` and `withdraw()` methods in the `BankAccount` class are marked as `synchronized`. This ensures that only one thread can execute these methods at a time, preventing concurrent access to the `balance` field and ensuring data consistency.

When you run this program, the final balance printed should be 1000.0 (the initial balance), since the number of deposits and withdrawals is equal.

These examples illustrate how Java's strong typing, persistence capabilities, and concurrency support help in building robust and reliable applications. Strong typing enforces type safety, persistence allows objects to survive beyond program execution, and concurrency enables parallel execution while providing mechanisms to handle shared resources safely.

* * *

# Conclusion

Throughout this blog, we've explored the fundamental concepts that underpin Object-Oriented Programming (OOP). We've delved into the building blocks of OOP, such as classes and objects, and how they enable the creation of modular, reusable, and extensible code.

We've examined the principles of abstraction and encapsulation, which provide a structured approach to managing complexity and ensuring data integrity. Abstraction allows us to focus on the essential features of an object, while encapsulation protects an object's internal state from unintended modifications.

Inheritance, a powerful mechanism in OOP, enables code reuse and the creation of hierarchical class relationships. By inheriting properties and methods from existing classes, we can create more specialized classes that extend and enhance the functionality of their parent classes.

Polymorphism, both compile-time and runtime, adds flexibility to our code by allowing objects of different classes to be treated as objects of a common superclass. This versatility enables methods to work with a wide range of object types without needing to know their specific implementations.

We've also explored the concept of strong typing, which enforces type safety and helps catch errors during compilation, leading to more robust and reliable code.

Additionally, we've discussed persistence, which allows objects to persist beyond the lifetime of the program, enabling data storage and retrieval across multiple sessions.

Finally, we've touched upon concurrency, a crucial aspect of modern software development, where multiple threads or processes can execute simultaneously, facilitating efficient utilization of system resources and improving application responsiveness.

By mastering these OOP concepts and applying them effectively, developers can create software that is not only functional but also maintainable, scalable, and adaptable to changing requirements. OOP provides a solid foundation for building complex systems, fostering collaboration, and promoting code reuse across projects.

As we continue our journey in OOP, remember to embrace best practices, such as writing clean and self-documenting code, adhering to design patterns, and leveraging the power of object-oriented principles to create efficient and elegant solutions.