Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
1. Abstraction
1.1.
Abstraction Using Classes
1.2.
Abstraction Using Header Files
1.3.
Example
2.
2. Encapsulation
2.1.
Example
3.
3. Inheritance
3.1.
Subclass
3.2.
Superclass
3.3.
Example 
4.
4. Polymorphism
4.1.
Operator Overloading 
4.2.
Function Overloading
4.3.
Example
5.
How to Use the Four Pillars of OOPs in Your Projects
6.
The Benefits of Incorporating the Four Pillars of OOPs
7.
Exploring the Advantages of the Four Pillars of OOPs
8.
Fundamental Design Principles of the Four Pillars of OOPs
9.
Frequently Asked Questions
9.1.
Which pillar of OOP indicates code reusability?
9.2.
What are the 4 pillars of OOP?
9.3.
What is the most important pillar of OOP?
9.4.
What is abstraction in OOP?
10.
Conclusion
Last Updated: Jun 5, 2024
Easy

Four Pillars of Object-Oriented Programming (OOPS)

Author RAJESH RUNIWAL
2 upvotes
Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

Object-oriented programming is an approach or a programming sample where the packages are structured around objects rather than functions and logic. It makes the data partitioned into memory areas, i.e., data and functions and helps make the code flexible and modular. The four pillars of OOPS are Inheritance, Polymorphism, Encapsulation and Abstraction.

Four Pillars of OOPs (Object Oriented Programming)

Object-oriented programming mainly focuses on objects which might be required to be manipulated. In OOPs, it may represent data as objects with attributes and functions.

The four pillars of OOPS:

  1. Inheritance
  2. Polymorphism
  3. Encapsulation
  4. Data Abstraction.

1. Abstraction

Data abstraction is the most essential function of object-oriented programming in C++. Abstraction means displaying only basic information and hiding the details. Data abstraction refers to providing only necessary information about the data to the outside world, hiding the background info or implementation.

Abstraction Using Classes

We can implement Abstraction in C++ using classes. The class helps us group data members and member functions using available access specifiers. A class can decide which data member will be visible to the outside world and not.

Abstraction Using Header Files

One more type of abstraction in C++ can be header files. For example, consider the pow() technique present in the math.h header file. Whenever we need to calculate the power of a number, we call the function pow() present in the math.h header file and pass the numbers as arguments without knowing the underlying algorithm according to which the function calculates the power of numbers.

Example

#include <iostream>
using namespace std;
class implementAbstraction
{
    private:
        int x, y;
    public:
        void set(int a, int b)
        {
            x = a;
            y = b;
        }        
        void display()
        {
            cout<<"x = " <<x << endl;
            cout<<"y = " << y << endl;
        }
};  
int main() 
{
    implementAbstraction obj;
    obj.set(40, 50);
    obj.display();
    return 0;
}

Output:

x = 40
y = 50

2. Encapsulation

In common terms, Encapsulation is defined as wrapping up of Data and Information under a single unit. In object-oriented programming, Encapsulation is defined as binding together the data and the functions that manipulate them.

Encapsulation also leads to data abstraction or hiding. Using Encapsulation also hides the data. In the previous example, the data of any of the sections like sales, finance, or accounts are hidden from any other area.

Example

#include<iostream>
using namespace std;
class Encapsulation
{
    private:
        int a;     
    public:
        void set(int x)
        {
            a =x;
        }
        int get()
        {
            return a;
        }
};
int main()
{
    Encapsulation obj;
    obj.set(10); 
    cout<<obj.get();
    return 0;
}

Output:

10
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

3. Inheritance

Inheritance is the process in which two classes have a relationship with 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.

Subclass

The class that inherits properties from another class is called Subclass or Derived class.

Superclass

The class that inherits sub-class properties is called Base class or superclass.

Example 

#include <bits/stdc++.h>
using namespace std;
class Parent
{
  public:
    int id_Parent;
};
  
class Child: public Parent
{
  public:
    int id_Child;
};
int main()
{
    Child obj1;
    obj1.id_Child = 10;
    obj1.id_Parent = 63;
    cout << "Child id is: " << obj1.id_Child << '\n';
    cout << "Parent id is: " << obj1.id_Parent << '\n';
    return 0;
}

Output:

Child id is 10
Parent id is 63

4. Polymorphism

The word polymorphism means having many forms. It is the ability to take more than one form. It is a feature that provides a function or an operator with more than one definition. It can be implemented using function overloading, operator overload, function overriding, virtual function. An operation may show off different behaviors at different times.

C++ supports operator overloading and function overloading.

Operator Overloading 

The system of making an operator show off different behaviors in different instances is known as operator overloading.

Function Overloading

Function overloading uses a single function name to perform different tasks.

Know What is Object in OOPs here in detail.

Example

#include <bits/stdc++.h>
using namespace std;
class Geeks
{
    public:
    void fun(int a)
    {
        cout << "value of a is " << a << endl;
    }
    void fun(double a)
    {
        cout << "value of a is " << a << endl;
    }
    void fun(int a, int b)
    {
        cout << "The value of a and b is " << a << " , " << b << endl;
    }
};
  int main() 
{
    Geeks obj1;
    obj1.fun(10);
    obj1.fun(6.324);
    obj1.fun(90 , 24);
    return 0;
} 

