Four pillers of OOPs
Class and objects --
A class is a blueprint or template that defines the properties and behavior of an object.
An object is an instance of a class created using the class defination.
class Car:
def __inti(self, make, model, year):
self.make = make
self.model= model
self.year= year
def start_engine(self):
print("the {self.make} {self.model}'s engine is starting ")Following are the four pilers of OOPs:
Encapsulation:
Encapsulation refers to the bundling of data (attributes) and methods (functions) that operate on the data into a single unit called a class. It keeps the data safe from outside interference and misuse.
concept of hiding the implementation details of an object from the outside world and only exposing the necessary information through public methods.
helps protect the object's internal state from external interference and misuse.
Abstraction:
Abstraction is the process of hiding the complex implementation details and showing only the essential features of the object. It allows the programmer to focus on what the object does rather than how it does it.
Abstraction is the concept of showing only the necessary information to the outside world while hiding unnecessary details.
helps to simplify complex systems and focus on the essential features
Inheritance:
Inheritance is a mechanism where a new class (derived class or subclass) is derived from an existing class (base class or superclass).
The derived class inherits the features (properties and methods) of the existing class, enabling code reuse and establishing a hierarchical relationship between classes.
Inheritance is a mechanism that allows a class to inherit properties and methods from another class, called the superclass or parent class.
The class that inherits is called the subclass or child class.
Polymorphism:
Polymorphism means the ability to take multiple forms. In OOP, it refers to the ability of different objects to respond to the same message (method call) in different ways.
There are two types of polymorphism: compile-time (method overloading) and runtime (method overriding).
Polymorphism is the ability of an object to take on multiple forms.
A common way to achieve polymorphism is method overriding.
Method overriding is when a subclass provides a specific implementation of a method that is already defined in its parent class.
Example:
Lets use an example of vehicle class to explain the four pilers of the OOPs:
1. Encapsulation:
Encapsulation involves bundling data (attributes) and methods (functions) that operate on the data into a single unit (class).
in python we can achieve encaplsulation using private attributes and methods denoted by a double underscore.
Here the __make and __model are private meaning it can't be accessed directly from outside the class.
Here's how encapsulation can be applied to a
Vehicleclass:
In this example:
__makeand__modelare encapsulated attributes of theVehicleclass.display_info,start, andstopare encapsulated methods that operate on the attributes.
2. Abstraction:
Abstraction involves hiding the complex implementation details and showing only the essential features of an object.
helps to simplify complex systems and focus on essential features.
In python we can achieve this using abstract basic classes(ABC) and abstract methods
Here's how abstraction is implemented in our
Vehicleclass:
In this example:
Let’s say we have an abstract base class called
Shape. TheShapeclass is marked as an abstract class by inheriting from theABCclass (Abstract Base Class).Inside the
Shapeclass, we define an abstract method calledarea()using the@abstractmethoddecorator.The
RectangleandCircleclasses inherit from theShapeclass.They provide their own implementations of the
area()method specific to their shapes.Note that the implementation details are hidden from the outside world, and only the interface defined by the abstract class is exposed.
3. Inheritance:
Inheritance allows one class (subclass) to inherit the attributes and methods of another class (superclass).
The child class inherits all the fields and methods of the parent class and can also add new fields and methods or override the ones inherited from the parent class.
Inheritance promotes code reuse and helps create a hierarchical structure.
Here's how inheritance can be used to extend our Vehicle class:
In this example:
The
Carclass inherits thecolorattribute and thehonkmethod from theVehicleclass promoting code reuse.The
Carclass also adds its own attributes and methods, such asspeedandaccelerate.
4. Polymorphism:
Polymorphism allows objects to be treated as instances of their superclass, enabling flexibility and reusability in code.
It enables you to write generic code that can work with objects of multiple types, as long as they share a common interface.
Here's how polymorphism is demonstrated using our
VehicleandCarclasses:
In this example:
Each subclass (
Pdf,Word) ofDocumentimplement theshowmethod differently (method overriding), but the interface remains consistent giving the ability to iterate over both the classes using a single for loop.
Last updated