Bird
Raised Fist0
Rubyprogramming~10 mins

Method overriding in Ruby - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to override the method greet in the subclass.

Ruby
class Parent
  def greet
    puts "Hello from Parent"
  end
end

class Child < Parent
  def [1]
    puts "Hello from Child"
  end
end

child = Child.new
child.greet
Drag options to blanks, or click blank then click option'
Agreeting
Bhello
Cgreet
Dsay_hello
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different method name in the subclass so it doesn't override.
Forgetting to define the method in the subclass.
2fill in blank
medium

Complete the code to call the parent class method greet inside the overridden method.

Ruby
class Parent
  def greet
    puts "Hello from Parent"
  end
end

class Child < Parent
  def greet
    [1]
    puts "Hello from Child"
  end
end

child = Child.new
child.greet
Drag options to blanks, or click blank then click option'
Asuper
Bsuper.greet
CParent.greet
Dself.greet
Attempts:
3 left
💡 Hint
Common Mistakes
Calling super.greet which is invalid syntax in Ruby.
Calling Parent.greet which is a class method call, not instance method.
3fill in blank
hard

Fix the error in the code by correctly overriding the method info.

Ruby
class Animal
  def info
    puts "I am an animal"
  end
end

class Dog < Animal
  def [1]
    puts "I am a dog"
  end
end

dog = Dog.new
dog.info
Drag options to blanks, or click blank then click option'
AInfo
Binfo
Cinformation
Ddetails
Attempts:
3 left
💡 Hint
Common Mistakes
Using capitalized method name Info which does not override.
Using a different method name that does not match the parent.
4fill in blank
hard

Fill both blanks to override the method describe and call the parent method inside it.

Ruby
class Vehicle
  def describe
    puts "This is a vehicle"
  end
end

class Car < Vehicle
  def [1]
    [2]
    puts "This is a car"
  end
end

car = Car.new
car.describe
Drag options to blanks, or click blank then click option'
Adescribe
Bsuper
Csuper.describe
Ddescription
Attempts:
3 left
💡 Hint
Common Mistakes
Using super.describe which is invalid syntax.
Using a different method name so the parent method is not overridden.
5fill in blank
hard

Fill all three blanks to override display, call the parent method, and add extra output.

Ruby
class Computer
  def display
    puts "Display from Computer"
  end
end

class Laptop < Computer
  def [1]
    [2]
    puts [3]
  end
end

laptop = Laptop.new
laptop.display
Drag options to blanks, or click blank then click option'
Adisplay
Bsuper
C"Display from Laptop"
Dshow
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different method name so override does not happen.
Calling super.display which is invalid.
Not printing the extra message correctly.

Practice

(1/5)
1. What does method overriding in Ruby allow a child class to do?
easy
A. Prevent the parent class from being used
B. Create a new method with a different name
C. Change the behavior of a method inherited from the parent class
D. Automatically call all parent methods

Solution

  1. Step 1: Understand method overriding concept

    Method overriding means a child class uses the same method name as the parent but changes its behavior.
  2. Step 2: Identify the effect on behavior

    The child class method replaces the parent's method behavior when called on the child object.
  3. Final Answer:

    Change the behavior of a method inherited from the parent class -> Option C
  4. Quick Check:

    Method overriding = change inherited method behavior [OK]
Hint: Same method name in child changes parent method behavior [OK]
Common Mistakes:
  • Thinking overriding creates a new method with a different name
  • Believing it prevents parent class usage
  • Assuming it calls all parent methods automatically
2. Which of the following is the correct way to override a method named greet in a Ruby child class?
easy
A. class Child < Parent\n def greet_child\n puts 'Hello from child'\n end\nend
B. class Child < Parent\n def greet\n puts 'Hello from child'\n end\nend
C. class Child < Parent\n def greet() puts 'Hello from child' end\nend
D. class Child < Parent\n def greet: puts 'Hello from child' end\nend

Solution

  1. Step 1: Check method name matches exactly

    Overriding requires the child method to have the same name as the parent method, here 'greet'.
  2. Step 2: Verify Ruby method syntax

    Ruby method definitions use def method_name followed by body and end. Multi-line with indentation is standard. Parentheses are optional, but def greet() puts ... end is valid syntax if properly formatted. Colon syntax is invalid.
  3. Final Answer:

    class Child < Parent\n def greet\n puts 'Hello from child'\n end\nend -> Option B
  4. Quick Check:

    Same method name + correct syntax = override [OK]
Hint: Use same method name and proper def/end syntax [OK]
Common Mistakes:
  • Changing method name instead of overriding
  • Using incorrect method definition syntax
  • Adding colon after method name
3. What will be the output of this Ruby code?
class Parent
  def greet
    puts 'Hello from Parent'
  end
end

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

obj = Child.new
obj.greet
medium
A. Hello from Child
B. Error: undefined method greet
C. No output
D. Hello from Parent

Solution

  1. Step 1: Identify class hierarchy and method overriding

    Child class overrides the greet method of Parent with its own version.
  2. Step 2: Determine which method is called

    Creating an instance of Child and calling greet calls the Child's version, printing 'Hello from Child'.
  3. Final Answer:

    Hello from Child -> Option A
  4. Quick Check:

    Child method overrides parent = Child output [OK]
Hint: Child instance calls overridden method, not parent's [OK]
Common Mistakes:
  • Expecting parent method output
  • Thinking no output occurs
  • Assuming method is undefined
4. Find the error in this Ruby code that tries to override a method:
class Parent
  def speak
    puts 'Parent speaks'
  end
end

class Child < Parent
  def speak()
    puts 'Child speaks'
  end
medium
A. Cannot override methods with parentheses
B. Method name should be different
C. Parent class must be declared inside Child
D. Missing end for Child class

Solution

  1. Step 1: Check class and method block endings

    The Child class is missing the closing end keyword to finish the class definition.
  2. Step 2: Confirm method overriding syntax

    The method speak is correctly named and defined; parentheses are allowed but optional.
  3. Final Answer:

    Missing end for Child class -> Option D
  4. Quick Check:

    Every class needs matching end [OK]
Hint: Count your end keywords carefully [OK]
Common Mistakes:
  • Thinking method name must change
  • Believing parentheses cause error
  • Confusing class nesting rules
5. Given this Ruby code, what will be the output?
class Animal
  def sound
    'Some sound'
  end
end

class Dog < Animal
  def sound
    super + ' and Bark'
  end
end

class Cat < Animal
  def sound
    super + ' and Meow'
  end
end

puts Dog.new.sound
puts Cat.new.sound
hard
A. Some sound and Bark\nSome sound and Meow
B. Some sound\nSome sound
C. and Bark\nand Meow
D. Error: super called incorrectly

Solution

  1. Step 1: Understand use of super in overridden methods

    Both Dog and Cat override sound and call super to get Animal's sound return value.
  2. Step 2: Combine parent and child method results

    Each child adds its own string to the parent's 'Some sound', resulting in 'Some sound and Bark' and 'Some sound and Meow'.
  3. Final Answer:

    Some sound and Bark\nSome sound and Meow -> Option A
  4. Quick Check:

    super adds parent method result [OK]
Hint: super calls parent method result to extend behavior [OK]
Common Mistakes:
  • Ignoring super and expecting only child string
  • Expecting error from super usage
  • Thinking output is only parent's string
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard