# SuperClass and Modules

**URL:** <https://www.ruby-forum.com/t/superclass-and-modules/148804>\
**Category:** Ruby\
**Created:** [September 17, 2008, 8:40am UTC](https://www.ruby-forum.com/t/superclass-and-modules/148804 "2008-09-17T08:40:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![shyl\_ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/51bf81/32.png) [@shyl\_ruby](https://www.ruby-forum.com/u/shyl_ruby)\
**Post date:** [September 17, 2008, 8:40am UTC](https://www.ruby-forum.com/t/superclass-and-modules/148804/1 "2008-09-17T08:40:37Z")

</div>

Hi,

I am very much new to Ruby

Ruby Consider Modules as super classes [Internally].  
My confusion is that there can be many modules either included or  
extended to a Ruby class where as only one Parent class is allowed [as  
in java] in Ruby… why so…? Is it something to do with Meta classes in  
Ruby… ?

Thanks,  
-shyl.

---

<div class="post-metadata">

**Author:** ![shyl\_ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/51bf81/32.png) [@shyl\_ruby](https://www.ruby-forum.com/u/shyl_ruby)\
**Post date:** [September 17, 2008, 9:22am UTC](https://www.ruby-forum.com/t/superclass-and-modules/148804/2 "2008-09-17T09:22:17Z")

</div>

Hi Shilesh,

> My confusion is that there can be many modules either included or  
> extended to a Ruby class where as only one Parent class is allowed [as  
> in java] in Ruby… why so…?

Multiple inheritance has one big problem usually denominated as “the  
diamond problem”. Check this link:  
[Multiple inheritance - Wikipedia](http://en.wikipedia.org/wiki/Diamond_problem).  
Ruby using single inheritance plus mixins gives almost the same  
advantages without these problems.

> Is it something to do with Meta classes in  
> Ruby… ?

Metaclasses have nothing to do with it. A metaclass/ghost  
class/singleton class/eigenclass is class that Ruby introduces in the  
inheritance chain to allow to define singleton methods (object specific  
methods), just that. 😉

Pedro S…

---

<div class="post-metadata">

**Author:** ![shyl\_ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/51bf81/32.png) [@shyl\_ruby](https://www.ruby-forum.com/u/shyl_ruby)\
**Post date:** [September 17, 2008, 9:33am UTC](https://www.ruby-forum.com/t/superclass-and-modules/148804/3 "2008-09-17T09:33:21Z")

</div>

> Multiple inheritance has one big problem usually denominated as “the  
> diamond problem”. Check this link:  
> [Multiple inheritance - Wikipedia](http://en.wikipedia.org/wiki/Diamond_problem).  
> Ruby using single inheritance plus mixins gives almost the same  
> advantages without these problems.

You cannot have multiple inheritance without the diamond problem – that  
page actually mentions how Ruby resolves method names in this case:

module A; end  
module B; include A; end  
module C; include A; end

class D  
include B  
include C  
end

Methods will be looked up in D, C, B and A (in that order) when you call  
a  
method on an instance of D.

---

<div class="post-metadata">

**Author:** ![shyl\_ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/51bf81/32.png) [@shyl\_ruby](https://www.ruby-forum.com/u/shyl_ruby)\
**Post date:** [September 17, 2008, 10:04am UTC](https://www.ruby-forum.com/t/superclass-and-modules/148804/4 "2008-09-17T10:04:53Z")

</div>

James C. wrote:

> > Multiple inheritance has one big problem usually denominated as “the  
> > diamond problem”. Check this link:  
> > [Multiple inheritance - Wikipedia](http://en.wikipedia.org/wiki/Diamond_problem).  
> > Ruby using single inheritance plus mixins gives almost the same  
> > advantages without these problems.
> 
> You cannot have multiple inheritance without the diamond problem – that  
> page actually mentions how Ruby resolves method names in this case:
> 
> module A; end  
> module B; include A; end  
> module C; include A; end
> 
> class D  
> include B  
> include C  
> end
> 
> Methods will be looked up in D, C, B and A (in that order) when you call  
> a  
> method on an instance of D.

Thanks James and Pedro,

I understood the diamond problem.

let me clarify myself.

To avoid the method confusion while having more than one module, Ruby  
looks in a particular order. This can be applied in the case of super  
classes as well and let ruby follow the order of import while searching  
for a method. Thus i may have,

class MyClass \< A \< B #[oder of method search will be from MyClass, A to  
B]

I believe that there might be some strong reason for allowing multiple  
Modules while restricting only one Parent …?

---

<div class="post-metadata">

**Author:** ![shyl\_ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/51bf81/32.png) [@shyl\_ruby](https://www.ruby-forum.com/u/shyl_ruby)\
**Post date:** [September 17, 2008, 10:48am UTC](https://www.ruby-forum.com/t/superclass-and-modules/148804/5 "2008-09-17T10:48:30Z")

</div>

> To avoid the method confusion while having more than one module, Ruby  
> looks in a particular order. This can be applied in the case of super  
> classes as well and let ruby follow the order of import while searching  
> for a method. Thus i may have,
> 
> class MyClass \< A \< B #[oder of method search will be from MyClass, A to  
> B]
> 
> I believe that there might be some strong reason for allowing multiple  
> Modules while restricting only one Parent …?

This is likely just my opinion, but it’s mostly a question of semantics,  
of  
indicating your intent to other programmers. If you want to make a more  
specialised kind of MyClass object, you’d subclass MyClass. If you have  
some  
behaviour that could be applied to many different types of objects, you  
use  
a module and mix it in.

## Parent-child inheritance is really just a special case of module inclusion, with some extra restrictions. Remember that Class inherits from Module

modules are objects that store methods, and classes are objects that  
store  
methods _and_ create new objects.

---

<div class="post-metadata">

**Author:** ![shyl\_ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/51bf81/32.png) [@shyl\_ruby](https://www.ruby-forum.com/u/shyl_ruby)\
**Post date:** [September 20, 2008, 11:16am UTC](https://www.ruby-forum.com/t/superclass-and-modules/148804/6 "2008-09-20T11:16:46Z")

</div>

On 17.09.2008 10:40, James C. wrote:

> > I believe that there might be some strong reason for allowing multiple  
> > Modules while restricting only one Parent …?
> 
> This is likely just my opinion, but it’s mostly a question of semantics, of  
> indicating your intent to other programmers. If you want to make a more  
> specialised kind of MyClass object, you’d subclass MyClass. If you have some  
> behaviour that could be applied to many different types of objects, you use  
> a module and mix it in.

+1

To throw in two other terms: this is inheritance as specialization (or  
generalization, if you look the other direction) vs. implementation  
inheritance.

> Parent-child inheritance is really just a special case of module inclusion,  
> with some extra restrictions. Remember that Class inherits from Module –  
> modules are objects that store methods, and classes are objects that store  
> methods _and_ create new objects.

Absolutely. You can also say: if multiple class inheritance was allowed  
classes and modules would have even less distinctive features which  
makes it less clear when to choose which. By having more differences  
between the two design choices become clearer. My 0.02EUR…

Kind regards

robert
