Python OOPs Concepts
Python OOPs Concepts
Inheritance
Polymorphism
Encapsulation
Abstraction
Inheritance
Inheritance is the process by which one class takes on the attributes
and methods of another. Inheritance is a way of creating a new class
for using details of an existing class without modifying it. The newly
formed class is a derived class (or child class). Similarly, the existing
class is a base class (or parent class).
# parent class
class Bird:
def __init__(self):
print("Bird is ready")
def whoisThis(self):
print("Bird")
# child class
class Penguin(Bird):
def __init__(self):
# call super() function
super().__init__()
print("Penguin is ready")
peggy = Penguin()
peggy.whoisThis()
Output
Bird is ready
Penguin is ready
Bird
In the above program, we created two classes i.e. Bird (parent class) and Penguin (child class). The
child class inherits the functions of parent class. We can see this from the whoisThis() method.
Polymorphism
Polymorphism means having multiple forms for an existed
concept, it allows the same interface for different objects. So, in
practice, you can have a function with the same name but
different implementation. Consider the inheritance, the child
class is inheriting methods and attributes of parent class. It is
possible for child class to have a method with the same name as
the parent but different implementation.
For example, we need to determine if the given species of birds fly or not, using polymorphism we
can do this using a single function.
# parent class
class Bird:
def __init__(self):
print("Bird is ready")
def whoisThis(self):
print("Bird")
# child class
class Penguin(Bird):
def __init__(self):
# call super() function
super().__init__()
print("Penguin is ready")
peggy = Penguin()
peggy.flight()
Output
Bird is ready
Penguin is ready
Penguins cannot fly.
Encapsulation
Encapsulation is one of the fundamental concepts
in object-oriented programming (OOP). It describes
the idea of wrapping data and the methods that
work on data within one unit. This puts restrictions
on accessing variables and methods directly and
can prevent the accidental modification of data. To
prevent accidental change, an object’s variable can
only be changed by an object’s method. Those
types of variables are known as private variables.
A class is an example of encapsulation as it encapsulates all the data that is member functions,
variables, etc.
Using OOP in Python, we can restrict access to methods and variables. This prevents data from direct
modification which is called encapsulation. In Python, we denote private attributes using underscore
as the prefix i.e single _ or double __.
# parent class
class Bird:
def __init__(self):
self.__mass = 100
print("Bird is ready")
def ShowMass(self):
print("Mass is", self.__mass)
# child class
class Penguin(Bird):
def __init__(self):
# call super() function
super().__init__()
print("Penguin is ready")
peggy = Penguin()
peggy.ShowMass()
peggy.__mass = 1000
peggy.ShowMass()
peggy.setMass(1000)
peggy.ShowMass()
Output
Bird is ready
Penguin is ready
Mass is 100
Mass is 100
Mass is 1000
self.__mass = 100
Here, we have tried to modify the value of __mass outside of the class. However, since __mass is a
private variable, this modification is not seen on the output.
As shown, to change the value, we have to use a setter function i.e setMass() which takes mass as a
parameter.
You will get an error if you call the member outside of the class:
peggy = Penguin()
print(peggy.__mass)
Output
builtins.AttributeError: 'Penguin' object has no attribute '__mass'
You can read more about protecting variables and methods here:
https://www.tutorialsteacher.com/python/public-private-protected-modifiers
Abstraction
Abstraction is one of the most important features of object-oriented programming. It is used to hide the
background details or any unnecessary implementation.
For example, when you use a washing machine for laundry purposes. What you do is you put your laundry and
detergent inside the machine and wait for the machine to perform its task. How does it perform it? What
mechanism does it use? A user is not required to know the engineering behind its work. This process is
typically known as data abstraction, when all the unnecessary information is kept hidden from the users.
class Fruit(ABC):
@abstractmethod
def taste(self):
pass
@abstractmethod
def color(self):
pass
class Apple(Fruit):
def taste(self):
print('tangy')
def color(self):
print('tangerine')
class Lemon(Fruit):
def taste(self):
print('sweet')
def color(self):
print('yellow')