user7692855
user7692855

Reputation: 1418

Python child class with more methods

I have a quick question about how to deal with an inheritance problem.

Let's say we have a vehicle object

class vehicle(object):

    __init__(registration):
         registration = self.registration

    get_details():
         return "This is a vehicle"

And then a truck that inherits from vehicle

class truck(vehicle):

      get_details():
         return "This is a truck"

We have lots of classes all with the same methods and properties e.g. bus, car, train. However, we also have an airplane which inherits from vehicle but only airplane has a new method called required_takeoff_distance()

Is it OK to only have it in the airplane class or should you also add it to the vehicle class with a default of raise NotImplementedError()?

Upvotes: 2

Views: 116

Answers (1)

wim
wim

Reputation: 362717

It's perfectly OK for a child class to define more methods than are available on the parent class. Indeed, this is the usual reason for creating a child class in the first place.

Do not add a method with raise NotImplementedError onto the parent class unless you're trying to define an interface / abstract base class. This is almost never needed in Python, so if you're not sure what it means you can safely forget about it.

Upvotes: 7

Related Questions