OOP: How to implement Object-Oriented Programming concepts in python

OOP: How to implement Object-Oriented Programming concepts in python

Object-oriented programming is a programming paradigm that uses the concept of "objects" in storing data and the procedures for manipulating the data. With OOP, programmers can easily reuse, maintain and modify software codes. Fortunately, Python is one of the programming languages that support OOP.

At the end of this tutorial, you'll be able to create classes and objects with Python. You'll also be able to implement some basic OOP concepts in Python.

Python class and object

The most important Python functionalities for applying Object-oriented programing concepts are classes and objects.

A class is a blueprint for creating an object. it stores the properties and methods that will be used by the object. A real-world example is creating the building plan of a house. It contains the properties (door, window, area,...) and procedures(floor plans, plumbing systems, electrical systems ...) for building the house. This description can then be used to create a house.

An object creates an instance of a class. It's when you put the functionalities in a class to use. It's like when you use a defined blueprint to create a house. Many objects can be created from the same class.

Here is how you create a class and object in Python:

# 01
class House:
    # 02
    def __init__(self, rooms, size):
        self.rooms = rooms
        self.size = size

    # 03
    def description(self):
        return f"We are building a {self.size} house with {self.rooms} rooms"

    def floor_plan(self):
        return "This is the description for the building's floor plans"

#creates an object
house1 = House(4, "3000 square feet")
print(house1)
#returns the object

print(p1.rooms)
#returns 4

print(p1.size)
#returns "3000 square feet"

print(p1.description() )
#returns We are building a 3000 square feet house with 4 rooms"

Now let's break down the code:

#01 
class House:
...

First, we use the class keyword to create a class named House.

...
# 02
def __init__(self, rooms, size):
    self.rooms = rooms
    self.size = size
...

Next, we used the __init__() function to assign values to the class properties. The self parameter reference the current instance of the class.

# 03
def description(self):
    return f"We are building a {self.size} house with {self.rooms} rooms"

def floor_plan(self):
    return "This is the description for the building's floor plans"

We created 2 methods. the first stores all the descriptions of the house while the second method stores the floor plans.

#creates an object
house1 = House(4, "3000 square feet")
print(house1)
#returns the object

print(house1.rooms)
#returns 4

print(house1.size)
#returns "3000 square feet"

print(house1.description() )
#returns "We are building a 3000 square feet house with 4 rooms"

When you create an object with the class name ( House(...) ), all the functionalities of the class becomes available for use with the object.

Inheritance

Inheritance is when a class inherits functionalities(properties and methods) from existing classes. Think of it like a mother passes down some of her traits and behaviour(hair colour, race,...) to her daughter.

A derived class is the class that inherits while a base class is a class whose functionality is being inherited.

Here is the Python implementation:

#base class
class Mother:
    def hair_color(self):
        return "I have a blonde hair"

    def hobby(self):
        return "I love dancing"

#derived class
class Daughter(Mother):
    pass

#creates object from Daughter class
daughter1 = Daughter()

#calls the methods inherited from Mother class
daughter1.hair_color()
#returns "I have a blonde hair"

daughter1.hobby()
#returns "I love dancing"

Encapsulation

Encapsulation is a method of preventing outer classes from accessing or overriding the functionalities of a class. Think of it like when an individual has some improper behaviour he doesn't want anyone to know.

in Python, we use private attributes for hiding data. private attributes are properties that can only be accessed within the class. It must be prefixed with a double underscore(__).

Here's the implementation in Python:

# 01
class Individual:
    # 02
    def __init__(self, name, age, status):
        self.name = name
        self.age = age
        self.__smokes = status

    # use getter function to get the private attribute
    def get_smokes(self):
        return self.__smokes

    #use setter function to modify the value of the private attribute
    def set_smokes(self, newStatus):
        self.__name = newStatus

p1 = Individual('Alex', 25, True)

p1.name
#returns 'Alex'

p1.age
#returns 25

p1.__smokes
#returns error because you can't access the value of a private attribute

Polymorphism

Polymorphism is when a subclass overrides or extends the functionalities of a parent class. It's like when a daughter doesn't have the same hobby as her mom.

Here's the implementation in Python.

#base class
class Mother:
    def hair_color(self):
        return "I have a blonde hair"

    def hobby(self):
        return "I love dancing"

#derived class
class Daughter(Mother):
    def hobby(self):
        return "I love dancing"

#creates object from Daughter class
daughter1 = Daughter()

#calls the method of the Mother class
daughter1.hair_color()
#returns "I have a blonde hair"

#aclls the method of the Daughter class instead
daughter1.hobby()
#returns "I love dancing"

In the above code, the hobby() method of the Daughter class has overridden the hobby() method of the Mother class.

Conclusion

If OOP is properly implemented, it can help developers write maintainable and clean code. At the end of this tutorial, you should have been able to understand how classes and objects are created. Also, you should have known how to implement the four pillars of OOP in Python. Thanks for reading.