Output:

value of x is 10
value of x is 6.324
The value of x and y is 90, 24

For a Complete and in-depth tutorial on OOPS in C++, you should watch the below video

How to Use the Four Pillars of OOPs in Your Projects

To effectively use the four pillars of Object-Oriented Programming (OOP) in your projects, follow these guidelines:

Encapsulation:

  • Define classes that encapsulate data (attributes) and methods (functions) that operate on that data.
  • Use access modifiers (e.g., private, protected, public) to control access to class members.
  • Encapsulate data with getters and setters to maintain control over data access and manipulation.

Abstraction:

  • Identify the essential attributes and behaviors of your objects.
  • Create abstract classes and interfaces that define common behaviors and attributes.
  • Hide unnecessary implementation details from external code, exposing only what's relevant.

Inheritance:

  • Identify relationships and hierarchies between classes. Use inheritance to model these relationships.
  • Create a base (super) class that defines common attributes and behaviors shared by subclasses.
  • Reuse code and promote consistency by inheriting attributes and methods from a superclass.

Polymorphism:

  • Define methods with the same name in different classes to enable method overriding (subclasses providing their implementations).
  • Utilize method overloading to define multiple methods with the same name but different parameter lists.
  • Take advantage of interfaces and abstract classes to achieve polymorphic behavior.

Overall, the effective use of OOP principles involves proper design and planning. Consider the specific needs of your project and how encapsulation, abstraction, inheritance, and polymorphism can help you create modular, maintainable, and extensible code. Regularly review and refactor your code to ensure it adheres to these principles, improving the quality and readability of your software.

The Benefits of Incorporating the Four Pillars of OOPs

Here are the benefits of incorporating the Four Pillars of OOP:

Encapsulation

  • It keeps your data safe and hidden inside the class.
  • It makes your code organized and allows independent handling of class-specific tasks.

Abstraction

  • It allows using classes without worrying about their complicated internal workings.
  • It helps to focus on what the classes do rather than how they do it. It makes code easier to understand.

Inheritance

  • It enables the creation of new classes that reuse features from existing classes.
  • It saves time and effort by building on existing code.

Polymorphism

  • It allows you to treat different objects similarly, even if they work differently internally.
  • It provides flexibility, which makes your code more adaptable to changes in different situations.

Exploring the Advantages of the Four Pillars of OOPs

Here are the advantages of the Four Pillars of OOPs:

Encapsulation

  • Data hiding for security and integrity.
  • Easier maintenance and troubleshooting.
  • Reusability of code in different parts of the program.

Inheritance

  • Code reusability by inheriting properties and behaviors from a base class.
  • The organized hierarchical relationship between classes.
  • Reduces redundancy and promotes a consistent codebase.

Polymorphism

  • Flexibility and extensibility by adding new classes without modifying existing code.
  • Simplifies code by using a common interface for different data types.
  • Improves code readability and maintainability.

Abstraction

  • Flexibility to change implementations without affecting other parts of the code.
  • Better communication among team members with a high-level view of objects.
  • It helps manage and understand complex systems more efficiently.

Fundamental Design Principles of the Four Pillars of OOPs

Here are the design principles of the Four Pillars of OOPs:

  • Encapsulation: This principle involves bundling data (attributes) and methods (functions) that operate on that data within a single unit, known as a class. The class acts as a blueprint for creating objects. Encapsulation hides the class's internal implementation details and provides a clean and controlled interface for interacting with the object.
  • Inheritance: Inheritance is a mechanism where a class (derived or subclass class) can inherit properties and behaviors (methods and attributes) from another class (superclass class). This allows developers to build on existing classes, saving time and effort.
  • Polymorphism: Polymorphism allows objects of different classes to be treated as belonging to a common group. You can use the same method names with other classes, and the program will automatically know which version of the method to use based on the object being used. Polymorphism helps write flexible, generic code that works with various object types.
  • Abstraction: Abstraction simplifies the representation of objects by highlighting only the essential characteristics and behaviors while hiding unnecessary details. It allows developers to create easy-to-use interfaces for working with objects without knowing the complexities of their implementation.

Frequently Asked Questions

Which pillar of OOP indicates code reusability?

The pillar of OOP that indicates code reusability is “Inheritance.” It allows classes to inherit properties and behaviors from other classes. It promotes code reuse and reduces redundancy in object-oriented systems.

What are the 4 pillars of OOP?

The four pillars of OOPS (object-oriented programming) are Inheritance, Polymorphism, Encapsulation and Data Abstraction.

What is the most important pillar of OOP?

"Encapsulation" is often considered the most crucial pillar of OOP, as it ensures data security and code organization.

What is abstraction in OOP?

In OOP, abstraction hides complex details, focusing on what an object does, not how it does it.

Conclusion

We learned about the four pillars of OOPS and also learned about all the type four pillars of OOPS. After reading about OOPS concepts in C++, you would have understood why you need object-oriented programming, what C++ OOPs are, and the fundamental ideas of OOPs like polymorphism, inheritance encapsulation, and many others.

For more DSA blogs, you can visit here.

Recommended Readings: 

Previous article
Encapsulation in C++
Next article
What is polymorphism?
Live masterclass