0
0
Rubyprogramming~15 mins

Super keyword behavior in Ruby - Mini Project: Build & Apply

Choose your learning style9 modes available
Super Keyword Behavior
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Create mgr = Manager.new and print mgr.greet using puts.