PYTHON INHERITANCE
Inheritance is a mechanism of object-oriented programming for deriving new classes (sub classes) from existing ones such as super class or base class. It allows us to define a class that inherits all the methods and properties from another class.
Parent class or base class, is the class being inherited from.
Child class or derived class, is the class that inherits from another class.
HOW TO CREATE A PARENT CLASS?
Any class can be a parent class.
Python Code
class Person:
def __init__(self, Fname, Lname):
self.Fname = Fname
self.Lname = Lname
def myfunction(self):
print(self.Fname, self.Lname)
p1 = Person("John", "Doe")
p1.myfunction()
Output
John Doe
HOW TO CREATE A CHILD CLASS?
Creating Child Class
Child class inherit the properties of Parent Class it can be created by providing the parent class as a parameter when creating the child class as shown in following example, we are creating child class named as Employee which inherits the properties of parent class named as Person(refer to above example).
Python Code to create child class in Python
class Employee(Person):
pass #Use the pass keyword for not adding new features
Adding the __init__() Function to Child Class
The __init__() function is called automatically every time the class is being used to create a new object.
When we add the __init__() function to the child class, the child class will no longer inherit the parentโs __init__() function. It will override the inheritance of the parentโs __init__() function.
To keep the inheritance of the parentโs __init__() function, add a call to the parentโs __init__() function
class Person:
def __init__(self, Fname, Lname):
self.Fname = Fname
self.Lname = Lname
def myfunction(self):
print(self.Fname, self.Lname)
class Employee(Person):
def __init__(self, Fname, Lname):
Person.__init__(self, Fname, Lname)
e1=Employee("Rakesh","Sharma")
e1.myfunction()
Output
Rakesh Sharma
Creating Child Class using the super() Function
super() Function
We can also use the super() function of Python that will make the child class inherit all the methods and properties from its parent. We need not to use the name of the parent class, it will automatically inherit the properties of the parent class.
Python code to use theย super() function to create aย child class
class Person:
def __init__(self, Fname, Lname):
self.Fname = Fname
self.Lname = Lname
def myfunction(self):
print(self.Fname, self.Lname)
class Employee(Person):
def __init__(self, Fname, Lname):
super().__init__(Fname, Lname)
e1=Employee("Rakesh","Sharma")
e1.myfunction()
Output
Rakesh Sharma
Adding properties to child class
To add additional property we can add it in the __init__() function. The property named id is added below to the Employee class.
Python Code
class Person:
def __init__(self, Fname, Lname):
self.Fname = Fname
self.Lname = Lname
def myfunction(self):
print(self.Fname, self.Lname)
class Employee(Person):
def __init__(self, Fname, Lname):
super().__init__(Fname, Lname)
self.id=11
e1=Employee("Rakesh","Sharma")
print(e1.id)
Output
11
We can also add property to the child class as variable as shown In the example below, the id should be a variable, and passed into the Employee class when creating employee objects by adding another parameter in theย __init__() function:
Python Code
class Person:
def __init__(self, Fname, Lname):
self.Fname = Fname
self.Lname = Lname
def myfunction(self):
print(self.Fname, self.Lname)
class Employee(Person):
def __init__(self, Fname, Lname, Eid):
super().__init__(Fname, Lname)
self.id=Eid
e1=Employee("Rakesh","Sharma",11)
print(e1.id)
Output
11
Adding methods to the child class
class Person:
def __init__(self, Fname, Lname):
self.Fname = Fname
self.Lname = Lname
def myfunction(self):
print(self.Fname, self.Lname)
class Employee(Person):
def __init__(self, Fname, Lname, Eid):
super().__init__(Fname, Lname)
self.id=Eid
def warning(self):
print("Access allowed to id -", self.id)
e1=Employee("Rakesh","Sharma",11)
e1.warning()
Output
Access allowed to id - 11
Note- Inheritance of parent method will be overridden on using the same name of method in child class as in the parent class.
Leave a Reply