Inheritance : – Deriving a new class from existing class and its contain all the properties of that class is called inheritance. With the help of inheritance we do not need to write same code for multiple times its save our time make the code more reusable. That’s the basic principle of oops programming , Do not repeat your self.

It is also called single level inheritance.

isinstance() :- isinstance () is a function it is use to check whether a object is a instance of the same class or not . It give True for if it is or it give False if it is not.

issubclass() :- issubclass() is a function use to check a class is instance of of that class or not , Its take two argument first as instance class or second is parent class. It is also return the answer in True or False.

Example :-

# base class
class person:

    def __init__(self,organization,profession):
        self.organization = organization
        self.profession = profession

    def get_details(self):
        return f"{self.profession} at {self.organization}"

# derived class from base class called inheritance 
class student(person):

    def get_details(self):
        return f"{self.profession} at {self.organization}"

# this is a person object 
p1 = person("Employee","Microsoft")

print(p1.get_details())

# this is a student object 
s1 = student("Student","Integral University")

print(s1.get_details())

print(isinstance(s1, student))

print(issubclass(student, person))

Output:-

True

True

Employee at Microsoft

Student at Integral University

Overriding :- After inherit the property from base class and make changes in those property is called overriding.

Example:-

class employee:

    company = "Google"

    def __init__(self,name,age):
        self.name = name
        self.age = age

    def get_details(self):
        return f"My name is {self.name} and age is {self.age} and a normal employee"

class programmer(employee):

    # overriding the class attribute
    company = "Facebook"

    language = "Python"

    # overriding the instance method 
    def get_details(self):
        return f"Name is {self.name} age is {self.age}, i'm a {self.language} 
                    developer and my company is {self.company}"


emp1 = employee("John",21)
print(emp1.get_details())

prog1 = programmer("Asif",20)
print(prog1.get_details())

Output:-

My name is John and age is 21 and a normal employee

Name is Asif age is 20, i'm a Python developer and my company is Facebook

Multilevel Inheritance :- More than One class is derived from base called is called multilevel inheritance.

Example :-

# Base class
class grand_father:

    def get_details(self):
        return "I am grandfather"

# Derived class from base class but this is base class for son
class dad(grand_father):

    def get_details(self):
        return "I am father and i have property of grandfather"


# Derived class from base class (dad) 
class son(dad):

    def get_details(self):
        return "I am son and i have property of both father and grandfather"


ob2 = grand_father()

ob3 = dad()

obj1 = son()

print(obj2.get_details())

print(obj3.get_details())

print(obj1.get_details())

Output:-

I am grandfather

I am father and i have property of grandfather

I am son and i have property of both father and grandfather

Another Example :- What if derives class does not have any property and we try to access the derived class lets see an example with son class

class grand_father:

    def get_details(self):
        return "I am grandfather"


class dad(grand_father):
    pass


class son(dad):
    pass

obj2 = grand_father()
obj3 = dad()
obj1 = son()

print(obj1.get_details())

Output :-

I am grandfather

Explanation :- When we try to access the property of son class, because it does not have any property then it goes into her base class in dad class and check there there is also no any property , Then it goes to grandfather class and then get the property and print the result because son class contain both the property of dad and grandfather. This is called inheritance

Multiple Inheritance :- A class is derived from two or more different class is called multiple inheritance.

Example :-

class employee:

    name =""

    age = ""


class player:

    game = ""


# it inherit from both employee and player class 
class programmer(employee,player):

    def get_details(self):
        return f"Name is {self.name}, age is {self.age}, 
                      and i love playing {self.game}"


prog1 = programmer()
prog1.name = "John"
prog1.age = "21"
prog1.game = "Cricket"

print(prog1.get_details())

Output :-

Name is John, age is 21, and i love playing Cricket

Explanation :- Employee is a class, player is a different class , and programmer is a class that inherit from both employee and player class means it have both the properties of that class.

This is all about inheritance and its types. I hope you understand the concept of inheritance please do a comment if you have any query or suggestion.

Thank You!