13/18 Override!


I don't understand this exercise. Please, explain it to me somebody,
How do class works?


Hey yourethanol. This is how I did it and it worked. Hope it helps.

class PartTimeEmployee(Employee):  
    def calculate_wage(self, hours):
        PartTimeEmployee.calculate_wage = employee.calculate_wage
        self.hours = hours
        wage = PartTimeEmployee.hours * 12.00
        return wage


@chickenmonkey_7 @yourethanol

Overriding is when you change the base classes' methods. You can use parts of the base or you can fully override the whole thing.

Base Class Example:

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

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

Now in the instructions it tells you to override the calculate wage method for a part time employee.


class PartTimeEmployee(Employee):
    def calculate_wage(self, hours):
        return hours * 12.00

Now the way this works is that our new part time class inherits from the base class of employee, that means that it get's everything that an employee gets. Now we have to override how to calculate the wages.

So we just create a new method for the part timer called calculate_wage. This overrides the base classes method that is named the same thing.


I should also mention that if you need to add something that needs to be created at the use/initialize of the class you can create an __init__ function in the new class but you will have to call the base classes __init__ method.

Like So

class Animal(name, weight, height):
    def __init__(self, name, weight, height):
        self.name = name
        self.weight = weight
        self.height = height

class Chicken(Animal):
    def __init__(self, name, weight, height, sex):
        Animal.__init__(self, name, weight, height)
        self.sex = sex

If your base classes' __init__ takes arguments you DO have to pass them to it when you use this method.

Any questions ask freely.


This is a great breakdown, thank you for this. :grin:


Your kind explanation helped me to understand the code. Thank you very much!