From: Rick DeNatale Date: 2007-09-27T02:28:24+09:00 Subject: Re: difference between inheritance and mixin On 9/26/07, David A. Black wrote: > Hi -- > > On Wed, 26 Sep 2007, Phrogz wrote: > > > On Sep 26, 12:06 am, Vellingiri Arul > > wrote: > >> Hai friends, > >> Can anybody can answer this question. > >> what is the difference between inheritance and mixin. > >> what is the use of mixin and what is the use of inheritance. > > > > Mixin modules are searched before the parent class. > > http://phrogz.net/RubyLibs/RubyMethodLookupFlow.png > > > > Also, you can only inherit from one parent class, but you can mixin > > many modules to the same class. > > > > Because modules cannot inherit from other modules or classes, there is > > no problem of 'diamond' inheritance. > > Modules can mix in modules, though, so you could have: > > module M > > module N module O > > class C > > > But the diamond problem is avoided by having the order of mixing in be > significant, so that there's no ambiguity about which module/class is > to be searched. On the other hand, Ruby has varied semantics for different versions of structures like this: Module M \ Class C | Class D Module M | \ | Class E If M defines a method m and D overrides m, it's version dependent which definition instances of E get. At least for a while in the 1.9 history, it would get the original version back, which seems to be the 'correct' semantics to me. Last time I checked though 1.9 went back to ignoring the re-inclusion. -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/