OOPS! It’s OOPs.

Vidhanshu Borade
GDSC GHRCE
Published in
7 min readJan 2, 2022

What do you mean by object-oriented programming?

Some of you might have coded in the C programming language, in which you first include header files in the program and then create the main function. It is a must in C++ object-oriented programming language as well, then you construct the program inside main, where you create variables and outside main you create some functions which can manipulate those variables. This type of programming approach is called procedural programming.

We Also have a new programming method called Object-Oriented Programming, abbreviated as OOPs. In this method, we use a user-defined data type called Class.

Object-Oriented Programming as the name implies is a language that uses Objects in programming. You might be confused with what this Object is? Whose Object it is? No worries, I will be covering this later in the blog. Grab your Popcorn box and enjoy reading!

The main aim of OOPs is to bind the data and the methods which can manipulate those data together so that data would only be accessible to those methods only and no other part of the program could manipulate it. OOPs help us to implement real-world entities like inheritance, polymorphism, data abstraction, encapsulation, etc. in programming which we will learn about further in the blog. OOPs, work on the principle of DRY.

What do you mean by dry?

DRY means Don’t Repeat Yourself. Simply, instead of writing the same code many times, just write it in one place and use it many times.

What is a Class and an Object?

Class is nothing but a data type like int, float, etc. But, it is a user-defined data type, we can define it according to our needs. The class helps us to bind variables and functions, that can manipulate the variables. And an Object is the instance of the Class. Didn’t understand what an object is? Let’s understand with a live example, let’s consider fruits to be a class then apple is an object of the class fruit. So eventually object is nothing but a variable of the data type class, for which it is created.

Using class, you can implement the real-world entity, let’s suppose you are building a system to manage the employee details like employee id, employee address, employee mobile number, etc. So far that you are not going to create all these variables separately as follows (I’m using c++ here)

Example:

string employeeName;int employeeId;string employeeAddress;int employeeMobileNumber;

since all these data have different data types you can’t use an array. Alternatively, you can create a template using a class like follow:

class employee {    public:        string employeeName;        int employeeId;        string employeeAddress;        int employeeMobileNumber;};

The above template is a Class. Which helps us to bind variables having different data types in a single place. Now you might have a doubt. What about functions?

You can create a function inside the class that can be used to manipulate or change the values of the variables or simply use those variables.

Example:

class employee{public:       string employeeName;       int employeeId;       string employeeAddress;       int employeeMobileNumber;       void getName() //function to get employee name from user       {              cout << " Enter employee name:";              cin >> employeeName;        }       void printName() //function print the employee name        {              cout << " The employee name is " << employeeName;        }};

The above class has a function getName() and printName() which can be used to get the name of the employee as user input and print the name of the employee respectively. Now with the above class, you can create your variables called objects, which can be used to access data and functions inside the class.

(Note: using objects you can only access public data outside the class, private and protected data cannot be accessed).

Now for the above class, we can create object as follows:

employee obj1; //creating objectobj1.getName(); //calling getName functionobj1.printName(); //calling the printName function

The above code will simply take the employee name as input from the user and print the name entered by the user. If you have coded in C programming language and used structure in C language you can relate structure with class.

Simple, right? Now let’s move on…

What makes oops so important in programming?

Most of you might be wondering as to why take so much interest in learning Objects and Classes. The simple reason is that OOPs enable users to create modules that remain as it is even if we include more data types in the program. It also provides features like inheritance, data abstraction, encapsulation, and polymorphism which make the four pillars of OOPs.

OOPs allows software engineers to break down large software into various modules which can be easy to understand and create the software with ease.

Four pillars of Object-Oriented Programming:

four pillars of OOPs

1) Encapsulation: In OOPs, Data encapsulation means to bind the data and the functions/methods which are made to manipulate data, together.

class (example of encapsulation)

2) Abstraction: Abstraction means showing only essential data and hiding the nonessential data from the outside world of that class. For example, When you buy a car, you know how to drive it, you can see the steering wheel, accelerator, etc. but you are not told how this accelerator works, or how the engine works.

Data abstraction

3) Inheritance: Inheritance is one of the core concepts of object-oriented programming. Inheritance means to derive attributes and methods from one class to another class. The class from which the attributes and methods are getting derived is called the parent class, and the class in which these attributes and methods are getting derived is called a derived class. Inheritance work on the principle of code reusability.

inheritance

4) Polymorphism: When you break the word into two parts, “poly” means “many” and “morphism” means “forms” giving it a complete meaning of “many forms”. A real-time example of polymorphism is, a person can be a father, son, and husband at the same time, so here a person has many forms depending upon the situation.

polymorphism

What are the uses of these four pillars in oops?

A) Using OOPs it is easy to model real-world problems as each object can be viewed as a real-world entity having some attributes and methods.

B) Using inheritance, we can reuse the existing class to derive a new class such that unnecessary code is eliminated and the use of the existing class is extended. This helps to save time and costs for the program.

C) In OOP, data can be made private to a class so that only member functions of the class can access the data. This principle of data hiding helps to hide the data to the outside of a class so that it won’t get mistakenly changed by the programmer.

D) Using polymorphism, the same function or same operator can be used for different purposes. This helps to manage the complexity of software easily.

E) Large problems can be reduced into smaller modules and more manageable problems. It is easy to divide the work into a project based on objects.

Let’s see some real-life examples of OOPs:

  • It is used to create various Real-Time Systems like real-time operating systems.
  • It is used in software development
  • It is used to create user interface designs such as windows, menus.
  • It is also used in the fields of Simulation and Modelling
  • It is used to create object-oriented databases
  • It is used to create AI and Expert Systems
  • It is used to create Neural Networks and parallel programming
  • It is also used in creating office automation systems etc.

Your Learnings:

1. What is OOPs?

2. Why use OOPs and what is its importance in the software field?

3. What are the real-life examples of OOPs?

The motive of this article is to introduce you to object-oriented programming and to tell you how important it is to study object-oriented programming while creating software as well as projects. I hope this article has helped you to better understand the concept of object-oriented programming and its need in the software field.

Thank you for reading 😊

--

--