Ruby Method Overriding

This chapter explores Ruby method overriding, an essential concept in object-oriented programming (OOP). Method overriding allows a subclass to redefine a method inherited from its parent class, enabling customization of behavior while preserving a shared structure.

Chapter Goals

  • Understand the concept and purpose of method overriding in Ruby.
  • Learn how to override methods in subclasses.
  • Explore the use of the super keyword to reuse parent class functionality.
  • Implement best practices for designing and using overridden methods effectively.

Key Characteristics of Ruby Method Overriding

  • Customization: Allows subclasses to modify inherited behavior.
  • Inheritance-Based: Requires a parent-child relationship between classes.
  • Selective Reuse: Combines new behavior with inherited functionality using super.
  • Dynamic: Enables runtime polymorphism.

Basic Rules for Method Overriding

  • Define a method in the child class with the same name as a method in the parent class.
  • Use super to call the parent class’s method within the overridden method if needed.
  • Ensure overridden methods maintain compatibility with the parent class’s interface.

Best Practices

  • Use meaningful method names to indicate their behavior.
  • Document overridden methods to clarify differences from the parent class.
  • Avoid redundant overriding; only override methods when necessary.
  • Combine overriding with super to reuse shared functionality efficiently.

Syntax Table

Serial No Concept Syntax/Example Description
1 Override Method def method_name \n code \n end Redefines a method in the child class.
2 Use super def method_name \n super \n end Calls the parent class’s method.
3 Preserve Parameters def method_name(params) \n super \n end Passes parameters to the parent method.

Syntax Explanation

Basic Method Overriding

What is Method Overriding?

Method overriding allows a subclass to redefine a method from its parent class.

Syntax

class Parent

  def greet

    “Hello from Parent”

  end

end

 

class Child < Parent

  def greet

    “Hello from Child”

  end

end

 

child = Child.new

puts child.greet

Detailed Explanation

  • The Child class defines a greet method that overrides the method in the Parent class.
  • Instances of the Child class use the overridden method instead of the parent’s method.

Example Explanation

  • Outputs “Hello from Child” because the Child class overrides the greet method.

Using super

What is super?

The super keyword calls the parent class’s method with the same name.

Syntax

class Parent

  def greet

    “Hello from Parent”

  end

end

 

class Child < Parent

  def greet

    “\#{super} and Hello from Child”

  end

end

 

child = Child.new

puts child.greet

Detailed Explanation

  • The super keyword invokes the greet method in the Parent class.
  • Combines the parent method’s behavior with additional logic in the Child class.

Example Explanation

  • Outputs “Hello from Parent and Hello from Child” by combining parent and child behavior.

Preserving Parameters with super

How Does super Work with Parameters?

The super keyword can pass arguments to the parent class’s method.

Syntax

class Parent

  def greet(name)

    “Hello, \#{name} from Parent”

  end

end

 

class Child < Parent

  def greet(name)

    “\#{super(name)} and Hello from Child”

  end

end

 

child = Child.new

puts child.greet(“Alice”)

Detailed Explanation

  • Passes the name parameter from the child method to the parent method using super.
  • Allows the parent method to process the argument while adding new behavior in the child method.

Example Explanation

  • Outputs “Hello, Alice from Parent and Hello from Child” by combining parent and child logic with the same parameter.

Real-Life Project

Project Name: Employee Management System

Project Goal

Create a hierarchy of employee classes using method overriding to customize behavior.

Code for This Project

class Employee

  def initialize(name, role)

    @name = name

    @role = role

  end

  def details

    "Name: \#{@name}, Role: \#{@role}"

  end

end

class Manager < Employee

  def details

    "\#{super}, Responsibilities: Oversee team"

  end

end

class Developer < Employee

  def details

    "\#{super}, Responsibilities: Write code"

  end

end

manager = Manager.new("Alice", "Manager")

developer = Developer.new("Bob", "Developer")

puts manager.details

puts developer.details

Steps

  1. Define a base Employee class with common attributes and methods.
  2. Create subclasses Manager and Developer that inherit from Employee.
  3. Override the details method in the subclasses to customize their behavior.
  4. Instantiate objects and call the details method to demonstrate overriding.

Expected Output

Name: Alice, Role: Manager, Responsibilities: Oversee team

Name: Bob, Role: Developer, Responsibilities: Write code

Project Explanation

  • Encapsulates shared behavior in the parent class (Employee).
  • Customizes behavior in the child classes using method overriding and super.

Insights

Ruby method overriding enables subclasses to customize inherited behavior while maintaining a shared interface. Understanding its principles ensures cleaner, more modular, and reusable code.

Key Takeaways

  • Use method overriding to customize behavior in subclasses.
  • Leverage super to reuse and extend parent class functionality.
  • Pass parameters with super to maintain compatibility between parent and child methods.
  • Combine method overriding with other OOP principles for efficient Ruby programming.