Bernard Aybouts - Blog - MiltonMarketing.com

FAQ: Exploring the Pillars of Object-Oriented Programming: A Pythonic Journey

FAQ

Approx read time: 7.3 min.

1. Encapsulation

Encapsulation refers to the bundling of data with the methods that operate on that data, or the restriction of direct access to some of an object’s components. This concept is often used to hide the internal representation, or state, of an object from the outside.

class BankAccount:
    def __init__(self, account_number, balance=0):
        self._account_number = account_number  # Protected attribute
        self._balance = balance  # Protected attribute

    def deposit(self, amount):
        if amount > 0:
            self._balance += amount
            return True
        return False

    def withdraw(self, amount):
        if 0 < amount <= self._balance:
            self._balance -= amount
            return True
        return False

    def get_balance(self):
        return self._balance

# Example usage
account = BankAccount('12345', 100)
account.deposit(50)
print(account.get_balance())  # 150
account.withdraw(20)
print(account.get_balance())  # 130

Encapsulation in Python is a fundamental concept of object-oriented programming (OOP) that involves bundling the data (attributes) and methods (functions) that operate on the data into a single unit, or class, and restricting access to some of the object’s components. This means that the internal representation of the object is hidden from the outside, only allowing access through a public interface. In simpler terms, encapsulation is about keeping the details about how an object does something hidden away from the outside world, allowing you to change those details without affecting the code that uses the object.

Encapsulation is achieved in Python in the following ways:

  1. Using Private Attributes and Methods: Python does not have explicit support for access modifiers like public, protected, and private found in other languages such as Java or C++. Instead, it relies on naming conventions to denote the privacy level of a member variable or method. Prefixing an attribute or method name with a single underscore (_) implies that it is protected and should not be accessed directly from outside the class. Prefixing with double underscores (__) makes an attribute or method private, causing name mangling which makes it harder (but not impossible) to access from outside the class.
  2. Using Property Decorators: Python provides property decorators that allow you to define methods that can be accessed like attributes. This feature can be used to implement getters and setters for manipulating private attributes, thereby controlling access to the attributes’ values.

2. Inheritance

Inheritance allows us to define a class that inherits all the methods and properties from another class. The new class is called a derived (or child) class, and the class that it inherits from is called the base (or parent) class.

class Vehicle:
    def __init__(self, name, max_speed):
        self.name = name
        self.max_speed = max_speed

    def drive(self):
        print(f"The {self.name} drives at  km/h.")

class Car(Vehicle):
    def __init__(self, name, max_speed, mileage):
        super().__init__(name, max_speed)
        self.mileage = mileage

    def drive(self):
        print(f"The {self.name} car with mileage {self.mileage} km/l drives at  km/h.")

# Example usage
my_vehicle = Vehicle("Generic Vehicle", 100)
my_vehicle.drive()

my_car = Car("Honda", 200, 15)
my_car.drive()

 

Inheritance in Python is a fundamental concept of object-oriented programming (OOP) that allows one class (the child or derived class) to inherit attributes and methods from another class (the parent or base class). This mechanism enables the reuse of code, helps to create a hierarchical classification of classes, and contributes to a more efficient and streamlined code structure.

Inheritance provides the following benefits:

  • Code Reusability: Allows a new class to reuse the methods and variables of an existing class.
  • Transitivity: If class B inherits from class A, and class C inherits from class B, then class C implicitly inherits from class A.
  • Extensibility: Enables modifying or extending the behaviors of a base class without altering it.

3. Polymorphism

Polymorphism allows us to define methods in the child class with the same name as defined in their parent class. It is the ability of an object to take on many forms.

class Bird: def intro(self): print(“There are many types of birds.”) def flight(self): print(“Most of the birds can fly but some cannot.”) class Sparrow(Bird): def flight(self): print(“Sparrows can fly.”) class Ostrich(Bird): def flight(self): print(“Ostriches cannot fly.”) # Example usage bird = Bird() sparrow = Sparrow() ostrich = Ostrich() bird.intro() bird.flight() sparrow.intro() sparrow.flight() ostrich.intro() ostrich.flight()

 

Types of Polymorphism in Python

