In object-oriented programming, inheritance allows one class to reuse the code of another class.
For instance, if you have an Animal
class, a Dog
class can inherit its behaviors (since a dog is a specific type of animal):
class Animal
def speak
puts "I'm an animal"
end
end
# Create Dog class that inherits from Animal
class Dog < Animal
end
Dog.new.speak
# Output: I'm an animal
This means the Dog
class automatically gets the speak
method from the Animal
class.
Ruby Inheritance Syntax
In Ruby, inheritance is implemented using the <
symbol:
class ChildClass < ParentClass
end
This tells Ruby that one class (the child) inherits from another class (the parent). For example,
class Dog < Animal
end
Here,
Dog
is the child class (or subclass).Animal
is the parent class (or superclass).- This means
Dog
inherits access to all methods of the parent class, along with the instance variables initialized by those methods.
Accessing Parent Methods and Instance Variables
Suppose the Animal
class has an instance variable @name
(set through the constructor) and a speak
method that prints a message using that variable.
Let's see how the Dog
child class can inherit this instance variable and method:
class Animal
def initialize(name)
@name = name
end
def speak
puts "#{@name} makes a sound"
end
end
class Dog < Animal
end
dog = Dog.new("Tommy")
dog.speak
Output
Tommy makes a sound
In this example, the Dog
class inherits from Animal
but doesn't add any new code of its own.
Yet, when we create an object of Dog
and call the speak
method, it automatically uses the initialize
method to set @name
before speak
is executed.
As you can see, both methods are defined in the parent class.
This shows that Dog
fully inherits the behavior and data from Animal
, even without redefining anything.
Adding Child-Specific Behavior
Inheritance not only lets a child class reuse its parent's code, it also lets the child add new behavior of its own.
Let's build on the previous example and add a wag_tail
method to the Dog
class:
class Animal
def initialize(name)
@name = name
end
def speak
puts "#{@name} makes a sound"
end
end
class Dog < Animal
def wag_tail
puts "#{@name} is wagging its tail"
end
end
dog = Dog.new("Tommy")
dog.speak
dog.wag_tail
Output
Tommy makes a sound Tommy is wagging its tail
Here, the Dog
class still inherits the initialize
and speak
methods from the Animal
class. But it also defines its own method, wag_tail
.
Overriding Parent Methods
Sometimes, you want the child class to change how an inherited method behaves.
In Ruby, you can do this by simply redefining the method in the child class; this is called method overriding.
Let's take the Animal
class we discussed earlier and override one of its methods in a child class:
class Animal
def initialize(name)
@name = name
end
def speak
puts "#{@name} makes a sound"
end
end
class Dog < Animal
# Override the speak method
def speak
puts "#{@name} says Woof!"
end
end
dog = Dog.new("Tommy")
dog.speak
# Output: Tommy says Woof!
Here, the Dog
class overrides the speak
method from Animal
.
The overridden method in Dog
replaces the generic message with a more specific one: "Tommy says Woof!"
.
Frequently Asked Question
Yes, you can. In Ruby, you can use the super
keyword inside the child method to call the original version from the parent class.
This lets you extend the parent behavior rather than fully replacing it. For example,
class Animal
def initialize(name)
@name = name
end
def speak
puts "#{@name} makes a sound"
end
end
class Dog < Animal
def speak
super
puts "#{@name} barks happily!"
end
end
dog = Dog.new("Tommy")
dog.speak
# Output:
# Tommy makes a sound
# Tommy barks happily!
With the super
keyword, you can build on top of what the parent class already does.
Ruby's Single Inheritance Model
Ruby supports single inheritance only, meaning a class can inherit from only one parent class at a time.
Allowed: Inheriting from one class
class A
end
class B < A
end
Here, the B
class inherits from the A
class. This is allowed and works perfectly in Ruby.
Not Allowed: Inheriting from multiple classes
class A
end
class B
end
# This will raise an error in Ruby
class C < A, B
end
Ruby does not support multiple inheritance, and you can't directly inherit from more than one class.