in

Polymorphism and Super Class in Python Inheritance – Python Oops

In this, we will learn about Polymorphism and Super Class in Python Inheritance – python Oops:-

Polymorphism

Start learning Python from scratch for Free.

Sometimes a child may not want to use what it has inherited from the parent. The same holds true for OOP as well. If the child class does not want to use a method inherited from the parent class then it may create its own method with the same name.

When the child has a method with the same name as that of the parent, it is said to override the parent’s method. This is called as Method Overriding. Method overriding is also called as Polymorphism.

Example:

class Phone:

    def __init__(self, price, brand, camera):

        print ("Inside phone constructor")

        self.__price = price

        self.brand = brand

        self.camera = camera

    def buy(self):

        print ("Buying a phone")

    def return_phone(self):

        print ("Returning a phone")

class FeaturePhone(Phone):

    pass

class SmartPhone(Phone):

    def buy(self):

        print ("Buying a smartphone")

s=SmartPhone(20000, "Apple", 13)

s.buy()

Super() 

Even though the child class may override the methods of the parent class, it might still decide to use the parent class overridden method. To invoke anything belonging to the parent class, the child class needs to use the super() function, as shown below:

Use of Super

Super is very useful when the child class wants to have its own constructor, by overriding the parent class constructor.

Consider the below code. Since the SmartPhone class has its own constructor, the Phone class constructor is not inherited. Hence the attributes in the Phone class are also not inherited.

Example:

class Phone:

    def __init__(self, price, brand, camera):

        print ("Inside phone constructor")

        self.__price = price

        self.brand = brand

        self.camera = camera

    def buy(self):

        print ("Buying a phone")

    def return_phone(self):

        print ("Returning a phone")

class FeaturePhone(Phone):

    pass

class SmartPhone(Phone):

    def __init__(self, os, ram):

        self.os = os

        self.ram = ram

        print ("Inside SmartPhone constructor")

    def buy(self):

        print("Buying a SmartPhone")

s=SmartPhone("Android", 2)

print (s.os)

print (s.brand)

To access the parent class constructor we can use super(). Thus, the data is passed to the child class constructor, from there the data is sent to the parent class constructor and thus the attributes of the parent class get inherited.

super() function can be used to access the constructor or methods of the parent class, but not the attributes. Also super() function can be used only inside a class and not outside it.

Summary

  • A class can inherit from another class.
  • Inheritance improves code reuse
  • Constructor, attributes, methods get inherited to the child class
  • The parent has no access to the child class
  • Private properties of parent are not accessible directly in child class
  • Child class can override the attributes or methods. This is called method overriding
  • super() is an inbuilt function which is used to invoke the parent class methods and constructor

What do you think?

Written by admin

Comments

Leave a Reply

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

GIPHY App Key not set. Please check settings

Loading…

0

Abstraction in Python

Successful Features of a Chat App in 2020