Inheritance Programs in Python for Practice

Inheritance Programs in Python:

Example-1:

class Parent():
       def first(self):
           print('Hi Parent!')
 
class Child(Parent):
       def second(self):
          print('Hi Child!')
 
ob = Child()
ob.first()
ob.second()

Example-2:

class GrandParent:
   def func1(self):
        print("It is My Grand Parent")
class Parent:
   def func2(self):
        print("It is My Parent")
class Child(GrandParent , Parent):
    def func3(self):
        print("It is My Child")
 
ob = Child()
ob.func1()
ob.func2()
ob.func3()

Overriding methods in Python using Inheritance:

Example-3:

class A:   
  "Parent Class"
  def display(self):
    print ('This is base class.')

class B(A):  
   "Child/Derived class"
   def display(self):
     print ('This is derived class.')

obj = B()
obj.display()

Leave a Reply

Your email address will not be published. Required fields are marked *