39

I thought that Ruby only allowed single inheritance besides mixin. However, when I have class Square that inherits class Thing, Thing in turn inherits Object by default.

class Thing
end

class Square < Thing
end

Doesn't this represent multiple inheritance?

Brickgao
  • 35
  • 4
Bhubhu Hbuhdbus
  • 1,489
  • 6
  • 24
  • 31
  • 6
    There's a difference between having multiple ancestors and having multiple direct parents. Even using mixins, everything is placed into a linear inheritance chain. – d11wtq Apr 21 '12 at 00:44
  • A simple google search will be able to clear out what exactly multiple inheritance is. If you reached here trying to find out the difference between include and extend, or to understand using include to achieve multiple inheritance, see [this question](https://stackoverflow.com/questions/156362/what-is-the-difference-between-include-and-extend-in-ruby) – kapad Jul 12 '18 at 06:48

4 Answers4

82

I think you are taking the meaning of multiple inheritance in a wrong way. Probably what you have in mind as multiple inheritance is like this:

class A inherits class B
class B inherits class C

If so, then that is wrong. That is not what multiple inheritance is, and Ruby has no problem with that. What multiple inheritance really means is this:

class A inherits class B
class A inherits class C

And you surely cannot do this in Ruby.

sawa
  • 165,429
  • 45
  • 277
  • 381
31

No, multi inheritance means one class have more than one parent class. For example in ruby you can have that behavior with modules like:

class Thing
  include MathFunctions
  include Taggable
  include Persistence
end

So in this example Thing class would have some methods from MathFunctions module, Taggable and Persistence, that wouldn't be possible using simple class inheritance.

Ismael Abreu
  • 16,443
  • 6
  • 61
  • 75
  • 13
    Do note however that MathFunctions, Taggable and Persistence are all inserted into the inheritance hierarchy in the order in which they are included... they aren't placed equally in the inheritance hierarchy. – d11wtq Apr 21 '12 at 00:46
  • 3
    Also, you include modules not classes. – Einar Mar 14 '13 at 12:49
  • 1
    @d11wtq Same for multiple inheritance in Python. – Franklin Yu Jun 11 '16 at 20:13
11

If class B inherits from class A, then instances of B have the behaviors of both class A and class B

class A
end

class B < A
  attr_accessor :editor
end

Ruby has single inheritance, i.e. each class has one and only one parent class. Ruby can simulate multiple inheritance using Modules(MIXINs)

module A
  def a1
  end

  def a2
  end
end

module B
  def b1
  end

  def b2
  end
end

class Sample
  include A
  include B

  def s1
  end
end


samp=Sample.new
samp.a1
samp.a2
samp.b1
samp.b2
samp.s1

Module A consists of the methods a1 and a2. Module B consists of the methods b1 and b2. The class Sample includes both modules A and B. The class Sample can access all four methods, namely, a1, a2, b1, and b2. Therefore, you can see that the class Sample inherits from both the modules. Thus you can say the class Sample shows multiple inheritance or a mixin.

Florian Pilz
  • 8,002
  • 4
  • 22
  • 30
Nitesh
  • 266
  • 4
  • 11
11

Multiple inheritance - This is absolutely not possible in ruby not even with modules.

multilevel inheritance - This is what is possible even with the modules.

Why ?

Modules acts as an absolute superclasses to the class that includes them.

Ex1 :

class A
end
class B < A
end

This is a normal inheritance chain, and you can check this by ancestors.

B.ancestors => B -> A -> Object -> ..

Inheritance with Modules -

Ex2 :

module B
end
class A
  include B
end

inheritance chain for above example -

B.ancestors => B -> A -> Object -> ..

Which is exactly same as a Ex1. That means all the methods in module B can override the methods in A, And it acts as a real class inheritance.

Ex3 :

module B
  def name
     p "this is B"
  end
end
module C
  def name
     p "this is C"
  end
end
class A
  include B
  include C
end

A.ancestors => A -> C -> B -> Object -> ..

if you see the last example even though two different modules are included they are in a single inheritance chain where B is a superclass of C, C is a superclass of A.

so,

A.new.name => "this is C"

if you remove the name method from module C, above code will return "this is B". which is same as inheriting a class.

so,

At any point there is only one multilevel inheritance chain in ruby, which nullifies having multiple direct parent to the class.

Gaurav Ingalkar
  • 1,217
  • 11
  • 20