0% found this document useful (0 votes)
36 views15 pages

What Are The Four Pillars of Oops? Why Do These Pillars Matter?

The document discusses object-oriented programming concepts including the four pillars of OOP - encapsulation, inheritance, polymorphism, and abstraction. It provides details on each pillar and their importance as well as examples to illustrate object-oriented concepts.

Uploaded by

wardah.2944
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views15 pages

What Are The Four Pillars of Oops? Why Do These Pillars Matter?

The document discusses object-oriented programming concepts including the four pillars of OOP - encapsulation, inheritance, polymorphism, and abstraction. It provides details on each pillar and their importance as well as examples to illustrate object-oriented concepts.

Uploaded by

wardah.2944
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Contents

OOP concepts: ...................................................................................................................... 2


What is OOP and why is it important? .................................................................................. 2
Advantages of OOPs ........................................................................................................... 2
Disadvantages of OOPs ...................................................................................................... 2
Pillars of OOP: .................................................................................................................... 2
What is OOP? ................................................................................................................ 2
Why Does OOP Matter? ................................................................................................ 3
Exploring the Four Pillars of Oops: Delving into the 4 Main Pillars .............................. 3
What Are the Four Pillars of Oops? ............................................................................ 3
Why Do These Pillars Matter? ..................................................................................... 4
Encapsulation: The First Pillar ............................................................................................. 4
Inheritance: The Second Pillar............................................................................................. 5
Polymorphism: The Third Pillar ............................................................................................ 6
Abstraction: The Fourth Pillar .............................................................................................. 6
Class: ................................................................................................................................ 7
What is an abstract class? .................................................................................................. 7
Constructor (__init__ method): .......................................................................................... 8
various types of constructors in C++?.................................................................................. 9
26. destructor? ................................................................................................................. 11
27. Can we overload the constructor in a class? ................................................................ 12
28. Can we overload the destructor in a class? .................................................................. 12
29. What is the virtual function? ........................................................................................ 12
30. What is pure virtual function? ...................................................................................... 13
main features of OOPs...................................................................................................... 14
What are access specifiers? What is their significance in OOPs?........................................ 14
limitations on Inheritance? ............................................................................................... 14
Types of inheritance ......................................................................................................... 15
OOP concepts:
What is OOP and why is it important?
Emphasizes the concept of objects rather than just functions and
procedures.
Advantages of OOPs

• OOPs provides enhanced code reusability.


• The code is easier to maintain and update.
• It provides better data security by restricting data access and
avoiding unnecessary exposure.
• Fast to implement and easy to redesign resulting in minimizing the
complexity of an overall program
Disadvantages of OOPs

• OOPs concept is not suitable for all kinds of problems.


• Proper planning is required because OOPs is a little bit tricky.
• The programmer should be well-skilled and should have excellent
thinking in terms of objects as everything is treated as an object in
OOPs.
• The length of the programs is much larger in comparison to the
procedural approach.

Pillars of OOP:
What is OOP?

At its essence, OOP is a paradigm that transforms code into organized,


reusable, and efficient modules. Think of it as a library stocked with pre-
designed, versatile tools. These tools, called "objects," carry data and
methods to manipulate that data. Imagine a car object: it holds information
like speed and fuel and can perform actions like accelerating or braking.
Why Does OOP Matter?

OOP isn't just another coding approach; it's a game-changer. Here's why
it matters:

1. Modularity: OOP divides complex systems into manageable


chunks (objects), making development more structured and
teamwork-friendly.
2. Reusability: Objects can be reused across projects, saving time
and effort, like having a toolbox that works for various DIY projects.
3. Flexibility: OOP accommodates changes and updates smoothly.
Alter an object's behavior without affecting the entire codebase.
4. Readability: Code written in OOP is like a well-organized novel.
It's easier to understand, maintain, and debug.
5. Real-World Application: From video games to e-commerce
platforms, OOP is everywhere. For instance, an e-commerce
system may use "product" objects to manage inventory and
customer shopping carts.

