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:

  1. Encapsulation:

    1. 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.

    2. concept of hiding the implementation details of an object from the outside world and only exposing the necessary information through public methods.

    3. helps protect the object's internal state from external interference and misuse.

  2. Abstraction:

    1. 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.

    2. Abstraction is the concept of showing only the necessary information to the outside world while hiding unnecessary details.

    3. helps to simplify complex systems and focus on the essential features

  3. Inheritance:

    1. Inheritance is a mechanism where a new class (derived class or subclass) is derived from an existing class (base class or superclass).

    2. The derived class inherits the features (properties and methods) of the existing class, enabling code reuse and establishing a hierarchical relationship between classes.

    3. Inheritance is a mechanism that allows a class to inherit properties and methods from another class, called the superclass or parent class.

    4. The class that inherits is called the subclass or child class.

  4. Polymorphism:

    1. 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.

    2. There are two types of polymorphism: compile-time (method overloading) and runtime (method overriding).

    3. Polymorphism is the ability of an object to take on multiple forms.

    4. A common way to achieve polymorphism is method overriding.

    5. 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 Vehicle class:

In this example:

  • __make and __model are encapsulated attributes of the Vehicle class.

  • display_info, start, and stop are 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 Vehicle class:

In this example:

  • Let’s say we have an abstract base class called Shape. The Shape class is marked as an abstract class by inheriting from the ABC class (Abstract Base Class).

  • Inside the Shape class, we define an abstract method called area() using the @abstractmethod decorator.

  • The Rectangle and Circle classes inherit from the Shape class.

  • 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 Car class inherits the color attribute and the honk method from the Vehicle class promoting code reuse.

  • The Car class also adds its own attributes and methods, such as speed and accelerate.

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 Vehicle and Car classes:

In this example:

  • Each subclass (Pdf, Word) of Document implement the show method differently (method overriding), but the interface remains consistent giving the ability to iterate over both the classes using a single for loop.

Last updated