Class inheritance is a term used in object-oriented programming to describe one class inheriting properties (class variables) and methods (class functions) from a parent class. Usually, the class that inherits methods and properties from another class will add on to those with its own methods and properties, resulting in a class that is similar to the parent class but extended.
If you are unfamiliar with classes in Python, you should have a look at
Object-oriented programming in Python
before learning about inheritance.
Inheritance in a nutshell
In programming, there are two kinds of classes involved in inheritance: a parent class and a child class, the child class is the one that inherits properties and methods from the parent class.
Inheriting properties and methods from another class in Python
In Python, inheriting properties and methods from a parent class is fairly simple, all you need to do is include the parent class in parentheses when defining your child class, and include a constructor for your parent class within the constructor of your child class.
class child( parent ):
def __init__( self ):
super().__init__( )
super() is used to refer to the parent class, this can be replaced with a direct reference to that class, but must then be passed a "self" value.
class child( parent ):
def __init__( self ):
parent.__init__( self )
Once you've inherited from a parent class, you can make use of any properties and methods from that class as if it were defined in the child class.
As an example, let's create the parent class "Person" with the properties "name" and "surname" and a "greet()" method that will output the name and surname, and the child class "Student" that will inherit from "Person" as well as add its own property "studentNo".
class Person:
def __init__( self, name, surname ):
self.name = name
self.surname = surname
def greet( self ):
print( "My name is " + self.name + " " + self.surname )
class Student( Person ):
def __init__( self, name, surname, studentNo ):
super().__init__( name, surname )
self.studentNo = studentNo
person = Person( "John", "Doe" )
person.greet()
student = Student( "Bob", "Doe", 152 )
student.greet()
As you can see in the above example, when we create a Student object, you can still make use of all the inherited properties and methods from the parent class.
When creating a constructor for a child class, you should ensure to include any parameters that were included for the parent class so you can pass those values to the parent constructor (if required), you can then also include more parameters to be used for any unique properties of the child class.
Overriding methods from the parent class
Sometimes when inheriting methods from a parent class, there could be methods that are no longer sufficient for the child class and would need to be rewritten. For example, if you were again inheriting from the Person class, the greet() method may not output all the necessary properties.
To override methods in Python, all you need to do is re-define them. As an example, let's again create the Person and Student parent and child classes, except this time we will override the greet() method such that it also outputs the student number.
class Person:
def __init__( self, name, surname ):
self.name = name
self.surname = surname
def greet( self ):
print( "My name is " + self.name + " " + self.surname )
class Student( Person ):
def __init__( self, name, surname, studentNo ):
super().__init__( name, surname )
self.studentNo = studentNo
def greet( self ):
print( "My name is " + self.name + " " + self.surname + ", student No: " + str( self.studentNo ) )
person = Person( "John", "Doe" )
person.greet()
student = Student( "Bob", "Doe", 152 )
student.greet()
This example would output the following:
My name is John Doe
My name is Bob Doe, student No: 1