INTRODUCTION:
Heyy !! How are you? Welcome to another lesson. Hope you
liked the surprise post, if you haven’t checked it out yet, here it is PYTHON INTERMEDIATE’S GUIDE | CREATING YOUR FIRST PYTHON GAME, HANGMAN.
Today, we will be learning about the four pillars of OOP (if you don’t know
what is oop? You should probably check this article out: PYTHON INTERMEDIATE’S GUIDE | OBJECT-ORIENTED PROGRAMMING).
The four pillars of oop are: Encapsulation, Polymorphism,
Abstraction and Inheritance. We will learn about all of them one by one,
starting with Encapsulation.
ENCAPSULATION:
Encapsulation refers to two concepts:
1. In
OOP, objects grow variables which hold state and methods that alter state in a
single unit – the object.
2. Encapsulation
can also mean hiding a class’ internal data to prevent the client form directly
accessing it.
In
programming, the code outside the class that uses an object is called a client.
Example:
class Data:
def __init__(self):
self.nums =
[1,2,3,4,5]
def change_data(self,
index, n):
self.nums[index]
= n
data = Data()
data.nums[0] = 100
data change_data(1,99)
Now, the problem is if you change the list into
a tuple, you break the code for all of your clients that are
expecting a mutable list.
Private variables and private methods solve these problems. These are variables and methods that
objects can access but clients cannot.
Python doesn’t have private methods and variables
(unfortunately). It uses naming
conventions to address the problem. In Python, if you have a variable or
method, the caller should not access, you should precede its name with an
underscore (‘_’).
ABSTRACTION:
It is a process of ‘”taking away” or removing
characteristics from something to reduce it to a set of essential
characteristics.
You can use
abstraction in Object Oriented Programming when you model objects using classes
and omit unnecessary details. For Example:
class
Person():
def
__init__(self, h, w):
self.weight
= w
self.height
= h
Here, we created a
‘Person()’ class and used characteristics like height and weight but we omitted
other details such as hair colour and eye colour.
POLYMORPHISM:
It is the ability in programming to present the same
interface for different data types. Example:
print(‘Hello’)
print(200)
print(200.1)
Here, we used the same
interface (print()) for three different data types.
INHERITANCE:
Inheritance in
programming is similar to genetic inheritance. Classes can inherit methods and variables from another class. The
class inherited from is called ‘parent
class’ and the class inherited is called ‘child class’.
You can use
inheritance in the following way
class
Shape():
def
__init__(self,w,l):
self.width
= w
self.length
= l
def
print_size(self):
print(‘’’{}
by {}’’’.format(self.width, self.length) )
You can learn about
the format() function in my article on manipulating strings.
Now we will be using
inheritance, using the following code:
class
Square(Shape):
pass
Here, we used the
‘Shape’ class as the parent class and the ‘Square’ class as the child class.
Now the ‘Square’ class has all the functions and the properties of the parent
class, ‘Shape’.
Important: 1) The
keyword ‘pass’ basically tells
Python not to do anything. This reduction in code is important because avoiding
repeating code makes your program smaller and more manageable.
2)
When a child class uses a method from the parent class, you
can overwrite it by using the same name. Example:
class Square(Shape):
def print_size(self):
print(“square”)
a_square = Square(20,20)
CONGRATULATIONS !!! You now know about the four pillars of OOP: Encapsulation, Abstraction, Polymorphism, and Inheritance. Like I always say, Have fun with it !!! Create some cool programs. Good luck !!!!
So, this was about the Four Pillars of Object-Oriented Programming. Stay tuned for another article next week, same time, where we will learn about Package Managers & GitHub. So more cool stuff coming your way, DON’T MISS IT !!
I hope this article answered all of your questions and even helped you in becoming a better programmer. IF IT DID, leave a like AND FOLLOW THIS BLOG TO BECOME A PROFESSIONAL PYTHON PROGRAMMER FROM A TOTAL BEGINNER. IF IT DIDN'T, feel free to ask any further queries in the comment section below.
HOPE YOU HAVE AN AWESOME DAY AHEAD !!!









0 Comments
Welcome to the comments section, this is where you can contact me personally for any doubts or feedback