Difference between inheritance and mixin

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.

Inheritance is we are deriving the superclass.
for example,I did like this,

cass A
  def B
  puts 'a and b'
  end
  end
  class C < A
  def D
  puts 'd'
  end
  end
  e=C.new
  e.B
  e.D
The output should be like this,
a and b
d
                I know very vell about the inheritance,but both the
cases we are using but Idon't about the mixin and difference two
methods.

by
vellingiri

···

--
Posted via http://www.ruby-forum.com/.

what is the use of mixin and what is the use of inheritance

"Programming Ruby (2nd)" :

p 119:
In fact, mixed-in modules effectively behave as superclasses."

p. 355:
If a module is included in a class definition, the module's constants,
class variables, and instance methods are effectively bundled into an
anonymous(and inaccessible) superclass for that class....Calls to
methods not defined in the class will be passed to the module(s) mixed
into the class before being passed to any parent class.

module Greet
  def friendly_greet
    puts "Hello."
  end

  def angry_greet
    puts "Go away."
  end

end

class NicePerson
  include Greet
end

class MadPerson
  include Greet
end

class SadPerson
  include Greet

  def friendly_greet
    puts "Cry, hi, cry."
  end

end

np = NicePerson.new
np.friendly_greet #Hello.
puts

mp = MadPerson.new
np.angry_greet #Go away.
puts

sp = SadPerson.new
sp.friendly_greet #Cry, hi, cry.
sp.angry_greet #Go away.

···

--
Posted via http://www.ruby-forum.com/\.

Mixin modules are searched before the parent class.


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.

···

On Sep 26, 12:06 am, Vellingiri Arul <hariharan....@rediffmail.com> 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.

what is the use of mixin and what is the use of inheritance

1)
class FourLeggedAnimal
  def speed
    puts "I run fast."
  end
end

class Mammal
  def birth
    puts "My babies don't hatch from eggs."
  end
end

class Dog < FourLeggedAnimal
  def speak
    puts "Bark, bark."
  end
end

d = Dog.new
d.speak
d.speed
d.birth

--output:--
Bark, bark.
I run fast.
r6test.rb:21: undefined method `birth' for #<Dog:0x251ac>
(NoMethodError)

2)
class FourLeggedAnimal
  def speed
    puts "I run fast."
  end
end

module Mammal
  def birth
    puts "My babies don't hatch from eggs."
  end
end

class Dog < FourLeggedAnimal
  include Mammal

  def speak
    puts "Bark, bark."
  end
end

d = Dog.new
d.speak
d.speed
d.birth

--ouput:--
Bark, bark.
I run fast.
My babies don't hatch from eggs.

···

--
Posted via http://www.ruby-forum.com/\.

I see that you got some good answers on this already, I find the
following text very interesting, maybe it gives you some useful
background information about Mixins and why they are er bad. (Well but
much less than MI).

http://www.iam.unibe.ch/~scg/Archive/PhD/schaerli-phd.pdf

HTH
Robert

···

On 9/26/07, Vellingiri Arul <hariharan.spc@rediffmail.com> wrote:
--
I'm an atheist and that's it. I believe there's nothing we can know
except that we should be kind to each other and do what we can for
other people.
-- Katharine Hepburn

Hi --

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.

David

···

On Wed, 26 Sep 2007, Phrogz wrote:

On Sep 26, 12:06 am, Vellingiri Arul <hariharan....@rediffmail.com> > wrote:

--
Upcoming training from Ruby Power and Light, LLC:
   * Intro to Ruby on Rails, Edison, NJ, October 23-26
   * Advancing with Rails, Edison, NJ, November 6-9
Both taught by David A. Black.
See http://www.rubypal.com for more info!

Hi --

>> 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.

···

On 9/26/07, David A. Black <dblack@rubypal.com> wrote:

On Wed, 26 Sep 2007, Phrogz wrote:
> On Sep 26, 12:06 am, Vellingiri Arul <hariharan....@rediffmail.com> > > wrote:

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/