🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to Python Notes
Topic #108

Python Inheritance


Python Inheritance

Inheritance allows us to define a class that inherits all the methods and properties from another class.

Parent class is the class being inherited from, also called base class.

Child class is the class that inherits from another class, also called derived class.


Create a Parent Class

Any class can be a parent class, so the syntax is the same as creating any other class:

Example

class Person:
  def __init__(self, fname, lname):

self.firstname = fname
    self.lastname = lname

def printname(self):
    print(self.firstname,
self.lastname)

#Use the Person class to create an object, and then
execute the printname method:

x = Person("John", "Doe")
x.printname()

Create a Child Class

To create a class that inherits the functionality from another class, send the parent class as a parameter when creating the child class:

Example

class Student(Person):
  pass

Note: Use the pass keyword when you do not want to add any other properties or methods to the class.

Now the Student class has the same properties and methods as the Person class.

Example

  x = Student("Mike", "Olsen")
x.printname()

Add the __init__() Function

So far we have created a child class that inherits the properties and methods from its parent.

We want to add the __init__() function to the child class (instead of the pass keyword).

Note: The __init__() function is called automatically every time the class is being used to create a new object.

Example

class Student(Person):
  def __init__(self, fname, lname):

#add properties etc.

When you add the __init__() function, the child class will no longer inherit the parent's __init__() function.

Note: The child's __init__() function overrides the inheritance of the parent's __init__() function.

To keep the inheritance of the parent's __init__() function, add a call to the parent's __init__() function:

Example

class Student(Person):
  def __init__(self, fname, lname):

Person.__init__(self, fname, lname)

Now we have successfully added the __init__() function, and kept the inheritance of the parent class, and we are ready to add functionality in the __init__() function.


Use the super() Function

Python also has a super() function that will make the child class inherit all the methods and properties from its parent:

Example

class Student(Person):
  def __init__(self, fname, lname):

super().__init__(fname, lname)

By using the super() function, you do not have to use the name of the parent element, it will automatically inherit the methods and properties from its parent.


Add Properties

Example

class Student(Person):
  def __init__(self, fname, lname):

super().__init__(fname, lname)
    self.graduationyear
= 2019

In the example below, the year 2019 should be a variable, and passed into the Student class when creating student objects. To do so, add another parameter in the __init__() function:

Example

class Student(Person):
  def __init__(self, fname, lname, year):

super().__init__(fname, lname)
    self.graduationyear
= year

x = Student("Mike", "Olsen", 2019)

Add Methods

Example

class Student(Person):
  def __init__(self, fname, lname, year):

super().__init__(fname, lname)
    self.graduationyear
= year

  def welcome(self):
    print("Welcome",
self.firstname, self.lastname, "to the class of", self.graduationyear)

If you add a method in the child class with the same name as a function in the parent class, the inheritance of the parent method will be overridden.

Want to go beyond the notes?

Join CodingNow 2.0's Python course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Python Inheritance – FAQs

Quick answers about learning Python Inheritance in Python.

This free note from CodingNow 2.0 explains Python Inheritance in Python — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Python topic on CodingNow 2.0, including Python Inheritance, is 100% free with no signup required.
With focused practice, most students grasp Python Inheritance in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now