In this article, we’ll learn about Object Oriented Programming (OOP) which is a programming paradigm well known for ages now. This tutorial will cover the basic concepts which enable programming to function in an object-oriented manner. After understanding the concepts, we’ll have a brief look at the popular languages which provide the ease of implementing this programming paradigm. Throughout this tutorial, we’ll use C++ syntax to demonstrate the examples where necessary. Without further ado, let’s jump on understanding what OOP really is!

This tutorial is the first of the two-part series that will cover this entire topic in detail. In this part, we’ll discuss about Object Orient Programming and some of its core concepts. In the second part, we’ll look at how to create classes and objects in C++.

What is Object Oriented Programming?

OOP, short for Object Oriented Programming, is an approach in programming that revolves around the concepts of classes and objects. Most of the modern programming languages follow OOP principles. It allows the developers to structure the code such that more functionality is achieved using minimal duplication of the code logic. It provides an environment to interact with the code in terms of groups (called objects) where these groups are defined in accordance with common behaviors and features. Too much jargon and technicalities? Don’t worry, we’ll revisit and explore this using real world examples as well.

What are Classes?

In Object Oriented Programming, classes are the templates or groups which we use again and again to generate entities which belong to this group. It is similar to a blueprint where we define and preserve the structure of an entity. In C++, we use a reserved keyword class to define any class.

What are Objects?

Where classes define a blueprint, objects are the entities which are created using these classes or blueprints. They embed the class properties in them and function according to the set of rules the class defines for them. In other words, objects are instances of a class. Moreover, we can create multiple objects from a single class.

If you wish to read more about classes and objects then please see our detailed tutorial here.

Why Object Oriented Programming?

Think of a car. What properties/attributes and functionalities of a car you can think of? A car can indeed have various properties that you might have thought as well but let’s just stick with a few common ones. A car has a color, plate number, company name, engine configuration, etc. These are certain attributes that belong to a car. In other words, these properties or attributes define a car. In addition to these properties, a car can execute certain functionalities as well. For instance, a car can change its speed, play a horn, refuel itself, etc. 

All the properties and functionalities discussed above belong to a car. If a developer has to code this scenario using procedural programming, he or she will probably code it as follows.

/* The following code piece is a C++ code. */
// Car's Attributes
string carColor;
string carPlateNumber;
string carCompany;
string carEngine;
// Car's Functionalities
changeCarSpeed()
{
  // some code to change car's speed
}
playCarHorn()
{
  // some code to play car's horn
}
refuelCar()
{
  // some code to refuel the car
}

Nothing wrong so far, right? Then what’s the issue? Imagine a scenario where you have multiple cars. Each car has these attributes and functionalities. We will have to declare and define these attributes and functionalities again and again for each car. See the problem now? If we keep on adding the attributes and functionalities of each car manually, how do we know when to stop? How do we save this information in a way which is effective and manageable? Moreover, the program will have an enormous amount of redundant and unmanageable code. 

Here comes Object Oriented Programming to the rescue. With object oriented paradigm, we can tie all these functionalities and attributes in a class which is easy to manage and exposes extremely less redundant code. 

/* The following code piece is a C++ code. */
class Car
{
  string carColor;
  string carPlateNumber;
  string carCompany;
  string carEngine;
  changeCarSpeed()
  {
    // some code to change car's speed
  }
  playCarHorn()
  {
    // some code to play car's horn
  }
  refuelCar()
  {
    // some code to refuel the car
  }
}

This way we can create new objects of Car without explicitly re-declaring each of the functionalities and properties associated to a car. Hence, we achieve optimal and manageable code with minimum redundancy.

Pillars of Object Oriented Programming

This section will define and briefly discuss the pillars of Object Oriented Programming. Why do we call them the pillars is because of the fact that OOP cannot function if any of these concepts are removed from this paradigm. 

Inheritance, polymorphism, encapsulation, and abstraction are the four key concepts in OOP. These core concepts are the reasons why OOP has become one of the most commonly used approaches in the programming world today. Let’s understand what these concepts are. 

Inheritance

Inheritance is one of the most important concepts of Object Oriented Programming. It is a procedure by which a class can share the properties and functionalities of another class in a hierarchical fashion. 

For instance, class A has a certain set of stand-alone features that it implements. If another class B inherits from this class A, the properties and functionalities of A are also automatically available in B.

Let’s look at an example to properly understand it.

Example

Suppose that we have a class Car which defines all the functionalities related to any car as shown below.

class Car
{
  private:
    string model;
    string company;
    string colour;
    int speed;

  public:
    string getModel();
    string getCompany();
    string getColour();
    int getSpeed();
}

Now if we wish to create classes for Truck, Motorcycle, Bus, etc., we will have to write the code for them as well. The code that will be written in those classes will mostly be same to that of a Car. This way there will be a lot of redundant code which obviously adds to bad design and other complexities. In this case, we can create a super class Vehicle and write all the common code related to Truck, Motorcycle, Bus and use inheritance to access the common functionalities. This way we reuse the code and promote redundancy-free code design.

Apart from multiple structural use cases that inheritance provides, code re-usability is the most important one to mention. 

Polymorphism

Polymorphism is the capability of a programming language to treat similar objects differently at run time depending upon their class or data type. It is a kind of extension to the concept of inheritance. In simple words, in order to process polymorphism, we must allow inheritance between classes first. This concept attains a significant value in OOP because it allows the dynamic treatment of objects at run time and can be used in a variety of ways according to the application requirements. 

Encapsulation

Encapsulation is an OOP concept which allows the grouping and bundling of attributes and functions. OOP allows its usage to often hide object’s internal state or representation from the outside world/application. Encapsulation helps in protecting data by providing privacy management keywords such as public, private and protected.

Abstraction

Abstraction is a concept of selecting smaller amount of data from a larger chunk to show only the relevant and appropriate details of an object to the user. By hiding unnecessary information, it creates an environment which helps in reducing effort and complexity of programming. Abstraction is a vital concept to understand as many design patterns utilise this concept for structuring and designing the code. 

Key Note

Java, C++, JavaScript, Python and PHP are some examples of popular languages which support the principles of OOP. To learn more about these languages, you can check our tutorials in their respective sections.