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.
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 :-
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 :-
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 :-
Now we've introduced the __init__()
code successfully and maintained the parent class heritage, and we are ready to add the __init__()
function.
Related Links
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 :-
You do not have to supply the parent element name using the super()
function and will automatically inherit the parent's method and properties.
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 :-
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 :-
Related Links
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 :-
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.