There are two primary types of polymorphism in Python:

  1. Ad-hoc Polymorphism: Achieved through function overloading or method overloading. However, Python does not support method overloading by default. It can be achieved indirectly through default arguments or variable-length arguments.
  2. Parametric Polymorphism: Achieved through functions that can operate on any type of data. In Python, this can be seen in functions that can take objects of any type, as long as they support a specific method or interface (e.g., iterable objects in a for loop).

4. Abstraction

Abstraction means hiding the complex implementation details and showing only the essential features of the object. In Python, we achieve abstraction by using abstract classes and methods.

from abc import ABC, abstractmethod class Shape(ABC): @abstractmethod def area(self): pass @abstractmethod def perimeter(self): pass class Rectangle(Shape): def __init__(self, width, height): self._width = width self._height = height def area(self): return self._width * self._height def perimeter(self): return 2 * (self._width + self._height) # Example usage rectangle = Rectangle(10, 20) print(rectangle.area()) # 200 print(rectangle.perimeter()) # 60

 

Abstraction in Python, an essential concept in object-oriented programming (OOP), involves hiding the complex reality while exposing only the necessary parts. It is a mechanism to hide the detailed implementation and show only the relevant features of an object. In simpler terms, abstraction focuses on what an object does instead of how it does it, allowing programmers to work with ideas at a higher level of complexity.

Abstraction is implemented in Python through abstract classes and abstract methods. Abstract classes are classes that cannot be instantiated on their own and are designed to be subclassed. They often contain one or more abstract methods.

Abstract Classes and Methods

An abstract method is a method that is declared in the abstract class but may not have any implementation. Subclasses of the abstract class are required to implement these abstract methods. This concept allows for creating a blueprint for other classes to follow, ensuring a certain level of consistency across subclasses.

Python provides the abc module (Abstract Base Classes) to enable abstraction. To define an abstract class, you inherit from ABC (Abstract Base Class), and to define an abstract method, you use the @abstractmethod decorator.

Exploring the Pillars of Object-Oriented Programming: A Pythonic Journey

This article provides a detailed line-by-line explanation of a Python code that illustrates the core concepts of Object-Oriented Programming (OOP): encapsulation, inheritance, polymorphism, and abstraction.

Code Explanation

We will examine a simplified Python code that demonstrates the OOP concepts. Note: The specific code snippet for detailed examination was not provided; thus, a generic explanation is given here. Replace this with the actual code snippet for precise explanations.

Example Python Code

# Define a class named BankAccount
class BankAccount:
    # Constructor method with parameters for account number and balance, defaulting balance to 0
    def __init__(self, account_number, balance=0):
        self._account_number = account_number  # Protected attribute
        self._balance = balance  # Protected attribute

    # Method to deposit money into the account
    def deposit(self, amount):
        if amount > 0:  # Check if the amount is positive
            self._balance += amount  # Add the amount to the balance
            return True  # Return True to indicate success
        return False  # Return False if the amount is not positive

    # Method to withdraw money from the account
    def withdraw(self, amount):
        if 0 < amount <= self._balance:  # Check if the amount is positive and less than or equal to the balance
            self._balance -= amount  # Subtract the amount from the balance
            return True  # Return True to indicate success
        return False  # Return False if the conditions are not met

    # Method to get the current balance of the account
    def get_balance(self):
        return self._balance  # Return the balance of the account

The above code demonstrates encapsulation by using protected attributes (prefixed with an underscore) and providing methods to interact with these attributes safely. This ensures that the internal representation of the account balance and number is hidden from external access, enforcing a layer of security and data integrity.

Leave A Comment


About the Author: Bernard Aybout (Virii8)

Avatar of Bernard Aybout (Virii8)
I am a dedicated technology enthusiast with over 45 years of life experience, passionate about computers, AI, emerging technologies, and their real-world impact. As the founder of my personal blog, MiltonMarketing.com, I explore how AI, health tech, engineering, finance, and other advanced fields leverage innovation—not as a replacement for human expertise, but as a tool to enhance it. My focus is on bridging the gap between cutting-edge technology and practical applications, ensuring ethical, responsible, and transformative use across industries. MiltonMarketing.com is more than just a tech blog—it's a growing platform for expert insights. We welcome qualified writers and industry professionals from IT, AI, healthcare, engineering, HVAC, automotive, finance, and beyond to contribute their knowledge. If you have expertise to share in how AI and technology shape industries while complementing human skills, join us in driving meaningful conversations about the future of innovation. 🚀