Override - code works, but I have a question about initializing


#1



https://www.codecademy.com/courses/python-intermediate-en-WL8e4/2/3?curriculum_id=4f89dab3d788890003000096#


Hello, the code works fine, I'm just confused about why the employee class needs to be initialized, but the part-time employee class does not need to be. Can you explain that? Thanks!


class Employee(object):
    """Models real-life employees!"""
    def __init__(self, employee_name):
        self.employee_name = employee_name

    def calculate_wage(self, hours):
        self.hours = hours
        return hours * 20.00

# Add your code below!
class PartTimeEmployee(Employee):
        def calculate_wage(self, hours):
            self.hours = hours
            return hours * 12.00


#2

__init__() is a (magic) method which gets called when you create a class instance:

employee = Employee("mhernandez58")

this will create a new employee instance of class Employee, init will be called automatic.

when to use init and when not, is the programmers choice


#3

If I understand correctly, you are saying that because PartTimeEmployee is a child class of Employee, it is inheriting Employee methods just as an instance of Employee would?


#4

i purely expained what init does.

currently, if you create a PartTimeEmployee, you can't give this PartTimeEmployee a name.

Did you design it this way, or did the exercise?


#5

The exercise designed the instruction that way..


#6

i guess there is no init because the exercise just wants to demonstrate how to override a parent method


#7

Your second comment is right. The PartTimeEmployee inherits all the methods of the Employee object, including the __init__ method. You can also override that as well, but in your example, where __init__ is not defined/overridden, it runs the method defined in the Employee class.

currently, if you create a PartTimeEmployee, you can't give this PartTimeEmployee a name.

This is incorrect, when you initialize a PartTimeEmployee, you can give it a name as it uses the init method defined in the Employee class.


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.