CSC/ECE 517 Fall 2010/ch3 3b ka

From Expertiza_Wiki
Jump to navigation Jump to search

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.


Now consider a class C that inherits functionality from both the modules A and B. Here is an illustration of the structure of class C after the multiple inheritance from the two mixins.

As it is clearly shown in the above hierarchy, class C inherits the modules A and B by using the "include" keyword. Now when an object of C is created, A and B will not be instantiated since modules have not particular instantiation. This object will still access to all the methods of modules A and B along with the modules defined in the class C. This is different from multiple inheritance in C++ where when a subclass is instantiated, the parent classes get instantiated as well.

Mixins vs Other multiple inheritance

Runtime Extensions

Mixins allows the programmer to extend the functionality, add methods and change the behavior of core classes at runtime. This feature is extremely useful since these changes will not effect the way the objects are instantiated in the program. This makes writing plugins, extensions and other changes to the framework very easy. In frameworks written in other languages, such as Java, plugging in new functionality means that you need to change how your objects are instantiated. This will require code changes and/or potentially configuration changes

Diamond Problem

Earlier we had discussed the Diamond Problem and its impact on multiple inheritance. Here is how the scenario changes with the usage of mixins in Ruby. When ever any ambiguity arises with regard to method inheritance, the method that was included last is considered. Hence the need for virtual inheritance is alleviated.

This can be illustrated with the following example. Consider a module A in inherited by modules B and C.

  module A
     def goodbye
        puts "hello"
     end
  end
  module B
     def goodbye
        puts "B goodbye world"
     end
  end
  module C
     def goodbye
        puts "C goodbye world"
     end
  end

Now consider a class D that inherits from B and C. This inheritance can be done in the following ways :-

Method 1:

  class D
     include A
     include B
  end 

or

Method 2:

  class D
     include B
     include A
  end

Suppose an object of class D calls the goodbye method, an ambiguity arises in other multiple inheritance implementations as to which goodbye method to invoke, B.goodbye or C.goodbye. In Ruby, the output will depend on which module is included last and the method associated to the last module included will be invoked. In the above example, Method 1 will output "B goodbye world" and Method 2 will output "A goodbye world".

Mixins vs interfaces

Java does not use multiple inheritance. Instead it uses interfaces to avoid the ambiguity problem that arises with multiple inheritance.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. Another point to note with mixins is that there are no type components, hence no type abstraction. There may also be the namespace overpopulation issue. This may arise from the fact that one wants growing functionality by adding more and more mixin classes.


Disadvantages of Mixins

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