computer scince and engineeringpython

python in Data Hiding Method Overriding Polymorphism Method Overriding by unitdiploma

0views

Data Hiding

•Method Overriding

•Polymorphism

Method Overriding

• We can provide some specific implementation of the parent class method in our child class.

•When the parent class method is defined in the child class with some specific implementation, then the concept is called method overriding.

•We may need to perform method overriding in the scenario where the different definition of a parent class method is needed in the child class.

Example of Method Overriding

class Animal:  

    def speak(self):  

        print(“speaking”)  

class Dog(Animal):  

    def speak(self):  

        print(“Barking”)  

d = Dog()  

d.speak()  

Output:  Barking

Real Life Example of method overriding

class Bank:  

    def getroi(self):  

        return 10;  

class SBI(Bank):  

    def getroi(self):  

        return 7;  

class ICICI(Bank):  

    def getroi(self):  

        return 8;  

b1 = Bank()  

b2 = SBI()  

b3 = ICICI()  

print(“Bank Rate of interest:”,b1.getroi());  

print(“SBI Rate of interest:”,b2.getroi());  

print(“ICICI Rate of interest:”,b3.getroi()); 

Data abstraction in python

•Abstraction is an important aspect of object-oriented programming.

•In python, we can also perform data hiding by adding the double underscore (___) as a prefix to the attribute which is to be hidden.

•After this, the attribute will not be visible outside of the class through the object.

Example of Data abstraction in python

class Employee:  

    __count = 0;  

    def __init__(self):  

        Employee.__count = Employee.__count+1  

    def display(self):  

        print(“The number of employees”,Employee.__count)  

emp = Employee()  

emp2 = Employee()  

try:  

    print(emp.__count)  

finally:  

    emp.display()  

Output

The number of employees 2 AttributeError: ‘Employee’ object has no attribute ‘__count’

Polymorphism

•Polymorphism and Method Overriding

•In literal sense, Polymorphism means the ability to take various forms.

•In Python, Polymorphism allows us to define methods in the child class with the same name as defined in their parent class.

•As we know, a child class inherits all the methods from the parent class.

Method Overloading

# Python program to demonstrate in-built poly-

# morphic functions

  # len() being used for a string

print(len(“geeks”))

  # len() being used for a list

print(len([10, 20, 30]))

Example of Method Overloading

def add(x, y, z = 0): 

    return x + y + z

  # Driver code 

print(add(2, 3))

print(add(2, 3, 4)) 

Polymorphism with Inheritance

•In Python, Polymorphism lets us define methods in the child class that have the same name as the methods in the parent class.

•In inheritance, the child class inherits the methods from the parent class. However, it is possible to modify a method in a child class that it has inherited from the parent class.

•This is particularly useful in cases where the method inherited from the parent class doesn’t quite fit the child class.

•In such cases, we re-implement the method in the child class.

•This process of re-implementing a method in the child class is known as Method Overriding.

Example of Polymorphism with Inheritance

class Bird:

  def intro(self):

    print(“There are many types of birds.”)

       def flight(self):

    print(“Most of the birds can fly but some cannot.”)

    class sparrow(Bird):

  def flight(self):

    print(“Sparrows can fly.”)

  class ostrich(Bird):

  def flight(self):

    print(“Ostriches cannot fly.”)

     obj_bird = Bird()

obj_spr = sparrow()

obj_ost = ostrich()

 obj_bird.intro()

obj_bird.flight()

 obj_spr.intro()

obj_spr.flight()

 obj_ost.intro()

obj_ost.flight() 

Output

•There are many types of birds.

• Most of the birds can fly but some cannot. There are many types of birds.

• Sparrows can fly.

•There are many types of birds.

•Ostriches cannot fly.

Constructor Overriding

#constructor overriding

class father:

   def __init__(self):

   self.p=10

   def disp(self):

   print(self.p)

class son(father):

def __init__(self):

         self.p1=20

def disp(self):

     print(self.p1)

s=son()

s.disp()

super() method

#using super() method

class father:

   def __init__(self,p=0):

   self.p=p

   def disp(self):

   print(self.p)

class son(father):

def __init__(self,p1=0,p=0):

     super().__init__(p)

     self.p1=p1

def disp(self):

     print(self.p+self.p1)

s=son(10,20)

s.disp()

Problems in multiple inheritance

#using super() method

class Gfather:

   def __init__(self):

   self.p1=’a’

   print(self.p1)

   class father:

   def __init__(self):

   self.p2=’b’

   print(self.p2)

class son(Gfather,father):

def __init__(self):

     super().__init__()

     self.p3=’c’

     print(self.p3)

s=son()

“””output

c

a”””

Method resolution order(MRO)

#using super() method

class Gfather:

   def __init__(self):

   self.p1=’a’

   print(self.p1)

   super().__init__()

  class father:

   def __init__(self):

     self.p2=’b’

   print(self.p2)

   super().__init__()

class son(Gfather,father):

def __init__(self):

       self.p3=’c’

     print(self.p3)

     super().__init__()

s=son()

Output– c  a  b

Leave a Response