What Are The Four Pillars of Oops? Why Do These Pillars Matter?
What Are The Four Pillars of Oops? Why Do These Pillars Matter?
Pillars of OOP:
What is OOP?
OOP isn't just another coding approach; it's a game-changer. Here's why
it matters:
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.
What Is Polymorphism?
Class:
A class is a blueprint for creating objects (instances), and it encapsulates
data (attributes) and behaviors (methods) that relate to those objects.
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.
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.
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.
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")
Example:
class base:
def func(self):
print("This is a virtual function")
Example:
class base {
virtual void pureVirFunc() = 0;
}
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.