0

Ruby – Basic Inheritance

This post talks about how to implement basic inheritance in Ruby using a simple example. I’ll throw in some comparisons with Java.

Our Parent and Child Classes

The simplest example of Parent-child inheritance is as follows.

In Java, this is equivalent to

Overriding methods

Let’s modify the original Ruby codes so that the Parent class has a method

These codes will output:

Now if we re-define the same walk method in the Child class that displays a different string

These codes will output:

That’s it! That’s pretty much it though there are other concepts around Ruby inheritance.