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

From Expertiza_Wiki
Jump to navigation Jump to search
Line 9: Line 9:
=Mixins=
=Mixins=


Mixins are a collection of methods that can be injected into a class.  
Mixins are a collection of methods that can be injected into a class without instantiation. It serves as a means of extending the functionality of the sub class. A class can inherit functionality from one or more mixins.
 
Here is an illustration of how mixins work in Ruby. Mixins are implemented as modules which contains various methods. In the following example, there are two modules, A and B.
 
[[image:Wiki1.png]]


Example:
Example:

Revision as of 23:07, 6 October 2010

Multiple inheritance and mixins

Introduction

Multiple inheritance is often deemed as a very powerful tool in the Object Oriented Programming paradigm. Implementation of multiple inheritance in languages like Java (interfaces) and C++ is restricted and can often lead to a number of difficulties like the Diamond Problem, Virtual Inheritance issue and increase in programming complexity.

Mixins provide a way to mimic the functionality of multiple inheritance without inheriting the limitations of traditional multiple inheritance. Mixins can be found in languages like Python, Scala, Ruby etc.

Mixins

Mixins are a collection of methods that can be injected into a class without instantiation. It serves as a means of extending the functionality of the sub class. A class can inherit functionality from one or more mixins.

Here is an illustration of how mixins work in Ruby. Mixins are implemented as modules which contains various methods. In the following example, there are two modules, A and B.

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 http://download.oracle.com/javase/tutorial/java/concepts/interface.html

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