Python Program on Inheritance
Master Python with 70+ Hands-on Projects and Get Job-ready - Learn Python
Inheritance, a fundamental concept in object-oriented programming, enables the creation of new classes that inherit attributes and behaviors from existing ones. This mechanism fosters code reuse, allowing developers to establish a hierarchy of classes with shared functionalities.
In Python, inheritance facilitates the development of more organized and efficient code by promoting the reuse of common attributes and methods.
This article explores the practical aspects of inheritance in Python, emphasizing its role in building upon existing code, enhancing code modularity, and simplifying the design of complex software systems.
Topic Explanation:
In Python, inheritance is a cornerstone of code organization, providing a mechanism for creating classes that inherit attributes and methods from their parent classes. Derived classes, also known as subclasses, can extend or override inherited functionalities, promoting code flexibility and adaptability. This enables the creation of specialized classes that inherit the general characteristics of a broader class, fostering a modular and scalable code structure.
The article delves into how inheritance streamlines code development, reduces redundancy, and establishes clear relationships between classes, ultimately contributing to a more maintainable and comprehensible codebase.
Practically, the implementation of inheritance in Python enables the creation of specialized classes that inherit the properties of a broader class while incorporating unique features. This approach simplifies the design of complex software systems by breaking down functionality into modular, interconnected components. The subclass inherits the characteristics of the superclass, reducing redundancy and promoting a more efficient and maintainable code structure.
Prerequisites:
Object-Oriented Programming (OOP) Understanding:
- Familiarity with essential OOP concepts such as classes, objects, and methods.
Python Class and Method Concepts:
- Knowledge of defining classes and methods in Python.
Basic Python Syntax:
- Proficiency in fundamental Python syntax, including variable assignments, function definitions, and class instantiation.
Understanding of Inheritance:
- Awareness of the principles of inheritance in OOP and its significance in code organization.
Code Modularity Knowledge:
- Understanding the benefits of code modularity and how inheritance contributes to creating modular and reusable code.
Code 1 with Comments:
# Class Definition: Base
class Base:
# Method for Addition
def addition(self, a, b):
c = a + b
return c
# Class Definition: Derived (Inherits from Base)
class Derived(Base): # IsA Relationship
# Method for Subtraction
def sub(self, a, b):
c = a - b
return c
# Instance Creation of Derived Class
D1 = Derived()
# Method Calls on Derived Instance
x = D1.addition(100, 20)
y = D1.sub(100, 20)
# Output Display
print("Addition is ", x)
print("Subtraction is ", y)Code 1 Output:
Addition is 120
Subtraction is 80
Code 1 with Explanation:
Class Definition: Base
- class Base: defines a base class with a method addition for performing addition.
Method for Addition
- def addition(self, a, b): defines the addition method within the Base class.
Class Definition: Derived (Inherits from Base)
- class Derived(Base): defines a derived class, establishing an “IsA” relationship with the Base class.
Method for Subtraction
- def sub(self, a, b): defines the subtraction method within the Derived class.
Instance Creation of Derived Class
- D1 = Derived() creates an instance D1 of the Derived class.
Method Calls on Derived Instance
- x = D1.addition(100, 20) calls the addition method on the Derived instance, storing the result in x.
- y = D1.sub(100, 20) calls the subtraction method on the Derived instance, storing the result in y.
Output Display
- print(“Addition is “, x) and print(“Subtraction is “, y) display the results of the addition and subtraction operations, respectively.
Code 2 With Comments
# Class Definition: Employee
class Employee:
# Method for Setting Employee ID
def setId(self, id):
self.id = id
# Method for Getting Employee ID
def getId(self):
return self.id
# Method for Setting Employee Name
def setName(self, name):
self.name = name
# Method for Getting Employee Name
def getName(self):
return self.name
# Method for Setting Employee Salary
def setSalary(self, sal):
self.sal = sal
# Method for Getting Employee Salary
def getSalary(self):
return self.sal
# Method for Setting Employee Department
def setDepartement(self, dept):
self.dept = dept
# Method for Getting Employee Department
def getDepartment(self):
return self.deptCode 2 Output
Id= 123
Name= John Doe
Salary= 50000
Department= HR
Code 2 Explanation:
Class Definition: Employee
- class Employee: defines a class named Employee with various methods for setting and getting employee information (ID, Name, Salary, and Department).
Method for Setting Employee ID
- def setId(self, id): defines a method for setting the employee ID.
Method for Getting Employee ID
- def getId(self): defines a method for getting the employee ID.
Method for Setting Employee Name
- def setName(self, name): defines a method for setting the employee name.
Method for Getting Employee Name
- def getName(self): defines a method for getting the employee name.
Method for Setting Employee Salary
- def setSalary(self, sal): defines a method for setting the employee salary.
Method for Getting Employee Salary
- def getSalary(self): defines a method for getting the employee salary.
Method for Setting Employee Department
- def setDepartement(self, dept): defines a method for setting the employee department.
Method for Getting Employee Department
- def getDepartment(self): defines a method for getting the employee department.
Code 3 With Comments:
# Importing the employee module as emp
import employee as emp
# Class Definition: Company (Inherits from Employee)
class Company(emp.Employee): # IsA Relationship
# Method for Setting Company Name
def setCompanyName(self, cname):
self.cname = cname
# Method for Getting Company Name
def getCompanyName(self):
return self.cname
# Instance Creation of Company Class
C1 = Company()
# Setting Values using Methods from the Employee Class
C1.setId(100)
C1.setName("Rahul Sharma")
C1.setDepartement("IT")
C1.setSalary(80000)
C1.setCompanyName("Data Flair Indore")
# Output Display
print("Id=", C1.getId())
print("Name=", C1.getName())
print("Salary=", C1.getSalary())
print("Department=", C1.getDepartment())
print("Company=", C1.getCompanyName())Code 3 Output:
Id= 100
Name= Rahul Sharma
Salary= 80000
Department= IT
Company= Data Flair Indore
Code 3 Explanation:
Importing the employee module as emp:
- import employee as emp imports the employee module and aliases it as emp.
Class Definition: Company (Inherits from Employee):
- class Company(emp.Employee): defines a class named Company that inherits from the Employee class, establishing an “IsA” relationship.
Method for Setting Company Name:
- def setCompanyName(self, cname): defines a method within the Company class for setting the company name.
Method for Getting Company Name:
- def getCompanyName(self): defines a method within the Company class for getting the company name.
Instance Creation of Company Class:
- C1 = Company() creates an instance C1 of the Company class.
Setting Values using Methods from the Employee Class:
- C1.setId(100), C1.setName(“Rahul Sharma”), C1.setDepartement(“IT”), C1.setSalary(80000),
- C1.setCompanyName(“Data Flair Indore”) set various attributes of the C1 instance using methods inherited from the Employee class.
Output Display:
- print(“Id=”, C1.getId()), print(“Name=”, C1.getName()), print(“Salary=”, C1.getSalary()), print(“Department=”, C1.getDepartment()), print(“Company=”, C1.getCompanyName()) display the values of the attributes for the C1 instance.
Conclusion:
The journey through the practical application of inheritance in Python underscores its indispensable role in shaping code reuse, organizational efficiency, and the architecture of modular, extensible software systems. Through the establishment of parent-child relationships, developers gain the capacity to craft specialized classes that inherit and adapt functionalities from broader foundational classes. This empowerment in Python, aligning with the natural hierarchy of entities within a problem domain, speaks volumes about the versatility of inheritance. The ability to extend or override inherited methods introduces a level of adaptability that enables developers to tailor class behaviors with precision, ultimately symbolizing the critical significance of inheritance in achieving both efficiency and elegance in Python software design.
If you are Happy with DataFlair, do not forget to make us happy with your positive feedback on Google

