Site icon DataFlair

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:

Python Class and Method Concepts:

Basic Python Syntax:

Understanding of Inheritance:

Code Modularity Knowledge:

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

Method for Addition

Class Definition: Derived (Inherits from Base)

Method for Subtraction

Instance Creation of Derived Class

Method Calls on Derived Instance

Output Display

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.dept

Code 2 Output
Id= 123
Name= John Doe
Salary= 50000
Department= HR

Code 2 Explanation:

Class Definition: Employee

Method for Setting Employee ID

Method for Getting Employee ID

Method for Setting Employee Name

Method for Getting Employee Name

Method for Setting Employee Salary

Method for Getting Employee Salary

Method for Setting Employee Department

Method for Getting 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:

Class Definition: Company (Inherits from Employee):

Method for Setting Company Name:

Method for Getting Company Name:

Instance Creation of Company Class:

Setting Values using Methods from the Employee Class:

Output Display:

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.

Exit mobile version