This chapter explores Ruby inheritance, a core concept of object-oriented programming (OOP). Inheritance allows classes to share behavior by inheriting methods and properties from other classes. Ruby supports single inheritance, meaning a class can inherit from only one other class, promoting simplicity and clarity.
Chapter Goals
- Understand the concept and purpose of inheritance in Ruby.
- Learn how to define and extend classes using inheritance.
- Explore the use of the super keyword to call methods in parent classes.
- Implement best practices for designing and using inheritance effectively.
Key Characteristics of Ruby Inheritance
- Code Reusability: Allows sharing of methods and attributes between classes.
- Parent-Child Relationship: Establishes a hierarchy between classes.
- Method Overriding: Enables child classes to customize inherited behavior.
- Single Inheritance: Each class can inherit from only one parent class.
Basic Rules for Inheritance
- Use the < symbol to define inheritance: class Child < Parent.
- Use super to call a method from the parent class.
- Avoid overusing inheritance; prefer composition for unrelated functionality.
Best Practices
- Use inheritance for “is-a” relationships (e.g., a Dog is a Animal).
- Limit the depth of inheritance hierarchies to avoid complexity.
- Document the relationships between parent and child classes.
- Use modules for shared behavior across unrelated classes.
Syntax Table
Serial No | Concept | Syntax/Example | Description |
1 | Define Inheritance | class Child < Parent | Defines a class that inherits from a parent. |
2 | Override Method | def method_name \n super \n end | Overrides a method while calling the parent. |
3 | Access Parent Method | super | Calls a method from the parent class. |
4 | Initialize with Super | super(parameters) | Calls the parent class’s initialize method. |
Syntax Explanation
Defining Inheritance
What is Inheritance?
Inheritance allows one class (child) to acquire the properties and methods of another class (parent).
Syntax
class Parent
def greet
“Hello from Parent”
end
end
class Child < Parent
end
child = Child.new
puts child.greet
Detailed Explanation
- The Child class inherits methods from the Parent class.
- Instances of the Child class can call methods defined in the Parent class.
Example Explanation
- Outputs “Hello from Parent” because the Child class inherits the greet method.
Overriding Methods
What is Method Overriding?
Method overriding allows a child class to redefine a method inherited from the 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 greet method in the Child class overrides the method in the Parent class.
- The child method completely replaces the inherited method unless super is used.
Example Explanation
- Outputs “Hello from Child” because the child class overrides the greet method.
Using super
What is super?
The super keyword calls the method with the same name from the parent class.
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 calls the greet method in the Parent class.
- Combines the parent method’s output with additional logic in the child method.
Example Explanation
- Outputs “Hello from Parent and Hello from Child” by combining parent and child behavior.
Initializing with super
How Does super Work with initialize?
The super keyword can be used to call the parent class’s initialize method.
Syntax
class Parent
def initialize(name)
@name = name
end
end
class Child < Parent
def initialize(name, age)
super(name)
@age = age
end
end
child = Child.new(“Alice”, 10)
Detailed Explanation
- The Child class’s initialize method calls the Parent class’s initialize method using super.
- Passes the name parameter to the parent class while adding the age attribute.
Real-Life Project
Project Name: Vehicle Inheritance Hierarchy
Project Goal
Create a hierarchy of vehicle classes using inheritance to share common behavior.
Code for This Project
class Vehicle
def initialize(make, model)
@make = make
@model = model
end
def details
"\#{@make} \#{@model}"
end
end
class Car < Vehicle
def details
"Car: \#{super}"
end
end
class Truck < Vehicle
def details
"Truck: \#{super}"
end
end
car = Car.new("Toyota", "Corolla")
truck = Truck.new("Ford", "F-150")
puts car.details
puts truck.details
Steps
- Define a Vehicle class with common attributes and methods.
- Create Car and Truck classes that inherit from Vehicle.
- Override the details method in the child classes to customize behavior.
- Instantiate objects and call methods to demonstrate inheritance.
Expected Output
Car: Toyota Corolla
Truck: Ford F-150
Project Explanation
- Encapsulates shared behavior in a parent class (Vehicle).
- Demonstrates method overriding and the use of super in child classes.
Insights
Ruby inheritance promotes code reuse and establishes relationships between classes. Understanding its syntax and principles enables cleaner, more modular programs.
Key Takeaways
- Use inheritance for hierarchical relationships between classes.
- Leverage super to reuse parent class functionality.
- Avoid excessive inheritance hierarchies; prefer composition for unrelated functionality.
- Combine inheritance with modules, loops, and methods for efficient Ruby programming.