---
title: "INTRODUCTION TO OOPS"  
description: "OOP is basically a programming style that involves the concept of objects and class.We can also say that OOP is an approach to a problem involving obj"  
author: "NAMAN PANDEY"  
published: 2018-06-19  
updated: 2019-03-03  
canonical: https://www.mindstick.com/articles/12956/introduction-to-oops  
category: "oops"  
tags: ["oops", "inheritance", "class", "object"]  
reading_time: 8 minutes  

---

# INTRODUCTION TO OOPS

**OBJECT-[ORIENTED PROGRAMMING](https://www.mindstick.com/interview/23571/what-is-the-difference-between-object-oriented-programming-and-object-based-programming) (OOP)**

## OOP:

OOP is basically a programming style that involves the concept of objects and class.We can also say that OOP is an approach to a problem involving objects. Our objective is to tell a set of instructions to a computer with the help of [programming language](https://www.mindstick.com/forum/156801/write-a-program-for-reverse-a-word-in-sentence-in-any-programming-language). To achieve this there are various ways of doing it and OOP is one of those ways.

There are various concepts under OOPs which are;

## 1) Objects

## 2) Classes

## 3) Inheritance

## 4) [Data encapsulation](https://answers.mindstick.com/qa/30565/what-is-data-encapsulation)

## 5) Data abstraction

## 6) Polymorphism

We will discuss these concepts in details,

## 1) OBJECTS:

Objects are the basic unit of OOPs. Objects are basically instances of a class. We can say that class as the description of a concept, and an object as the realization of this description to create an independent distinguishable entity.

Objects can be concrete or conceptual each with its own individual identity.

These real-world objects share two characteristics: they all have state and they all have behavior. For example, we humans have hands, legs, eyes, nose, ears these are the data and walking, reading, writing, sleeping, eating are the methods.

Objects are made after real-world objects. A software object maintains its data in variables and implements the methods.

## 2) CLASSES:

Classes are basically a blueprint of the functional entity which defines its properties and function. Classes contain both data and functions. A class can have the subclass which can inherit data/properties of that class.

The above diagrams show an exact meaning of classes.

## 3) INHERITANCE:

Inheritance is a process in which properties of parent class are inherited by the child class. For example, if there is a parent class and a child class and we declare a variable or method in the parent class we can use that in child class without declaring that variable or method again.

There following are the types of inheritance:

## 1) SINGLE INHERITANCE

Above diagram shows the [concept of single](https://answers.mindstick.com/qa/32901/from-which-source-india-got-the-concept-of-single-order-of-the-court) inheritance.

In single inheritance base class derive the properties from only one parent class.

## Syntax:

```
class subclass_name : access_mode base_class{  //body of the subclass};
```

## EXAMPLE:

```
// C++ program to explain// Single inheritance#include <iostream>using namespace std;// base classclass Vehicle {  public:    Vehicle()    {      cout << "This is a Vehicle" << endl;    }};// sub class derived from two base classesclass Car: public Vehicle{};// main functionint main(){    // creating object of subclass will    // invoke the constructor of base classes    Car obj;    return 0;}Output:This is a vehicle
```

## 2) [MULTIPLE INHERITANCE](https://www.mindstick.com/forum/2519/java-multiple-inheritance)

In multiple inheritance properties of sub/child class are derived from two super/parent classes.

## EXAMPLE:

```
// C++ program to explain// multiple inheritance#include <iostream>using namespace std;// first base classclass Vehicle {  public:    Vehicle()    {      cout << "This is a Vehicle" << endl;    }};// second base classclass FourWheeler {  public:    FourWheeler()    {      cout << "This is a 4 wheeler Vehicle" << endl;    }};// sub class derived from two base classesclass Car: public Vehicle, public FourWheeler {};// main functionint main(){    // creating object of sub class will    // invoke the constructor of base classes    Car obj;    return 0;}Output:This is a VehicleThis is a 4 wheeler Vehicle
```

**3) [MULTILEVEL INHERITANCE](https://www.mindstick.com/interview/23096/can-you-explain-me-multilevel-inheritance):**

In multilevel inheritance, a derived class is derived from another base class.

## EXAMPLE:

```
// C++ program to implement// Multilevel Inheritance#include <iostream>using namespace std;// base classclass Vehicle{  public:    Vehicle()    {      cout << "This is a Vehicle" << endl;    }};class fourWheeler: public Vehicle{ public:    fourWheeler()    {      cout<<"Objects with 4 wheels are vehicles"<<endl;    }};// sub class derived from two base classesclass Car: public fourWheeler{   public:     car()     {       cout<<"Car has 4 Wheels"<<endl;     }};// main functionint main(){    //creating object of the subclass will    //invoke the constructor of base classes    Car obj;    return 0;}output:This is a VehicleObjects with 4 wheels are vehiclesCar has 4 Wheels
```

## 4) HIERARCHICAL INHERITANCE:

In hierarchical inheritance, more than one subclass is inherited from a single base class. i.e. more than one derived class is created from a single base class.

## EXAMPLE:

```
// C++ program to implement// Hierarchical Inheritance#include <iostream>using namespace std;// base classclass Vehicle{  public:    Vehicle()    {      cout << "This is a Vehicle" << endl;    }};// first sub classclass Car: public Vehicle{};// second sub classclass Bus: public Vehicle{};// main functionint main(){    // creating object of sub class will    // invoke the constructor of base class    Car obj1;    Bus obj2;    return 0;}Output:This is a VehicleThis is a Vehicle
```

## 5) HYBRID INHERITANCE:

Hybrid Inheritance is implemented by combining more than one type of inheritance. For example: Combining Hierarchical inheritance and Multiple Inheritance.

## EXAMPLE:

```
// C++ program for Hybrid Inheritance#include <iostream>using namespace std;// base classclass Vehicle{  public:    Vehicle()    {      cout << "This is a Vehicle" << endl;    }};//base classclass Fare{    public:    Fare()    {        cout<<"Fare of Vehicle\n";    }};// first sub classclass Car: public Vehicle{};// second sub classclass Bus: public Vehicle, public Fare{};// main functionint main(){    // creating object of sub class will    // invoke the constructor of base class    Bus obj2;    return 0;}Output:This is a VehicleFare of Vehicle
```

## Syntax:

```
class subclass_name : access_mode base_class{  //body of subclass};
```

## EXAMPLE:

```
// C++ program to explain// Single inheritance#include <iostream>using namespace std;// base classclass Vehicle {  public:    Vehicle()    {      cout << "This is a Vehicle" << endl;    }};// sub class derived from two base classesclass Car: public Vehicle{};// main functionint main(){    // creating object of subclass will    // invoke the constructor of base classes    Car obj;    return 0;}Output:This is a vehicle
```

## 4) DATA ABSTRACTION:

Data abstraction means showcasing only the required things to the outside world while hiding the details. Basically, data abstraction is done to hide the complexity of the program.

Example: When we drive a car and press the accelerator to increase the speed or press the brakes to stop the car even without knowing how the accelerator or the brakes work as the driver is not concerned with the working. This is an example of data abstraction as here we hide the working and providing only the essential part to the user.

Data abstraction helps in various ways such as:

**1)** Prevent from writing low-level code

**2)** avoid code duplication and increases reusability.

**3)** Helps to increase security.

