Debunking Object-Oriented Programming

Debunking Object-Oriented Programming

ยท

4 min read

23|09|2023

It is Day 5, today, in my coding interview preparation. I will write about programming paradigms and specifically object-oriented programming today.

Let us understand programming paradigms. We will try to understand what is a programming paradigm. How many different types of programming paradigms are available out there? Also, we will briefly look into each paradigm before we delve deep into OOP. We will also understand why OOP is important from a coding interview perspective.

What is a programming paradigm?

A programming paradigm is a set of principles, and concepts that govern how we structure our code while solving a specific problem.

Imagine we are solving a problem. We want to write a program that identifies if a given number is a prime or not.

In C language which is a procedural language we would structure our code in the following way:

#include<stdio.h>
#include<conio.h>
isPrime(int num);
  void main(){
     int a;
     scanf("Enter the number %d\n", &a);
     isPrime(a);
  }
  bool isPrime(int num){
     //logic to identify it is prime or not goes here...
  }

Similarly, we would structure our code in Java in the following way:

import java.util.*;

class MainClass {
  public static void main(String args[]){
       int num = sc.nextInt();
       IsPrime numObj = new IsPrime();
       numObj.isPrime(num);
  }
}
class IsPrime {
    public boolean isPrime(int num){
    //Logic to identify prime or not goes here.
  }
}

Since C is procedural language we have made use of functions to structure our code to solve the problem of identifying prime. Similarly, classes and objects are used in Java programming language to solve the same problem because it is an object-oriented programming language.

NOTE: If a language identifies itself as a certain paradigm language, it is a good practice to obey the paradigm while structuring the code however it is not a necessity. Whenever we think of Java, we associate it with the Object-oriented paradigm. Similarly, we tend to see C as a procedural language. Most of the modern programming languages are not strictly bound to a certain paradigm. It provides flexibility.

Time to understand OOP

As we discussed earlier, object-oriented programming is a programming paradigm in which we structure our code and organize our code using classes and objects.

Class is a template with the help of which we create objects. It is a collection of variable declarations and method definitions. If we talk about objects, they are instances of classes.

Advantages of OOP

  1. Modularity

  2. Reusability

  3. Abstraction

  4. Encapsulation

  5. Flexibility and Extensibility

  6. Code Organization

  7. Collaborative development

  8. Improved Debugging

  9. Real World Modeling

  10. Security

  11. Standardization

Main pillars of OOP

25|09|2023

Inheritance, Polymorphism, Abstraction and Encapsulation are called the four main pillars of Object Oriented Programming because they allow developers to write modular, reusable, extensible, maintainable and readable code while building software systems.

Let us have a look at each one of them. Let's begin with Inheritance.

  1. Inheritance:

    • Fact: Inheritance is a mechanism that allows one class (the derived or child class) to inherit properties and behaviors (attributes and methods) from another class (the base or parent class).

    • Contribution: Inheritance promotes code reuse by allowing developers to create new classes based on existing ones, inheriting their attributes and methods. This helps in modeling real-world relationships and building hierarchical class structures.

  2. Polymorphism:

    • Fact: Polymorphism enables objects of different classes to respond to the same method or function call in a way that is specific to their individual types.

    • Contribution: Polymorphism enhances flexibility and code readability. It allows you to write code that can work with objects of different types as long as they implement a common interface or have a common ancestor. This is achieved through method overriding and interfaces, ensuring that code can handle diverse object types seamlessly.

  3. Abstraction:

    • Fact: Abstraction is the process of simplifying complex systems by breaking them down into smaller, more manageable parts.

    • Contribution: Abstraction allows you to define classes and objects that hide unnecessary details while exposing only essential features and behaviors. It simplifies code and promotes modularity, making it easier to understand and maintain. Abstraction also helps in creating models that closely resemble real-world entities.

  4. Encapsulation:

    • Fact: Encapsulation involves bundling data (attributes) and the methods (functions) that operate on that data into a single unit called a class.

    • Contribution: Encapsulation provides data hiding and protection by restricting direct access to an object's internal state. It enforces controlled access to data through methods, typically getters and setters. This helps in maintaining data integrity, reducing the risk of unintended modifications, and promoting better control over data access and validation.

That's a wrap by the way. It was Sunday yesterday so I spent my whole day working on building my project so didn't write about anything yesterday. I revised the pillars of OOP today on day 7 of my #180daysDSA.

See you tomorrow with the next topic in preparation for the coding interview. See ya! ๐Ÿ‘‹

ย