Bird
0
0

You want to override a method calculate in a child class but also add extra behavior before and after calling the parent's method. Which is the correct way to do this in Ruby?

hard📝 Application Q8 of 15
Ruby - Inheritance
You want to override a method calculate in a child class but also add extra behavior before and after calling the parent's method. Which is the correct way to do this in Ruby?
ADefine <code>calculate</code> in child and do not call <code>super</code>.
BCall parent's <code>calculate</code> outside the child class.
CDefine <code>calculate</code> in child, call <code>super</code> between extra code blocks.
DUse alias to rename parent's method and call it manually.
Step-by-Step Solution
Solution:
  1. Step 1: Understand adding behavior around parent method

    To add code before and after parent's method, override and call super in between.
  2. Step 2: Confirm correct approach

    Calling super inside child method allows parent's method to run within extra code.
  3. Final Answer:

    Define calculate in child, call super between extra code blocks. -> Option C
  4. Quick Check:

    Use super inside override to wrap parent method [OK]
Quick Trick: Call super inside method to wrap parent behavior [OK]
Common Mistakes:
  • Forgetting to call super
  • Calling parent's method outside class
  • Using alias unnecessarily

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes