Introduction To Python Inheritance

In this article you will learn about Python inheritance.

Python inheritance – Before moving ahead, let’s learn a little bit about Python class

Python Inheritance – It is the main class that inherited all methods and properties from another class which are parent (base) class and child (derived) class.

Parent Class – It is the class from all methods and property will be taken.

Child Class – It is the class that will be inherited from another class.

Create a Parent Class – There is no new syntax to create this class. It is similar to another class, in other words any class can be a parent class.

Example 1- Create a parent class with properties.

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

     def Myweb(self):
         print(self.name, self.age)

 x = Myblog('Alex', 20)
 x.Myweb() 
python-inheritance

Explanation – In this example, here class named – Myblog is a parent class; inside it two function is created by using def keyword. One of them (functions) is stored property as name and age and another is self to get the value in pirnt statement. When class called (class value stored in the variable x), __init__() function initialized and transferred the value to property and executed the statement.

Create a Child Class – It inherited all functions from parent class as parameter.

Example 2- Create an empty class.

class Student (Myclass):
        pass

Explanation – In this example, here class named – Myclass is a parent class of child class name Student, (child class – Student) taking the value from parent class (Myclass).

Note: Use ‘pass’ keyword when not need to pass any function and value.

Example 3- Create a class name Myblog which will inherit functions and property.

class Myblog:

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

     def Myweb(self):
         print(self.name, self.age)

class Student (Myblog):
     pass

 x = Myblog('Alex', 20)
 x.Myweb() 
python-inheritance

Output –

python-inheritance

Explanation – In this example, here class named – Myblog is a parent class of child class name Student, (child class – Student) taking the value from parent class (Myblog) and executing the def function name Myweb.

Add the __init__() Function – When a class is used to create an object, then __init__() Function called automatically.

Example 4- Add the __init__() function to the parent class.

class Myblog(Student):
    def __init__(self, name, age):

Explanation – In this example, here class named – Myblog is a parent class that stored a function in it name __init__ function.

Note:  Adding the __init__() function, the child class will no longer work for the parent’s __init__() function.

To save the inheritance of the parent’s __init__() function, add a call to the parent’s __init__() function

Example 5- Using __init__ function to replace ‘pass’ keyword.

class Myblog:

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

     def Myweb(self):
         print(self.name, self.age)

class Student (Myblog):
     def __init__(self, name, age):
           super.__init__(self, name, age)
 
 x = Myblog('Alex', 20)
 x.Myweb() 
python-inheritance

Output –

python-inheritance

Explanation – In this example, here class named – Myblog is a parent class of child class name Student, (child class – Student) taking the value from parent class (Myblog) and executing the def function name Myweb.

Now it’s done with adding __init__() function and saved the inheritance of parent class. Now to add function in __init__() function.

The super() Function – This function allows the child class to take all methods and property from its parent class.

Example 6- Added super() function method to take all methods and property from its parent class.

class Myblog:

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

     def Myweb(self):
         print(self.name, self.age)

class Student (Myblog):
     def __init__(self, name, age):
           super().__init__(self, name, age)
 
 x = Myblog('Alex', 20)
 x.Myweb() 
python-inheritance

Output –

python-inheritance

Explanation – In this example, here class named – Myblog is a parent class of child class name Student, (child class – Student) taking the value from parent class (Myblog) and executing the def function name Myweb.

By using the super() function, no need to have the name of the parent object, it will automatically inherit the methods and properties from its parent class.

Add a property – Adding a property to its class name.

Example 7- Adding a property to its class name.

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

    def Myweb(self):
         print(self.name, self.age)

class Student (Myblog):
     def __init__(self, name, age):
           super().__init__(self, name, age)
          self.address = 'NYC'           
 
x = Student('Alex', 20)
print(x.address) 
python-inheritance

Output

python-inheritance

Example 8- Added a method called intro to Student class.

class Myblog:
     
     def __init__(self, name, age):
         self.name = name
         self.age = age
     
     def Myweb(self):
         print(self.name, self.age)

 class Student (Myblog):
     def __init__(self, name, age):
           super().__init__(self, name, age)
          self.address = 'NYC'

     def intro(self):
           print('I  am', self.name, 'and my age is', self.age, 'and I live in', 
         self.address )
 
x = Student('Alex', 20)
print(x.address) 
python-inheritance

Output

python-inheritance

Explanation – In this example, here class named – Myblog is a parent class of child class name Student, (child class – Student) taking the value from parent class (Myblog) and executing the def function name Myweb.

Also added a method to return a statement from object’ value and to execute it.

If you find anything incorrect in the above-discussed topic and have any further questions, please comment down below.

Like us on

Leave a Comment

Stay in the loop

codingstreets