0% found this document useful (0 votes)
22 views4 pages

Python OOPs Concepts

mskswkao

Uploaded by

nastiavizigina
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
22 views4 pages

Python OOPs Concepts

mskswkao

Uploaded by

nastiavizigina
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 4

Python OOPs Concepts.

Object-Oriented Programming methodologies (paradigm) deal with the following 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")

# We create a method for all birds.


def flight(self):
print("Most of the birds can fly but some cannot.")

# child class
class Penguin(Bird):

def __init__(self):
# call super() function
super().__init__()
print("Penguin is ready")

#Method overriding because that method exists in the parent class.


def flight(self):
print("Penguins cannot fly.")

peggy = Penguin()
peggy.flight()

Output

Bird is ready
Penguin is ready
Penguins cannot fly.

Method Overriding - process of re-implementing a method in the child class.


OR
Method Overriding - If a subclass provides the specific implementation of the method that has been
declared by one of its parent class.

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)

def setMass(self, mass):


self.__mass = 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

In the above program, we defined a Bird class.


We used __init__() method to store the mass of Bird. Here, notice the code:

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.

In Python, we can achieve abstraction by incorporating abstract classes and


methods.
To declare an Abstract class, we firstly need to import the abc module. The abc module also provides
the @abstractmethod decorator for indicating abstract methods. Any class that contains abstract
method(s) is called an abstract class. Abstract methods do not include any implementations – they are always
defined and implemented as part of the methods of the sub-classes inherited from the abstract class. Look at
the sample syntax below for an abstract class:
from abc import ABC, abstractmethod

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')

You can read more about abstraction in Python here: https://www.scaler.com/topics/python/data-


abstraction-in-python/

You might also like