Ruby Inheritance
Reusing code through parent-child relationships
🌳 What is Ruby Inheritance?
Inheritance allows a class to inherit methods and attributes from another class. The child class gets all features from the parent class and can add its own unique features.
# Parent class
class Animal
def speak
puts "Some sound"
end
end
# Child class inherits from Animal
class Dog < Animal
end
dog = Dog.new
dog.speak
Output:
Some sound
Key Inheritance Concepts
Parent Class
Base class with common features
class Vehicle
end
Child Class
Inherits using < symbol
class Car < Vehicle
end
Override
Replace parent methods
def method
# New code
end
Super
Call parent class methods
super
🔹 Basic Inheritance
Create a parent class with common features, then child classes inherit those features. Use the < symbol to establish inheritance relationships:
class Vehicle
def initialize(brand)
@brand = brand
end
def start
puts "#{@brand} is starting..."
end
end
class Car < Vehicle
def honk
puts "Beep beep!"
end
end
my_car = Car.new("Toyota")
my_car.start # Inherited method
my_car.honk # Own method
Output:
Toyota is starting...
Beep beep!
🔹 Method Overriding
Child classes can replace parent methods with their own versions. This is called overriding and lets you customize behavior for specific child classes:
class Animal
def speak
puts "Some generic sound"
end
end
class Cat < Animal
def speak
puts "Meow!"
end
end
class Dog < Animal
def speak
puts "Woof!"
end
end
cat = Cat.new
dog = Dog.new
cat.speak
dog.speak
Output:
Meow!
Woof!
🔹 Using Super
The super keyword calls the parent class method. This is useful when you want to extend parent functionality rather than completely replace it:
class Employee
def initialize(name, salary)
@name = name
@salary = salary
end
def details
"Name: #{@name}, Salary: $#{@salary}"
end
end
class Manager < Employee
def initialize(name, salary, department)
super(name, salary) # Call parent initialize
@department = department
end
def details
super + ", Department: #{@department}"
end
end
manager = Manager.new("Alice", 80000, "IT")
puts manager.details
Output:
Name: Alice, Salary: $80000, Department: IT
🔹 Inheritance Chain
Classes can inherit from other classes that also inherit, creating a chain. Each class in the chain adds or modifies functionality:
class LivingThing
def breathe
puts "Breathing..."
end
end
class Animal < LivingThing
def move
puts "Moving..."
end
end
class Bird < Animal
def fly
puts "Flying..."
end
end
sparrow = Bird.new
sparrow.breathe # From LivingThing
sparrow.move # From Animal
sparrow.fly # From Bird
Output:
Breathing...
Moving...
Flying...
🔹 When to Use Inheritance
Inheritance is best when there's a clear "is-a" relationship. Use it to avoid code duplication and create logical hierarchies:
class Shape
attr_reader :color
def initialize(color)
@color = color
end
end
class Circle < Shape
def initialize(color, radius)
super(color)
@radius = radius
end
def area
3.14 * @radius * @radius
end
end
class Square < Shape
def initialize(color, side)
super(color)
@side = side
end
def area
@side * @side
end
end
circle = Circle.new("red", 5)
square = Square.new("blue", 4)
puts "Circle: #{circle.color}, Area: #{circle.area}"
puts "Square: #{square.color}, Area: #{square.area}"
Output:
Circle: red, Area: 78.5
Square: blue, Area: 16