0
0
Rubyprogramming~10 mins

Method overriding in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Method overriding
Define Parent Class with method
Define Child Class overriding method
Create Child object
Call method on Child object
Child's method runs, not Parent's
End
The child class replaces the parent's method with its own version when called on a child object.
Execution Sample
Ruby
class Parent
  def greet
    puts "Hello from Parent"
  end
end

class Child < Parent
  def greet
    puts "Hello from Child"
  end
end

child = Child.new
child.greet
This code shows a child class overriding a method from its parent class and calling the new method.
Execution Table
StepActionEvaluationOutput
1Define Parent class with greet methodParent#greet defined
2Define Child class inheriting Parent and override greetChild#greet defined
3Create Child objectchild is instance of Child
4Call greet on childCalls Child#greet (overrides Parent#greet)Hello from Child
5End of executionNo more code to run
💡 Program ends after calling overridden method on child object
Variable Tracker
VariableStartAfter Step 3Final
childundefinedChild instance createdChild instance
Key Moments - 2 Insights
Why does calling greet on child run Child's method, not Parent's?
Because the child class defines its own greet method, it overrides the parent's version. See execution_table step 4 where Child#greet is called.
What if Child did not define greet method?
Then calling greet on child would run Parent's greet method, since child inherits it. This is shown by the absence of Child#greet in step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, which method is called when child.greet runs?
ABoth Parent#greet and Child#greet
BParent#greet
CChild#greet
DNo method is called
💡 Hint
Check the 'Evaluation' column at step 4 in execution_table
At which step is the child object created?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at the 'Action' column for object creation in execution_table
If Child did not override greet, what would be the output at step 4?
A"Hello from Child"
B"Hello from Parent"
CNo output
DError
💡 Hint
Refer to key_moments about what happens if Child does not define greet
Concept Snapshot
Method overriding lets a child class replace a parent's method.
Define method in parent class.
Define same method name in child class.
Calling method on child runs child's version.
If child lacks method, parent's runs.
Used for customizing behavior in subclasses.
Full Transcript
This example shows method overriding in Ruby. First, a Parent class defines a greet method that prints a message. Then, a Child class inherits Parent and defines its own greet method, replacing the parent's. When we create a Child object and call greet on it, Ruby runs the Child's greet method, not the Parent's. This is method overriding: the child class changes the behavior of a method inherited from the parent. If the child did not define greet, calling greet on the child would run the parent's method instead. This lets subclasses customize or replace behavior from their parent classes.