Inheritance

In Python object class is super class all python classs

In python our class is child class by default

if method or constructor or variable is not available in child class. then implicitly execute parent class method or constructor or variable

always creative child class object it is recommended

class A10:
	pass
class B10:
	pass
class C10:
	pass
class D10(object):
	pass
print(issubclass(A10,object))
print(issubclass(B10,object))
print(issubclass(C10,object))				
print(issubclass(D10,object))
True
True
True
True
class A10:
	pass
class B10(A10):
	pass
class C10(B10):
	pass
class D10(object):
	pass
print(issubclass(A10,object))
print(issubclass(B10,object))
print(issubclass(C10,object))				
print(issubclass(D10,object))
True
True
True
True
class A10:
	pass
class B10(A10,object):
	pass
class C10(B10,object):
	pass
class D10(C10,object):
	pass
print(issubclass(A10,object))
print(issubclass(B10,object))
print(issubclass(C10,object))				
print(issubclass(D10,object))
True
True
True
True
class A10:
	def displayInsatneMethod1(self):#instance method
		print("Instance Method A10")
class B10(A10):
	def displayInsatneMethod2(self):#instance method
		print("Instance Method B10")
b10=B10();#child class Object
b10.displayInsatneMethod1()
b10.displayInsatneMethod2()	
Instance Method A10
Instance Method B10

if child have no constructor implicitly execute parent class constructor

class A10:
	
	def __init__(self):
		print("parent class Constructor")

class B10(A10):
	
	pass

b10=B10();#child class Object

parent class Constructor

if child have constructor then it not go to the parent class

class A10:
	
	def __init__(self):
		print("parent class Constructor")

class B10(A10):
	
	def __init__(self):
		print("child class Constructor")

b10=B10();#child class Object
child class Constructor

Type of inheritance in python

  • Single Inheritance
  • Multilevel Inheritance
  • Multiple Inheritance
  • Hierarchical Inheritance
  • Hybrid Inheritance

Single Inheritance

One parent class One child class

class A10:
	pass
class B10(A10):
	pass

Multilevel Inheritance

One Grand Father One parent class One child class

class A10:
	pass
class B10(A10):
	pass
class C10(B10):
	pass
class A10:
	def getName1(self):
		print("getName1")
class B10(A10):
	def getName2(self):
		print("getName2")
class C10(B10):
	def getName3(self):
		print("getName3")

c10=C10()
c10.getName3()
c10.getName2()
c10.getName1()	
getName3
getName2
getName1

Multiple Inheritance

one child class with multiple parent class

class A10:
	pass
class B10:
	pass
class C10(A10,B10):
	pass
class A10:
	def getName1(self):
		print("getName1")
class B10:
	def getName2(self):
		print("getName2")
class C10(A10,B10):
	def getName3(self):
		print("getName3")

c10=C10()
c10.getName1()
c10.getName2()
c10.getName3()
getName1
getName2
getName3

Hierarchical Inheritance

One parent class with multiple child class

class A10:
	pass
class B10(A10):
	pass
class C10(A10):
	pass
class A10:
	def getName1(self):
		print("getName1")
class B10(A10):
	def getName2(self):
		print("getName2")
class C10(A10):
	def getName3(self):
		print("getName3")

c10=C10()
c10.getName1()
c10.getName3()


b10=B10()
b10.getName1()
b10.getName2()
getName1
getName3
getName1
getName2

Hybrid Inheritance

Hybrid inheritance is combination of Multiple and Hierarchical Inheritance

def __str__(self) function python

class HrEmploye:
	
	def __init__(self,fname,lname):
		self.fname=fname
		self.lname=lname
class Employee(HrEmploye):

	def __init__(self,fname,lname,email):
		#super().__init__(fname,lname)
					# OR
		HrEmploye.__init__(self,fname,lname)
		self.email=email


	def __str__(self):
		return "Fname:{} Lname:{} Email:{}".format(self.fname,self.lname,self.email)

emp= Employee("Ashutosh","Mishra","ashumishra9015@gmail.com")
print(emp)
emp= Employee("Arun","Singh","arunsingh9015@gmail.com")
print(emp)
Fname:Ashutosh Lname:Mishra Email:ashumishra9015@gmail.com
Fname:Arun Lname:Singh Email:arunsingh9015@gmail.com

isinstance function python

class A11:
	pass
class B11(A11):
	pass
a1=A11()
a2=B11()	
print(isinstance(a1,A11))
print(isinstance(a2,B11))	
print(isinstance(a2,A11))
print(isinstance(a1,B11))
True
True
True
False