top of page
Search
  • jkundycki

Pillars of Object Oriented Programming (in Python)



I have been working in Python through the lens of Data Science which has given me great opportunities to work with Data Science libraries and tools to analyze data and build interesting models. To take a break from data cleaning and exploratory data analysis, I thought I’d take a closer look at something I’ve had less exposure to, Object Oriented Programming or OOP.

To start off, let’s look at the definition of OOP.

Here is Wikipedia’s definition:

Object-oriented programming (OOP) is a programming paradigm based on the concept of “objects”, which can contain data and code: data in the form of fields (often known as attributes or properties), and code, in the form of procedures (often known as methods).

The definition does a good job of defining Object Oriented Programming, but it can be confusing to those who haven’t been exposed to OOP languages as it is an abstract concept.

The way that I’ve been using OOP is to create what is called a Domain Model, which is a representation of a real world concept that is translated into software. For example, we could use a zoo as our Domain Model. If we have a zoo as our model, then something we would want in our zoo are animals. We will create objects to represent the animals will will contain data about the animals in form of something like species or size. These animals (objects) will also have methods associated with them, which are actions each animal will be able to perform. A good example for a method an animal could have would be to speak or to make a sound. Now, using our example of a zoo as a Domain Model, let’s further define OOP by looking at the four pillars of OOP with examples.


Abstraction

The first pillar of OOP that I will cover is Abstraction. Abstraction is the ability to represent complex software systems as a domain of models of classes and objects. In the case of our example, the Domain Model we chose is a zoo. We know that we want to represent real world objects through the use of animals staying in our imagined zoo. To bring these objects to life, we will give them attributes of real animals such as species name, color, size and diet.

Some methods we might give the animals are the ability to make noise or to eat.


class Animal(object):

    def __init__(self, name):
        self.name = name
        self.size  = None
        self.species = None
        self.food_type = None
        self.color = None

    def make_noise(self):
        return("Roaaaaaaaaar!")


Encapsulation

Encapsulation is the second pillar of OOP we’ll look at. The concept of Encapsulation is one of information hiding, where you might want some sensitive information or redundant information to remain private except on a need-to-know basis. Some programming languages have keywords to explicitly state if some information is public or private but Python is not one of those languages. There is a convention in Python that a variable whose name is prefixed by an underscore should be considered private (called Name Mangling). Let’s use our zoo example to showcase this by changing the defined color of an animal.


class Animal(object):
   
    def __init__(self, name):
        self.name = name
        self.size  = None
        self.species = None
        self.food_type = None
        self._color = None
        
    def color(self):
        return(f"This animal is {self._color}")tiger = Animal("Tony")
tiger._color = 'Orange'
tiger.color()

In this example, there is a private function (denoted by the underscore prefix) that allows the user to change the color of the animal. In the example shown above, the tiger, Tony, was defined to be orange. Using the the _.color, the color was changed to orange which then shows Tony true colors when using the .color() method that was defined in the Animal Object.



Inheritance

Up to this point, we have used a great example of Abstraction in the abstract concept of an animal. To reduce the amount of redundant code, we will use the principle of Inheritance. We would not want to type all of the information each time we got an animal in our zoo. So what we could do here is define the different types of animals we expect to receive in our zoo with their attributes. So what we can do here is create a subclass of Animal that inherits the properties of it’s superclass, animal. Let’s create a subclass for a tiger from the Animal superclass since we made one for the Encapsulation example.


class Animal(object):
    
    def __init__(self):
        self.size  = None
        self.species = None
        self.food_type = None
        self.color = None
        
    def make_noise(self):
        return("Roaaaaaaaaar!")
    
class Tiger(Animal):
    
    def __init__(self):
        super().__init__()
        self.species = "Panthera tigris"
        self.size = "Large"
        self.food_type = "Carnivore"
        self.color = "Orange"

There you have it, we have created the Tiger subclass which has inherited the properties of the Animal superclass. We save time when adding new tigers to the zoo, with prefilled information about the tiger that retains properties from the Animal class. Now, if you make a tiger as shown below:


Tony = Tiger()

Tony will have inherited the ability to make noise from the Animal superclass:

Tony.make_noise()


Polymorphism

The last example will tie directly into the last pillar to be covered: Polymorphism. To start defining this, let’s start with another example. For this example, we will create a new subclass of animal. For this example, let’s create a Snake class:



class Snake(Animal):
    
    def __init__(self):
        super().__init__()
        self.size = "Medium"
        self.food_type = 'Carnivore'
        
    def make_noise(self):
        return("SSSSsssss!")

Polymorphism is the ability for multiple objects of the same base class but different subclasses to override a method of the superclass and perform different operations on that same method.

When we defined the tiger class, the animal class had a base method of make_noise() which would return “Roaaaaaaaaar!” which worked well for the Tiger subclass so nothing was changed. This make_noise() did not make much sense for the Snake subclass. So we overwrote the original superclass method with a method of the same name but it returns a snake-like noise of “SSSSsssss!” when a Snake class uses make_noise() which is an example of Polymorphism.



Concluding Remarks

There it is, a brief summary of Object Oriented Programming and the Four Pillars that define it. I hope this post was helpful to those wanting to take an initial look at OOP.


Best of luck!




0 views

Recent Posts

See All
bottom of page