Inheritance in Python

Inheritance in Python programming is one of the key feature of OOP. Inheritance allows us to inherit attributes and methods from one class to another class.

Program Inheritance

Program

class cls: #'cls' is name of class 'cls'.
    def fun(self):
        print("This is Function of class cls")
class cls2(cls): #class 'cls2' is child class of Class 'cls'.
    def fun2(self):
        print("This is Function of class cls2")
ob = cls2() #'ob' is object of class 'cls2'.
ob.fun2() #calling 'fun2()' function.
ob.fun() #calling 'fun()' function.
Output
This is Function of class cls2
This is Function of class cls

Program Inheritance Calling Functions

Program

class cls: #'cls' is name of class 'cls'.
    def fun(self):
        print("This is Function of class cls")
class cls2(cls): #class 'cls2' is Child of Class 'cls'.
    def fun2(self):
        print("This is Function of class cls2")
        ob.fun() #calling 'fun()' function.
ob = cls2() #'ob' is object of class 'cls2'.
ob.fun2() #calling 'fun2()' function.
Output
This is Function of class cls2
This is Function of class cls