Inheritance

General#

Inheritance helps form relationships between classes. This means we can have child classes which inherit certain functionalities/characteristics from a parent class.

Example:

class LogicGate:
def __init__(self,n):
self.label = n
self.output = None
def getLabel(self):
return self.label
def getOutput(self):
self.output = self.performGateLogic()
return self.output
class BinaryGate(LogicGate):
def __init__(self,n):
LogicGate.__init__(self,n)
self.pinA = None
self.pinB = None
def getPinA(self):
return int(input("Enter Pin A input for gate "+ self.getLabel()+"-->"))
def getPinB(self):
return int(input("Enter Pin B input for gate "+ self.getLabel()+"-->"))

Multiple Inheritance#

Python supports multiple inheritance. Example:

class Liger(Lion, Tiger):
# define any static variables
animal_type = 'hybrid'
def __init__(self):
Lion.__init__(self)
Tiger.__init__(self)
self.state = 'happy'

In Python the class hierarchy is defined right to left. Hence, if both Lion and Tiger have a common methodtest, calling test will call the method from Lion.