Inheritance / super / override
The syntax for class inheritance and method overriding in Ruby. Explains how to call a parent class method using super.
Syntax
# Define a subclass that inherits from a parent class.
class SubclassName < ParentClassName
def initialize(arg)
super(arg) # Call the parent class's initialize.
@extra_var = value
end
# Override a method from the parent class.
def method_name
super # Call the parent method, passing along all arguments as-is.
extra_processing
end
def method_name(arg)
super(arg) # Call the parent method with a specific argument.
end
end
# Check the parent class.
Subclass.superclass # Returns the direct parent class.
Subclass.ancestors # Returns the full inheritance chain as an array.
object.is_a?(ClassName) # Returns true if the object is an instance of that class (or a subclass).
Method List
| Syntax / Method | Description |
|---|---|
| class Child < Parent | Defines a subclass that inherits from the parent class. All of the parent's methods are inherited. |
| super | Calls the same-named method in the parent class. All arguments are passed through as-is. |
| super(args) | Calls the parent class method with the specified arguments. |
| super() | Calls the parent class method with no arguments (does not pass through any arguments). |
| superclass | A class method that returns the direct parent class. |
| ancestors | Returns the inheritance chain (the order of classes and modules) as an array. |
| is_a?(klass) | Returns true if the object is an instance of the specified class or any of its subclasses. |
Sample Code
# Define the parent class.
class Animal
attr_reader :name, :sound
def initialize(name, sound)
@name = name
@sound = sound
end
def speak
"#{@name} said \"#{@sound}\"."
end
def info
"Animal: #{@name}"
end
end
# Define a child class (inherits from Animal).
class Dog < Animal
attr_reader :breed
def initialize(name, breed)
super(name, "Woof") # Call the parent class's initialize.
@breed = breed
end
# Override: replaces the parent class's method.
def info
super + " (Dog / #{@breed})" # Appends to the parent's info.
end
def fetch(item)
"#{@name} fetched the #{item}!"
end
end
class Cat < Animal
def initialize(name)
super(name, "Meow")
end
end
# Create instances and use them.
dog = Dog.new("Rex", "Shiba Inu")
cat = Cat.new("Whiskers")
puts dog.speak # Rex said "Woof".
puts cat.speak # Whiskers said "Meow".
puts dog.info # Animal: Rex (Dog / Shiba Inu)
puts dog.fetch("ball") # Rex fetched the ball!
# Check the inheritance.
puts dog.is_a?(Dog) # true
puts dog.is_a?(Animal) # true (is_a? also returns true for parent classes)
puts Dog.superclass # Animal
Notes
Ruby classes use class Child < Parent to implement single inheritance. A subclass inherits all instance methods from its parent and can override only the methods it needs to change.
The behavior of super depends on how you write it. Bare super passes the current method's arguments through to the parent as-is. super() calls the parent method with no arguments, and super(args) passes the specified arguments. This can be a source of confusion, so it is recommended to always be explicit.
If you forget to call super in initialize, the parent class's instance variables will not be initialized. When you define initialize in a subclass, make it a rule to call super (or the necessary initialization logic) first.
For an alternative to multiple inheritance using modules, see module / include / extend. For customizing comparison operators, see Comparable / <=>.
If you find any errors or copyright issues, please contact us.