Bird
0
0

How can you modify this Ruby code so that the Child class's greet method calls the Parent class's greet method with a different argument?

hard📝 Application Q9 of 15
Ruby - Inheritance
How can you modify this Ruby code so that the Child class's greet method calls the Parent class's greet method with a different argument?
class Parent
  def greet(name)
    "Hello, #{name}"
  end
end

class Child < Parent
  def greet
    super
  end
end
AChange child method to: <code>def greet; super('Alice'); end</code>
BChange child method to: <code>def greet(name); super; end</code>
CChange child method to: <code>def greet; super(); end</code>
DChange child method to: <code>def greet; super(name); end</code>
Step-by-Step Solution
Solution:
  1. Step 1: Understand parent's greet method requires an argument

    The parent method expects one argument name.
  2. Step 2: Modify child's greet to call super with a specific argument

    To call parent's method with a different name, pass the argument explicitly: super('Alice').
  3. Final Answer:

    Change child method to: def greet; super('Alice'); end -> Option A
  4. Quick Check:

    Pass arguments explicitly to super to change parent's input [OK]
Quick Trick: Pass new arguments in super() to override parent's parameters [OK]
Common Mistakes:
  • Calling super without arguments when parent expects one
  • Using undefined variable 'name' in child
  • Calling super with empty parentheses when argument needed

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes