Inheritance - why do I need to redefine the subclass attributes that are already in the superclass?


#1

Ok - so they are asking me to create an electric car named my_car with a “molten salt” battery_type.


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")

Here is what I don’t understand. I thought that subclasses inherit the attributes of the superclass (which they def do), so why the heck when we create my new class ElectricCar, I need to re define all of the attributes that are already in the superclass car. e.g. (self.model, self.color, self.mpg)… this seems extremely redundant and I thought the point of inheritance is to streamline the process.

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

#2

Please post a link to the exercise you are on.


#3

https://www.codecademy.com/courses/learn-python/lessons/classes/exercises/inheritance-1?action=lesson_resume


#4

Frankly, inheritence isn’t all that useful in a dynamic language.

In some statically typed language you might have a function that accepts list right? Want to send in a custom value? It’ll have to BE a list, it’ll have to inherit it.

Classes themselves on the other hand are super useful because they let us define behaviour for objects. And a function in python that wants a list doesn’t care (shouldn’t anyway) about what the input does or doesn’t inherit or is. It just needs to behave like one.

Somebody else will tell you the opposite. Perhaps they’ll have a good reason.


#5

So in short, for every subclass I create, I have to re-write out at minimum all of the attributes that init the superclass?


#6

You could leave your subclass completely empty.

A good example is exceptions, your project should probably create its own exceptions so that they can be caught separately by other code, and you’d subclass Exception or maybe one of its subclasses

…Exceptions in general make decent use of inheritance, so yeah, sure, it’ll have uses, but you’ll know that you need it, it’s not something you’d use otherwise


#7

So I went back to a previous lesson.

So in this instance you can see class Equilateral(Triangle) in the def init is doesn’t call out all of the attributes of the superclass. E.g. angle1, angle2, angle3. When I use (self) as the only attribute to initiative it does that just mean it needs the same as the super class?

class Triangle(object):
  number_of_sides = 3
  def __init__(self, angle1, angle2, angle3):
    self.angle1 = angle1
    self.angle2 = angle2
    self.angle3 = angle3
    
  def check_angles(self):
    if (self.angle1 + self.angle2 + self.angle3) == 180:
      return True
    else:
      return False
    
class Equilateral(Triangle):
  angle = 60
  def __init__(self):
    self.angle1 = self.angle
    self.angle2 = self.angle
    self.angle3 = self.angle

#8

You’re not making sense.


#9

Why in an earlier exercise, when we created a subclass, in this example, class Equilateral(Triangle), when we def it, it only takes (self). Not (self, angle1, angle2, angle3)? But in the exercise I started this thread, it does def with the attributes of the superclass.

class Triangle(object):
  number_of_sides = 3
  def __init__(self, angle1, angle2, angle3):
    self.angle1 = angle1
    self.angle2 = angle2
    self.angle3 = angle3
    
  def check_angles(self):
    if (self.angle1 + self.angle2 + self.angle3) == 180:
      return True
    else:
      return False
    
class Equilateral(Triangle):
  angle = 60
  def __init__(self):
    self.angle1 = self.angle
    self.angle2 = self.angle
    self.angle3 = self.angle

Not in the init(self): there is no correlation to


#10

The angles are already known, doesn’t make sense to ask for them
equilateral -> equal sides -> all angles are also equal -> there are three angles in a triangle -> 180 / 3 = 60


#11

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