Python Polymorphism

Table Of Contents:

  1. What Is Polymorphism?
  2. Examples Of Polymorphism.
  3. Polymorphism With Class Methods.
  4. Polymorphism With Inheritance.
  5. Method Overloading.
  6. Method Overriding.

(1) What Is Polymorphism?

  • Polymorphism is a technique, by using which you can make a particular object to be used in different ways.
  • For example, you can use your car for personal transport or as a passenger car.
  • You can use your mobile phone for voice calls or watch videos on it.
  • As these single objects can be used in multiple ways, this is called Polymorphism

(2) Examples Of Polymorphism?

Example-1:Polymorphism In Addition Operator.

  • + Operator is mostly used for the addition of two digits.
  • It also can be used for joining two strings together.

Use-1

sum = 24 + 55

print(sum)
Output:
79

Use-2

join = 'Subrat ' + ' Abhispa'

print(join)
Output:
Subrat Abhispa

Example-2: Polymorphic len() Function

  • + Operator is mostly used for the addition of two digits.
  • It also can be used for joining two strings together.

Use-1

name = 'Abhispa Pattnaik'

print(len(name))
Output:
16

Use-2

lst = ['A','B','C','D','E']

print(len(lst))
Output:
5

(3) Polymorphism With Class Methods?

  • We can create a method inside the class that will be useful for multiple purposes.
  • This method will be called the polymorphic method.

Example-1

class Addition:
    def add(self,*args):
        return sum(args)
Output:
79

Creating Object Of Addition Class

obj = Addition()
obj.add(5,6)
obj.add(5,6,7,8)
obj.add(-1,2,-3,4,-5)
Output:
11
26
-3

Note:

  • Here you can see that one add() method is used to add multiple numbers.
  • Hence this add() method is called the polymorphic method.

Example-2

class Man:
    def __init__(self,having):
        self.having = having
    def relation(self):
        if self.having == 'Sister':
            print('Relationship Brother')
        elif self.having == 'Wife':
            print('Relationship Husband')
        elif self.having == 'Mother':
            print('Relationship Son')

Creating Object Of Addition Class

role1 = Man('Sister')
role2 = Man('Wife')
role3 = Man('Mother')

role1.relation()
role2.relation()
role3.relation()
Output:
Relationship Brother
Relationship Husband
Relationship Son

Note:

  • Here relationship() is the polymorphic method.
  • Which plays multiple roles like Brother, Husband and Son.

(4) Polymorphism With Inheritance.

  • Inheritance helps us inherit the parent class’s properties and behavior into the child class.
  • If you don’t like implementing the parent class method, you can change it inside the child class.

Example

Parent Class

class Mobiles:
    def default_settings(self):
        self.model = 'Samsung'
        self.camera = '5MP'
        self.battery = '1000MAH'
        self.memory = '10GB'
        
    def view_settings(self):
        print('Model:',self.model)
        print('Camera:',self.camera)
        print('Battery:',self.battery)
        print('Memory:',self.memory)

Child Class

class Apple(Mobiles):
    def default_settings(self):
        self.model = 'Apple'
        self.camera = '15MP'
        self.battery = '5000MAH'
        self.memory = '128GB'

Creating Object Child Class

my_mobile = Apple()
my_mobile.default_settings()
my_mobile.view_settings()

Note:

  • Here if you don’t like the default settings of your mobile phone, you can always change them.

(5) Method Overriding.

  • If you don’t like the implementation of the parent class method, you can always change the implementation of it in the child class.
  • In the other words, you are overriding the parent class method.

Example

Parent Class

class SongsLiked:
    def my_songs(self):
        print('90\'s Songs')

Child Class

class MyPlayList(SongsLiked):
    def my_songs(self):
        print('Hip Hop Songs')

Creating Object Child Class

playlist = MyPlayList()
playlist.my_songs()
Output:
Hip Hop Songs

Note:

  • Here if you don’t like 90’s old songs, you can always change your playlist.
  • Here my_song() method is overridden.

(5) Method Overloading

  • If a class has multiple methods having the same name but different parameters, it is known as Method Overloading.
  • In Python, method overloading is not possible.

Note:

  • Here if you don’t like 90’s old songs, you can always change your playlist.
  • Here my_song() method is overridden.

Example

class Addition:
    def add(self,a,b):
        return a+b
    def add(self,a,b,c):
        return a+b+c

Creating Object Of Addition Class

obj = Addition()
obj.add(1,2)
Output:
TypeError                                 Traceback (most recent call last)
Input In [26], in <cell line: 1>()
----> 1 obj.add(1,2)

TypeError: add() missing 1 required positional argument: 'c'

Note:

  • Here I have defined two add() methods with different arguments.
  • But when I am calling with only two arguments, it is giving me an error.
  • Because the latest method will only be considered in Python, here it is add(a,b,c), Hence it is asking for argument ‘c’

Leave a Reply

Your email address will not be published. Required fields are marked *