Here is an example of multi level inheritance in ruby, here we have 3 classes A, B and C. B inherits from A and C inherits from B, so at the end class C has all methods of A, B and C.
class A
def hello_by_a
puts "A says hello"
end
end
class B < A
def hello_by_b
puts "B says hello"
end
end
class C < B
def hello_by_c
puts "C says hello"
end
end
c = C.new
c.hello_by_a #=> A says hello
c.hello_by_b #=> B says hello
c.hello_by_c #=> C says hello
p c.methods-Object.methods #=> [:hello_by_c, :hello_by_b, :hello_by_a]
And here is the same thing as mixins, here instead of classes A and B, we have modules A and B which gets included to class C. Now class C has all 3 methods
module A
def hello_by_a
puts "A says hello"
end
end
module B
def hello_by_b
puts "B says hello"
end
end
class C
include A
include B
def hello_by_c
puts "C says hello"
end
end
c = C.new
c.hello_by_a #=> A says hello
c.hello_by_b #=> B says hello
c.hello_by_c #=> C says hello
p c.methods-Object.methods #=> [:hello_by_c, :hello_by_b, :hello_by_a]
At the end if we do it in both ways class C will have all the methods of class A and B or module A and B. So why is it better to use modules instead of multilevel inheritance with classes?
I know we should use mixins but don't really know why we shouldn't use multilevel inheritance like above. What are the disadvantages and advantages. if any?