Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
The method
greet in the subclass overrides the one in the parent class.2fill in blank
mediumComplete 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'
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.✗ Incorrect
Using
super calls the parent class method with the same name.3fill in blank
hardFix 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'
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.
✗ Incorrect
Method names are case-sensitive. The method must be named exactly
info to override.4fill in blank
hardFill 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'
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.
✗ Incorrect
Override with the same method name
describe and call super to invoke the parent method.5fill in blank
hardFill 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'
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.
✗ Incorrect
Override
display, call super to run parent method, then print extra message.