Multiple Inheritance in Ruby? Multiple Inheritance in Ruby? ruby ruby

Multiple Inheritance in Ruby?


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 Bclass 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 Bclass A inherits class C

And you surely cannot do this in Ruby.


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 Persistenceend

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.


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

class Aendclass B < A  attr_accessor :editorend

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  endendmodule B  def b1  end  def b2  endendclass Sample  include A  include B  def s1  endendsamp=Sample.newsamp.a1samp.a2samp.b1samp.b2samp.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.