Python Inheritance

In object oriented programming inheritance is the most important while you are creating a complex class and modules in your project.Follow this article we will explain in a simple way to understand the complex feature .

What is Inheritance?

Inheritance is nothing but creating new classes which are built upon the existing class. in simple manner you can say create a new class by using the behavior of the existing class.

In another way lets understand inheritance , where we can use attributes or functions of one class into another class , without writing it from the beginning you can use that functionality in your new class.

So here we used the new class , so what is that?, The class you defined at first or beginning of the code is called as base class and the new class you creating by using your base class is called as derived class.

Also we are saying it super class, parent class or base class , similarly there is derived class or child class .

So one more important thing about inheritance is re-usability of your code so no need to write the same thing again and again.

Syntax of inheritance:
class BaseClass:

    <statements...>
    ......:
    .....
        
class DerivedClass(BaseClass):

    <  statements.... >
    .....

Here we declare two classes , one is BaseClass and one is DerivedClass .

Where we used base class inside a parenthesis of derived class.

Example:

class Department():
    def __init__(self, dept_name,dept_type):
        self.dept_name = dept_name
        self.dept_type = dept_type

    def details(self):
        print("I am from ", self.dept_name, )
        print ("I am working in ", self.dept_type)


class Employee(Department):
    def __init__(self, dept_name, dept_type):
        self.dept_name = dept_name
        self.dept_type = dept_type
        self.name = "Etik"


derived_obj = Employee("IT", "Software")
derived_obj.details()

Output:

I am from  IT
I am working in  Software

Explanation:

Here we called base class function  using derived class object even if def details(self):  function present inside base class and we are able to access that.

Now we have to call the __init__ method from  our derived class  and access it directly without repeating the code.

Calling Constructor of base class.

 

Example:

class Department():
    def __init__(self, dept_name,dept_type):
        self.dept_name = dept_name
        self.dept_type = dept_type
    def details(self):
        print("I am from ", self.dept_name, 'department' )
        print ("I am working in ", self.dept_type)


class Employee(Department):
    def __init__(self, dept_name, dept_type,name):
        super().__init__(dept_name, dept_type)
        self.name = name
        print ("My name is ",self.name)

base_obj = Employee("IT", "Software",'Evan')
base_obj.details()

Output:

My name is  Evan
I am from  IT department
I am working in  Software

Explanation:

Here you created a object or instance of derived class .When you did that it will go to class Employee(Department) this line.

Then it will call the __init__() method inside Employee and then called the Department  __init__() method and initialize the value.

Again come to the Employee __init__() method and initialize the name, that’s why My name is Evan came as the first output,

Then finally it called the display() method and  executed.


Hope you understand the simple behavior of inheritance , follow our site to get more info regarding python, comment and share this article.

Be a master in python, join our crash course and trained from the  well experienced professionals contact us.

Happy Learning!!

8 thoughts on “Python Inheritance”

  1. I am an investor of gate io, I have consulted a lot of information, I hope to upgrade my investment strategy with a new model. Your article creation ideas have given me a lot of inspiration, but I still have some doubts. I wonder if you can help me? Thanks.

  2. Pdgfra deletion at earlier embryonic stages caused excessive apoptosis of these regions, while Pdgfra deletion at later stage resulted in much reduced apoptosis and only in a subset of these regions online cialis Pharmacotherapeutic group determination in north america