Skip to content

Instantly share code, notes, and snippets.

@vanderhoop
Last active August 29, 2015 14:11
Show Gist options
  • Select an option

  • Save vanderhoop/1e2c0466a1cc0cc9f012 to your computer and use it in GitHub Desktop.

Select an option

Save vanderhoop/1e2c0466a1cc0cc9f012 to your computer and use it in GitHub Desktop.

Using super to Invoke the Method of the Same Name in a superclass

Sears has contracted you to continue working on an application that models its inventory. Sears is really struggling, and right now the only things they have in stock are bikes and shirts. The CEO of Sears took a computer programming course in college, and he knows that both bikes and shirts are examples of a Product. Thus, he has instructed you to reuse the code found in product.rb.

The Sears Class Heirarchy:

                             *Product* 
                             /       \  
                            /         \  
                           /           \
                         Bike        Shirt

Directions

  1. cd into the sears directory in w02/d04/student
  2. Examine the code in product.rb
  • what does a product expect upon instantiation?
  1. Add class definitions for Bike and Shirt in their respective files so that they both inherit from the Product class
  2. Use super so that not only does a Bike get assigned a price and brand upon instantion, but also a gear_count and tire_diameter
  • provide getter methods for the bike-specific attributes
  1. Instantiate some bikes in main.rb to make sure they behavior the way you'd expect
  2. Use super so that not only does a Shirt get assigned a price and brand upon instantion, but also a size, a color, and some text
  • provide getter methods for the shirt-specific attributes
  1. Instantiate some shirts in main.rb to make sure they behavior the way you'd expect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment