Inheritance In Python Programming
Inheritance In Python Programming

Tutorial for Python Inheritance – OOPs in python

Inheritance is one of the powerful features of object-oriented programming (OOPs) language. Like other OOPs programming languages, Python too supports inheritance. Which allows efficient and easy code reusability.

By using inheritance, we can inherit methods and data members of one class into another. All inherited members/methods of a class become part of the new class.

In the terminology of OOPs, the inherited class is a base class or parent class and the class inheriting is a Derived or Child class.

Inheritance in Python with an example:

Base and Derived Class Example in Python
Base and Derived Class Example in Python

In the real-world a person can be a good example for a base class and the derived classes could be an employee, student, doctors, etc. Because employee information also includes personal information. We will cover the inheritance tutorial with one base class for a person and two derived class one for the employee and another student.

Declare the base class person:

In a person, the class will have data members like name, age, and address. It will implement the methods for getting, setting, and print to access, change, and printing data members.

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());
Output-
py Person.py
Name = Andy
Age = 26
Address = Camborn UK

Create a Derived Class – Example

In the following, we need to create software for employee management in a company. In that case, we need to write an employee class. As each Employee is also a Person, so let’s reuse the code by inheriting person class into employee class.

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());
Output-
>py Person.py
Name = Andy
Age = 26
Address = Camborn UK

Now we can access all Person class members too from the object of the Employee class. But the derived class will have its own additional members, to enhance the base class. We can add any method in a derived class (even 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 Employee class.

Define 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()
Output - 
py Person.py
Designation = Programmer
Employee Id = 12345
Name = Andy
Age = 26
Address = Camborn UK

.