Inheritance in Python

Different types of Inheritance in Python

Polymorphism in Python :

Polymorphism is one of the core concepts of object-oriented programming (OOP) and it describes the concept that we can access objects of different types through the same interface.

Polymorphism and Inheritance using Method Overriding:

Method Overriding

Method Overriding is an important concept in object-oriented programming (OOP) and itallows us to redefine a method by overriding it.

	
	class A:
		def __init__(self):
			print("__init__() of class A")
			
		def display(self):
			print("display() from class A")
        
	class B(A):
		def __init__(self):
			print("__init__() of class B")
			
		def display(self):
			print("display() from class B")
        
	b = B()
	print(b.display())
		
	
	#  Output : 
		__init__() of class B
		display() from class B
	

Note : Method Overloading is not supported in Python:

	
	class A:
		def __init__(self):
			print("__init__() of class A")
			
		def display(self):
			print("display() from class A")
			
		def display(self, a):
			print("display({a}) from class A".format(a))
			
	a = A()
	print(a.display())
	print(a.display("Sam"))
		
	
	#  Output : 
	__init__() of class A
	Traceback (most recent call last):
	  File "", line 12, in 
	TypeError: display() missing 1 required positional argument: 'a'