Ruby, Ruby Tutorial

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.

Loading

Got comments or suggestions? We disabled the comments on this site to fight off spammers, but you can still contact us via our Facebook page!.


You Might Also Like