Extend a class with class Dog < Animal, override its methods, and let super borrow the parent version exactly when you need it.
The big picture
Children learn from parents
A subclass starts with everything its parent has, then adds or overrides — and super is the trapdoor back up to the parent version.
1
< Inherits
class Dog < Animal gives Dog every method and behavior Animal defines.
2
Override by Redefining
Defining speak in Dog replaces Animals speak — the most specific version wins.
3
super Reaches Up
Inside an override, super calls the same method one level up with the same receiver.
4
is_a? Sees the Family
A Dog is_a? Animal — inheritance models genuine "is a" relationships.
Children learn from parents
< Inherits1class Dog < Animal gives Dog every method and behavior Animal defines.
Override by Redefining2Defining speak in Dog replaces Animals speak — the most specific version wins.
super Reaches Up3Inside an override, super calls the same method one level up with the same receiver.
is_a? Sees the Family4A Dog is_a? Animal — inheritance models genuine "is a" relationships.
4 building blocks make up Children learn from parents
How it works
One call to speak, two methods
Press Run and watch an override delegate upward with super, then finish the job.
Watch it run
super in slow motion
Dog overrides speak, borrows the parent version with super, then adds its own twist.
inherit.rb
1class Animal
2definitialize(name)
3 @name = name
4end
5
6def speak
7"#{@name} makes a sound"
8end
9end
10
11class Dog < Animal
12def speak
13super+"... and wags!"
14end
15end
16
17pet = Dog.new("Rex")
18puts pet.speak
19puts pet.class
20puts pet.is_a?(Animal)
main
class Animal opens the base blueprint.
Memory
no variables yet — press play
Console
output appears as the program runs…
Step 1/12
Visualize
The method lookup climb
Ruby always checks the most specific class first, then climbs — the first hit runs.
process decision input / output start / end
Challenge
Predict the output
super as a value — the parent answer becomes part of the child answer.
shapes.rb
1class Shape
2def describe
3"a shape"
4end
5end
6
7class Circle < Shape
8def describe
9"a circle (super says: "+super+")"
10end
11end
12
13c = Circle.new
14puts c.describe
15puts Shape.new.describe
Your predicted output — one line per console line
Hint: super calls the same method in the superclass and evaluates to its return value, so Circle splices it into its own sentence. Shape.new never touches Circle at all.
Recap
Key takeaways
Key takeaways6 takeaways
01
class Dog< Animal makes Dog inherit every method Animal defines.
02
Override a method by defining the same name in the subclass.
03
super calls the parent version from inside an override — it evaluates to its return value.
04
Inherited initialize works too: subclasses reuse parent setup for free.
05
class checks the exact type; is_a? checks the whole family tree.
06
Keep hierarchies shallow — prefer small focused classes over deep trees.