## 5) DATA ENCAPSULATION:

The process of hiding the implementation details of the class from the user is called as data encapsulation. This an achieved through the private fields and the public methods of classes. Data abstraction is basically bound data [variables and functions](https://www.mindstick.com/forum/161066/what-is-hoisting-of-variables-and-functions-in-javascript) together in a class.

Data encapsulation is also known as data hiding. Data encapsulation can be achieved by access specifiers which are public, protected, and private. These specifiers help in hiding the data and exposing those only which need to be exposed.

**EXAMPLE**:

```
#include <iostream>using namespace std;class Adder {   public:      // constructor      Adder(int i = 0) {         total = i;      }      // interface to outside world      void addNum(int number) {         total += number;      }      // interface to outside world      int getTotal() {         return total;      };   private:      // hidden data from outside world      int total;};int main() {   Adder a;   a.addNum(10);   a.addNum(20);   a.addNum(30);   cout << "Total " << a.getTotal() <<endl;   return 0;}When the above code is compiled and executed, it produces the following result −Total 60
```

Above class adds numbers together, and returns the sum. The public members addNum and getTotal are the interfaces to the outside world and a user needs to know them to use the class. The private member total is something that is hidden from the outside world but is needed for the class to operate properly.

**6) POLYMORPHISM**:

The word polymorphism means having many forms. Polymorphism is a concept which allows us to redefine the way something works, by either changing how it is done or by changing the parts using which it is done.

It is a feature which let us create functions with same name but different arguments, which will perform different actions.

Polymorphism can be static as well as dynamic both. [Method overloading](https://www.mindstick.com/forum/156845/what-is-method-overloading-in-c-sharp-give-an-example) is static polymorphism whereas [method overriding](https://www.mindstick.com/forum/53/method-overriding) is the dynamic polymorphism.

Overloading means that when the method name is same but have different arguments. It is also called as compile-time polymorphism or early binding.

Some rules regarding method overloading are:

**1)** The method name must be same.

**2)** Method argument must be different.

**3)** Method signature must be different.

Overriding means to redefine a function. The method written in the parent class can be redefined in the child class. It is also called runtime polymorphism or late binding.

Some rules regarding method overriding are:

**1)** The method name must be same

**2)** Method argument must be same including the order

**3)** Method signature must be same

**ADVANTAGES OF OOPS**:

**1)** Objects created in object-oriented programs can be used in other programs hence it helps in code reusability.

**2)** It helps in data redundancy.

**3)** It helps in data security.

**4)** It helps in data hiding.

**5)** It helps in code maintenance.

\

---

Original Source: https://www.mindstick.com/articles/12956/introduction-to-oops

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
