Introduction to Object Oriented Programming (1/3)

Dhruv UniyalDhruv Uniyal
7 min read

Object Oriented Programming (or OOP in short) is a programming paradigm that emphasizes the reusability of code. In this article, we take a look at what the various programming paradigms are and develop our understanding of how OOP is used to write clean and modular programs and improve the reusability of code in the program.

What is a "Programming Paradigm"?

A Programming Paradigm defines the style in which programs are written. There are a few languages that allow multiple styles of writing programs. On the other hand, there are a few programming languages, that strictly follow a specific paradigm, and facilitate that kind of programming more than other programming languages.

There are a few programming paradigms that are commonly used. They are

  • Imperative Programming

  • Procedural Programming

  • Declarative Programming

  • Functional Programming

  • Object-oriented Programming

In this article, we shall look at what these programming paradigms are, and then understand why the need for a new paradigm (OOP) was introduced.

Imperative Programming

Imperative Programming is the oldest and the most basic approach in programming. In this paradigm, the code describes a step-by-step process for the execution of a program. Each step contains a single instruction, which can be an individual statement or a function call. For example, let us take a simple program that takes in 5 numbers as input, and then displays the sum of the numbers to the user.

The pseudocode for this program using this paradigm would look something as shown

Declare an array of size 5
Take input & store it in the array
Initialise a variable sum to 0
Initialise a loop to iterate over the array
For each iteration, add the value at the index in the array to the sum
Display the output to the user

As it can be seen from the example, it is a time-consuming process. If we needed to find the sum of multiple arrays, we would have to re-write the code for each array. For larger programs, this becomes increasingly complex.

Many languages support this paradigm. These programming languages are typically older ones, such as FORTRAN, BASIC and C.

Procedural Programming

Procedural Programming is a type of Imperative Programming, where the instructions are combined to create procedures. Procedures are similar to functions, both being subroutines that are used to re-execute a block of code. While a function usually returns a value, a procedure does not. Writing a program using an Imperative style required us to rewrite the same piece of code each time we needed to execute something. In procedural programming, we turn this piece of code into a procedure and then call the procedure to execute the code instead of rewriting it.

For example, to calculate the sum of an array, we do the following

Procedure Add:
    Declare an array of size 5
    Take input & store it in the array
    Initialise a variable sum to 0
    Initialise a loop to iterate over the array
    For each iteration, add the value at the index in the array to the sum
    Display the output to the user

To execute, we simply call the Add procedure which then declares an array, takes input, calculates the sum and outputs it to the user.

Declarative Programming

Declarative Programming is a way of programming in which a step-by-step approach is not followed. Instead, a set of higher-order methods are used to specify what the end result should be. The code just describes what the desired outcome of the program is, and lets the compiler/interpreter figure out how that outcome is produced. This is a very high-level programming concept, and is usually used in Database Management Systems (DBMS) or markup languages such as HTML.

SQL is a popular language that uses declarative programming. For example, if we wish to return the records from a database, we simply use the command

select * from table_name

We do not specify how to retrieve the records from the table, instead, we simply use the select statement and let the interpreter decide the approach to complete the task.

To perform the same task that we have done in the previous examples, the declarative approach would be

Declare Array
Take input
Add array
Show output

Functional Programming

Functional Programming is an extension of the Declarative approach. In this method, we make use of a few higher-order functions that are part of a programming language to achieve a specific result. These functions are declared in the programming language, and hence are abstract functions - meaning we don't know how the function is implemented internally. For example, functions such as sum() to find the sum of a list, or sort() to sort a list in Python.

To add an array using Functional Programming, the following code can be used

Declare Array
Take input
Add(array) //Add function takes care of displaying the output to the user.

Difference between Declarative and Imperative Programming

Imperative Programming focuses on how to achieve a result from a code, whereas Declarative Programming focuses on what to achieve as a result. To better understand the differences, let us assume that a person, say Jack, wants to have pizza for dinner. For Jack to have a pizza, he can do two things. He can either call a fast food outlet and order a pizza for himself, or he can follow a set of instructions and make a pizza for himself.

In this scenario, calling a pizza store and ordering a pizza is similar to the declarative approach. Jack doesn't worry about how the pizza is being made, he just leaves the task up to the store. Similarly, Jack following instructions and making a pizza for himself is similar to the imperative approach.

But, which of these two approaches is better?

There is no specific answer to this question. Jack can order a pizza if he wishes to have a quick meal, or he can make his own pizza if he wishes to have complete control over how the pizza is being made. Making a pizza by yourself might seem a lengthy approach, but sometimes it might be what you are looking for. Similarly, there is no clear better approach among the two programming paradigms. While the declarative approach makes the code a lot cleaner and decreases the number of lines of code being written, the programmer has no control over the logic which is being used to execute the program. Similarly, though using an imperative style greatly increases the number of lines of code being written, it gives the programmer complete control over how they want the logic of the program to work. Which approach to use solely depends on the conditions in which the code is being written.

There are languages nowadays that support both the programming paradigms, giving programmers more freedom to write code in a style that best suits the requirements. Examples of these programming languages are C++, Java and Python.

What is Object Oriented Programming, and why is it used?

Object Oriented Programming is another type of Imperative Programming. As we have seen, all the different approaches emphasize the result of a program. We have not yet discussed an approach that prioritizes data abstraction (hiding data, in simpler terms). For larger programs where keeping data secured is crucial, all the approaches discussed so far are not suitable, as they provide very little in terms of hiding data.

This is where Object Oriented Programming comes into the picture.

Object Oriented Programming, or OOP is a programming paradigm that allows programmers to enclose data and the functions that operate on the data into a single entity, which is known as a Class. This class now acts as a blueprint, which can be used to create instances of it known as Objects. Classes enforce abstraction by separating the implementation and the interface so that the users do not know how the program is running internally, but can still use the program. Classes also allow us to create multiple instances of itself, meaning that this approach increases the reusability of code in our program. As a result, programming using OOP principles results in a modular, flexible, reusable and abstract program.

We will look at the basics and the principles of OOP in the next parts of this article. Stay tuned!

3
Subscribe to my newsletter

Read articles from Dhruv Uniyal directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Dhruv Uniyal
Dhruv Uniyal