Jovian
⭐️
Sign In

Python Inheritance

Python supports inheritance; it also supports multiple inheritances. A class can inherit attributes and behavior methods from another class (super class or parent class) called subclass or child 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:

In [ ]:
class person():
    def __init__(self,firstN,lastN):
        self.firstname = firstN
        self.lastname = lastN
        
    def printdetails(self):
        print(" Student name is " + self.firstname +" "+ self.lastname)

obj = person("Elliot", "Smith")
obj.printdetails()

    

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:

In [ ]:
class Student(Person):
  pass

Created a class named Student, which will inherit the properties and methods from the Person class:

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.

In [66]:
class person():
    def __init__(self,firstN,lastN):
        self.firstname = firstN
        self.lastname = lastN
        
    def printdetails(self):
        print(" Student name is " + self.firstname +" "+ self.lastname)

class Student(person):
 pass


obj = Student("Jerry","Tom")
obj.printdetails()
Student name is Jerry Tom

The init() Function in child class

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.

In [ ]:
class Student(Person):
  def __init__(self, fname, lname):
    #add properties etc.

Added the __init__() function to the Student class:

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

In [2]:
class person:
  def __init__(self, fname, lname):
    self.firstname = fname
    self.lastname = lname

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

class Student(person):
  def __init__(self, fname, lname):
    person.__init__(self, fname, lname)

obj = Student("Jerry", "Tom")
obj.printdetails()
Jerry Tom

The super() Function

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

Let's see an example..

In [42]:
class Person:
  def __init__(self, fname, lname):
    self.firstname = fname
    self.lastname = lname

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

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

obj = Student("Jerry", "Tom")
obj.printdetails()
Jerry Tom

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.

Adding Properties

In [45]:
class Person:
  def __init__(self, fname, lname):
    self.firstname = fname
    self.lastname = lname

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

class Student(Person):
  def __init__(self, fname, lname):
    super().__init__(fname, lname)
    self.rollno = 101

obj = Student("Jerry", "Tom")
obj.printdetails()
print(obj.rollno)
Jerry Tom 101

Added a property called rollno to the student class

Let's see another example..

In [3]:
class Person:
  def __init__(self, fname, lname):
    self.firstname = fname
    self.lastname = lname

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

class Student(Person):
  def __init__(self, fname, lname,Rollno):
    super().__init__(fname, lname)
    self.rollno = Rollno

obj = Student("Mike", "Posner", 101)
obj.printdetails()
print(obj.rollno)

Mike Posner 101

Add a rollno parameter, and pass the correct rollno when creating objects:

Adding Methods

In [64]:
class Person:
  def __init__(self, fname, lname):
    self.firstname = fname
    self.lastname = lname

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

class Student(Person):
  def __init__(self, fname, lname, Rollno):
    super().__init__(fname, lname)
    self.rollno = Rollno

  def welcome(self):
    print("Welcome Roll no", self.rollno , "Mr" , self.firstname , self.lastname , "to the class")

obj = Student("Criss", "Angels", 101)
obj.welcome()
Welcome Roll no 101 Mr Criss Angels to the class

Added a method called welcome to the Student class: