all classes in python inherit code example
Example 1: inheritance in python
class Parent:
BloodGroup = 'A'
Gender = 'Male'
Hobby = 'Chess'
class Child(Parent):
BloodGroup = 'A+'
Gender = 'Female
def print_data():
print(BloodGroup, Gender, Hobby)
child1 = Child()
child1.print_data()
Example 2: Inheritance example python
class Parent:
def abc(self):
print("Parent")
class LeftChild(Parent):
def pqr(self):
print("Left Child")
class RightChild(Parent):
def stu(self):
print("Right Child")
class GrandChild(LeftChild,RightChild):
def xyz(self):
print("Grand Child")
obj1 = LeftChild()
obj2 = RightChild()
obj3 = GrandChild()
obj1.abc()
obj2.abc()
obj3.abc()
Example 3: how to inherit a class in python
class Bird():
def eat(self):
print ("eating")
class Sparrow(Bird):
def sound(self):
print ("ChiChi!")
birdobj = Sparrow()
birdobj.eat()
birdobj.sound()