Inheritance

Inheritance

In Ruby, inheritance describes the relation between classes.

Syntax

Inheritance is expressed when the < is used to connect the parent class, Animal, with the child class, Dog:

Loading...

The Dog class inherits all the methods from its parent Animal class, including .species.

Overriding Methods

An inheriting child class can override methods defined in its parent and replace with code specific to it:

Loading...

The .make_sound method from Animal was overridden in Dog with a return string specific to that class.

Interested in helping build Docs? Read the Contribution Guide or share your feedback.

Learn Ruby on Codecademy