📖 Scenario: Imagine you are creating a simple system to manage employees and managers in a company. Managers are employees but with extra responsibilities. You want to reuse the employee's greeting message and add a special note for managers.
🎯 Goal: Build a Ruby program that shows how the super keyword works by calling a method from a parent class inside a child class method.
📋 What You'll Learn
Create a class called Employee with a method greet that returns the string "Hello from Employee".
Create a class called Manager that inherits from Employee.
In Manager, override the greet method to call super and add " and Manager here!" to the returned string.
Print the result of calling greet on an instance of Manager.
💡 Why This Matters
🌍 Real World
Understanding how to reuse code from parent classes helps you write cleaner and more maintainable programs, especially when working with related objects like employees and managers.
💼 Career
Inheritance and the <code>super</code> keyword are fundamental concepts in object-oriented programming used in many software development jobs to build scalable and organized code.
Progress0 / 4 steps
1
Create the Employee class with greet method
Create a class called Employee with a method greet that returns the string "Hello from Employee".
Ruby
Hint
Use class Employee to start the class and define def greet method inside it.
2
Create Manager class inheriting Employee
Create a class called Manager that inherits from Employee.
Ruby
Hint
Use class Manager < Employee to inherit from Employee.
3
Override greet in Manager using super
In class Manager, override the greet method to call super and add " and Manager here!" to the returned string.
Ruby
Hint
Inside def greet, use super to call the parent method and add the extra string.
4
Create Manager instance and print greet result
Create an instance of Manager called mgr and print the result of calling mgr.greet.
Ruby
Hint
Create mgr = Manager.new and print mgr.greet using puts.
Practice
(1/5)
1. What does the super keyword do in Ruby?
easy
A. Creates a new instance of the parent class
B. Overrides the parent class's method permanently
C. Calls the parent class's method with the same name
D. Deletes the parent class's method
Solution
Step 1: Understand the role of super
The super keyword is used inside a child class method to call the method with the same name from its parent class. It does not create, override, or delete methods but simply calls the parent method to reuse or extend its behavior.
Final Answer:
Calls the parent class's method with the same name -> Option C
Quick Check:
super calls parent method [OK]
Hint: Remember: super calls parent method with same name [OK]
Common Mistakes:
Thinking super creates or deletes methods
Confusing super with instance creation
Assuming super overrides methods permanently
2. Which of the following is the correct syntax to call the parent method with no arguments using super?
easy
A. super()
B. super[]
C. super
D. super{}
Solution
Step 1: Understand super with parentheses
Using super() calls the parent method explicitly with no arguments, ignoring any current method arguments. Only super() uses parentheses correctly to pass no arguments; other forms are invalid syntax.
Final Answer:
super() -> Option A
Quick Check:
super() means no arguments [OK]
Hint: Use parentheses to call parent with no args: super() [OK]
Common Mistakes:
Using super without parentheses to mean no arguments
Using brackets or braces instead of parentheses
Confusing super() with super without parentheses
3. What will be the output of this Ruby code?
class Parent
def greet(name)
"Hello, #{name} from Parent"
end
end
class Child < Parent
def greet(name)
super + " and Child"
end
end
puts Child.new.greet("Alice")
medium
A. Hello, Alice from Parent and Child
B. Hello, Alice from Child
C. Hello, from Parent and Child
D. Error: wrong number of arguments
Solution
Step 1: Trace the method call in Child#greet
The Child's greet method calls super without parentheses, so it passes the argument "Alice" to Parent#greet. Parent#greet returns "Hello, Alice from Parent". Child appends " and Child" to this string.
Final Answer:
Hello, Alice from Parent and Child -> Option A
Quick Check:
super passes args, output combines strings [OK]
Hint: super without () passes all args automatically [OK]
Common Mistakes:
Assuming super passes no arguments by default
Expecting an error due to argument mismatch
Ignoring the appended string in Child method
4. Identify the error in this Ruby code:
class Parent
def initialize(age)
@age = age
end
end
class Child < Parent
def initialize
super
@name = "Child"
end
end
Child.new
medium
A. NameError for @name variable
B. No error, code runs fine
C. SyntaxError due to missing parentheses
D. ArgumentError: wrong number of arguments for super
Solution
Step 1: Check Parent initialize method
Parent's initialize requires one argument age. Child's initialize calls super without arguments, so it tries to call Parent's initialize with zero arguments, causing an ArgumentError.
Final Answer:
ArgumentError: wrong number of arguments for super -> Option D
Quick Check:
super must match parent args or use super() [OK]
Hint: super without args passes current args; mismatch causes error [OK]
Thinking missing parentheses cause syntax error here
5. Given this code, what will Child.new.greet output?
class Parent
def greet(name = "Guest")
"Hello, #{name} from Parent"
end
end
class Child < Parent
def greet(name = nil)
if name
super(name.upcase)
else
super()
end
end
end
puts Child.new.greet
puts Child.new.greet("alice")
hard
A. Hello, from Parent
Hello, alice from Parent
B. Hello, Guest from Parent
Hello, ALICE from Parent
C. Hello, Guest from Parent
Hello, alice from Parent
D. Error due to wrong arguments in super calls
Solution
Step 1: Analyze Child#greet with no argument
When called without argument, name is nil, so super() calls Parent#greet with no arguments, using default "Guest". When called with "alice", name.upcase is "ALICE" passed to super(name.upcase), so Parent#greet returns "Hello, ALICE from Parent".
Final Answer:
Hello, Guest from Parent
Hello, ALICE from Parent -> Option B
Quick Check:
super() calls parent with defaults; super(args) passes modified args [OK]
Hint: Use super() for no args, super(args) to pass specific args [OK]
Common Mistakes:
Assuming super() passes current args
Ignoring default parameter in Parent#greet
Not uppercasing the argument before passing
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.