Four Pillars of OOPs (Object Oriented Programming)

Learn via video courses
Topics Covered

Overview

Object-Oriented Programming (OOP) is a software development paradigm emphasizing efficient and well-organized code. OOP is founded on four essential principles, the Four Pillars: abstraction, encapsulation, inheritance, and polymorphism. Abstraction allows us to concentrate on the fundamental characteristics of an object while concealing superfluous complications. Encapsulation emphasizes the concept of data concealing and data bundling with useful functions or procedures. Inheritance allows developers to create new classes based on existing ones by establishing hierarchical links between them. Polymorphism is the belief that different types of objects can be treated interchangeably, providing flexibility and adaptability in program design.

Introduction to OOP

Object-Oriented Programming (OOP) has emerged as a powerful paradigm in the ever-changing world of software development, revolutionizing how we design and build programs. With its emphasis on objects, encapsulation, and reusability, OOP offers a systematic approach to software development that encourages modularity, flexibility, and ease of maintenance. In this section, we will look at the underlying notions of OOP and why it has become such an important part of modern software development.

Benefits of OOP:

Developers can gain various benefits by using OOP principles, including:

  • Modularity: OOP fosters the division of complicated systems into smaller, manageable modules, which improves code organization and understanding.
  • Reusability: OOP allows developers to reuse classes and objects across projects, saving time and effort.
  • Maintenance: The modularity and encapsulation of OOP make it easier to maintain and update code, reducing the impact of changes.
  • Scalability: Because of OOP's hierarchical structure and inheritance, developers can extend and modify code without affecting the entire system.

The Four Pillars of OOPs

Object-Oriented Programming (OOP) has developed as a powerful paradigm in programming, facilitating the construction of resilient and scalable software systems. OOP enables developers to organize code logically and efficiently, improving code reuse, maintainability, and overall software quality. At the heart of OOP are the Four Pillars, which serve as the framework for developing well-structured, modular programs. This section will examine the Four Pillars of OOP and their significance in real-world situations.

1. Abstraction

Abstraction is breaking down complex systems into manageable and reusable components to simplify them. It enables us to construct classes and objects that accurately replicate real-world entities while disregarding superfluous features. We can create more efficient and maintainable software by focusing on the essentials. Abstraction in OOP is accomplished through the usage of abstract classes and interfaces. Consider the following scenario: We have an abstract base class called Shape, which declares a pure virtual function calculateArea(), used to calculate the area of various forms. Rectangle and Circle are derived classes that inherit from the Shape class and provide their implementations of the calculateArea() function.

Example 1 - Abstract Class:

Example 2 - Interface:

Consider an interface named Drawable, which defines a contract for things that can be drawn. The Circle class implements the Drawable interface by implementing a draw() function.

2. Encapsulation

Encapsulation groups data and methods within a class to provide a protective barrier over internal implementation details. It allows the construction of objects that encapsulate data and reveal only the methods or interfaces required for interaction with the outside world. This method encourages information concealment by prohibiting direct access to an object's internal state.

Example 1

Consider the following example to grasp encapsulation in practice better. Assume we're creating a banking system and need a class representing a bank account. We want to encapsulate the account balance and give ways for depositing, withdrawing, and retrieving it.

3. Inheritance

Inheritance creates an "is-a" relationship between classes by allowing a derived or child class to inherit characteristics from a parent or base class. This relationship allows the derived class to access and utilize the base class's properties and methods, boosting code reuse and decreasing redundancy.

Example

Consider the following example to see inheritance in action. We'll start with a base class called Shape, followed by two derived classes called Circle and Rectangle, which will inherit from the Shape class. The Shape class will share characteristics and methods the circle and the rectangle share.

The Circle and Rectangle classes in the preceding code inherit from the Shape class via the public access specifier. This option allows derived classes to access the base class's protected members. The Shape class defines common properties (width and height) and methods (setDimensions and displayDimensions) that derived classes can use.

We can now build Circle and Rectangle objects and use their inherited properties and methods. :

4. Polymorphism

Polymorphism, derived from the Greek words "poly" (many) and "morph" (shape), refers to an object's ability to take on numerous forms or behaviors. Polymorphism in the context of OOP allows objects of different kinds to be treated consistently when they share a common superclass.

Example:

Consider a basic example involving several forms. We have a base class called Shape and two subclasses called Rectangle and Circle. Each subclass derives from the 'Shape' class and implements a common behavior known as area(). The code is as follows:

The output of the program will be:

FAQs

Q. What are the Four Pillars of Object-Oriented Programming?

A. The Four Pillars of Object-Oriented Programming, often known as the four principles or notions, are the pillars upon which object-oriented systems are designed and implemented.

Q. How do these pillars contribute to object-oriented programming?

A. Encapsulation protects data integrity, encourages code organization, and improves security by encapsulating related data and behavior within objects. Inheritance allows code reuse, promotes extensibility, and permits building hierarchies of related classes. Polymorphism enables the production of versatile code that can interact with objects of many types, encouraging code reuse and increasing flexibility.

Q. What is the purpose of encapsulation in object-oriented programming?

A. In object-oriented programming, encapsulation serves several functions. For starters, it aids in the organization and structure of code by grouping data and methods into a class. Second, encapsulation allows an object's internal implementation details to be hidden, exposing only a public interface for interacting with it. This safeguards the object's data and prevents unauthorized access.

Q. How does inheritance promote code reuse in object-oriented programming?

A. In object-oriented programming, inheritance is a fundamental notion that encourages code reuse by allowing the construction of new classes (subclasses) based on existing classes (superclasses). The subclass inherits the superclass's attributes, methods, and behaviors, avoiding the need to redo common code.

Q. How does polymorphism contribute to flexible and extensible code?

A. Polymorphism is a powerful element in object-oriented programming that helps make programs more flexible and extendable. It enables programs to work with objects dynamically at runtime by treating objects of diverse types as objects of a shared superclass. Method overriding and method overloading are the two strategies that allow for polymorphism. Method overriding allows subclasses to implement a method provided in the superclass, allowing the same method call to behave differently depending on the object type.

Q. How does abstraction simplify complex systems in object-oriented programming?

A. Abstraction is a fundamental notion in object-oriented programming that simplifies complex systems by focusing on core aspects while concealing extraneous details. It entails breaking a system into manageable and intelligible modules and letting developers work at various levels of abstraction. Abstraction in object-oriented programming is accomplished through abstract classes and interfaces. Abstract classes serve as a design template for related classes, defining common methods and properties that subclasses must implement. Interfaces define a contract that classes must follow by describing implementation methods.

Conclusion

  • Encapsulation, inheritance, polymorphism, and abstraction are the pillars of OOPs, providing code reuse, modularity, and maintainability.
  • Encapsulation groups data and procedures, ensuring data protection and organization within classes.
  • Inheritance creates new classes based on existing ones, promoting code hierarchy and modularity.
  • Polymorphism allows different types to be treated as the same base type, enhancing code flexibility and extensibility.
  • Abstraction focuses on essential object traits while hiding unnecessary details, improving code readability and maintenance.
  • Combining these principles results in efficient and maintainable code for complex software systems.