OOP transforms code from a jumbled mess into a symphony of logic and
structure. And to truly appreciate OOP's brilliance, we must explore its
Four Pillars, the cornerstones of this coding masterpiece. But first, let's
dive deeper into these pillars to grasp their significance.

Exploring the Four Pillars of Oops: Delving into the 4


Main Pillars
What are four pillars of OOPS? Imagine a grand architectural
masterpiece—your software project. Now, consider the four pillars that
uphold its grandeur. These are the Four Pillars of Object Oriented
Programming. As we venture deeper into this coding cathedral, we'll
uncover its profound significance. Let’s explain four pillars of OOPS:

What Are the Four Pillars of Oops?

1. Encapsulation: This pillar encapsulates data within objects,


safeguarding it from unauthorized access. It's like a treasure chest
with a lock, only accessible by the right key.
2. Inheritance: In object-oriented programming, Inheritance enables
a class to inherit the attributes and behaviors of another class. This
way, the new class can reuse the code of the parent class and add
or modify its properties and methods. It's like passing down traits in
a family that can be altered or extended in a new generation.
3. Polymorphism: Objects of different types can be treated as
instances of a common superclass. For example, various vehicles
can be treated as vehicles and driven using the same "drive"
command.
4. Abstraction: Abstraction simplifies complexity by providing a high-
level view of an object. Think of it as a car dashboard; you don't
need to know the inner workings to drive.

Why Do These Pillars Matter?

• Modularity: Each pillar promotes modular code, making it easier


to manage, understand, and maintain.
• Reusability: By embracing these pillars, you can reuse and
extend code components, saving precious time.
• Scalability: Thanks to these principles, your software can grow
without becoming a tangled mess.
• Real-World Examples: Consider a banking system.
Encapsulation secures customer data, Inheritance handles
different account types, Polymorphism enables transactions, and
Abstraction simplifies user interactions.

Encapsulation: The First Pillar

Encapsulation bundles data (attributes) and the methods (functions) that


operate on it into a class unit. This class acts like a fortress, guarding its
inner workings from external meddling.

Why Is Encapsulation Important?

1. Data Security: Encapsulation keeps sensitive data safe from


unauthorized access. For instance, the password attribute is
encapsulated in a user class, preventing direct access.
2. Modularity: It promotes modular code, making it easy to maintain
and understand. Changes to the internal structure don't ripple
across the entire codebase.
Real-Life Examples of Encapsulation

1. Banking System: In a banking system, customer account details


like balance and account number are encapsulated within a class.
External access to these attributes is limited to specific methods,
ensuring data integrity.
2. Smartphone: Smartphone manufacturers encapsulate the
hardware components (e.g., camera, screen) within classes. The
user interacts with these components through well-defined
methods (e.g., taking a photo).

Inheritance: The Second Pillar


What Is Inheritance?

Inheritance is a fundamental concept in OOPS, i.e., object-oriented


programming that allows a subclass to inherit properties and methods
from a superclass without re-implementing them. Think of it as a child
inheriting genetic traits and skills from their parents.

How Inheritance Simplifies Code

1. Code Reusability: You don't have to reinvent the wheel. If you


have a class for "Vehicle," you can create subclasses like "Car"
and "Bicycle" that inherit the standard features like "move" and
"stop." There is no need to rewrite these functions for each type of
vehicle.
2. Modularity: Inheritance promotes a modular approach. Changes
or updates to the base class automatically apply to all its
subclasses. For example, enhancing the "Vehicle" class with a
"fuelEfficiency" attribute will benefit all its subclasses.

Real-life examples of Inheritance

1. Animal Kingdom: In a virtual zoo application, you might have a


base class "Animal." From there, you can create subclasses like
"Mammal," "Bird," and "Reptile," each inheriting common traits like
"eat" and "sleep."
2. Software Interfaces: In Java, interfaces enable multiple classes to
inherit common methods, ensuring a consistent interaction with
different objects. For example, the "Serializable" interface allows
various classes to be serialized for storage or transmission.
Polymorphism: The Third Pillar
Polymorphism is like the chameleon of coding—it adapts to its
environment. It's the third pillar of Oops, and it's all about the power of
versatility.

