Introduction to Object Oriented Programming

Object oriented programming is a programming paradigm based on the concept of objects that contain data and instructions or code. Object oriented programming (OOP) can also be referred to as a programming concept working on the principles of abstraction, encapsulation, inheritance and polymorphism. It allows users to create objects they want and create methods to handle those objects. The basic concept of OOPs is to create objects, re-use them throughout the program, and manipulate these objects to get results. The popular object oriented languages are C++, Java, Python, JavaScript etc. 

Objects

Any entity that has a state and a behavior is referred to as an object. For example a pen, chair, bottle etc. Objects can be physical or logical. An object can be defined as an instance of a class. An object contains an address and takes up some space in memory. When an object of a class is created, the class is said to be instantiated. All the instances share the attributes and the behavior of the class. But the values of those attributes, that is, the state are unique for each object. A single class may have any number of instances. 

Class

A class can be defined as a blueprint from which objects are defined or created. Classes don’t consume any space in the memory. It represents the set of properties or methods that are common to all objects of one type. Class declarations include the following components:

  • Class keyword
  • Class name: Generally written in sentence case. 
  • Modifiers: A class can either be public, private or protected.
  • Body: Consists of methods and data fields. The body is generally surrounded by curly brackets in languages like C++ and Java.  

There are four pillars of object oriented programming namely:

  • Abstraction
  • Inheritance
  • Encapsulation
  • Polymorphism

Abstraction:

Data Abstraction is the property by virtue of which only the essential details are displayed to the user. The trivial or the non-essentials units are not displayed to the user. We can understand more about abstraction by taking a simple example. While driving a car the driver knows that pressing the accelerator would increase the speed of the car but the driver does not know about the internal functioning of the car or the implementation of the accelerator. 

Encapsulation:

Binding data and code together into a single unit is referred to as encapsulation. We can also consider it to be a protective shield that prevents data access by code outside this shield. Technically, the data or variables of a class are hidden from other classes and can only be accessed by member functions of the own class. Hence clearly data is hidden from other classes. Encapsulation can be achieved by setting all the variables in a class as private and writing public member functions to access the variables. 

Inheritance:

The capability of a class to derive properties and characteristics from another class is referred to as inheritance. Inheritance is the process in which two classes have an is-a relationship among each other and objects of one class acquire properties and features of the other class. The class which inherits the features is known as the child class, and the class whose features it inherited is called the parent class.  For example, Class Vehicle is the parent class, and Class Bus, Car, and Bike are child classes.

Polymorphism:

Polymorphism refers to the ability of a variable, object or function to take multiple forms. For example, in English, the verb run has a different meaning if you use it with a laptop, a foot race, and business. Here, we understand the meaning of run based on the other words used along with it. The same also applied to Polymorphism.

Advantages of OOPs:

  • OOPs makes development and maintenance easier, whereas, in a procedure-oriented programming language, it is not easy to manage if code grows as project size increases.
  • It enhances program modularity because every object exists independently.
  • Objects created for Object-Oriented Programs can be reused in other programs. Thus it saves significant development cost.
  • OOPs provide data hiding, whereas, in a procedure-oriented programming language, global data can be accessed from anywhere.