In this, we will learn about Polymorphism and Super Class in Python Inheritance – python Oops:-
Polymorphism
Start learning Python from scratch for Free.
- Introduction to Programming – Day1
- Introduction to Algorithms – Day 2
- Python operators and variables – Day 3
- Python Control Structures – Day 4
- Functions in Python – Day 5 of Python Series
- How to setup Eclipse IDE for Python? – 6th Day of Python Series
- Collection Data types in Python – 7th Day of Python Series
- Python Debugging and Testing –Python Series-8th day
- Python Built-in Functions, Libraries, and modules – Day 9
- Python regular expressions and lambda functions – Day 10
- Introduction of Python Object-oriented programming(Python Oops) – Day 12
- Python Object and Classes – Day 13
- Encapsulation in Python- Day 14
- Inheritance in Python – Day 15
- Abstraction in Python
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
GIPHY App Key not set. Please check settings