Method overriding in Ruby - Time & Space Complexity
We want to understand how the time it takes to run code changes when we use method overriding in Ruby.
Specifically, we ask: does overriding a method affect how long the program runs as input grows?
Analyze the time complexity of the following code snippet.
class Animal
def speak
"Hello"
end
end
class Dog < Animal
def speak
"Woof!" # Overrides Animal's speak
end
end
animals = [Animal.new, Dog.new, Animal.new]
animals.each { |a| puts a.speak }
This code defines a base class and a subclass that overrides a method. It then calls the method on a list of objects.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through the array of objects and calling
speakon each. - How many times: Once for each object in the array (3 times here, but could be more).
Each object in the list has its speak method called once. As the list grows, the number of calls grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 method calls |
| 100 | 100 method calls |
| 1000 | 1000 method calls |
Pattern observation: The number of method calls grows directly with the number of objects.
Time Complexity: O(n)
This means the time to run grows in a straight line with the number of objects we call the method on.
[X] Wrong: "Overriding a method makes the program slower because it adds extra work."
[OK] Correct: Overriding just changes which code runs, but calling the method still happens once per object, so the time grows the same way.
Understanding how method overriding affects performance helps you explain your code choices clearly and shows you know how programs run as they grow.
What if the speak method called another method inside it that loops over a list? How would the time complexity change?
Practice
Solution
Step 1: Understand method overriding concept
Method overriding means a child class uses the same method name as the parent but changes its behavior.Step 2: Identify the effect on behavior
The child class method replaces the parent's method behavior when called on the child object.Final Answer:
Change the behavior of a method inherited from the parent class -> Option CQuick Check:
Method overriding = change inherited method behavior [OK]
- Thinking overriding creates a new method with a different name
- Believing it prevents parent class usage
- Assuming it calls all parent methods automatically
greet in a Ruby child class?Solution
Step 1: Check method name matches exactly
Overriding requires the child method to have the same name as the parent method, here 'greet'.Step 2: Verify Ruby method syntax
Ruby method definitions usedef method_namefollowed by body andend. Multi-line with indentation is standard. Parentheses are optional, butdef greet() puts ... endis valid syntax if properly formatted. Colon syntax is invalid.Final Answer:
class Child < Parent\n def greet\n puts 'Hello from child'\n end\nend -> Option BQuick Check:
Same method name + correct syntax = override [OK]
- Changing method name instead of overriding
- Using incorrect method definition syntax
- Adding colon after method name
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.greetSolution
Step 1: Identify class hierarchy and method overriding
Child class overrides thegreetmethod of Parent with its own version.Step 2: Determine which method is called
Creating an instance of Child and callinggreetcalls the Child's version, printing 'Hello from Child'.Final Answer:
Hello from Child -> Option AQuick Check:
Child method overrides parent = Child output [OK]
- Expecting parent method output
- Thinking no output occurs
- Assuming method is undefined
class Parent
def speak
puts 'Parent speaks'
end
end
class Child < Parent
def speak()
puts 'Child speaks'
end
Solution
Step 1: Check class and method block endings
The Child class is missing the closingendkeyword to finish the class definition.Step 2: Confirm method overriding syntax
The methodspeakis correctly named and defined; parentheses are allowed but optional.Final Answer:
Missingendfor Child class -> Option DQuick Check:
Every class needs matchingend[OK]
end keywords carefully [OK]- Thinking method name must change
- Believing parentheses cause error
- Confusing class nesting rules
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.soundSolution
Step 1: Understand use of
Both Dog and Cat overridesuperin overridden methodssoundand callsuperto get Animal'ssoundreturn value.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'.Final Answer:
Some sound and Bark\nSome sound and Meow -> Option AQuick Check:
super adds parent method result [OK]
- Ignoring super and expecting only child string
- Expecting error from super usage
- Thinking output is only parent's string
Ready to prove it?
Pass the test and this pattern moves unknown
