How to Use Inheritance in Ruby

Inheritance is the core of object oriented programming. Building class structures makes smaller programs easy, and large programs possible. In Ruby, inheritance is used to create a relationship between classes that can be used in your code. When one class is a type of a different class, you can use inheritance to make "child classes". For example, you might have a Shape class. Making child classes of the Shape class, like a Circle or Square class, means a method that takes a Shape argument could also take a Circle as a type of shape.

Instructions

  1. Use Inheritance in Ruby

    • 1

      Start with a base (or "super") class. The following example uses a class named Microwave:
      class Microwave
      def put_food_in(food)
      @food = food
      enddef take_food_out
      food = @food
      @food = nil
      return food
      enddef turn_on
      puts "Microwave is on"
      end def turn_off
      puts "Microwave is off"
      end
      end

    • 2

      Write an inherited class statement. This is the same as a normal class statement, but adds the "< Superclass" component. When you see "class ChildClass < Superclass", it means "class ChildClass which inherits from SuperClass." Here, inheritance is used to add a timer to the Microwave class:
      class TimedMicrowave < Microwave
      end

    • 3

      Add new methods to the class. The class will have all of the methods of the superclass, as well any additional methods you add. You can also add new member variables and attr_* accessors:
      class TimedMicrowave < Microwave
      attr_reader :timerdef set_timer(seconds)
      @timer = seconds
      end def clear_timer
      @timer = 0
      end
      end

    • 4

      Override methods in the superclass with new methods. Since in the previous example a timer was added to the microwave, a turn_on method that will automatically turn the microwave off can now be implemented. Simply define a method with the same name as the method in the superclass. When it's called, the method in the child class will override the method from the superclass. Here, any code that expects to see a Microwave object can use a TimedMicrowave object:
      class TimedMicrowave < Microwave
      attr_reader :timerdef set_timer(seconds)
      @timer = seconds
      enddef clear_timer
      @timer = 0
      enddef turn_on
      while @timer > 0
      puts "Microwave is on. #{@timer} second(s) remaining."
      @timer -= 1
      sleep 1
      end# We're done cooking, turn the microwave off
      turn_off
      end
      end

Tips & Warnings

  • If you have more than one person working on a specific program, keep in mind you should not consider anything following the operator "#" as part of the code. These notes are intended to give guidance to programmers.

Related Searches:

Comments

You May Also Like

  • How to Use Inheritance in Python

    When programming a task with moderate complexity, it is often a good idea to incorporate principles of object-oriented design into your code....

  • How to Compare Strings in Ruby

    Ruby is an extremely powerful programming/scripting language. With powerful capabilities and an efficient syntax, many things are possible with Ruby. This article...

  • How to Create a Class in Ruby

    Ruby is a language built for defining classes, as it is first and foremost an object-oriented language. Ruby provides a number of...

  • Uses for Star Ruby Gems

    Uses for Star Ruby Gems. Star rubies have a brilliant, star-shaped sheen caused in part by inclusions of the mineral rutile, often...

  • Uses of Ruby Stones

    Rubies, a variety of corundum, are composed of aluminum oxide. Sapphires are another variety of corundum. Both sapphires and rubies can have...

  • What Are the Uses for the Gemstone Ruby?

    Rubies have long been valued as beautiful and rare gemstones. The color of rubies ranges all along the spectrum of red from...

  • How to Compete With Roller Pigeons

    Roller pigeons may have originated in India, and were brought to Germany by traders in the 1870's. In 1875, the breed became...

  • What Are Rubies Used for Today?

    Rubies are gemstones that are known for their red color and hexagonal shape. Large rubies are often more expensive than diamonds because...

  • How to Use Ruby Scripts in Sketchup

    Google SketchUp is a 3D Modelling program that can create both simple and sophisticated models of buildings. It is used most heavily...

  • Transition Chair Rail Ideas

    Transition Chair Rail Ideas. A chair rail is useful for defining different treatments on the upper and lower walls of a room....

  • Java Exceptions Tutorial

    An exception occurs when an abnormal situation (such as an error or problem) occurs that a method is unable to handle. The...

  • How to Create a Custom Exception in Java

    Exceptions are a key tool in helping Java programs cope with the unexpected. As a developer, you can use the standard exception...

  • How to Make a Genetic-Inheritance Pedigree Chart

    A genetic-inheritance pedigree chart is similar to a family tree but more useful for studying the transmission of hereditary diseases. The chart...

  • Places to Retire in Tennessee

    Places to Retire in Tennessee. Tennessee offers a variety of options for retirees to enjoy the golden years of life. Culture buffs...

  • Advantages & Disadvantages to Object-Oriented Programming

    Object-oriented programming is a concept that swept through university computer science departments in the 80s and the production software community in the...

  • Uses for Rubies

    Rubies are a stunning precious gemstone that have always stood out with their rich, red color. It's the red variety of the...

  • How to Set an Appliance Timer

    Microwaves and ovens are two kitchen appliances on which you can set timers. A timer can be used to limit the amount...

Related Ads

Featured