What Is Polymorphism?

Polymorphism is a powerful tool that enables objects of various classes to


be treated as objects of a common superclass. This feature allows for the
assumption of multiple forms, making it an invaluable asset in software
development.

Real-World Applications of Polymorphism

1. Shapes in Graphics: Consider graphics software. You have


shapes like circles, squares, and triangles, each with a "draw"
method. Polymorphism allows you to treat all these shapes
uniformly, simplifying code. When you call "draw" in any form, it
behaves according to its specific class.
2. Payment Methods: In an e-commerce system, you may have
multiple payment methods like credit cards, PayPal, and bank
transfers. Each method has a "processPayment" function.
Polymorphism enables you to use a generic "processPayment"
function that adapts to the chosen payment method at runtime.
3. Animal Sounds: In a virtual pet game, you can have a variety of
animals—dogs, cats, and birds. Each animal makes a sound when
you interact with it. Polymorphism lets you call a "makeSound"
function on any animal object, producing the appropriate sound
based on the animal's class.

Abstraction: The Fourth Pillar


What Is Abstraction?

Abstraction focuses on simplifying complex reality by modeling classes


based on real-world objects. It hides the unnecessary details and exposes
only the relevant aspects. Think of it as using a TV remote—you don't
need to know the internal circuitry to change channels.

How Abstraction Enhances Code Readability

1. Focus on Essentials: Abstraction lets you concentrate on the


core functionality of a class without being distracted by
implementation details. For instance, in a car class, you're
concerned with "start" and "stop" methods, not the intricacies of
the engine.
2. User-Friendly: It makes code more user-friendly. Other
developers can understand and use your classes without
comprehending every intricate detail. It's like driving a car without
needing to be an automotive engineer.

Real-Life Examples of Abstraction

1. Smartphones: Smartphone users interact with a high-level


interface, like tapping icons on the screen. The inner workings of
the OS, hardware, and software are abstracted, offering a user-
friendly experience.
2. Online Shopping: When you add items to your cart and click
"checkout," you interact with an abstracted system. It simplifies the
shopping process without revealing the complexities of inventory
management, payment processing, and shipping logistics.

Class:
A class is a blueprint for creating objects (instances), and it encapsulates
data (attributes) and behaviors (methods) that relate to those objects.

What is an abstract class?


In general terms, an abstract class is a class that is intended to be used for
inheritance. It cannot be instantiated. An abstract class can consist of
both abstract and non-abstract methods.
Constructor (__init__ method):
The constructor, also known as the `__init__` method in Python, is a
special method that gets automatically called when you create an instance
of a class. It is used to initialize the attributes of the object.

Here's a detailed explanation of the constructor (`__init__` method):

1. **Purpose**: The primary purpose of the constructor is to initialize the


instance variables (attributes) of an object when it is created.

2. **Syntax**: The constructor method is defined within the class like any
other method but has a reserved name `__init__`. It takes at least one
parameter, conventionally named `self`, which refers to the current
instance of the class. Additionally, it can take other parameters needed to
initialize the object's attributes.

3. **Initialization of Attributes**: Inside the constructor, you assign


values to the instance variables using the `self` keyword followed by a dot
(`.`) and the name of the variable. This binds the attribute to the object.

4. **Automatic Invocation**: When you create an object of the class


using the class name followed by parentheses (like a function call), Python
automatically invokes the `__init__` method for that class, passing the
newly created object as the `self` parameter and any other arguments
specified during object creation.

5. **Example**:
```python
class MyClass:
def __init__(self, arg1, arg2):
self.attribute1 = arg1
self.attribute2 = arg2
```
In this example, the `__init__` method takes two arguments (`arg1` and
`arg2`) in addition to `self`. It assigns the values of `arg1` and `arg2` to
the instance variables `attribute1` and `attribute2`, respectively.

6. **Initialization with Default Values**: You can also provide default


values for parameters in the constructor, allowing you to create objects
without specifying all parameters explicitly.

The constructor is fundamental in Python classes as it ensures that objects


are properly initialized with the required attributes, providing a clean and
organized way to create objects and manage their state.

various types of constructors in C++?


The most common classification of constructors includes:

Default Constructor
Non-Parameterized Constructor
Parameterized Constructor
Copy Constructor
1. Default Constructor
The default constructor is a constructor that doesn’t take any arguments. It
is a non-parameterized constructor that is automatically defined by the
compiler when no explicit constructor definition is provided.

It initializes the data members to their default values.


2. Non-Parameterized Constructor
It is a user-defined constructor having no arguments or parameters.

Example:

class base:
def __init__(self):
print("This is a non-parameterized constructor")
3. Parameterized Constructor
The constructors that take some arguments are known as parameterized
constructors.

Example:

class base:
def __init__(self, a):
print("Constructor with argument: {}".format(a))
4. Copy Constructor
A copy constructor is a member function that initializes an object using
another object of the same class.

Example:
class Student {
String name, surname; int rollNo;
Student(Student& student) // copy constructor
{
name = student.name;
surname=student.surname;
rollNo= student.rollNo;
}
}

In Python, we do not have built-in copy constructors like Java and C++ but
we can make a workaround using different methods.

26. destructor?
A destructor is a method that is automatically called when the object is
made of scope or destroyed.

In C++, the destructor name is also the same as the class name but with
the (~) tilde symbol as the prefix.
In Python, the destructor is named __del__.

Example:

class base:
def __del__(self):
print("This is destructor")

In Java, the garbage collector automatically deletes the useless objects so


there is no concept of destructor in Java. We could have used finalize()
method as a workaround for the java destructor but it is also deprecated
since Java 9.

27. Can we overload the constructor in a class?


Yes We can overload the constructor in a class in Java. Constructor
Overloading is done when we want constructor with different constructor
with different parameter(Number and Type).

28. Can we overload the destructor in a class?


No. A destructor cannot be overloaded in a class. The can only be one
destructor present in a class.

29. What is the virtual function?


A virtual function is a function that is used to override a method of the
parent class in the derived class. It is used to provide abstraction in a
class.
In C++, a virtual function is declared using the virtual keyword,

In Java, every public, non-static, and non-final method is a virtual function.

Python methods are always virtual.

Example:
class base:
def func(self):
print("This is a virtual function")

30. What is pure virtual function?


A pure virtual function, also known as an abstract function is a member
function that doesn’t contain any statements. This function is defined in
the derived class if needed.

Example:
class base {
virtual void pureVirFunc() = 0;
}

In Python, we achieve this using @abstractmethod from the ABC (Abstract


Base Class) module.
main features of OOPs
The main feature of the OOPs, also known as 4 pillars or basic principles of
OOPs are as follows:
1. Encapsulation
2. Data Abstraction
3. Polymorphism
4. Inheritance

What are access specifiers? What is their


significance in OOPs?
Access specifiers are special types of keywords that are used to specify
or control the accessibility of entities like classes, methods, and so on.
Private, Public, and Protected are examples of access specifiers or
access modifiers.
The key components of OOPs, encapsulation and data hiding, are
largely achieved because of these access specifiers.

limitations on Inheritance?
Yes, there are more challenges when you have more authority. Although
inheritance is a very strong OOPs feature, it also has significant
drawbacks.
• As it must pass through several classes to be implemented,
inheritance takes longer to process.
• The base class and the child class, which are both engaged in
inheritance, are also closely related to one another (called tightly
coupled). Therefore, if changes need to be made, they may need to
be made in both classes at the same time.
• Implementing inheritance might be difficult as well. Therefore, if not
implemented correctly, this could result in unforeseen mistakes or
inaccurate outputs.
Types of inheritance
1. Single Inheritance: Child class derived directly from the base class
2. Multiple Inheritance: Child class derived from multiple base
classes.
3. Multilevel Inheritance: Child class derived from the class which is
also derived from another base class.
4. Hierarchical Inheritance: Multiple child classes derived from a
single base class.
5. Hybrid Inheritance: Inheritance consisting of multiple inheritance
types of the above specified.

You might also like