inheritance types in python code example

Example 1: inheritance in python

# creating parent class
class Parent:
    BloodGroup = 'A'
    Gender = 'Male'
    Hobby = 'Chess'
    
# creating child class
class Child(Parent): # inheriting parent class
    BloodGroup = 'A+'
    Gender = 'Female
    
    def print_data():
        print(BloodGroup, Gender, Hobby)
    
# creating object for child class
child1 = Child()
# as child1 inherits it's parent's hobby printed data would be it's parent's
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: inheritance in python 3 example

class Robot:
    
    def __init__(self, name):
        self.name = name
        
    def say_hi(self):
        print("Hi, I am " + self.name)
        
class PhysicianRobot(Robot):
    pass

x = Robot("Marvin")
y = PhysicianRobot("James")

print(x, type(x))
print(y, type(y))

y.say_hi()