Menu Close

Python Inheritance Tutorial

Python Inheritance

Hello Python Programmer, In this article we are going to learn all about Python inheritance with examples.
Inheritance is the most important part of Object-Oriented Programming languages.
If you don’t know about Python inheritance, don’t worry at the end of this article, you will able to work with Python inheritance.

What is Python Inheritance?

Python Inheritance is the most powerful feature of the object oriented programming language. Inheritance in Python allows us to create a new class using another existing class.


Inheritance is used for reusability. Inheritance allows users to define a class that accesses all the properties and methods from the base or parent class.
The parent class is the class being inherited from, also called a base class.

The child class is a class that inherits all the properties and methods from the parent class or base class. The child class is also called the derived class.

Syntax

The syntax of Python inheritance is:-

class BaseClass:
  Body of base class
  
class DerivedClass(BaseClass):
  Body of derived class

Create Parent class

In Python, any class can be parent class.

Example:

Create a class named Person, with properties firstname and lastname and method fullName.

class Person:
    def __init__(self, firstname, lastname):
        self.first = firstname
        self.last = lastname
	
    def fullName(self):
        print('My first name is:- ', self.first)
        print('My last name is:- ',self.last)
	
	
p1 = Person('Mariya', 'Alex')
p1.fullName()

Create child or derived class

Create a class which inherit the functionality of the parent class, pass the parent class name as parameter in derived class.

Example:

Create a child class named Student which inherit the functionality of the Person class.

class Student(Person):
	pass

Learn more about Python pass.

create an object s1 using Student class and access the properties and methods from the Person class.

s1 = Student('John', 'Doe')
s1.fullName()
print(s1.firstname)
print(s1.lastname)

Add __init__() function

So far we have created a child class Student that inherits all the properties and methods from the Person class. You want to add the __init__() function to the child class.

Note:- The __init__() is called automatically when the class is being used to create a new object.

Example:

Add __init__() function to the Student class.

class Student(Person):
	def __init__(self, firstname, lastname):
		#add property, etc

When you add __init__() function, child class no longer inherit the parent class __init__() function.
To keep the inheritance of the parent’s __init__() function, add a call to the parent’s __init__() function.

class Student(Person):
	def __init__(self,firstname,lastname):
		Person.__init__(self,firstname, lastname)

Now we are ready to add functionality in the __init__() function.

super() function

Python has a function named super() method that provides functionality to access the properties and methods from the parent class.

class Student(Person):
	def __init__(self,firstname,lastname):
		super().__init__(firstname, lastname)

Add Properties

Add property called birthyear to the Student class.

class Student(Person):
	def __init__(self,firstname,lastname,year):
		super().__init__(firstname, lastname)
		self.birthyear = year

Add Methods

Add method Welcome to the class Student.

class Student(Person):
	def __init__(self,firstname,lastname,year):
		super().__init__(firstname, lastname)
		self.birthyear = year
		
	def Welcome(self):
		print("Welcome: ",self.first, self.last, self.birthyear)

Code summary

class Person:
    def __init__(self, firstname, lastname):
        self.first = firstname
        self.last = lastname

        
    def fullName(self):
        print('My first name is:- ', self.first)
        print('My last name is:- ', self.last)

		
class Student(Person):
    def __init__(self,firstname,lastname, year):
        super().__init__(firstname, lastname)
        self.birthyear = year
        
    def Welcome(self):
        print("Welcome: ",self.first, self.last, self.birthyear)		


	
s1 = Student("Mike", "Olsen", 2019)
s1.fullName()
s1.Welcome()

Conclusion

In this guide, you have learned all about Python Inheritance along with examples. This is the most important part of Object-Oriented Programming. Python inheritance provides a facility to create a class using another class. In a later article, we will learn all about types of inheritance along with examples.

If you like this article, please share and keep visiting for further Python interesting tutorials.

Python OOPs Tutorials

For more information:- Click Here


Frequently Asked Questions

  1. Is there inheritance in Python?

    Ans:- Yes, Python is a modern programming language that supports object-oriented programming concepts.

  2. What are the types of Inheritance in Python?

    Ans:- There are two types of Python inheritance.

    Single inheritance.
    Multiple inheritances.
    Multilevel Inheritance.

  3. Does Python support multiple inheritances?

    Ans:- Yes, Python supports multiple inheritances. A class that can be derived from more than one class is class multiple inheritances.

  4. What is single inheritance?

    Ans:- A class can be drived from only class is class single inheritance. In single inheritance, the newly created class is called drived class and the existing class is called base or parent class.

  5. How does inheritance work in Python?

    Ans:- Python inheritance allows us to define a class that inherits the properties and methods from another class. The parent class is a class being inherited from the child class. The child class is a class the inherits from the parent class is called the derived class.

Happy Coding
~ Vishvajit Rao

Python range() Function
Python set() function

Related Posts