1.3 Redefining, overriding, and super

Redefining methods

Let's clear up some concepts before we move on. Redefining a method involves simply replacing one method with another. The original method is simply... lost.

Here's an example where you can try it out. You've probably already solved this as an exercise - now you get to break it.

Example Code:

Output Window

Since almost every method in Ruby can be redefined, great care must be taken especially with core Ruby classes like Object, Array and so on. A thoughtless method redefinition can break the language entirely. A good rule of thumb is "Never redefine methods, ever, especially with classes supplied by the language."

Now that I've told you how dangerous this is, we both know you want to try it out. Let's get it out of your system right here where it won't hurt anyone. As an exercise, let's break addition for integers (well, Fixnums to be precise).

As always, make the tests pass. Please don't try this at home, and especially not at work.

Hint

Open the Fixnum class and create a method + that takes one parameter. Have this method return 42. Since + already exists, you're basically redefining it - and breaking integer addition for your program.

Output Window

Overriding methods

You've already done this once when overriding initialize in the exercise where you created the Square class.

Overriding in the context of classes involves defining a method in a subclass that is already defined in the superclass. This results in the method being overridden in the subclass, but doesn't in any way affect the method in the superclass.

In the example below, we subclass Array to create MyArray and override Array#map. Run the tests to see that Array is unaffected by the change.

Example Code:

Output Window

Super Powered

A common use of inheritance is to have overridden methods in a subclass do something in addition to what the superclass method did, rather than something entirely different (like in previous examples). This allows us to re-use behaviour that exists in a superclass, then modify to suit the needs of the subclass.

Most object oriented languages offer a mechanism by which an overridden method can be called by the overriding method. Ruby uses the super keyword to make this happen. Using super will call the same method, but as defined in the superclass and give you the result.

In the following example, we define behaviour on the class Animal that describes how it moves. Now a Dolphin is an Animal that can move, but it also wants to talk about how it moves.

Example Code:

Output Window

Your turn. Make these tests pass, and we're all done with this chapter.

Hint

You'll need to create new classes named Human and Penguin. Human inherits from Animal and Penguin from Bird. Now create a move method on each that uses super to get the message from the superclass. Note that the Penguin is actually discarding what its superclass offers because it can't fly. In this case, you can't use super and have to implement the entire message again.

Output Window

Congratulations, guest!


% of the book completed

or

This lesson is Copyright © 2011-2024 by Jasim A Basheer