41

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?

2
  • 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. Commented Apr 21, 2012 at 0: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 Commented Jul 12, 2018 at 6:48

4 Answers 4

87

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.

Sign up to request clarification or add additional context in comments.

Comments

34

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.

3 Comments

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.
Also, you include modules not classes.
@d11wtq Same for multiple inheritance in Python.
12

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.

1 Comment

In Ex 2: B.ancestors will print just [B] I think you were trying to write A.ancestors.
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.