Learn
Inheritance
Remove Duplicate Code

At the start of this lesson we had duplicate code in Sedan and Truck. We know that duplicated code leads to errors, so we created a superclass Vehicle to contain that code.

But one version of the duplicated code lives on in Truck! Once we have Truck inherit from Vehicle we can remove that code as well. At that point, we’ll have three classes that have Speed, LicensePlate, SlowDown(), etc. but we’ll have it written in only one place.

Instructions

1.

Make Truck inherit from Vehicle:

  • Use colon syntax to announce that Truck inherits the Vehicle class.
  • Remove the duplicated properties and methods from Truck.
Folder Icon

Take this course for free

Already have an account?