Bird
0
0

What happens when a child class overrides a method from its parent class in Ruby?

easy📝 Conceptual Q1 of 15
Ruby - Inheritance
What happens when a child class overrides a method from its parent class in Ruby?
ABoth parent and child methods run automatically in sequence.
BThe parent class method is called instead of the child's method.
CRuby throws an error because methods cannot be overridden.
DThe child class provides its own version of the method, replacing the parent's version.
Step-by-Step Solution
Solution:
  1. Step 1: Understand method overriding concept

    In Ruby, when a child class defines a method with the same name as in its parent, it replaces the parent's method.
  2. Step 2: Effect of overriding in child class

    Calling the method on a child class instance uses the child's version, not the parent's.
  3. Final Answer:

    The child class provides its own version of the method, replacing the parent's version. -> Option D
  4. Quick Check:

    Method overriding = Child method replaces parent method [OK]
Quick Trick: Child method replaces parent method with same name [OK]
Common Mistakes:
  • Thinking both methods run automatically
  • Believing parent method always runs
  • Assuming overriding causes errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes