Why bother inheriting? Classes 9/11


I finally had to use “Get the Code” because I clearly don’t understand how this is supposed to work.

In Introduction to Classes 13/18, we created the PartTimeEmployee class which inherits from Employee without having the “name” member variable redefined in PartTimeEmployee.

Using this as an example, I attempted the same approach for the ElectricCar in Classes 9/11 by only defining a new member variable for the battery. This unconditionally failed. After days of retries, I finally had CodeAcademy give me the code and saw that, as far as I can tell, there’s no point to bothering with inheritance (at least in this scenario). The only thing that ElectricCar takes from Car is the “condition”. We saved ourselves and entire ONE line of code. Yippee?

Can anyone help me see the connection I’m missing? Why were we able to inherit a member variable in one lesson, but not in another?

Introduction to Classes 13/18

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

class PartTimeEmployee(Employee):
  def calculate_wage(self, hours):
    self.hours = hours
    return hours * 12
  def full_time_wage(self, hours):
    return super(PartTimeEmployee, self).calculate_wage(hours)

milton = PartTimeEmployee("Milton")
print milton.full_time_wage(10)

Classes 9/11

class Car(object):
  condition = "new"
  def __init__(self, model, color, mpg):
    self.model = model
    self.color = color
    self.mpg   = mpg
  def display_car(self):
    print "This is a %s %s with %s MPG." % (self.color, self.model, str(self.mpg))
  def drive_car(self):
    self.condition = "used"
class ElectricCar(Car):
  def __init__(self, model, color, mpg, battery_type):
    self.model = model
    self.color = color
    self.mpg   = mpg
    self.battery_type = battery_type

my_car = ElectricCar("DeLorean", "silver", 88, "molten salt")


If the child class doesn’t have an init method, the parent class init method is called

if the child class does have an init method, this init method is called instead. The parent method isn’t called at all, which is why we have to redefine everything, which is indeed tedious

So we could shorten this using super, which allows to call parent methods:

def __init__(self, model, color, mpg, battery_type):
    super(ElectricCar, self).__init__(model, color, mpg)
    self.battery_type = battery_type

or if you use python3 you can shorten this a lot:

def __init__(self, model, color, mpg, battery_type):
    self.battery_type = battery_type

haven’t done the exercise in a while, i guess its a learning experience how overwriting methods works


Thank you!

Your version makes a lot more sense than theirs. If your example had been the provided code, it would have all come together and I could have simply rolled my eyes at myself and moved forward.


understanding how inheritance and overwriting methods works in general is quite useful information :wink:


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