class Employee(BaseClass1, BaseClass2, BaseClass3):
Here, BaseClass1, BaseClass2, and BaseCLass3 are three base classes from where the Employee class is derived.
Now, let’s look at the constructor of the Employee class.
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
Here, the super() method in the constructor calls the constructor of the base class. When the constructor of the base class is called, the name, personal email address, and contact number variables are assigned values. Then, the company_name, employee_id, and company_email variables are assigned values in the derived class.
Now, let’s look at some methods of the derived class.
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
Here, the get_company_name() method returns the name of the company. And, the get_emplyee_id() method returns the employee id of the employee.
Now, let’s say we want to call the get_email() method and get the company email address of the employee. Please note that if we call Person.get_email() method, then it will return the personal email address of the person. But, we want to get the company email address of the employee by calling the method get_email(). So, we can use method overriding here.
Using method overriding in Python, we can define a function in the derived class with the same name as that of the base class. When the function in the derived class is called, the function in the base class will not be executed. Instead, the function is …






0 Comments