Python Inheritance Tutorial.
A key feature of any object-oriented programming (OOPs) language is inheritance. Python also supports inheritance, just like other OOPs programming languages. This facilitates the efficient design and easy reusability of code.
As per object-oriented programming terminology, the inherited class is called the base class or parent class, and the class inheriting is called the derived class or child class.
A programmer can use inheritance to use existing methods and data members of one class in another. All members/methods inherited from a base class are included in the derived class.
Inheritance in Python with an example:
A person can serve as a good example of a base class, and derived classes may include an employee, student, doctor, etc. The information provided by employees includes personal information as well.
As part of the inheritance tutorial, we will create a base class for a person and two derived classes from it, one for the employee and another for the student.
Following is the declaration of the base class named person:
The person class will include data members such as name, age, address, etc. Each member will be accessed, changed, and printed using the methods for getting, setting, and printing.
class Person:
def __init__(self, name, age, address):
self.name = name;
self.age = age;
self.address = address;
def getName(self):
return self.name;
def getAge(self):
return self.age;
def getAddress(self):
return self.address;
def setName(self, name):
self.name = name;
def setAge(self, age):
self.age = age;
def setAddress(self, address):
self.address = address;
# Create The Object of class person
personObj = Person("Andy", 26, "Camborn UK");
print(" Name = ", personObj.getName());
print(" Age = ", personObj.getAge());
print(" Address = ", personObj.getAddress());
py Person.py
Name = Andy
Age = 26
Address = Camborn UK
How to create a drived class from a base class.
In the following example, a programmer is required to develop a software solution for the management of employees within an organization. As a result, a new class titled employee must be created. We can reuse the code by inheriting the person class from the employee class since each Employee is also a Person.
class Employee(Person):
pass;
class Person:
def __init__(self, name, age, address):
self.name = name;
self.age = age;
self.address = address;
def getName(self):
return self.name;
def getAge(self):
return self.age;
def getAddress(self):
return self.address;
def setName(self, name):
self.name = name;
def setAge(self, age):
self.age = age;
def setAddress(self, address):
self.address = address;
#Person Class Ends here
class Employee(Person):
pass;
# Create The Object of class person
employeeObj = Employee("Andy", 26, "Camborn UK");
print(" Name = ", employeeObj.getName());
print(" Age = ", employeeObj.getAge());
print(" Address = ", employeeObj.getAddress());
py Person.py
Name = Andy
Age = 26
Address = Camborn UK
Now we can access all members of the class Person
from the object of the Employee
class. A derived class may have additional members along with base class members to add more functionality. A programmer can add any method in a derived class (even if the same is present in the base class).
In the above example, the __init__ function of the base class is used. We can write the same function in the derived class. After that, the employee object will use the init of the Employee class.
Defining the same function again in the derived class is called function overriding.
class Employee(Person):
def __init__(self,designation, employeeId, name, age, address):
self.designation = designation;
self.employeeId = employeeId;
# Calling init function of person class
Person.__init__(self,name, age, address);
Define own functions and call base class functions from Employee class – Full example of inheritance.
class Person:
def __init__(self, name, age, address):
self.name = name;
self.age = age;
self.address = address;
def getName(self):
return self.name;
def getAge(self):
return self.age;
def getAddress(self):
return self.address;
def setName(self, name):
self.name = name;
def setAge(self, age):
self.age = age;
def setAddress(self, address):
self.address = address;
#Person Class Ends here
class Employee(Person):
def __init__(self,designation, employeeId, name, age, address):
self.designation = designation;
self.employeeId = employeeId;
Person.__init__(self,name, age, address);
def printDetails(self):
print(" Designation = ", self.designation);
print(" Employee Id = ", self.employeeId);
print(" Name = ", Person.getName(self));
print(" Age = ", Person.getAge(self));
print(" Address = ", Person.getAddress(self));
# Create The Object of class Employee
employeeObj = Employee("Programmer", 12345, "Andy", 26, "Camborn UK");
employeeObj.printDetails()
py Person.py
Designation = Programmer
Employee Id = 12345
Name = Andy
Age = 26
Address = Camborn UK
.