Write a program to inheritance with two child (derived) classes in Python


This Python program defines three classes: Details, Student, and Staff. These classes are used to collect and display information about students and staff members. Here's an explanation of the program:

  • class Details:: This is the base class, which contains common attributes and methods that both Student and Staff classes inherit.
    • __init__(self) : The constructor initializes the idn, name, and gender attributes to default values.
    • setDetails(self): This method allows you to set the details of a person, including the ID number, name, and gender. It collects this information from the user via input.
    • showDetails(self): This method displays the details of a person, including the ID, name, and gender.
  • class Student(Details):: This is a subclass of the Details class, specifically designed to hold information about students.
    • __init__(self): The constructor initializes the total and per attributes to default values. It also calls the constructor of the parent class (Details) using super().
    • setStudent(self): This method collects additional information related to students, such as the total marks and percentage, using input from the user. It also calls the setDetails method from the parent class to collect common details.
    • showStudent(self): This method displays all the details of a student, including the common details (ID, name, and gender) from the Details class and the specific details (total marks and percentage) from the Student class.
  • class Staff(Details):: This is another subclass of the Details class, designed to hold information about staff members.
    • __init__(self): The constructor initializes the depart and salary attributes to default values and calls the constructor of the parent class (Details) using super().
    • setStaff(self): This method collects additional information related to staff, such as the department and salary, using input from the user. It also calls the setDetails method from the parent class to collect common details.
    • showStaff(self): This method displays all the details of a staff member, including the common details (ID, name, and gender) from the Details class and the specific details (department and salary) from the Staff class.
  • The program starts by creating instances of the Student and Staff classes to collect and display information for a student and a staff member. It prompts the user for input to set the details for each and then displays the information.

This program demonstrates the use of class inheritance, where the Student and Staff classes inherit common attributes and methods from the Details class. It showcases how you can collect and display specific information for different types of objects (students and staff) while reusing and customizing common functionality provided by the parent class.


Source Code

class Details:
    def __init__(self):
        self.idn = 0
        self.name = ""
        self.gender = ""
 
    def setDetails(self):
        self.idn = input("Enter the ID Number : ")
        self.name = input("Enter the Name : ")
        self.gender = input("Enter the Gender : ")
 
    def showDetails(self):
        print("ID : ",self.idn)
        print("Name : ",self.name)
        print("Gender : ",self.gender)
 
class Student(Details):
    def __init__(self):
        self.total = 0
        self.per = 0
 
    def setStudent(self):
        self.setDetails()
        self.total = int(input("Enter the Total Mark : "))
        self.per = float(input("Enter the Percentage : "))
 
    def showStudent(self):
        self.showDetails()
        print("Total : ",self.total)
        print("Percentage : ",self.per)
 
class Staff(Details):
    def __init__(self):
        self.depart = ""
        self.salary = ""
 
    def setStaff(self):
        self.setDetails()
        self.depart = input("Enter the Department : ")
        self.salary = float(input("Enter the Salary : "))
 
    def showStaff(self):
        self.showDetails()
        print("Department : ",self.depart)
        print("Salary : ",self.salary)
 
print("Student Details : ")
stu = Student()
stu.setStudent()
stu.showStudent()
 
print("\nStaff Details : ")
stf = Staff()
stf.setStaff()
stf.showStaff()

Output

Student Details :
Enter the ID Number : 103
Enter the Name : Mithra
Enter the Gender : Female
Enter the Total Mark : 350
Enter the Percentage : 70.0
ID :  103
Name :  Mithra
Gender :  Female
Total :  350
Percentage :  70.0

Staff Details :
Enter the ID Number : ST012
Enter the Name : Ram Kumar
Enter the Gender : Male
Enter the Department : Computer Science
Enter the Salary : 45000
ID :  ST012
Name :  Ram Kumar
Gender :  Male
Department :  Computer Science
Salary :  45000.0

Example Programs