CSC/ECE 517 Fall 2010/ch3 3b ka: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
 
Line 1: Line 1:
=Multiple inheritance and mixins=
=Multiple inheritance and mixins=
Introduction
Multiple inheritance is a feature present in some object-oriented languages like C++ wherein a class can inherit behaviors and features from more than one superclass. While C++ supports multiple inheritance, Java does the same using interfaces. Languages like Ruby, however, do not support multiple inheritance. Instead they have what is known as mixins, a collection of methods that can be injected into a class.
Example:
module A
def method_1
end
module B
def method_2
end
class C
include A
include B
end
Here an object in class C can use all the methods in modules A & B.
Q1. "Do mixins solve all of these problems?
One very common problem with multiple inheritance is called the Diamond Problem. This can be illustrated with the following example:
Say two classes B & C both inherit from A and D inherits from B & C. Suppose in class D an object calls a method defined in class A, where B & C have overridden the method, then there is the problem of which class does it inherit from, B or C? The Diamond Problem is solved using virtual inheritance in languages like C++.  This is taken care of in Ruby, as the method with the same name that was most recently included would be considered. Hence there is no need of virtual inheritance.
This is demonstrated by the following example:
module A
  def hello
    puts "hello"
  end
end
module B
  def hello
    puts "hello world"
  end
end
class C
  include B
  include A
  puts C.new.hello
end
Output: hello
nil
Now instead if we change the order of include in class C
class C
  include A
  include B
  puts C.new.hello
end
Output: hello world
nil
However, it is better to avoid such situations altogether. So even though it may seem viable in the above example, it may not be when multiple modules are included within a class.
Q2. Are mixins a clear advance over interfaces?
Java does not use multiple inheritance. Instead it uses interfaces. Moreover in Java, each class can have only one parent. Also, functions declared within the interface cannot be implemented within that interface. An interface cannot have any member variables.
However with a mixin, modules can include the implementation of the methods along with their definitions.
Q3. Do mixins have any disadvantages not shared by multiple inheritance or interfaces
Although mixins help solve the diamond problem, there is a disadvantage to it. If the method having the same as the modules which it includes, it implements the method in the class, without providing the user any warning.
module A
  def hello
    puts "hello"
  end
end
module B
  def hello
    puts "hello world"
  end
end
class C
  include A
  include B
  def hello
    puts "this is the Hello from Class C"
  end
  puts C.new.hello
end
Output: this is the Hello from Class C
nil
Conclusion:
Although mixins have certain advantages over multiple inheritance and interfaces, they also have their own share of disadvantages. So, we cannot say that mixins are indeed better.
References:
CSC 517 Lecture Notes
http://en.wikipedia.org/wiki/Multiple_inheritance
http://en.wikipedia.org/wiki/Diamond_problem
http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html
Further reading:
http://www.cprogramming.com/tutorial/virtual_inheritance.html

Revision as of 20:36, 6 October 2010

Multiple inheritance and mixins

Introduction

Multiple inheritance is a feature present in some object-oriented languages like C++ wherein a class can inherit behaviors and features from more than one superclass. While C++ supports multiple inheritance, Java does the same using interfaces. Languages like Ruby, however, do not support multiple inheritance. Instead they have what is known as mixins, a collection of methods that can be injected into a class.

Example:

module A

def method_1

end

module B

def method_2

end


class C

include A

include B

end

Here an object in class C can use all the methods in modules A & B.


Q1. "Do mixins solve all of these problems?

One very common problem with multiple inheritance is called the Diamond Problem. This can be illustrated with the following example:

Say two classes B & C both inherit from A and D inherits from B & C. Suppose in class D an object calls a method defined in class A, where B & C have overridden the method, then there is the problem of which class does it inherit from, B or C? The Diamond Problem is solved using virtual inheritance in languages like C++. This is taken care of in Ruby, as the method with the same name that was most recently included would be considered. Hence there is no need of virtual inheritance.

This is demonstrated by the following example:

module A

 def hello
   puts "hello"
 end

end

module B

 def hello
   puts "hello world"
 end

end

class C

 include B
 include A
 puts C.new.hello

end

Output: hello nil

Now instead if we change the order of include in class C

class C

 include A
 include B
 puts C.new.hello

end

Output: hello world nil


However, it is better to avoid such situations altogether. So even though it may seem viable in the above example, it may not be when multiple modules are included within a class.


Q2. Are mixins a clear advance over interfaces?

Java does not use multiple inheritance. Instead it uses interfaces. Moreover in Java, each class can have only one parent. Also, functions declared within the interface cannot be implemented within that interface. An interface cannot have any member variables. However with a mixin, modules can include the implementation of the methods along with their definitions.


Q3. Do mixins have any disadvantages not shared by multiple inheritance or interfaces

Although mixins help solve the diamond problem, there is a disadvantage to it. If the method having the same as the modules which it includes, it implements the method in the class, without providing the user any warning.

module A

 def hello
   puts "hello"
 end

end

module B

 def hello
   puts "hello world"
 end

end

class C

 include A
 include B
 def hello
   puts "this is the Hello from Class C"
 end
 puts C.new.hello

end

Output: this is the Hello from Class C nil


Conclusion:

Although mixins have certain advantages over multiple inheritance and interfaces, they also have their own share of disadvantages. So, we cannot say that mixins are indeed better.


References: CSC 517 Lecture Notes http://en.wikipedia.org/wiki/Multiple_inheritance http://en.wikipedia.org/wiki/Diamond_problem http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html

Further reading: http://www.cprogramming.com/tutorial/virtual_inheritance.html