the derived class will be executed.
For example, the get_email() method in our example can be implemented in the following way:
class Person: def __init__(self, name, personal_email, contact_number): self.name = name self.email = personal_email self.contact_number = contact_number def get_email(self): return self.email def get_contact_number(self): return self.contact_number class Employee(Person): def __init__(self, name, personal_email, contact_number, company_name, employee_id, company_email): super().__init__(name, personal_email, contact_number) self.company_name = company_name self.employee_id = employee_id self.company_email = company_email def get_company_name(self): return self.company_name def get_employee_id(self): return self.employee_id def get_email(self): return self.company_email employee = Employee("Ajay", "[email protected]", "1234", "ABC Company", "0123", "[email protected]") print(employee.get_email())
The get_email() function is defined in both the Person class and the Employee class. Here, we are creating an employee object of the Employee class and then we are calling the employee.get_email() method. Here, though the base class has a function with the same name, the get_email() function of the derived class will be executed and the function will return the company email address of the employee.
So, the above program will print the following output:






0 Comments