Show students what code looks like with and without inheritance to motivate the reasons for using it.

  • Comparing code, like the examples listed below, can help:

    • students understand inheritance by mapping the code with it back to code without it.

    • motivate inheritance by helping students see how it makes the code easier to understand.

  • Code Comparison Examples:

    • Example code with inheritance:

      // Example taken from:
      // http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html
      // Here is the sample code for a possible implementation of a Bicycle class that was presented in the Classes and Objects lesson:
      public class Bicycle {
          // the Bicycle class has three fields
           public int cadence;
           public int gear;
           public int speed;

           // the Bicycle class has one constructor
           public Bicycle(int startCadence, int startSpeed, int startGear) {
               gear = startGear;
               cadence = startCadence;
               speed = startSpeed;
           }

           // the Bicycle class has four methods
           public void setCadence(int newValue) {
               cadence = newValue;
               }

           public void setGear(int newValue) {
               gear = newValue;
           }

           public void applyBrake(int decrement) {
               speed -= decrement;
           }

           public void speedUp(int increment) {
           speed += increment;
           }
      }

      \\ A class declaration for a MountainBike class that is a subclass of Bicycle might look like this:
      public class MountainBike extends Bicycle {
           // the MountainBike subclass adds one field
           public int seatHeight;

           // the MountainBike subclass has one constructor
           public MountainBike(int startHeight, int startCadence, int startSpeed, int startGear) {
               super(startCadence, startSpeed, startGear);
               seatHeight = startHeight;
               }

           // the MountainBike subclass adds one method
           public void setHeight(int newValue) {
               seatHeight = newValue;
           }
      }

    • Example code without inheritance:
      /// WITHOUT inheritance, we’d need to make the MountainBike have all of the content repeated: Like this:
      public class MountainBikeWithoutInheritance {

           public int cadence; // copied from the Bicycle class
           public int gear; // copied from the Bicycle class
           public int speed; // copied from the Bicycle class
           public int seatHeight; // Added!

           // the MountainBikeWithoutInheritance class has one constructor
           public MountainBikeWithoutInheritance(int startHeight, int startCadence, int startSpeed, int startGear) {
               gear = startGear;
               cadence = startCadence;
               speed = startSpeed;
               seatHeight = startHeight;
           }

           // the MountainBike adds one method
           public void setHeight(int newValue) {
               seatHeight = newValue;
           }

           // must be copy and pasted from the Bicycle class!
           public void setCadence(int newValue) {
               cadence = newValue;
           }

           // must be copy and pasted from the Bicycle class!
           public void setGear(int newValue) {
               gear = newValue;
           }

           // must be copy and pasted from the Bicycle class!
           public void applyBrake(int decrement) {
               speed -= decrement;
           }

           // must be copy and pasted from the Bicycle class!
           public void speedUp(int increment) {
               speed += increment;
           }
      }

More about this tip

External Source

Interview with Samar Swaid.