14.inheritance
Inheritance
Inheritance allows us to define new classes using classes that have already been defined.
Child classes inherits all the methods and properties from the parent class.
Parent class is also called as base class. Child class is also called derived class.
Any class can be a parent class, that is we can derive the child classes from any class.
Create a Child Class
To create a class that inherits the functionality from the parent class, we need to pass the parent class as a parameter while creating the child class.
Creating a child class from person 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):
pass
x = Student("Bhavya", "Bindela")
x.printname()
Result:
Bhavya Bindela
In the above example, student class will have the same properties and methods as the person class.
__init__()
function
As of now, Student class inherits the properties from the Person class. In order to add any properties to the Student class exclusively, we need to add init() function.
Defining __init__()
function in the derived class
class Student(Person):
def __init__(self, fname, lname):
# we can add properties below
When you add the __init__()
function, the child class will no longer inherit the parent's __init__()
function.
To keep the inheritance of the parent's __init__()
function, add a call to the parent's __init__()
function.
Calling the parent's __init__()
function
class Student(Person):
def __init__(self, fname, lname):
Person.__init__(self, fname, lname)
Super() function
Super() function in python allows the child class to inherit all the properties and methods from it's parent class.
Inherit the properties of parent class using super() function
class Student(Person):
def __init__(self, fname, lname):
super().__init__(fname, lname)
Add properties in child class
As we are defining the initialize the function (constructor), we can add as many properties as needed.
Add properties
class Student(Person):
def __init__(self, fname, lname):
super().__init__(fname, lname)
self.graduationyear = 2020
In the above example, as graduation year changes from student to student, we want to make the year (2019) as a variable and pass this as a parameter to the student class. Then
Adding parameters to the child class
class Student(Person):
def __init__(self, fname, lname, year):
super().__init__(fname, lname)
self.graduationyear = year
x = Student("Bhavya", "Bindela", 2020)
print(x.graduationyear)
Result:
>> 2020
Add Methods in child class
We can add the methods in child class, just like we add methods in normal classes.
Adding methods
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("Bhavya", "Bindela", 2020)
x.welcome()
Reuslt:
>> Welcome Bhavya Bindela welcome to the class of 2020