📖 Scenario: Imagine you are creating a simple program to manage animals. You want to show how a child class can use a method from its parent class.
🎯 Goal: You will create a parent class with a method, then a child class that calls the parent's method using super. Finally, you will print the result.
📋 What You'll Learn
Create a parent class called Animal with a method sound that returns the string "Some sound".
Create a child class called Dog that inherits from Animal.
In the Dog class, create a method sound that calls the parent sound method using super and adds " and Bark" to the returned string.
Create an instance of Dog and print the result of calling its sound method.
💡 Why This Matters
🌍 Real World
In real programs, child classes often need to use or extend the behavior of their parent classes. This helps keep code organized and avoids repetition.
💼 Career
Understanding how to access parent methods is important for software developers working with object-oriented languages like Ruby, as it is a common pattern in many applications.
Progress0 / 4 steps
1
Create the parent class
Create a class called Animal with a method sound that returns the string "Some sound".
Ruby
Hint
Use class Animal to start the class and def sound to define the method.
2
Create the child class
Create a class called Dog that inherits from Animal. Inside it, define a method sound that calls super and adds " and Bark" to the returned string.
Ruby
Hint
Use class Dog < Animal to inherit. Use super inside sound to call the parent method.
3
Create an instance of Dog
Create a variable called dog and set it to a new instance of the Dog class.
Ruby
Hint
Use dog = Dog.new to create the instance.
4
Print the dog's sound
Write a puts statement to print the result of calling the sound method on the dog instance.
Ruby
Hint
Use puts dog.sound to print the sound.
Practice
(1/5)
1. In Ruby, how do you call a method from a parent class inside a child class method?
easy
A. Use parent.method_name
B. Call the parent method by its name directly
C. Use this.parent.method_name
D. Use super inside the child method
Solution
Step 1: Understand method inheritance in Ruby
Child classes inherit methods from parent classes but to call the parent's version inside an overridden method, you use super.
Step 2: Identify the correct syntax for calling parent method
Using super inside the child method calls the parent method with the same name.
Final Answer:
Use super inside the child method -> Option D
Quick Check:
Calling parent method = super [OK]
Hint: Use super to call parent method inside child [OK]
Common Mistakes:
Trying to call parent method by name directly
Using non-existent syntax like parent.method_name
Confusing this with Ruby's self
2. Which of the following is the correct syntax to call a parent class method with arguments in Ruby?
easy
A. super arg1, arg2
B. super(arg1 arg2)
C. super[arg1, arg2]
D. super{arg1, arg2}
Solution
Step 1: Recall Ruby method call syntax
In Ruby, method arguments are passed separated by commas without brackets unless parentheses are used.
Step 2: Identify correct super usage with arguments
The correct way is super arg1, arg2 or super(arg1, arg2). super arg1, arg2 uses the correct syntax without parentheses.
Final Answer:
super arg1, arg2 -> Option A
Quick Check:
Pass args to super with commas [OK]
Hint: Pass arguments to super separated by commas [OK]
Common Mistakes:
Using brackets [] or braces {} instead of parentheses or commas
Omitting commas between arguments
Using incorrect syntax like super(arg1 arg2)
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(name) + " and Child"
end
end
puts Child.new.greet("Alice")
medium
A. "Hello, Alice from Child"
B. "Hello, Alice from Parent and Child"
C. "Hello, Alice from Parent"
D. Error: wrong number of arguments
Solution
Step 1: Understand method overriding and super call
The Child#greet method calls super(name), which calls Parent#greet(name) and returns "Hello, Alice from Parent".
Step 2: Concatenate the returned string
The child method adds " and Child" to the parent's returned string, resulting in "Hello, Alice from Parent and Child".
Final Answer:
"Hello, Alice from Parent and Child" -> Option B
Quick Check:
super returns parent's string + child's addition [OK]
Hint: super returns parent's result; child adds more [OK]
Common Mistakes:
Thinking super calls child's method again causing recursion
Ignoring the concatenation after super call
Assuming super without arguments forwards none
4. Identify the error in this Ruby code snippet:
class Parent
def info
"Parent info"
end
end
class Child < Parent
def info
super()
puts "Child info"
end
end
Child.new.info
medium
A. super() returns value but is ignored; only child info prints
B. Runtime error: super() called without arguments
C. Syntax error: super() should not have parentheses
D. No error; outputs both parent and child info
Solution
Step 1: Analyze the super call with parentheses
Calling super() calls the parent method with no arguments and returns "Parent info" but does not print it.
Step 2: Check what is printed
The child method prints only "Child info" because the parent's return value is ignored.
Final Answer:
super() returns value but is ignored; only child info prints -> Option A
Quick Check:
super() returns but doesn't print [OK]
Hint: super() returns value; print it to see output [OK]
Common Mistakes:
Assuming super() prints parent's return value automatically
Thinking super() without args causes error
Confusing syntax error with runtime behavior
5. Given this Ruby code, what will be the output?
class A
def message
"A"
end
end
class B < A
def message
"B" + super
end
end
class C < B
def message
super + "C"
end
end
puts C.new.message
hard
A. "ABC"
B. "BCA"
C. "BAC"
D. "CAB"
Solution
Step 1: Trace method calls from C to A
C#message calls super which is B#message. B#message returns "B" + super which calls A#message returning "A".
Step 2: Combine returned strings step-by-step
B#message returns "B" + "A" = "BA". Then C#message returns "BA" + "C" = "BAC".
Final Answer:
"BAC" -> Option C
Quick Check:
super chains add strings in order [OK]
Hint: super calls chain up; combine results in call order [OK]
Common Mistakes:
Mixing order of concatenation
Assuming super calls only immediate parent
Ignoring that super returns parent's result
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.