Object Oriented Programming

Mahabub Arafat
7 min readSep 2, 2021

OOP or Object Oriented Programming . Ohhh… Sounds scary right? When I started to learn writing codes the one term I heard the most was OOP or object oriented programming. When I googled and read some answers and asked some seniors about the phrase OOP and they gave me why its absolute necessary to know OOP and gave me some gibberish that kinda looked like this:
class ClassName:
def __init__(self,attribute1,attribute2):
self.attribute1=attribute1
self.attribute2=attribute2
def method/action(self):
random codes
def method/action(self):
random codes
To be honest I didn’t make any sense out of it. What it actually is? Why do I need it even in code? What do these gibberish mean in the real world?
This is the explanation I needed when I was learning. So, This article is for the learners.
Actually what it is:
Its a way for us to mimic something from the real world in our codes. That’s it. You can stop reading right now, this is the tangible learning you will get from this long article. Still want more explanation and want to watch how it mimics a real world object? Fasten your seat belts, cause I am gonna take you for a ride in the world of real world objects in the universe of code.
In your codes you have many things like lists, dictionaries/hashes , functions, variables and what not. But tell me this: do you have all the items from the real world? No , right? Like you are making a forest with your code, so to make a forest you need trees. Do you have anything in your programming language that can act like a tree? That’s where OOP comes into play. This bad boy can be anything, can do anything, the limit? is your imagination.
Let’s take a Tree for example, How do we know a Tree is a Tree? Well, a tree has some attributes or characteristics like color of the leaves, how tall it is, how far the roots are spread, what kind of tree. And also a tree performs some actions like growing fruits, carbon cycle, growing flowers, collecting water and yada yada. So all these attributes and actions are the thing that defines a tree, so if we can make something that has the same attributes and actions, that can also be called a tree. So if we make a tree class:
class Tree:
def __init__(self,color_of_leaves,root_size,tallness):
self.color_leaves=color_of_leaves
self.root_size=root_size
self.tallness=tallness
def growing_flowers(self):
steps to grow flowers
def collecting_water(self):
steps of collection of water
So this is what means OOP, with OOP we can mimic real world objects. And yes we make the real world object mimics as class in OOP.
Yeah I know, Now I am speaking gibberish, but hang on, I am going to explain those gibberish one by one.

The self keyword:
self refers to the class itself, when we are saying self.attribute= attribute, that means we are declaring that class thing we built, it will have an property or attribute like this, when we are defining methods in the class, method(self) we actually passing in the class itself in the function, we know that functions work locally (means it tries to keep things as local as possible, like variables), if something is from the outside of the function, we need to pass that thing in the functions, right? like i need a variable var_1 to work inside function(),so what do we do? we pass in the function like this, function(var_1),right? by the self keyword, we are doing exactly that, we are passing in the class as a parameter of the class, and turning the function as a method to perform tasks for the class blueprint. without this self keyword there would be no difference between normal functions and methods/action in a class.

The 4 pillars:
1. Encapsulation:
Encapsulation of a class means taking all the properties and methods an object has , wrapping them up, serving them altogether what looks like a blueprint for that object. Didn’t get that? Hold on there, I am gonna explain it to you.
The attributes we have of an object , we can access them from the class like this, ClassName.attribute . So, if a class’s only duty is to serve you some info about an object then why do we even need a class, we can make a dictionary and get the same result, right?
and again, if a class’s only soul duty is to perform one action or method, so why do we even need a class, we can have the same result with functions.
So, here is the thing Encapsulation come into play while we are doing object oriented programming, that we take all the attributes of an object, all the methods of an object , put them all altogether as a package, set internal relationship between methods and attributes mimicking the real world on how they are connected, and at last serving them as a blueprint of a real world object in our codes, That’s what Encapsulation is.
2.Abstraction:
Its like hiding the complex stuff from the user, like when we have an Tree class and we need to grow some flowers , we actually don’t need to know what is the process of growing flowers or what are steps are taken inside the growing_flowers() method , we just need some flowers that we can grow with this method or action, so what this class is doing , it is hiding the complex stuffs underneath the hood. And that’s good right? because we just can not learn or know everything , our heads will burst if we try to do so. Of course we need to know some things, but not all. So what Abstraction is , it is a thing with class that hides the complex stuff underneath the hood and gives access to the user only things he/she or machine needs.
3.Inheritance:
class Tree:
def collect_water(self):
print(“collecting water”)

class AppleTree(Tree):
def __init__(self, co2):
self.co2 = co2
def grow_apples(self, leaf_color):
print(f”Have some apples of {leaf_color} color”)
def necessity(self):
print(f”give me {self.co2} to survive”)
class MangoTree(Tree):
def __init__(self, co2):
self.co2 = co2
def grow_mangoes(self, taste):
print(f”Mango Mango {taste} Mango”)
def necessity(self):
print(f”I also need {self.co2} to produce fruits”)

All these AppleTree and MangoTree classes are still Trees, and all Trees collect water , right?so we made a class Tree whose functionality is common to all trees and will be used by every kind of tree.So we created AppleTree and MangoTree class so that we can have different fruits,i.e to do different tasks, but they still Trees , right? So they will have a method or action of collecting water,so we inherited that common functionality from the Tree class. So Tree is the parent class and AppleTree and MangoTree are derived class, this is inheritance
# Of course we can change and add functionalities of the MangoTree and AppleTree class just as we need, we can even change some of the things inherited from the parent class Tree according to our need.
# Tree > parent Class
# AppleTree & MangoTree > subclass or derived class
Try These Codes out:
tree1 = AppleTree(“carbon-di-oxide”)
tree1.grow_apples(“red”)
tree1.collect_water() # see how we inherited this from Tree class
tree1.necessity()
tree2 = MangoTree(“Nitrogen”)
tree2.necessity()
tree2.grow_mangoes(“sweet”)
tree2.collect_water()

4. Polymorphism:
Poly means many and morphism means forms, so it means many forms, this is the idea that object classes can share a common action/method name,like necessity method in here , it is shared by both MangoTree and AppleTree.
class Tree:
def collect_water(self):
print(“collecting water”)

class AppleTree(Tree):
def __init__(self, gas):
self.gas = gas
def grow_apples(self, leaf_color):
print(f”Have some apples of {leaf_color} color”)
def necessity(self):
print(f”give me {self.gas} to survive”)

class MangoTree(Tree):
def __init__(self, gas):
self.gas = gas
def grow_mangoes(self, taste):
print(f”Mango Mango {taste} Mango”)
def necessity(self):
print(f”I also need {self.gas} to produce fruits”)
tree1 = AppleTree(“Carbon-di-oxide”)
tree2 = MangoTree(“Nitrogen”)

# Although method name necessity is shared by these two classes, in both classes it is doing different things based on their role in the class. So we can do this :
Try These Codes out:
tree1.necessity()
tree2.necessity()

— -> Both outputs are different, they are supposed to be different ,right? Whatever characteristics I define within the class it’s gonna obey that instruction. Although they may look the same they are not going to interfere with each other. They are going to obey their set of instructions as defined. So one action/method can have many forms and still be able to execute their task, from the example above we can say the method necessity() has two different forms but it is still executing two different tasks. So we can say the necessity() has polymorphism.
def tree_plantion(treename):
treename.necessity()
tree_plantion(tree1)
tree_plantion(tree2)

# different ways to show polymorphism
for tree in [tree1, tree2]:
tree.necessity()

[[ I took python as the base language of this article. So you need to have some knowledge of python to make the best use of this article.]]

--

--