Ruby uses single inheritance to keep things simple and clear. It helps avoid confusion by allowing a class to inherit from only one parent class.
Why single inheritance in Ruby
Introduction
Syntax
Ruby
class ChildClass < ParentClass # child class code here end
Ruby allows only one parent class after the < symbol, which means single inheritance.
To add more features, Ruby uses modules and mixins instead of multiple inheritance.
Examples
Ruby
class Animal def speak puts "Hello" end end class Dog < Animal def bark puts "Woof!" end end
Ruby
module Swimmable def swim puts "Swimming" end end class Fish < Animal include Swimmable end
Sample Program
This program shows a Car class inheriting from Vehicle. Car can use the start method from Vehicle and its own honk method.
Ruby
class Vehicle def start puts "Vehicle started" end end class Car < Vehicle def honk puts "Beep beep!" end end my_car = Car.new my_car.start my_car.honk
Important Notes
Single inheritance keeps the class hierarchy simple and easy to follow.
Ruby uses modules to add extra behavior without the problems of multiple inheritance.
Mixins (modules) are a flexible way to share code across classes.
Summary
Ruby uses single inheritance to keep code simple and clear.
Only one parent class is allowed, but modules can add extra features.
This design helps avoid conflicts and makes code easier to maintain.
Practice
1. Why does Ruby use single inheritance instead of multiple inheritance?
easy
Solution
Step 1: Understand Ruby's inheritance model
Ruby allows only one parent class for inheritance, which is called single inheritance.Step 2: Reason why single inheritance is chosen
This design avoids conflicts that can happen when inheriting from multiple classes and keeps code easier to understand and maintain.Final Answer:
To keep code simple and avoid conflicts between parent classes -> Option AQuick Check:
Single inheritance avoids conflicts = D [OK]
Hint: Single inheritance avoids confusion and keeps code clean [OK]
Common Mistakes:
- Thinking Ruby supports multiple inheritance directly
- Believing inheritance makes code slower
- Confusing modules with classes in inheritance
2. Which of the following is the correct way to declare a class inheriting from another class in Ruby?
easy
Solution
Step 1: Recall Ruby class inheritance syntax
In Ruby, inheritance is declared using the less-than symbol (<) between class names.Step 2: Match syntax with options
Only class ChildClass < ParentClass; end uses the correct Ruby syntax:class ChildClass < ParentClass; end.Final Answer:
class ChildClass < ParentClass; end -> Option DQuick Check:
Ruby inheritance uses < symbol = C [OK]
Hint: Use < symbol to inherit a class in Ruby [OK]
Common Mistakes:
- Using 'inherits' keyword which Ruby does not have
- Using ':' or 'extends' like other languages
- Forgetting the < symbol for inheritance
3. What will be the output of this Ruby code?
class A
def greet
'Hello from A'
end
end
class B < A
end
b = B.new
puts b.greetmedium
Solution
Step 1: Understand inheritance in the code
Class B inherits from class A but does not override the greet method.Step 2: Determine method call result
Since B does not have greet, Ruby looks in A and finds greet returning 'Hello from A'.Final Answer:
Hello from A -> Option CQuick Check:
Inherited method runs = B [OK]
Hint: Inherited methods run if not overridden [OK]
Common Mistakes:
- Expecting 'Hello from B' without method override
- Thinking NoMethodError occurs if subclass lacks method
- Confusing syntax errors with inheritance behavior
4. Identify the error in this Ruby code that tries to inherit from two classes:
class Parent1 end class Parent2 end class Child < Parent1, Parent2 end
medium
Solution
Step 1: Check Ruby inheritance rules
Ruby supports only single inheritance, so a class cannot inherit from two classes at once.Step 2: Analyze the code error
The syntaxclass Child < Parent1, Parent2is invalid and causes a syntax error.Final Answer:
SyntaxError because Ruby does not support multiple inheritance -> Option BQuick Check:
Multiple inheritance causes syntax error = A [OK]
Hint: Ruby allows only one parent class, multiple inheritance is syntax error [OK]
Common Mistakes:
- Assuming Ruby supports multiple inheritance like some other languages
- Confusing runtime errors with syntax errors
- Thinking Parent2 is undefined when it is declared
5. How can Ruby add features from multiple sources to a class despite single inheritance? Choose the best explanation.
hard
Solution
Step 1: Understand Ruby's way to add multiple behaviors
Ruby uses modules to add extra methods to classes without multiple inheritance.Step 2: Identify how modules are used
Modules are mixed in withincludeorextendkeywords to add features.Final Answer:
By using modules that can be mixed in with 'include' or 'extend' to add methods -> Option AQuick Check:
Modules add features despite single inheritance = A [OK]
Hint: Use modules with include/extend to add features [OK]
Common Mistakes:
- Thinking multiple inheritance is possible with commas
- Believing manual copying is the Ruby way
- Expecting special keywords to override inheritance rules
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
