Python Inheritance

Python Inheritance


Inheritance lets you to define a class which inherits all other classes methods and properties.

The parent class is the class from which the basic class is inherited.

The child class is the class of another class that is also known as derived class.

You can also search for these topics, types of python inheritance, same method name in python inheritance, use the inheritance of python hierarchy, list the python inheritance, example for python inheritance, python super inheritance and init method, python multiple inheritance and multiple classes, python inheritance metaclass.

Create a Parent Class

Any class may be a parent class, so that the syntax of any other class is the same :

Example :- Create a class called Person having firstname and lastname fields and a method of printname :

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()

Output :-

John Doe

You can also search for these topics, create base class python, create child class from parent python, create abstract base class python, Example for creation of parent class.

Create a Child Class

Send a parent class as parameter in generating a child class to build a class inheriting the functionality of another class :

Example 1 :- Create a class called Student, which inherits the person class properties and methods :

class Student(Person):
  pass

Note : When not adding any other properties or methods in the class, use the pass keyword.

The class now has the same properties and methods as the class of the individual.

Example 2 :- To create an object, use the student class and run the printname method :

class Person:
  def __init__(self, fname, lname):
    self.firstname = fname
    self.lastname = lname
  def printname(self):
    print(self.firstname, self.lastname)
class Student(Person):
  pass
x = Student("Mike", "Olsen")
x.printname()

Output :-

Mike Olsen

You can also search for these topics, how to make a child class in python, using inheritance to create a derived class in python, create a child class in python an exception, create a child class in python static, defining python Create a Child Class, how to make a child class in python, Example for python child class using python.

Add the __init__() Function

We've construct a child class so far that inherits its parent's properties and methods.

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

Note : When the class is used in order to create a new object, the function __init__() is executed automatically.

Example 1 :- To the Student class, add the __init__() function :

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

The child class will no longer inherit __init__() as a parent function, if you add the __init__() function.

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

Example 2 :- In order to retain the inheritance of the parent function __init__(), the parent function __init__() needs to be called :

class Person:
  def __init__(self, fname, lname):
    self.firstname = fname
    self.lastname = lname
  def printname(self):
    print(self.firstname, self.lastname)
class Student(Person):
  def __init__(self, fname, lname):
    Person.__init__(self, fname, lname)
x = Student("Mike", "Olsen")
x.printname()

Output :-

Mike Olsen

Now we've introduced the __init__() code successfully and maintained the parent class heritage, and we are ready to add the __init__() function.



You can also search for these topics, how to make a child class in python, using inheritance to create a derived class in python, create a child class in python an exception, create a child class in python static, defining python Create a Child Class, how to make a child class in python, Example for python child class using python.

Use the super() Function

Python is also super() that will enable the children's class to possess all of their parent's methods and properties :

Example :- class Student(Person) :

class Person:
  def __init__(self, fname, lname):
    self.firstname = fname
    self.lastname = lname
  def printname(self):
    print(self.firstname, self.lastname)
class Student(Person):
  def __init__(self, fname, lname):
    super().__init__(fname, lname)
x = Student("Mike", "Olsen")
x.printname()

Output :-

Mike Olsen

You do not have to supply the parent element name using the super() function and will automatically inherit the parent's method and properties.

You can also search for these topics, use the super keyword in python, use the python super function average, python use the super function match, python child class to inherit super() function.

Add Properties

Example 1 :- To the Student class, add a graduationyear attribute :

class Person:
  def __init__(self, fname, lname):
    self.firstname = fname
    self.lastname = lname
  def printname(self):
    print(self.firstname, self.lastname)
class Student(Person):
  def __init__(self, fname, lname):
    super().__init__(fname, lname)
    self.graduationyear = 2019
x = Student("Mike", "Olsen")
print(x.graduationyear)

Output :-

2019

In the following example, 2019 should be a variable and the student class must be passed on when constructing objects. To do this, in the __init__() method add another parameter :

Example 2 :- Add a parameter year and pass the year in which objects were created :

class Person:
  def __init__(self, fname, lname):
    self.firstname = fname
    self.lastname = lname
  def printname(self):
    print(self.firstname, self.lastname)
class Student(Person):
  def __init__(self, fname, lname, year):
    super().__init__(fname, lname)
    self.graduationyear = year
x = Student("Mike", "Olsen", 2019)
print(x.graduationyear)

Output :-

2019



You can also search for these topics, python property decorator, python @property example, python add property to class, python add property object, add attribute in python, add attribute in class python, python property add setter, add properties in python array, add properties in python system variables, add properties list python, add function python properties, python object add properties.

Add Methods

Example :- Add a method called welcome to the Student class :

class Person:
  def __init__(self, fname, lname):
    self.firstname = fname
    self.lastname = lname
  def printname(self):
    print(self.firstname, self.lastname)
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)
x = Student("Mike", "Olsen", 2019)
x.welcome()

Output :-

Welcome Mike Olsen to the class of 2019

The parent method iheritance will be overridden if you introduce a method in a child class with the same name as a function in the parent class.

You can also search for these topics, python add method to class definition, set add method in python, add function in python set, add method metaclass python, add magic method in python, add method to mock python, how to use Add Methods in python, python Add Methods object, Example for Add Methods in python.