CSC/ECE 517 Fall 2010/ch3 3b ka: Difference between revisions
No edit summary |
|||
Line 5: | Line 5: | ||
[http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch1_25_ag#What_is_Multiple_Inheritance Multiple inheritance] is often deemed as a very powerful tool in the [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch1_1e_bb#Object_Oriented_Programming Object Oriented Programming] paradigm. Implementation of multiple inheritance in languages like [http://en.wikipedia.org/wiki/Java_(programming_language) Java] ([http://download.oracle.com/javase/tutorial/java/concepts/interface.html interfaces]) and [http://en.wikipedia.org/wiki/C%2B%2B C++] is restricted and can often lead to a number of difficulties like the [http://en.wikipedia.org/wiki/Diamond_problem Diamond Problem], [http://en.wikipedia.org/wiki/Virtual_inheritance Virtual Inheritance] issue and increase in programming complexity. | [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch1_25_ag#What_is_Multiple_Inheritance Multiple inheritance] is often deemed as a very powerful tool in the [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch1_1e_bb#Object_Oriented_Programming Object Oriented Programming] paradigm. Implementation of multiple inheritance in languages like [http://en.wikipedia.org/wiki/Java_(programming_language) Java] ([http://download.oracle.com/javase/tutorial/java/concepts/interface.html interfaces]) and [http://en.wikipedia.org/wiki/C%2B%2B C++] is restricted and can often lead to a number of difficulties like the [http://en.wikipedia.org/wiki/Diamond_problem Diamond Problem], [http://en.wikipedia.org/wiki/Virtual_inheritance 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 [ http://www.ruby-lang.org/en/ Python], [ http://www.scala-lang.org/ scala ], [ http://www.ruby-lang.org/en/ ruby ] etc. | 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 [http://www.ruby-lang.org/en/ Python], [http://www.scala-lang.org/ scala], [http://www.ruby-lang.org/en/ ruby] etc. | ||
=Mixins= | =Mixins= |
Revision as of 22:44, 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.
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