<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Snallan</id>
	<title>Expertiza_Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Snallan"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Snallan"/>
	<updated>2026-05-13T00:56:58Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012&amp;diff=66442</id>
		<title>CSC/ECE 517 Fall 2012</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012&amp;diff=66442"/>
		<updated>2012-10-03T10:42:09Z</updated>

		<summary type="html">&lt;p&gt;Snallan: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;*[[CSC/ECE 517 Fall 2012/ch1 n xx]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w1 rk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w20 pp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w5 su]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w6 pp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w4 aj]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w7 am]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w8 aa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w9 av]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w10 pk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w11 ap]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1a 1w12 mv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w14 gv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w17 ir]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w18 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w22 an]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w21 aa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w21 wi]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w31 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1a 1w16 br]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1a 1w23 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w24 nr]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w15 rt]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w3 pl]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w32 cm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w67 ks]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w27 ms]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w29 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w33 op]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w19 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w34 vd]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w35 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w30 rp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w47 sk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w69 mv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w44 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w45 is]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w53 kc]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w40 ar]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w39 sn]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w54 go]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w64 nn]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w66 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w40 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w42 js]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w46 sm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w71 gs]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w63 dv]]&lt;/div&gt;</summary>
		<author><name>Snallan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012&amp;diff=66441</id>
		<title>CSC/ECE 517 Fall 2012</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012&amp;diff=66441"/>
		<updated>2012-10-03T10:41:38Z</updated>

		<summary type="html">&lt;p&gt;Snallan: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;*[[CSC/ECE 517 Fall 2012/ch1 n xx]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w1 rk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w20 pp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w5 su]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w6 pp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w4 aj]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w7 am]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w8 aa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w9 av]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w10 pk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w11 ap]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1a 1w12 mv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w14 gv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w17 ir]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w18 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w22 an]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w21 aa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w21 wi]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w31 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1a 1w16 br]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1a 1w23 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w24 nr]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w15 rt]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w3 pl]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w32 cm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w67 ks]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w27 ms]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w29 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w33 op]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w19 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w34 vd]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w35 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w30 rp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w47 sk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w69 mv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w44 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w45 is]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w53 kc]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w40 ar]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w39 sn]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w54 go]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w64 nn]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w66 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w40 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w42 js]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w46 sm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w71 gs]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w63 dv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w100 dv]]&lt;/div&gt;</summary>
		<author><name>Snallan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1a_1w23_as&amp;diff=65473</id>
		<title>CSC/ECE 517 Fall 2012/ch1a 1w23 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1a_1w23_as&amp;diff=65473"/>
		<updated>2012-09-21T18:40:15Z</updated>

		<summary type="html">&lt;p&gt;Snallan: /* See Also */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''1w23:  Multiple Inheritance and Mixins&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
The purpose of this article is to give a brief introduction about multiple inheritance, consider some of its issues and discuss techniques employed by different programming languages to handle such issues. In this relation, the article also compares solutions provided by different programming languages, especially against Ruby mixins, by listing their advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
Multiple Inheritance is a technique of establishing hierarchical relationships within a set of classes, such that a single class can inherit  behaviors from multiple parent classes. Although it might seem useful to include methods from multiple classes, the major issue with multiple inheritance is the ambiguity in referencing the parent class methods,  when more than one parent classes share a method with the same name.This ambiguity is predominant in cases where the inheritance hierarchy is more than one level deep, where the class in the lowermost level inherits from two classes, that have overridden the same method from their parent in different ways.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A Real World Example==&lt;br /&gt;
A known example of multiple inheritance to represent winged animals and mammals, using classes and objects is cited here &amp;lt;ref&amp;gt; http://stackoverflow.com/questions/9359948/multiple-inheritance-whats-a-good-example&amp;lt;/ref&amp;gt;. A Winged animal and a mammal inherit primitive attributes of an animal. While a winged animal can fly; a mammal can breathe and reproduce; they both eat, move and share features generic to animals. A bat being a winged mammal possesses features common to both winged animal and mammal. It follows that class Bat inherits from classes WingedAnimal and Mammal and in turn classes WingedAnimal and Mammal inherit from class Animal.   In reference to this context, let us write our own implementation and explore the issues of multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
[[File:Class_Diagram_-_Example.jpeg|thumb|right|x300px|alt=Class Diagram for the example.]]&lt;br /&gt;
&lt;br /&gt;
==Issues of Multiple Inheritance==&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Due to inheritance, the parent class’s objects get duplicated to child class. As a result, Class Bat duplicates Animal objects through each of WingedAnimal and Mammal classes. When Bat accesses any of the Animal's attributes, the compiler would be ambiguous about determining the correct path to use since two paths are available from Animal to Bat. This problem of multiple inheritance is called diamond problem &amp;lt;ref&amp;gt;http://www.artima.com/weblogs/viewpost.jsp?thread=246488&amp;lt;/ref&amp;gt;. Compiler takes arbitrary decisions during such situations. &amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt; Objects of parent classes can be separately managed by using [http://en.wikipedia.org/wiki/Virtual_method_table virtual tables]as in C++. By explicitly specifying the virtual table pointer, the Bat class can access WingedAnimal and Mammal classes unambiguously. However virtual tables occupy significant portion of memory thereby limiting their extensive usage. &amp;lt;/li&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt; The order by which a child class inherits parent classes also matters. If Bat inherits WingedAnimal prior to Mammal, then WingedAnimal precedes Mammal and common methods are accessed in this order of precedence.  Hence if the Programmer is not aware of such precedence, debugging would be difficult. &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
Programming languages provide different solutions to handle the issues of multiple inheritance. Here we consider the techniques offered by C++, Java, Ruby, Scala and Python.    &lt;br /&gt;
===Multiple Inheritance in C++===&lt;br /&gt;
C++ supports multiple inheritance with the notion of virtual classes and virtual functions. In the example, the Animal class defines virtual function canEat() with WingedAnimal and Mammal classes virtually inheriting the features of Animal. The Bat class can now unambiguously access canEat() method of Animal class, using virtual table pointers of WingedAnimal and Mammal. The example written using C++ virtual functions is shown below.&lt;br /&gt;
&lt;br /&gt;
 class Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void virtual canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class WingedAnimal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Mammal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Bat :public WingedAnimal, public Mammal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }  &lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
  int i = 0;&lt;br /&gt;
  Animal *instances[3];&lt;br /&gt;
  WingedAnimal w;&lt;br /&gt;
  Mammal m;&lt;br /&gt;
  Bat b;&lt;br /&gt;
  instances[0] = &amp;amp;w;&lt;br /&gt;
  instances[1] = &amp;amp;m;&lt;br /&gt;
  instances[2] = &amp;amp;b;&lt;br /&gt;
  while(i&amp;lt;3)&lt;br /&gt;
    instances[i++]-&amp;gt;canEat();&lt;br /&gt;
  return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Java===&lt;br /&gt;
Java doesn’t support multiple inheritance but provides a feature called interfaces to simulate inheritance from multiple classes. Interface is an abstract class which provides a generic template by just declaring variables and methods without implementing them. [http://www.brpreiss.com/books/opus4/html/page611.html Concrete classes] that inherit interfaces are supposed to implement its methods. The example written using Java interfaces is shown below.&lt;br /&gt;
&lt;br /&gt;
 interface Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface WingedAnimal extends Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();  &lt;br /&gt;
   void canFly();  &lt;br /&gt;
 }&lt;br /&gt;
 class ImplementWingedAnimal implements WingedAnimal  &lt;br /&gt;
 {&lt;br /&gt;
    public void canEat() {    &lt;br /&gt;
    }&lt;br /&gt;
    public void canFly() {    &lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Mammal extends Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();&lt;br /&gt;
   void canGiveBirth();&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementMammal implements Mammal&lt;br /&gt;
 {&lt;br /&gt;
    public void canEat() {    &lt;br /&gt;
    }&lt;br /&gt;
    public void canGiveBirth() {    &lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Bat extends WingedAnimal, Mammal&lt;br /&gt;
 {&lt;br /&gt;
    void canEat();    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class ImplementBat implements Bat&lt;br /&gt;
 {  &lt;br /&gt;
    ImplementWingedAnimal w = new ImplementWingedAnimal();&lt;br /&gt;
    ImplementMammal m = new ImplementMammal();     &lt;br /&gt;
    public void canEat() {            &lt;br /&gt;
      m.canEat();  &lt;br /&gt;
      w.canEat();      &lt;br /&gt;
    }&lt;br /&gt;
    public void canFly() {&lt;br /&gt;
    }&lt;br /&gt;
    public void canGiveBirth() {&lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class MultipleInheritance&lt;br /&gt;
 {&lt;br /&gt;
   public static void main(String args[])&lt;br /&gt;
   {&lt;br /&gt;
     ImplementBat b = new ImplementBat ();&lt;br /&gt;
     b.canEat();       &lt;br /&gt;
   } &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Ruby===&lt;br /&gt;
Ruby doesn't support multiple inheritance but provides a similar feature using modules and mixins.  &lt;br /&gt;
====Modules====&lt;br /&gt;
A Module is an encapsulation of methods and variables, although similar in function to a Class, differs from a class in the sense that it cannot be instantiated. Modules are  more dynamic and allow us to organize the code in a more granular way.Each module can add something to the existing class. A module can be thought of an encapsulation of a  subset of the full capabilities of an object.&lt;br /&gt;
An example found in the book ,The Well-Grounded Rubyist &amp;lt;ref&amp;gt;http://www.manning.com/black2/SampleChapter4.pdf [Pg.92] &amp;lt;/ref&amp;gt;, is the stack-like module. The module encapsulates the characteristics of being like a stack,implements the operations that can be performed by a stack but is not the actual Stack class. This allows us to use this module to mix-in  stack-like behaviour into other classes that require them.&lt;br /&gt;
&lt;br /&gt;
====Mixins====&lt;br /&gt;
A Mixin is a concept that allows inclusion of special characteristics into existing classes, so that common features that need to be reused in several classes can be selectively included, thus avoiding implementation of unnecessary functions like in the case of interfaces.&lt;br /&gt;
Mixins allow us to dynamically bind special features defined in modules, into core classes without really making any changes to the objects. This improves flexibility when we want to add plugins and extensions to the existing framework.&amp;lt;ref&amp;gt;http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-frameworks/&amp;lt;/ref&amp;gt;.The source code for the same example implemented using ruby mixins is shown below.&lt;br /&gt;
&lt;br /&gt;
 module Animal&lt;br /&gt;
   def canEat     &lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module WingedAnimal&lt;br /&gt;
   include Animal    &lt;br /&gt;
   def canEat&lt;br /&gt;
   end      &lt;br /&gt;
   def canFly&lt;br /&gt;
   end      &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Mammal&lt;br /&gt;
   include Animal&lt;br /&gt;
   def canEat&lt;br /&gt;
   end&lt;br /&gt;
   def canGiveBirth&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Bat&lt;br /&gt;
   include WingedAnimal&lt;br /&gt;
   include Mammal&lt;br /&gt;
   include Animal  &lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 Bat.new().canEat()&lt;br /&gt;
&lt;br /&gt;
===Multiple inheritance in other languages===&lt;br /&gt;
[http://www.scala-lang.org/ Scala] offers [http://www.scala-lang.org/node/126 traits] that are similar to Java interfaces to support multiple inheritance. Traits define a template with methods and variables which any class can make use of. The difference between interfaces and traits is that traits can implement methods. A Scala class can inherit from any number of traits together with a class in a way to support multiple inheritance. Traits can be mixed in to a class even without inheriting it but just by including it at the time of creating instances similar to extend feature in ruby. &amp;lt;ref&amp;gt;http://joelabrahamsson.com/entry/learning-scala-traits&amp;lt;/ref&amp;gt;. Trait can also extend a class and methods to be overridden are usually prefixed by the keyword override. The language enforces method names to be different across inherited  traits to avoid the issues of multiple inheritance. [http://www.python.org/ Python] supports a limited version of multiple inheritance by extending multiple classes in depth-first left-right order &amp;lt;ref&amp;gt;http://docs.python.org/tutorial/classes.html&amp;lt;/ref&amp;gt;. If a python class D inherits classes A, B, C and if an instance variable or method is not available in the current class, it checks in A and its base classes, then moves to B and its base classes and eventually to C thus moving from left to right. In this way, python avoids the diamond problem by making the search order linear, by which the derived class accesses attributes of its parent classes. In addition python also defines mixins to access reusable features within a class.&lt;br /&gt;
&lt;br /&gt;
==Advantages of mixins==&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Firstly, when a module gets mixed into different classes, each class acquires the same  reference  to the module and hence even dynamic changes to the module gets reflected in the behavior of the classes &amp;lt;ref&amp;gt;http://www.linuxtopia.org/online_books/programming_books/ruby_tutorial/Ruby_Modules_Mixins.html&amp;lt;/ref&amp;gt;. In this way, the module’s code is not just copied to individual classes as in static languages instead its reference is provided. &amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt; Secondly, the mixed in state and behavior can be accessed by the instance of target class just like accessing its own features. &amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt; Thirdly, a class can include any number of modules in a way it can simulate multiple inheritance without having the related issues. &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Questions to Ponder==&lt;br /&gt;
===Do mixins solve all of the problems of multiple inheritance?===&lt;br /&gt;
Undoubtedly mixins solve some of the problems of multiple inheritance. There is no ambiguity in resolving the request to a common method implemented by multiple modules. In that case, the lastly included module is responsible for handling accesses to common methods. Though the order of precedence of included modules matters, it looks clearer than C++.  However languages like Scala and Eiffel implement multiple inheritance far more safely  by explicitly forcing the user to have different method names across inherited classes.&lt;br /&gt;
&lt;br /&gt;
===Are mixins a clear advantage over interfaces?===&lt;br /&gt;
Mixins are more advantageous than interfaces because of their features. Interfaces have some limitations. As seen from the example, the derived class Bat uses the instances of WingedAnimal and Mammal classes (has-a relationship) to access respective attributes. Interface is nice in the sense it provides a generic template that could be implemented differently by different classes.  However the target class has to necessarily implement every method of the interface. This would pose problems to a Class which is interested in implementing specific features of the interface. On the contrary, Mixins eliminate this inconvenience by mixing in the state and behavior of the modules in the target class and allows it to use them as it needs. Hence the target class can just use module’s features selectively or completely instead of implementing by itself. The size of multiple inheritance code using mixins is smaller than interfaces and C++ variants and this is obvious from the example.&lt;br /&gt;
&lt;br /&gt;
===Do mixins have any disadvantages not shared by multiple inheritance or interfaces?===&lt;br /&gt;
Inspite of making the lives of programmers better with effective code reuse and avoiding unnecessary implementation of methods that cause clutter in the code, the concept of Mixins can easily be abused resulting in namespace pollution. This is caused when a class includes multiple modules each of which contains several hundred method implementations. Having hundreds of methods mixed into a single class, makes it difficult to identify the source modules of the methods, thereby causing confusion while implementing these methods, whereas in the case of multiple inheritance we can easily identify the class to which the method belongs.&lt;br /&gt;
Another disadvantage that follows from the namespace collision is the case where we have multiple methods with the same name in different modules.In such a case whenever a call to that method is made, the method which belongs to the module that was included last will be called. In the above example, the method call canEat() invokes Animal's canEat() method since Animal was the last included module. The problem with this is that, this takes place silently without any warning. This may result in accidentally inheriting wrong methods.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
In this article, we have discussed about multiple inheritance and means by which it is implemented in different languages. Though multiple inheritance is a useful feature,&lt;br /&gt;
many languages do not support them due to its associated issues. Given its advantages, mixin in ruby seems to be a viable option to achieve multiple inheritance. However mixins too have limitations which should be taken care of during implementation.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
==See Also==&lt;br /&gt;
1. Dominic Duggan and Ching Ching Techaubol- OOPSLA '01 Proceedings of the 16th ACM SIGPLAN conference on Object-oriented programming, systems, languages, and applications, :[http://www.google.com/url?sa=t&amp;amp;rct=j&amp;amp;q=&amp;amp;esrc=s&amp;amp;source=web&amp;amp;cd=10&amp;amp;ved=0CF8QFjAJ&amp;amp;url=http%3A%2F%2Fciteseerx.ist.psu.edu%2Fviewdoc%2Fdownload%3Fdoi%3D10.1.1.111.6648%26rep%3Drep1%26type%3Dpdf&amp;amp;ei=Pp9cULzCF4-E9QSc_YDwCw&amp;amp;usg=AFQjCNG8xhSWsH832wD2scfm8d5GdQCDzA           Modular Mixin-Based Inheritance for Application Frameworks ]&lt;br /&gt;
&lt;br /&gt;
2. Lucas Carlson and Leonard Richardson. 2006. [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=1212915&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 Ruby Cookbook] (Cookbooks (O'Reilly)). O'Reilly Media, Inc..&lt;br /&gt;
&lt;br /&gt;
3. Herbert Schildt. 2000. Java 2: [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=557816&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 The Complete Reference] (4th ed.). McGraw-Hill Professional.&lt;br /&gt;
&lt;br /&gt;
4. http://en.wikipedia.org/wiki/Mixin&lt;/div&gt;</summary>
		<author><name>Snallan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1a_1w23_as&amp;diff=65472</id>
		<title>CSC/ECE 517 Fall 2012/ch1a 1w23 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1a_1w23_as&amp;diff=65472"/>
		<updated>2012-09-21T18:21:49Z</updated>

		<summary type="html">&lt;p&gt;Snallan: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''1w23:  Multiple Inheritance and Mixins&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
The purpose of this article is to give a brief introduction about multiple inheritance, consider some of its issues and discuss techniques employed by different programming languages to handle such issues. In this relation, the article also compares solutions provided by different programming languages, especially against Ruby mixins, by listing their advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
Multiple Inheritance is a technique of establishing hierarchical relationships within a set of classes, such that a single class can inherit  behaviors from multiple parent classes. Although it might seem useful to include methods from multiple classes, the major issue with multiple inheritance is the ambiguity in referencing the parent class methods,  when more than one parent classes share a method with the same name.This ambiguity is predominant in cases where the inheritance hierarchy is more than one level deep, where the class in the lowermost level inherits from two classes, that have overridden the same method from their parent in different ways.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A Real World Example==&lt;br /&gt;
A known example of multiple inheritance to represent winged animals and mammals, using classes and objects is cited here &amp;lt;ref&amp;gt; http://stackoverflow.com/questions/9359948/multiple-inheritance-whats-a-good-example&amp;lt;/ref&amp;gt;. A Winged animal and a mammal inherit primitive attributes of an animal. While a winged animal can fly; a mammal can breathe and reproduce; they both eat, move and share features generic to animals. A bat being a winged mammal possesses features common to both winged animal and mammal. It follows that class Bat inherits from classes WingedAnimal and Mammal and in turn classes WingedAnimal and Mammal inherit from class Animal.   In reference to this context, let us write our own implementation and explore the issues of multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
[[File:Class_Diagram_-_Example.jpeg|thumb|right|x300px|alt=Class Diagram for the example.]]&lt;br /&gt;
&lt;br /&gt;
==Issues of Multiple Inheritance==&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Due to inheritance, the parent class’s objects get duplicated to child class. As a result, Class Bat duplicates Animal objects through each of WingedAnimal and Mammal classes. When Bat accesses any of the Animal's attributes, the compiler would be ambiguous about determining the correct path to use since two paths are available from Animal to Bat. This problem of multiple inheritance is called diamond problem &amp;lt;ref&amp;gt;http://www.artima.com/weblogs/viewpost.jsp?thread=246488&amp;lt;/ref&amp;gt;. Compiler takes arbitrary decisions during such situations. &amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt; Objects of parent classes can be separately managed by using [http://en.wikipedia.org/wiki/Virtual_method_table virtual tables]as in C++. By explicitly specifying the virtual table pointer, the Bat class can access WingedAnimal and Mammal classes unambiguously. However virtual tables occupy significant portion of memory thereby limiting their extensive usage. &amp;lt;/li&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt; The order by which a child class inherits parent classes also matters. If Bat inherits WingedAnimal prior to Mammal, then WingedAnimal precedes Mammal and common methods are accessed in this order of precedence.  Hence if the Programmer is not aware of such precedence, debugging would be difficult. &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
Programming languages provide different solutions to handle the issues of multiple inheritance. Here we consider the techniques offered by C++, Java, Ruby, Scala and Python.    &lt;br /&gt;
===Multiple Inheritance in C++===&lt;br /&gt;
C++ supports multiple inheritance with the notion of virtual classes and virtual functions. In the example, the Animal class defines virtual function canEat() with WingedAnimal and Mammal classes virtually inheriting the features of Animal. The Bat class can now unambiguously access canEat() method of Animal class, using virtual table pointers of WingedAnimal and Mammal. The example written using C++ virtual functions is shown below.&lt;br /&gt;
&lt;br /&gt;
 class Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void virtual canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class WingedAnimal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Mammal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Bat :public WingedAnimal, public Mammal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }  &lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
  int i = 0;&lt;br /&gt;
  Animal *instances[3];&lt;br /&gt;
  WingedAnimal w;&lt;br /&gt;
  Mammal m;&lt;br /&gt;
  Bat b;&lt;br /&gt;
  instances[0] = &amp;amp;w;&lt;br /&gt;
  instances[1] = &amp;amp;m;&lt;br /&gt;
  instances[2] = &amp;amp;b;&lt;br /&gt;
  while(i&amp;lt;3)&lt;br /&gt;
    instances[i++]-&amp;gt;canEat();&lt;br /&gt;
  return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Java===&lt;br /&gt;
Java doesn’t support multiple inheritance but provides a feature called interfaces to simulate inheritance from multiple classes. Interface is an abstract class which provides a generic template by just declaring variables and methods without implementing them. [http://www.brpreiss.com/books/opus4/html/page611.html Concrete classes] that inherit interfaces are supposed to implement its methods. The example written using Java interfaces is shown below.&lt;br /&gt;
&lt;br /&gt;
 interface Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface WingedAnimal extends Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();  &lt;br /&gt;
   void canFly();  &lt;br /&gt;
 }&lt;br /&gt;
 class ImplementWingedAnimal implements WingedAnimal  &lt;br /&gt;
 {&lt;br /&gt;
    public void canEat() {    &lt;br /&gt;
    }&lt;br /&gt;
    public void canFly() {    &lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Mammal extends Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();&lt;br /&gt;
   void canGiveBirth();&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementMammal implements Mammal&lt;br /&gt;
 {&lt;br /&gt;
    public void canEat() {    &lt;br /&gt;
    }&lt;br /&gt;
    public void canGiveBirth() {    &lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Bat extends WingedAnimal, Mammal&lt;br /&gt;
 {&lt;br /&gt;
    void canEat();    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class ImplementBat implements Bat&lt;br /&gt;
 {  &lt;br /&gt;
    ImplementWingedAnimal w = new ImplementWingedAnimal();&lt;br /&gt;
    ImplementMammal m = new ImplementMammal();     &lt;br /&gt;
    public void canEat() {            &lt;br /&gt;
      m.canEat();  &lt;br /&gt;
      w.canEat();      &lt;br /&gt;
    }&lt;br /&gt;
    public void canFly() {&lt;br /&gt;
    }&lt;br /&gt;
    public void canGiveBirth() {&lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class MultipleInheritance&lt;br /&gt;
 {&lt;br /&gt;
   public static void main(String args[])&lt;br /&gt;
   {&lt;br /&gt;
     ImplementBat b = new ImplementBat ();&lt;br /&gt;
     b.canEat();       &lt;br /&gt;
   } &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Ruby===&lt;br /&gt;
Ruby doesn't support multiple inheritance but provides a similar feature using modules and mixins.  &lt;br /&gt;
====Modules====&lt;br /&gt;
A Module is an encapsulation of methods and variables, although similar in function to a Class, differs from a class in the sense that it cannot be instantiated. Modules are  more dynamic and allow us to organize the code in a more granular way.Each module can add something to the existing class. A module can be thought of an encapsulation of a  subset of the full capabilities of an object.&lt;br /&gt;
An example found in the book ,The Well-Grounded Rubyist &amp;lt;ref&amp;gt;http://www.manning.com/black2/SampleChapter4.pdf [Pg.92] &amp;lt;/ref&amp;gt;, is the stack-like module. The module encapsulates the characteristics of being like a stack,implements the operations that can be performed by a stack but is not the actual Stack class. This allows us to use this module to mix-in  stack-like behaviour into other classes that require them.&lt;br /&gt;
&lt;br /&gt;
====Mixins====&lt;br /&gt;
A Mixin is a concept that allows inclusion of special characteristics into existing classes, so that common features that need to be reused in several classes can be selectively included, thus avoiding implementation of unnecessary functions like in the case of interfaces.&lt;br /&gt;
Mixins allow us to dynamically bind special features defined in modules, into core classes without really making any changes to the objects. This improves flexibility when we want to add plugins and extensions to the existing framework.&amp;lt;ref&amp;gt;http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-frameworks/&amp;lt;/ref&amp;gt;.The source code for the same example implemented using ruby mixins is shown below.&lt;br /&gt;
&lt;br /&gt;
 module Animal&lt;br /&gt;
   def canEat     &lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module WingedAnimal&lt;br /&gt;
   include Animal    &lt;br /&gt;
   def canEat&lt;br /&gt;
   end      &lt;br /&gt;
   def canFly&lt;br /&gt;
   end      &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Mammal&lt;br /&gt;
   include Animal&lt;br /&gt;
   def canEat&lt;br /&gt;
   end&lt;br /&gt;
   def canGiveBirth&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Bat&lt;br /&gt;
   include WingedAnimal&lt;br /&gt;
   include Mammal&lt;br /&gt;
   include Animal  &lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 Bat.new().canEat()&lt;br /&gt;
&lt;br /&gt;
===Multiple inheritance in other languages===&lt;br /&gt;
[http://www.scala-lang.org/ Scala] offers [http://www.scala-lang.org/node/126 traits] that are similar to Java interfaces to support multiple inheritance. Traits define a template with methods and variables which any class can make use of. The difference between interfaces and traits is that traits can implement methods. A Scala class can inherit from any number of traits together with a class in a way to support multiple inheritance. Traits can be mixed in to a class even without inheriting it but just by including it at the time of creating instances similar to extend feature in ruby. &amp;lt;ref&amp;gt;http://joelabrahamsson.com/entry/learning-scala-traits&amp;lt;/ref&amp;gt;. Trait can also extend a class and methods to be overridden are usually prefixed by the keyword override. The language enforces method names to be different across inherited  traits to avoid the issues of multiple inheritance. [http://www.python.org/ Python] supports a limited version of multiple inheritance by extending multiple classes in depth-first left-right order &amp;lt;ref&amp;gt;http://docs.python.org/tutorial/classes.html&amp;lt;/ref&amp;gt;. If a python class D inherits classes A, B, C and if an instance variable or method is not available in the current class, it checks in A and its base classes, then moves to B and its base classes and eventually to C thus moving from left to right. In this way, python avoids the diamond problem by making the search order linear, by which the derived class accesses attributes of its parent classes. In addition python also defines mixins to access reusable features within a class.&lt;br /&gt;
&lt;br /&gt;
==Advantages of mixins==&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Firstly, when a module gets mixed into different classes, each class acquires the same  reference  to the module and hence even dynamic changes to the module gets reflected in the behavior of the classes &amp;lt;ref&amp;gt;http://www.linuxtopia.org/online_books/programming_books/ruby_tutorial/Ruby_Modules_Mixins.html&amp;lt;/ref&amp;gt;. In this way, the module’s code is not just copied to individual classes as in static languages instead its reference is provided. &amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt; Secondly, the mixed in state and behavior can be accessed by the instance of target class just like accessing its own features. &amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt; Thirdly, a class can include any number of modules in a way it can simulate multiple inheritance without having the related issues. &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Questions to Ponder==&lt;br /&gt;
===Do mixins solve all of the problems of multiple inheritance?===&lt;br /&gt;
Undoubtedly mixins solve some of the problems of multiple inheritance. There is no ambiguity in resolving the request to a common method implemented by multiple modules. In that case, the lastly included module is responsible for handling accesses to common methods. Though the order of precedence of included modules matters, it looks clearer than C++.  However languages like Scala and Eiffel implement multiple inheritance far more safely  by explicitly forcing the user to have different method names across inherited classes.&lt;br /&gt;
&lt;br /&gt;
===Are mixins a clear advantage over interfaces?===&lt;br /&gt;
Mixins are more advantageous than interfaces because of their features. Interfaces have some limitations. As seen from the example, the derived class Bat uses the instances of WingedAnimal and Mammal classes (has-a relationship) to access respective attributes. Interface is nice in the sense it provides a generic template that could be implemented differently by different classes.  However the target class has to necessarily implement every method of the interface. This would pose problems to a Class which is interested in implementing specific features of the interface. On the contrary, Mixins eliminate this inconvenience by mixing in the state and behavior of the modules in the target class and allows it to use them as it needs. Hence the target class can just use module’s features selectively or completely instead of implementing by itself. The size of multiple inheritance code using mixins is smaller than interfaces and C++ variants and this is obvious from the example.&lt;br /&gt;
&lt;br /&gt;
===Do mixins have any disadvantages not shared by multiple inheritance or interfaces?===&lt;br /&gt;
Inspite of making the lives of programmers better with effective code reuse and avoiding unnecessary implementation of methods that cause clutter in the code, the concept of Mixins can easily be abused resulting in namespace pollution. This is caused when a class includes multiple modules each of which contains several hundred method implementations. Having hundreds of methods mixed into a single class, makes it difficult to identify the source modules of the methods, thereby causing confusion while implementing these methods, whereas in the case of multiple inheritance we can easily identify the class to which the method belongs.&lt;br /&gt;
Another disadvantage that follows from the namespace collision is the case where we have multiple methods with the same name in different modules.In such a case whenever a call to that method is made, the method which belongs to the module that was included last will be called. In the above example, the method call canEat() invokes Animal's canEat() method since Animal was the last included module. The problem with this is that, this takes place silently without any warning. This may result in accidentally inheriting wrong methods.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
In this article, we have discussed about multiple inheritance and means by which it is implemented in different languages. Though multiple inheritance is a useful feature,&lt;br /&gt;
many languages do not support them due to its associated issues. Given its advantages, mixin in ruby seems to be a viable option to achieve multiple inheritance. However mixins too have limitations which should be taken care of during implementation.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
==See Also==&lt;br /&gt;
1. Dominic Duggan and Ching Ching Techaubol- OOPSLA '01 Proceedings of the 16th ACM SIGPLAN conference on Object-oriented programming, systems, languages, and applications, :[http://www.google.com/url?sa=t&amp;amp;rct=j&amp;amp;q=&amp;amp;esrc=s&amp;amp;source=web&amp;amp;cd=10&amp;amp;ved=0CF8QFjAJ&amp;amp;url=http%3A%2F%2Fciteseerx.ist.psu.edu%2Fviewdoc%2Fdownload%3Fdoi%3D10.1.1.111.6648%26rep%3Drep1%26type%3Dpdf&amp;amp;ei=Pp9cULzCF4-E9QSc_YDwCw&amp;amp;usg=AFQjCNG8xhSWsH832wD2scfm8d5GdQCDzA           Modular Mixin-Based Inheritance for Application Frameworks ]&lt;br /&gt;
&lt;br /&gt;
2. Lucas Carlson and Leonard Richardson. 2006. [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=1212915&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 Ruby Cookbook] (Cookbooks (O'Reilly)). O'Reilly Media, Inc..&lt;br /&gt;
&lt;br /&gt;
3. Herbert Schildt. 2000. Java 2: [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=557816&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 The Complete Reference] (4th ed.). McGraw-Hill Professional.&lt;/div&gt;</summary>
		<author><name>Snallan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65466</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65466"/>
		<updated>2012-09-21T17:26:23Z</updated>

		<summary type="html">&lt;p&gt;Snallan: /* A Real World Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''1w23:  Multiple Inheritance and Mixins&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
The purpose of this article is to give a brief introduction about multiple inheritance, consider some of its issues and discuss techniques employed by different programming languages to handle such issues. In this relation, the article also compares solutions provided by different programming languages, especially against Ruby mixins, by listing their advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
Multiple Inheritance is a technique of establishing hierarchical relationships within a set of classes, such that a single class can inherit  behaviors from multiple parent classes. Although it might seem useful to include methods from multiple classes, the major issue with multiple inheritance is the ambiguity in referencing the parent class methods,  when more than one parent classes share a method with the same name.This ambiguity is predominant in cases where the inheritance hierarchy is more than one level deep, where the class in the lowermost level inherits from two classes, that have overridden the same method from their parent in different ways.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A Real World Example==&lt;br /&gt;
A known example of multiple inheritance to represent winged animals and mammals, using classes and objects is cited here &amp;lt;ref&amp;gt; http://stackoverflow.com/questions/9359948/multiple-inheritance-whats-a-good-example&amp;lt;/ref&amp;gt;. A Winged animal and a mammal inherit primitive attributes of an animal. While a winged animal can fly; a mammal can breathe and reproduce; they both eat, move and share features generic to animals. A bat being a winged mammal possesses features common to both winged animal and mammal. It follows that class Bat inherits from classes WingedAnimal and Mammal and in turn classes WingedAnimal and Mammal inherit from class Animal.   In reference to this context, let us write our own implementation and explore the issues of multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
[[File:Class_Diagram_-_Example.jpeg|thumb|right|x300px|alt=Class Diagram for the example.]]&lt;br /&gt;
&lt;br /&gt;
==Issues of Multiple Inheritance==&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Due to inheritance, the parent class’s objects get duplicated to child class. As a result, Class Bat duplicates Animal objects through each of WingedAnimal and Mammal classes. When Bat accesses any of the Animal's attributes, the compiler would be ambiguous about determining the correct path to use since two paths are available from Animal to Bat. This problem of multiple inheritance is called diamond problem &amp;lt;ref&amp;gt;http://www.artima.com/weblogs/viewpost.jsp?thread=246488&amp;lt;/ref&amp;gt;. Compiler takes arbitrary decisions during such situations. &amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt; Objects of parent classes can be separately managed by using [http://en.wikipedia.org/wiki/Virtual_method_table virtual tables]as in C++. By explicitly specifying the virtual table pointer, the Bat class can access WingedAnimal and Mammal classes unambiguously. However virtual tables occupy significant portion of memory thereby limiting their extensive usage. &amp;lt;/li&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt; The order by which a child class inherits parent classes also matters. If Bat inherits WingedAnimal prior to Mammal, then WingedAnimal precedes Mammal and common methods are accessed in this order of precedence.  Hence if the Programmer is not aware of such precedence, debugging would be difficult. &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
Programming languages provide different solutions to handle the issues of multiple inheritance. Here we consider the techniques offered by C++, Java, Ruby, Scala and Python.    &lt;br /&gt;
===Multiple Inheritance in C++===&lt;br /&gt;
C++ supports multiple inheritance with the notion of virtual classes and virtual functions. In the example, the Animal class defines virtual function canEat() with WingedAnimal and Mammal classes virtually inheriting the features of Animal. The Bat class can now unambiguously access canEat() method of Animal class, using virtual table pointers of WingedAnimal and Mammal. The example written using C++ virtual functions is shown below.&lt;br /&gt;
&lt;br /&gt;
 class Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void virtual canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class WingedAnimal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Mammal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Bat :public WingedAnimal, public Mammal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }  &lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
  int i = 0;&lt;br /&gt;
  Animal *instances[3];&lt;br /&gt;
  WingedAnimal w;&lt;br /&gt;
  Mammal m;&lt;br /&gt;
  Bat b;&lt;br /&gt;
  instances[0] = &amp;amp;w;&lt;br /&gt;
  instances[1] = &amp;amp;m;&lt;br /&gt;
  instances[2] = &amp;amp;b;&lt;br /&gt;
  while(i&amp;lt;3)&lt;br /&gt;
    instances[i++]-&amp;gt;canEat();&lt;br /&gt;
  return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Java===&lt;br /&gt;
Java doesn’t support multiple inheritance but provides a feature called interfaces to simulate inheritance from multiple classes. Interface is an abstract class which provides a generic template by just declaring variables and methods without implementing them. [http://www.brpreiss.com/books/opus4/html/page611.html Concrete classes] that inherit interfaces are supposed to implement its methods. The example written using Java interfaces is shown below.&lt;br /&gt;
&lt;br /&gt;
 interface Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface WingedAnimal extends Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();  &lt;br /&gt;
   void canFly();  &lt;br /&gt;
 }&lt;br /&gt;
 class ImplementWingedAnimal implements WingedAnimal  &lt;br /&gt;
 {&lt;br /&gt;
    public void canEat() {    &lt;br /&gt;
    }&lt;br /&gt;
    public void canFly() {    &lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Mammal extends Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();&lt;br /&gt;
   void canGiveBirth();&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementMammal implements Mammal&lt;br /&gt;
 {&lt;br /&gt;
    public void canEat() {    &lt;br /&gt;
    }&lt;br /&gt;
    public void canGiveBirth() {    &lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Bat extends WingedAnimal, Mammal&lt;br /&gt;
 {&lt;br /&gt;
    void canEat();    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class ImplementBat implements Bat&lt;br /&gt;
 {  &lt;br /&gt;
    ImplementWingedAnimal w = new ImplementWingedAnimal();&lt;br /&gt;
    ImplementMammal m = new ImplementMammal();     &lt;br /&gt;
    public void canEat() {            &lt;br /&gt;
      m.canEat();  &lt;br /&gt;
      w.canEat();      &lt;br /&gt;
    }&lt;br /&gt;
    public void canFly() {&lt;br /&gt;
    }&lt;br /&gt;
    public void canGiveBirth() {&lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class MultipleInheritance&lt;br /&gt;
 {&lt;br /&gt;
   public static void main(String args[])&lt;br /&gt;
   {&lt;br /&gt;
     ImplementBat b = new ImplementBat ();&lt;br /&gt;
     b.canEat();       &lt;br /&gt;
   } &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Ruby===&lt;br /&gt;
Ruby doesn't support multiple inheritance but provides a similar feature using modules and mixins.  &lt;br /&gt;
====Modules====&lt;br /&gt;
A Module is an encapsulation of methods and variables, although similar in function to a Class, differs from a class in the sense that it cannot be instantiated. Modules are  more dynamic and allow us to organize the code in a more granular way.Each module can add something to the existing class. A module can be thought of an encapsulation of a  subset of the full capabilities of an object.&lt;br /&gt;
An example found in the book ,The Well-Grounded Rubyist &amp;lt;ref&amp;gt;http://www.manning.com/black2/SampleChapter4.pdf [Pg.92] &amp;lt;/ref&amp;gt;, is the stack-like module. The module encapsulates the characteristics of being like a stack,implements the operations that can be performed by a stack but is not the actual Stack class. This allows us to use this module to mix-in  stack-like behaviour into other classes that require them.&lt;br /&gt;
&lt;br /&gt;
====Mixins====&lt;br /&gt;
A Mixin is a concept that allows inclusion of special characteristics into existing classes, so that common features that need to be reused in several classes can be selectively included, thus avoiding implementation of unnecessary functions like in the case of interfaces.&lt;br /&gt;
Mixins allow us to dynamically bind special features defined in modules, into core classes without really making any changes to the objects. This improves flexibility when we want to add plugins and extensions to the existing framework.&amp;lt;ref&amp;gt;http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-frameworks/&amp;lt;/ref&amp;gt;.The source code for the same example implemented using ruby mixins is shown below.&lt;br /&gt;
&lt;br /&gt;
 module Animal&lt;br /&gt;
   def canEat     &lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module WingedAnimal&lt;br /&gt;
   include Animal    &lt;br /&gt;
   def canEat&lt;br /&gt;
   end      &lt;br /&gt;
   def canFly&lt;br /&gt;
   end      &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Mammal&lt;br /&gt;
   include Animal&lt;br /&gt;
   def canEat&lt;br /&gt;
   end&lt;br /&gt;
   def canGiveBirth&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Bat&lt;br /&gt;
   include WingedAnimal&lt;br /&gt;
   include Mammal&lt;br /&gt;
   include Animal  &lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 Bat.new().canEat()&lt;br /&gt;
&lt;br /&gt;
===Multiple inheritance in other languages===&lt;br /&gt;
[http://www.scala-lang.org/ Scala] offers [http://www.scala-lang.org/node/126 traits] that are similar to Java interfaces to support multiple inheritance. Traits define a template with methods and variables which any class can make use of. The difference between interfaces and traits is that traits can implement methods. A Scala class can inherit from any number of traits together with a class in a way to support multiple inheritance. Traits can be mixed in to a class even without inheriting it but just by including it at the time of creating instances similar to extend feature in ruby. &amp;lt;ref&amp;gt;http://joelabrahamsson.com/entry/learning-scala-traits&amp;lt;/ref&amp;gt;. Trait can also extend a class and methods to be overridden are usually prefixed by the keyword override. The language enforces method names to be different across inherited  traits to avoid the issues of multiple inheritance. [http://www.python.org/ Python] supports a limited version of multiple inheritance by extending multiple classes in depth-first left-right order &amp;lt;ref&amp;gt;http://docs.python.org/tutorial/classes.html&amp;lt;/ref&amp;gt;. If a python class D inherits classes A, B, C and if an instance variable or method is not available in the current class, it checks in A and its base classes, then moves to B and its base classes and eventually to C thus moving from left to right. In this way, python avoids the diamond problem by making the search order linear, by which the derived class accesses attributes of its parent classes. In addition python also defines mixins to access reusable features within a class.&lt;br /&gt;
&lt;br /&gt;
==Advantages of mixins==&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Firstly, when a module gets mixed into different classes, each class acquires the same  reference  to the module and hence even dynamic changes to the module gets reflected in the behavior of the classes &amp;lt;ref&amp;gt;http://www.linuxtopia.org/online_books/programming_books/ruby_tutorial/Ruby_Modules_Mixins.html&amp;lt;/ref&amp;gt;. In this way, the module’s code is not just copied to individual classes as in static languages instead its reference is provided. &amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt; Secondly, the mixed in state and behavior can be accessed by the instance of target class just like accessing its own features. &amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt; Thirdly, a class can include any number of modules in a way it can simulate multiple inheritance without having the related issues. &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Questions to Ponder==&lt;br /&gt;
===Do mixins solve all of the problems of multiple inheritance?===&lt;br /&gt;
Undoubtedly mixins solve some of the problems of multiple inheritance. There is no ambiguity in resolving the request to a common method implemented by multiple modules. In that case, the lastly included module is responsible for handling accesses to common methods. Though the order of precedence of included modules matters, it looks clearer than C++.  However languages like Scala and Eiffel implement multiple inheritance far more safely  by explicitly forcing the user to have different method names across inherited classes.&lt;br /&gt;
&lt;br /&gt;
===Are mixins a clear advantage over interfaces?===&lt;br /&gt;
Mixins are more advantageous than interfaces because of their features. Interfaces have some limitations. As seen from the example, the derived class Bat uses the instances of WingedAnimal and Mammal classes (has-a relationship) to access respective attributes. Interface is nice in the sense it provides a generic template that could be implemented differently by different classes.  However the target class has to necessarily implement every method of the interface. This would pose problems to a Class which is interested in implementing specific features of the interface. On the contrary, Mixins eliminate this inconvenience by mixing in the state and behavior of the modules in the target class and allows it to use them as it needs. Hence the target class can just use module’s features selectively or completely instead of implementing by itself. The size of multiple inheritance code using mixins is smaller than interfaces and C++ variants and this is obvious from the example.&lt;br /&gt;
&lt;br /&gt;
===Do mixins have any disadvantages not shared by multiple inheritance or interfaces?===&lt;br /&gt;
Inspite of making the lives of programmers better with effective code reuse and avoiding unnecessary implementation of methods that cause clutter in the code, the concept of Mixins can easily be abused resulting in namespace pollution. This is caused when a class includes multiple modules each of which contains several hundred method implementations. Having hundreds of methods mixed into a single class, makes it difficult to identify the source modules of the methods, thereby causing confusion while implementing these methods, whereas in the case of multiple inheritance we can easily identify the class to which the method belongs.&lt;br /&gt;
Another disadvantage that follows from the namespace collision is the case where we have multiple methods with the same name in different modules.In such a case whenever a call to that method is made, the method which belongs to the module that was included last will be called. In the above example, the method call canEat() invokes Animal's canEat() method since Animal was the last included module. The problem with this is that, this takes place silently without any warning. This may result in accidentally inheriting wrong methods.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
In this article, we have discussed about multiple inheritance and means by which it is implemented in different languages. Though multiple inheritance is a useful feature,&lt;br /&gt;
many languages do not support them due to its associated issues. Given its advantages, mixin in ruby seems to be a viable option to achieve multiple inheritance. However mixins too have limitations which should be taken care of during implementation.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
==See Also==&lt;br /&gt;
1. Lucas Carlson and Leonard Richardson. 2006. [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=1212915&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 Ruby Cookbook] (Cookbooks (O'Reilly)). O'Reilly Media, Inc..&lt;br /&gt;
&lt;br /&gt;
2. Herbert Schildt. 2000. Java 2: [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=557816&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 The Complete Reference] (4th ed.). McGraw-Hill Professional.&lt;/div&gt;</summary>
		<author><name>Snallan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65465</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65465"/>
		<updated>2012-09-21T17:26:08Z</updated>

		<summary type="html">&lt;p&gt;Snallan: /* A Real World Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''1w23:  Multiple Inheritance and Mixins&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
The purpose of this article is to give a brief introduction about multiple inheritance, consider some of its issues and discuss techniques employed by different programming languages to handle such issues. In this relation, the article also compares solutions provided by different programming languages, especially against Ruby mixins, by listing their advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
Multiple Inheritance is a technique of establishing hierarchical relationships within a set of classes, such that a single class can inherit  behaviors from multiple parent classes. Although it might seem useful to include methods from multiple classes, the major issue with multiple inheritance is the ambiguity in referencing the parent class methods,  when more than one parent classes share a method with the same name.This ambiguity is predominant in cases where the inheritance hierarchy is more than one level deep, where the class in the lowermost level inherits from two classes, that have overridden the same method from their parent in different ways.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A Real World Example==&lt;br /&gt;
A known example of multiple inheritance to represent winged animals and mammals, using classes and objects is cited here &amp;lt;ref&amp;gt; http://stackoverflow.com/questions/9359948/multiple-inheritance-whats-a-good-example&amp;lt;/ref&amp;gt;. A Winged animal and a mammal inherit primitive attributes of an animal. While a winged animal can fly; a mammal can breathe and reproduce; they both eat, move and share features generic to animals. A bat being a winged mammal possesses features common to both winged animal and mammal. It follows that class Bat inherits from classes WingedAnimal and Mammal and in turn classes WingedAnimal and Mammal inherit from class Animal.   In reference to this context, let us write our own implementation and explore the issues of multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
[[File:Class_Diagram_-_Example.jpeg|thumb|right|x350px|alt=Class Diagram for the example.]]&lt;br /&gt;
&lt;br /&gt;
==Issues of Multiple Inheritance==&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Due to inheritance, the parent class’s objects get duplicated to child class. As a result, Class Bat duplicates Animal objects through each of WingedAnimal and Mammal classes. When Bat accesses any of the Animal's attributes, the compiler would be ambiguous about determining the correct path to use since two paths are available from Animal to Bat. This problem of multiple inheritance is called diamond problem &amp;lt;ref&amp;gt;http://www.artima.com/weblogs/viewpost.jsp?thread=246488&amp;lt;/ref&amp;gt;. Compiler takes arbitrary decisions during such situations. &amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt; Objects of parent classes can be separately managed by using [http://en.wikipedia.org/wiki/Virtual_method_table virtual tables]as in C++. By explicitly specifying the virtual table pointer, the Bat class can access WingedAnimal and Mammal classes unambiguously. However virtual tables occupy significant portion of memory thereby limiting their extensive usage. &amp;lt;/li&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt; The order by which a child class inherits parent classes also matters. If Bat inherits WingedAnimal prior to Mammal, then WingedAnimal precedes Mammal and common methods are accessed in this order of precedence.  Hence if the Programmer is not aware of such precedence, debugging would be difficult. &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
Programming languages provide different solutions to handle the issues of multiple inheritance. Here we consider the techniques offered by C++, Java, Ruby, Scala and Python.    &lt;br /&gt;
===Multiple Inheritance in C++===&lt;br /&gt;
C++ supports multiple inheritance with the notion of virtual classes and virtual functions. In the example, the Animal class defines virtual function canEat() with WingedAnimal and Mammal classes virtually inheriting the features of Animal. The Bat class can now unambiguously access canEat() method of Animal class, using virtual table pointers of WingedAnimal and Mammal. The example written using C++ virtual functions is shown below.&lt;br /&gt;
&lt;br /&gt;
 class Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void virtual canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class WingedAnimal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Mammal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Bat :public WingedAnimal, public Mammal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }  &lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
  int i = 0;&lt;br /&gt;
  Animal *instances[3];&lt;br /&gt;
  WingedAnimal w;&lt;br /&gt;
  Mammal m;&lt;br /&gt;
  Bat b;&lt;br /&gt;
  instances[0] = &amp;amp;w;&lt;br /&gt;
  instances[1] = &amp;amp;m;&lt;br /&gt;
  instances[2] = &amp;amp;b;&lt;br /&gt;
  while(i&amp;lt;3)&lt;br /&gt;
    instances[i++]-&amp;gt;canEat();&lt;br /&gt;
  return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Java===&lt;br /&gt;
Java doesn’t support multiple inheritance but provides a feature called interfaces to simulate inheritance from multiple classes. Interface is an abstract class which provides a generic template by just declaring variables and methods without implementing them. [http://www.brpreiss.com/books/opus4/html/page611.html Concrete classes] that inherit interfaces are supposed to implement its methods. The example written using Java interfaces is shown below.&lt;br /&gt;
&lt;br /&gt;
 interface Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface WingedAnimal extends Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();  &lt;br /&gt;
   void canFly();  &lt;br /&gt;
 }&lt;br /&gt;
 class ImplementWingedAnimal implements WingedAnimal  &lt;br /&gt;
 {&lt;br /&gt;
    public void canEat() {    &lt;br /&gt;
    }&lt;br /&gt;
    public void canFly() {    &lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Mammal extends Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();&lt;br /&gt;
   void canGiveBirth();&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementMammal implements Mammal&lt;br /&gt;
 {&lt;br /&gt;
    public void canEat() {    &lt;br /&gt;
    }&lt;br /&gt;
    public void canGiveBirth() {    &lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Bat extends WingedAnimal, Mammal&lt;br /&gt;
 {&lt;br /&gt;
    void canEat();    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class ImplementBat implements Bat&lt;br /&gt;
 {  &lt;br /&gt;
    ImplementWingedAnimal w = new ImplementWingedAnimal();&lt;br /&gt;
    ImplementMammal m = new ImplementMammal();     &lt;br /&gt;
    public void canEat() {            &lt;br /&gt;
      m.canEat();  &lt;br /&gt;
      w.canEat();      &lt;br /&gt;
    }&lt;br /&gt;
    public void canFly() {&lt;br /&gt;
    }&lt;br /&gt;
    public void canGiveBirth() {&lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class MultipleInheritance&lt;br /&gt;
 {&lt;br /&gt;
   public static void main(String args[])&lt;br /&gt;
   {&lt;br /&gt;
     ImplementBat b = new ImplementBat ();&lt;br /&gt;
     b.canEat();       &lt;br /&gt;
   } &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Ruby===&lt;br /&gt;
Ruby doesn't support multiple inheritance but provides a similar feature using modules and mixins.  &lt;br /&gt;
====Modules====&lt;br /&gt;
A Module is an encapsulation of methods and variables, although similar in function to a Class, differs from a class in the sense that it cannot be instantiated. Modules are  more dynamic and allow us to organize the code in a more granular way.Each module can add something to the existing class. A module can be thought of an encapsulation of a  subset of the full capabilities of an object.&lt;br /&gt;
An example found in the book ,The Well-Grounded Rubyist &amp;lt;ref&amp;gt;http://www.manning.com/black2/SampleChapter4.pdf [Pg.92] &amp;lt;/ref&amp;gt;, is the stack-like module. The module encapsulates the characteristics of being like a stack,implements the operations that can be performed by a stack but is not the actual Stack class. This allows us to use this module to mix-in  stack-like behaviour into other classes that require them.&lt;br /&gt;
&lt;br /&gt;
====Mixins====&lt;br /&gt;
A Mixin is a concept that allows inclusion of special characteristics into existing classes, so that common features that need to be reused in several classes can be selectively included, thus avoiding implementation of unnecessary functions like in the case of interfaces.&lt;br /&gt;
Mixins allow us to dynamically bind special features defined in modules, into core classes without really making any changes to the objects. This improves flexibility when we want to add plugins and extensions to the existing framework.&amp;lt;ref&amp;gt;http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-frameworks/&amp;lt;/ref&amp;gt;.The source code for the same example implemented using ruby mixins is shown below.&lt;br /&gt;
&lt;br /&gt;
 module Animal&lt;br /&gt;
   def canEat     &lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module WingedAnimal&lt;br /&gt;
   include Animal    &lt;br /&gt;
   def canEat&lt;br /&gt;
   end      &lt;br /&gt;
   def canFly&lt;br /&gt;
   end      &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Mammal&lt;br /&gt;
   include Animal&lt;br /&gt;
   def canEat&lt;br /&gt;
   end&lt;br /&gt;
   def canGiveBirth&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Bat&lt;br /&gt;
   include WingedAnimal&lt;br /&gt;
   include Mammal&lt;br /&gt;
   include Animal  &lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 Bat.new().canEat()&lt;br /&gt;
&lt;br /&gt;
===Multiple inheritance in other languages===&lt;br /&gt;
[http://www.scala-lang.org/ Scala] offers [http://www.scala-lang.org/node/126 traits] that are similar to Java interfaces to support multiple inheritance. Traits define a template with methods and variables which any class can make use of. The difference between interfaces and traits is that traits can implement methods. A Scala class can inherit from any number of traits together with a class in a way to support multiple inheritance. Traits can be mixed in to a class even without inheriting it but just by including it at the time of creating instances similar to extend feature in ruby. &amp;lt;ref&amp;gt;http://joelabrahamsson.com/entry/learning-scala-traits&amp;lt;/ref&amp;gt;. Trait can also extend a class and methods to be overridden are usually prefixed by the keyword override. The language enforces method names to be different across inherited  traits to avoid the issues of multiple inheritance. [http://www.python.org/ Python] supports a limited version of multiple inheritance by extending multiple classes in depth-first left-right order &amp;lt;ref&amp;gt;http://docs.python.org/tutorial/classes.html&amp;lt;/ref&amp;gt;. If a python class D inherits classes A, B, C and if an instance variable or method is not available in the current class, it checks in A and its base classes, then moves to B and its base classes and eventually to C thus moving from left to right. In this way, python avoids the diamond problem by making the search order linear, by which the derived class accesses attributes of its parent classes. In addition python also defines mixins to access reusable features within a class.&lt;br /&gt;
&lt;br /&gt;
==Advantages of mixins==&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Firstly, when a module gets mixed into different classes, each class acquires the same  reference  to the module and hence even dynamic changes to the module gets reflected in the behavior of the classes &amp;lt;ref&amp;gt;http://www.linuxtopia.org/online_books/programming_books/ruby_tutorial/Ruby_Modules_Mixins.html&amp;lt;/ref&amp;gt;. In this way, the module’s code is not just copied to individual classes as in static languages instead its reference is provided. &amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt; Secondly, the mixed in state and behavior can be accessed by the instance of target class just like accessing its own features. &amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt; Thirdly, a class can include any number of modules in a way it can simulate multiple inheritance without having the related issues. &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Questions to Ponder==&lt;br /&gt;
===Do mixins solve all of the problems of multiple inheritance?===&lt;br /&gt;
Undoubtedly mixins solve some of the problems of multiple inheritance. There is no ambiguity in resolving the request to a common method implemented by multiple modules. In that case, the lastly included module is responsible for handling accesses to common methods. Though the order of precedence of included modules matters, it looks clearer than C++.  However languages like Scala and Eiffel implement multiple inheritance far more safely  by explicitly forcing the user to have different method names across inherited classes.&lt;br /&gt;
&lt;br /&gt;
===Are mixins a clear advantage over interfaces?===&lt;br /&gt;
Mixins are more advantageous than interfaces because of their features. Interfaces have some limitations. As seen from the example, the derived class Bat uses the instances of WingedAnimal and Mammal classes (has-a relationship) to access respective attributes. Interface is nice in the sense it provides a generic template that could be implemented differently by different classes.  However the target class has to necessarily implement every method of the interface. This would pose problems to a Class which is interested in implementing specific features of the interface. On the contrary, Mixins eliminate this inconvenience by mixing in the state and behavior of the modules in the target class and allows it to use them as it needs. Hence the target class can just use module’s features selectively or completely instead of implementing by itself. The size of multiple inheritance code using mixins is smaller than interfaces and C++ variants and this is obvious from the example.&lt;br /&gt;
&lt;br /&gt;
===Do mixins have any disadvantages not shared by multiple inheritance or interfaces?===&lt;br /&gt;
Inspite of making the lives of programmers better with effective code reuse and avoiding unnecessary implementation of methods that cause clutter in the code, the concept of Mixins can easily be abused resulting in namespace pollution. This is caused when a class includes multiple modules each of which contains several hundred method implementations. Having hundreds of methods mixed into a single class, makes it difficult to identify the source modules of the methods, thereby causing confusion while implementing these methods, whereas in the case of multiple inheritance we can easily identify the class to which the method belongs.&lt;br /&gt;
Another disadvantage that follows from the namespace collision is the case where we have multiple methods with the same name in different modules.In such a case whenever a call to that method is made, the method which belongs to the module that was included last will be called. In the above example, the method call canEat() invokes Animal's canEat() method since Animal was the last included module. The problem with this is that, this takes place silently without any warning. This may result in accidentally inheriting wrong methods.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
In this article, we have discussed about multiple inheritance and means by which it is implemented in different languages. Though multiple inheritance is a useful feature,&lt;br /&gt;
many languages do not support them due to its associated issues. Given its advantages, mixin in ruby seems to be a viable option to achieve multiple inheritance. However mixins too have limitations which should be taken care of during implementation.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
==See Also==&lt;br /&gt;
1. Lucas Carlson and Leonard Richardson. 2006. [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=1212915&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 Ruby Cookbook] (Cookbooks (O'Reilly)). O'Reilly Media, Inc..&lt;br /&gt;
&lt;br /&gt;
2. Herbert Schildt. 2000. Java 2: [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=557816&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 The Complete Reference] (4th ed.). McGraw-Hill Professional.&lt;/div&gt;</summary>
		<author><name>Snallan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65464</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65464"/>
		<updated>2012-09-21T17:25:47Z</updated>

		<summary type="html">&lt;p&gt;Snallan: /* A Real World Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''1w23:  Multiple Inheritance and Mixins&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
The purpose of this article is to give a brief introduction about multiple inheritance, consider some of its issues and discuss techniques employed by different programming languages to handle such issues. In this relation, the article also compares solutions provided by different programming languages, especially against Ruby mixins, by listing their advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
Multiple Inheritance is a technique of establishing hierarchical relationships within a set of classes, such that a single class can inherit  behaviors from multiple parent classes. Although it might seem useful to include methods from multiple classes, the major issue with multiple inheritance is the ambiguity in referencing the parent class methods,  when more than one parent classes share a method with the same name.This ambiguity is predominant in cases where the inheritance hierarchy is more than one level deep, where the class in the lowermost level inherits from two classes, that have overridden the same method from their parent in different ways.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A Real World Example==&lt;br /&gt;
A known example of multiple inheritance to represent winged animals and mammals, using classes and objects is cited here &amp;lt;ref&amp;gt; http://stackoverflow.com/questions/9359948/multiple-inheritance-whats-a-good-example&amp;lt;/ref&amp;gt;. A Winged animal and a mammal inherit primitive attributes of an animal. While a winged animal can fly; a mammal can breathe and reproduce; they both eat, move and share features generic to animals. A bat being a winged mammal possesses features common to both winged animal and mammal. It follows that class Bat inherits from classes WingedAnimal and Mammal and in turn classes WingedAnimal and Mammal inherit from class Animal.   In reference to this context, let us write our own implementation and explore the issues of multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
[[File:Class_Diagram_-_Example.jpeg|thumb|right|x400px|alt=Class Diagram for the example.]]&lt;br /&gt;
&lt;br /&gt;
==Issues of Multiple Inheritance==&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Due to inheritance, the parent class’s objects get duplicated to child class. As a result, Class Bat duplicates Animal objects through each of WingedAnimal and Mammal classes. When Bat accesses any of the Animal's attributes, the compiler would be ambiguous about determining the correct path to use since two paths are available from Animal to Bat. This problem of multiple inheritance is called diamond problem &amp;lt;ref&amp;gt;http://www.artima.com/weblogs/viewpost.jsp?thread=246488&amp;lt;/ref&amp;gt;. Compiler takes arbitrary decisions during such situations. &amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt; Objects of parent classes can be separately managed by using [http://en.wikipedia.org/wiki/Virtual_method_table virtual tables]as in C++. By explicitly specifying the virtual table pointer, the Bat class can access WingedAnimal and Mammal classes unambiguously. However virtual tables occupy significant portion of memory thereby limiting their extensive usage. &amp;lt;/li&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt; The order by which a child class inherits parent classes also matters. If Bat inherits WingedAnimal prior to Mammal, then WingedAnimal precedes Mammal and common methods are accessed in this order of precedence.  Hence if the Programmer is not aware of such precedence, debugging would be difficult. &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
Programming languages provide different solutions to handle the issues of multiple inheritance. Here we consider the techniques offered by C++, Java, Ruby, Scala and Python.    &lt;br /&gt;
===Multiple Inheritance in C++===&lt;br /&gt;
C++ supports multiple inheritance with the notion of virtual classes and virtual functions. In the example, the Animal class defines virtual function canEat() with WingedAnimal and Mammal classes virtually inheriting the features of Animal. The Bat class can now unambiguously access canEat() method of Animal class, using virtual table pointers of WingedAnimal and Mammal. The example written using C++ virtual functions is shown below.&lt;br /&gt;
&lt;br /&gt;
 class Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void virtual canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class WingedAnimal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Mammal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Bat :public WingedAnimal, public Mammal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }  &lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
  int i = 0;&lt;br /&gt;
  Animal *instances[3];&lt;br /&gt;
  WingedAnimal w;&lt;br /&gt;
  Mammal m;&lt;br /&gt;
  Bat b;&lt;br /&gt;
  instances[0] = &amp;amp;w;&lt;br /&gt;
  instances[1] = &amp;amp;m;&lt;br /&gt;
  instances[2] = &amp;amp;b;&lt;br /&gt;
  while(i&amp;lt;3)&lt;br /&gt;
    instances[i++]-&amp;gt;canEat();&lt;br /&gt;
  return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Java===&lt;br /&gt;
Java doesn’t support multiple inheritance but provides a feature called interfaces to simulate inheritance from multiple classes. Interface is an abstract class which provides a generic template by just declaring variables and methods without implementing them. [http://www.brpreiss.com/books/opus4/html/page611.html Concrete classes] that inherit interfaces are supposed to implement its methods. The example written using Java interfaces is shown below.&lt;br /&gt;
&lt;br /&gt;
 interface Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface WingedAnimal extends Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();  &lt;br /&gt;
   void canFly();  &lt;br /&gt;
 }&lt;br /&gt;
 class ImplementWingedAnimal implements WingedAnimal  &lt;br /&gt;
 {&lt;br /&gt;
    public void canEat() {    &lt;br /&gt;
    }&lt;br /&gt;
    public void canFly() {    &lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Mammal extends Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();&lt;br /&gt;
   void canGiveBirth();&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementMammal implements Mammal&lt;br /&gt;
 {&lt;br /&gt;
    public void canEat() {    &lt;br /&gt;
    }&lt;br /&gt;
    public void canGiveBirth() {    &lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Bat extends WingedAnimal, Mammal&lt;br /&gt;
 {&lt;br /&gt;
    void canEat();    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class ImplementBat implements Bat&lt;br /&gt;
 {  &lt;br /&gt;
    ImplementWingedAnimal w = new ImplementWingedAnimal();&lt;br /&gt;
    ImplementMammal m = new ImplementMammal();     &lt;br /&gt;
    public void canEat() {            &lt;br /&gt;
      m.canEat();  &lt;br /&gt;
      w.canEat();      &lt;br /&gt;
    }&lt;br /&gt;
    public void canFly() {&lt;br /&gt;
    }&lt;br /&gt;
    public void canGiveBirth() {&lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class MultipleInheritance&lt;br /&gt;
 {&lt;br /&gt;
   public static void main(String args[])&lt;br /&gt;
   {&lt;br /&gt;
     ImplementBat b = new ImplementBat ();&lt;br /&gt;
     b.canEat();       &lt;br /&gt;
   } &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Ruby===&lt;br /&gt;
Ruby doesn't support multiple inheritance but provides a similar feature using modules and mixins.  &lt;br /&gt;
====Modules====&lt;br /&gt;
A Module is an encapsulation of methods and variables, although similar in function to a Class, differs from a class in the sense that it cannot be instantiated. Modules are  more dynamic and allow us to organize the code in a more granular way.Each module can add something to the existing class. A module can be thought of an encapsulation of a  subset of the full capabilities of an object.&lt;br /&gt;
An example found in the book ,The Well-Grounded Rubyist &amp;lt;ref&amp;gt;http://www.manning.com/black2/SampleChapter4.pdf [Pg.92] &amp;lt;/ref&amp;gt;, is the stack-like module. The module encapsulates the characteristics of being like a stack,implements the operations that can be performed by a stack but is not the actual Stack class. This allows us to use this module to mix-in  stack-like behaviour into other classes that require them.&lt;br /&gt;
&lt;br /&gt;
====Mixins====&lt;br /&gt;
A Mixin is a concept that allows inclusion of special characteristics into existing classes, so that common features that need to be reused in several classes can be selectively included, thus avoiding implementation of unnecessary functions like in the case of interfaces.&lt;br /&gt;
Mixins allow us to dynamically bind special features defined in modules, into core classes without really making any changes to the objects. This improves flexibility when we want to add plugins and extensions to the existing framework.&amp;lt;ref&amp;gt;http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-frameworks/&amp;lt;/ref&amp;gt;.The source code for the same example implemented using ruby mixins is shown below.&lt;br /&gt;
&lt;br /&gt;
 module Animal&lt;br /&gt;
   def canEat     &lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module WingedAnimal&lt;br /&gt;
   include Animal    &lt;br /&gt;
   def canEat&lt;br /&gt;
   end      &lt;br /&gt;
   def canFly&lt;br /&gt;
   end      &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Mammal&lt;br /&gt;
   include Animal&lt;br /&gt;
   def canEat&lt;br /&gt;
   end&lt;br /&gt;
   def canGiveBirth&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Bat&lt;br /&gt;
   include WingedAnimal&lt;br /&gt;
   include Mammal&lt;br /&gt;
   include Animal  &lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 Bat.new().canEat()&lt;br /&gt;
&lt;br /&gt;
===Multiple inheritance in other languages===&lt;br /&gt;
[http://www.scala-lang.org/ Scala] offers [http://www.scala-lang.org/node/126 traits] that are similar to Java interfaces to support multiple inheritance. Traits define a template with methods and variables which any class can make use of. The difference between interfaces and traits is that traits can implement methods. A Scala class can inherit from any number of traits together with a class in a way to support multiple inheritance. Traits can be mixed in to a class even without inheriting it but just by including it at the time of creating instances similar to extend feature in ruby. &amp;lt;ref&amp;gt;http://joelabrahamsson.com/entry/learning-scala-traits&amp;lt;/ref&amp;gt;. Trait can also extend a class and methods to be overridden are usually prefixed by the keyword override. The language enforces method names to be different across inherited  traits to avoid the issues of multiple inheritance. [http://www.python.org/ Python] supports a limited version of multiple inheritance by extending multiple classes in depth-first left-right order &amp;lt;ref&amp;gt;http://docs.python.org/tutorial/classes.html&amp;lt;/ref&amp;gt;. If a python class D inherits classes A, B, C and if an instance variable or method is not available in the current class, it checks in A and its base classes, then moves to B and its base classes and eventually to C thus moving from left to right. In this way, python avoids the diamond problem by making the search order linear, by which the derived class accesses attributes of its parent classes. In addition python also defines mixins to access reusable features within a class.&lt;br /&gt;
&lt;br /&gt;
==Advantages of mixins==&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Firstly, when a module gets mixed into different classes, each class acquires the same  reference  to the module and hence even dynamic changes to the module gets reflected in the behavior of the classes &amp;lt;ref&amp;gt;http://www.linuxtopia.org/online_books/programming_books/ruby_tutorial/Ruby_Modules_Mixins.html&amp;lt;/ref&amp;gt;. In this way, the module’s code is not just copied to individual classes as in static languages instead its reference is provided. &amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt; Secondly, the mixed in state and behavior can be accessed by the instance of target class just like accessing its own features. &amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt; Thirdly, a class can include any number of modules in a way it can simulate multiple inheritance without having the related issues. &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Questions to Ponder==&lt;br /&gt;
===Do mixins solve all of the problems of multiple inheritance?===&lt;br /&gt;
Undoubtedly mixins solve some of the problems of multiple inheritance. There is no ambiguity in resolving the request to a common method implemented by multiple modules. In that case, the lastly included module is responsible for handling accesses to common methods. Though the order of precedence of included modules matters, it looks clearer than C++.  However languages like Scala and Eiffel implement multiple inheritance far more safely  by explicitly forcing the user to have different method names across inherited classes.&lt;br /&gt;
&lt;br /&gt;
===Are mixins a clear advantage over interfaces?===&lt;br /&gt;
Mixins are more advantageous than interfaces because of their features. Interfaces have some limitations. As seen from the example, the derived class Bat uses the instances of WingedAnimal and Mammal classes (has-a relationship) to access respective attributes. Interface is nice in the sense it provides a generic template that could be implemented differently by different classes.  However the target class has to necessarily implement every method of the interface. This would pose problems to a Class which is interested in implementing specific features of the interface. On the contrary, Mixins eliminate this inconvenience by mixing in the state and behavior of the modules in the target class and allows it to use them as it needs. Hence the target class can just use module’s features selectively or completely instead of implementing by itself. The size of multiple inheritance code using mixins is smaller than interfaces and C++ variants and this is obvious from the example.&lt;br /&gt;
&lt;br /&gt;
===Do mixins have any disadvantages not shared by multiple inheritance or interfaces?===&lt;br /&gt;
Inspite of making the lives of programmers better with effective code reuse and avoiding unnecessary implementation of methods that cause clutter in the code, the concept of Mixins can easily be abused resulting in namespace pollution. This is caused when a class includes multiple modules each of which contains several hundred method implementations. Having hundreds of methods mixed into a single class, makes it difficult to identify the source modules of the methods, thereby causing confusion while implementing these methods, whereas in the case of multiple inheritance we can easily identify the class to which the method belongs.&lt;br /&gt;
Another disadvantage that follows from the namespace collision is the case where we have multiple methods with the same name in different modules.In such a case whenever a call to that method is made, the method which belongs to the module that was included last will be called. In the above example, the method call canEat() invokes Animal's canEat() method since Animal was the last included module. The problem with this is that, this takes place silently without any warning. This may result in accidentally inheriting wrong methods.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
In this article, we have discussed about multiple inheritance and means by which it is implemented in different languages. Though multiple inheritance is a useful feature,&lt;br /&gt;
many languages do not support them due to its associated issues. Given its advantages, mixin in ruby seems to be a viable option to achieve multiple inheritance. However mixins too have limitations which should be taken care of during implementation.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
==See Also==&lt;br /&gt;
1. Lucas Carlson and Leonard Richardson. 2006. [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=1212915&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 Ruby Cookbook] (Cookbooks (O'Reilly)). O'Reilly Media, Inc..&lt;br /&gt;
&lt;br /&gt;
2. Herbert Schildt. 2000. Java 2: [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=557816&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 The Complete Reference] (4th ed.). McGraw-Hill Professional.&lt;/div&gt;</summary>
		<author><name>Snallan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65463</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65463"/>
		<updated>2012-09-21T17:19:10Z</updated>

		<summary type="html">&lt;p&gt;Snallan: /* A Real World Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''1w23:  Multiple Inheritance and Mixins&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
The purpose of this article is to give a brief introduction about multiple inheritance, consider some of its issues and discuss techniques employed by different programming languages to handle such issues. In this relation, the article also compares solutions provided by different programming languages, especially against Ruby mixins, by listing their advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
Multiple Inheritance is a technique of establishing hierarchical relationships within a set of classes, such that a single class can inherit  behaviors from multiple parent classes. Although it might seem useful to include methods from multiple classes, the major issue with multiple inheritance is the ambiguity in referencing the parent class methods,  when more than one parent classes share a method with the same name.This ambiguity is predominant in cases where the inheritance hierarchy is more than one level deep, where the class in the lowermost level inherits from two classes, that have overridden the same method from their parent in different ways.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A Real World Example==&lt;br /&gt;
A known example of multiple inheritance to represent winged animals and mammals, using classes and objects is cited here &amp;lt;ref&amp;gt; http://stackoverflow.com/questions/9359948/multiple-inheritance-whats-a-good-example&amp;lt;/ref&amp;gt;. A Winged animal and a mammal inherit primitive attributes of an animal. While a winged animal can fly; a mammal can breathe and reproduce; they both eat, move and share features generic to animals. A bat being a winged mammal possesses features common to both winged animal and mammal. It follows that class Bat inherits from classes WingedAnimal and Mammal and in turn classes WingedAnimal and Mammal inherit from class Animal.   In reference to this context, let us write our own implementation and explore the issues of multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
[[File:Class_Diagram-Example.jpeg|thumb|right|x400px|alt=Class Diagram for the example.]]&lt;br /&gt;
&lt;br /&gt;
==Issues of Multiple Inheritance==&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Due to inheritance, the parent class’s objects get duplicated to child class. As a result, Class Bat duplicates Animal objects through each of WingedAnimal and Mammal classes. When Bat accesses any of the Animal's attributes, the compiler would be ambiguous about determining the correct path to use since two paths are available from Animal to Bat. This problem of multiple inheritance is called diamond problem &amp;lt;ref&amp;gt;http://www.artima.com/weblogs/viewpost.jsp?thread=246488&amp;lt;/ref&amp;gt;. Compiler takes arbitrary decisions during such situations. &amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt; Objects of parent classes can be separately managed by using [http://en.wikipedia.org/wiki/Virtual_method_table virtual tables]as in C++. By explicitly specifying the virtual table pointer, the Bat class can access WingedAnimal and Mammal classes unambiguously. However virtual tables occupy significant portion of memory thereby limiting their extensive usage. &amp;lt;/li&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt; The order by which a child class inherits parent classes also matters. If Bat inherits WingedAnimal prior to Mammal, then WingedAnimal precedes Mammal and common methods are accessed in this order of precedence.  Hence if the Programmer is not aware of such precedence, debugging would be difficult. &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
Programming languages provide different solutions to handle the issues of multiple inheritance. Here we consider the techniques offered by C++, Java, Ruby, Scala and Python.    &lt;br /&gt;
===Multiple Inheritance in C++===&lt;br /&gt;
C++ supports multiple inheritance with the notion of virtual classes and virtual functions. In the example, the Animal class defines virtual function canEat() with WingedAnimal and Mammal classes virtually inheriting the features of Animal. The Bat class can now unambiguously access canEat() method of Animal class, using virtual table pointers of WingedAnimal and Mammal. The example written using C++ virtual functions is shown below.&lt;br /&gt;
&lt;br /&gt;
 class Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void virtual canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class WingedAnimal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Mammal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Bat :public WingedAnimal, public Mammal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }  &lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
  int i = 0;&lt;br /&gt;
  Animal *instances[3];&lt;br /&gt;
  WingedAnimal w;&lt;br /&gt;
  Mammal m;&lt;br /&gt;
  Bat b;&lt;br /&gt;
  instances[0] = &amp;amp;w;&lt;br /&gt;
  instances[1] = &amp;amp;m;&lt;br /&gt;
  instances[2] = &amp;amp;b;&lt;br /&gt;
  while(i&amp;lt;3)&lt;br /&gt;
    instances[i++]-&amp;gt;canEat();&lt;br /&gt;
  return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Java===&lt;br /&gt;
Java doesn’t support multiple inheritance but provides a feature called interfaces to simulate inheritance from multiple classes. Interface is an abstract class which provides a generic template by just declaring variables and methods without implementing them. [http://www.brpreiss.com/books/opus4/html/page611.html Concrete classes] that inherit interfaces are supposed to implement its methods. The example written using Java interfaces is shown below.&lt;br /&gt;
&lt;br /&gt;
 interface Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface WingedAnimal extends Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();  &lt;br /&gt;
   void canFly();  &lt;br /&gt;
 }&lt;br /&gt;
 class ImplementWingedAnimal implements WingedAnimal  &lt;br /&gt;
 {&lt;br /&gt;
    public void canEat() {    &lt;br /&gt;
    }&lt;br /&gt;
    public void canFly() {    &lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Mammal extends Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();&lt;br /&gt;
   void canGiveBirth();&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementMammal implements Mammal&lt;br /&gt;
 {&lt;br /&gt;
    public void canEat() {    &lt;br /&gt;
    }&lt;br /&gt;
    public void canGiveBirth() {    &lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Bat extends WingedAnimal, Mammal&lt;br /&gt;
 {&lt;br /&gt;
    void canEat();    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class ImplementBat implements Bat&lt;br /&gt;
 {  &lt;br /&gt;
    ImplementWingedAnimal w = new ImplementWingedAnimal();&lt;br /&gt;
    ImplementMammal m = new ImplementMammal();     &lt;br /&gt;
    public void canEat() {            &lt;br /&gt;
      m.canEat();  &lt;br /&gt;
      w.canEat();      &lt;br /&gt;
    }&lt;br /&gt;
    public void canFly() {&lt;br /&gt;
    }&lt;br /&gt;
    public void canGiveBirth() {&lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class MultipleInheritance&lt;br /&gt;
 {&lt;br /&gt;
   public static void main(String args[])&lt;br /&gt;
   {&lt;br /&gt;
     ImplementBat b = new ImplementBat ();&lt;br /&gt;
     b.canEat();       &lt;br /&gt;
   } &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Ruby===&lt;br /&gt;
Ruby doesn't support multiple inheritance but provides a similar feature using modules and mixins.  &lt;br /&gt;
====Modules====&lt;br /&gt;
A Module is an encapsulation of methods and variables, although similar in function to a Class, differs from a class in the sense that it cannot be instantiated. Modules are  more dynamic and allow us to organize the code in a more granular way.Each module can add something to the existing class. A module can be thought of an encapsulation of a  subset of the full capabilities of an object.&lt;br /&gt;
An example found in the book ,The Well-Grounded Rubyist &amp;lt;ref&amp;gt;http://www.manning.com/black2/SampleChapter4.pdf [Pg.92] &amp;lt;/ref&amp;gt;, is the stack-like module. The module encapsulates the characteristics of being like a stack,implements the operations that can be performed by a stack but is not the actual Stack class. This allows us to use this module to mix-in  stack-like behaviour into other classes that require them.&lt;br /&gt;
&lt;br /&gt;
====Mixins====&lt;br /&gt;
A Mixin is a concept that allows inclusion of special characteristics into existing classes, so that common features that need to be reused in several classes can be selectively included, thus avoiding implementation of unnecessary functions like in the case of interfaces.&lt;br /&gt;
Mixins allow us to dynamically bind special features defined in modules, into core classes without really making any changes to the objects. This improves flexibility when we want to add plugins and extensions to the existing framework.&amp;lt;ref&amp;gt;http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-frameworks/&amp;lt;/ref&amp;gt;.The source code for the same example implemented using ruby mixins is shown below.&lt;br /&gt;
&lt;br /&gt;
 module Animal&lt;br /&gt;
   def canEat     &lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module WingedAnimal&lt;br /&gt;
   include Animal    &lt;br /&gt;
   def canEat&lt;br /&gt;
   end      &lt;br /&gt;
   def canFly&lt;br /&gt;
   end      &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Mammal&lt;br /&gt;
   include Animal&lt;br /&gt;
   def canEat&lt;br /&gt;
   end&lt;br /&gt;
   def canGiveBirth&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Bat&lt;br /&gt;
   include WingedAnimal&lt;br /&gt;
   include Mammal&lt;br /&gt;
   include Animal  &lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 Bat.new().canEat()&lt;br /&gt;
&lt;br /&gt;
===Multiple inheritance in other languages===&lt;br /&gt;
[http://www.scala-lang.org/ Scala] offers [http://www.scala-lang.org/node/126 traits] that are similar to Java interfaces to support multiple inheritance. Traits define a template with methods and variables which any class can make use of. The difference between interfaces and traits is that traits can implement methods. A Scala class can inherit from any number of traits together with a class in a way to support multiple inheritance. Traits can be mixed in to a class even without inheriting it but just by including it at the time of creating instances similar to extend feature in ruby. &amp;lt;ref&amp;gt;http://joelabrahamsson.com/entry/learning-scala-traits&amp;lt;/ref&amp;gt;. Trait can also extend a class and methods to be overridden are usually prefixed by the keyword override. The language enforces method names to be different across inherited  traits to avoid the issues of multiple inheritance. [http://www.python.org/ Python] supports a limited version of multiple inheritance by extending multiple classes in depth-first left-right order &amp;lt;ref&amp;gt;http://docs.python.org/tutorial/classes.html&amp;lt;/ref&amp;gt;. If a python class D inherits classes A, B, C and if an instance variable or method is not available in the current class, it checks in A and its base classes, then moves to B and its base classes and eventually to C thus moving from left to right. In this way, python avoids the diamond problem by making the search order linear, by which the derived class accesses attributes of its parent classes. In addition python also defines mixins to access reusable features within a class.&lt;br /&gt;
&lt;br /&gt;
==Advantages of mixins==&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Firstly, when a module gets mixed into different classes, each class acquires the same  reference  to the module and hence even dynamic changes to the module gets reflected in the behavior of the classes &amp;lt;ref&amp;gt;http://www.linuxtopia.org/online_books/programming_books/ruby_tutorial/Ruby_Modules_Mixins.html&amp;lt;/ref&amp;gt;. In this way, the module’s code is not just copied to individual classes as in static languages instead its reference is provided. &amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt; Secondly, the mixed in state and behavior can be accessed by the instance of target class just like accessing its own features. &amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt; Thirdly, a class can include any number of modules in a way it can simulate multiple inheritance without having the related issues. &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Questions to Ponder==&lt;br /&gt;
===Do mixins solve all of the problems of multiple inheritance?===&lt;br /&gt;
Undoubtedly mixins solve some of the problems of multiple inheritance. There is no ambiguity in resolving the request to a common method implemented by multiple modules. In that case, the lastly included module is responsible for handling accesses to common methods. Though the order of precedence of included modules matters, it looks clearer than C++.  However languages like Scala and Eiffel implement multiple inheritance far more safely  by explicitly forcing the user to have different method names across inherited classes.&lt;br /&gt;
&lt;br /&gt;
===Are mixins a clear advantage over interfaces?===&lt;br /&gt;
Mixins are more advantageous than interfaces because of their features. Interfaces have some limitations. As seen from the example, the derived class Bat uses the instances of WingedAnimal and Mammal classes (has-a relationship) to access respective attributes. Interface is nice in the sense it provides a generic template that could be implemented differently by different classes.  However the target class has to necessarily implement every method of the interface. This would pose problems to a Class which is interested in implementing specific features of the interface. On the contrary, Mixins eliminate this inconvenience by mixing in the state and behavior of the modules in the target class and allows it to use them as it needs. Hence the target class can just use module’s features selectively or completely instead of implementing by itself. The size of multiple inheritance code using mixins is smaller than interfaces and C++ variants and this is obvious from the example.&lt;br /&gt;
&lt;br /&gt;
===Do mixins have any disadvantages not shared by multiple inheritance or interfaces?===&lt;br /&gt;
Inspite of making the lives of programmers better with effective code reuse and avoiding unnecessary implementation of methods that cause clutter in the code, the concept of Mixins can easily be abused resulting in namespace pollution. This is caused when a class includes multiple modules each of which contains several hundred method implementations. Having hundreds of methods mixed into a single class, makes it difficult to identify the source modules of the methods, thereby causing confusion while implementing these methods, whereas in the case of multiple inheritance we can easily identify the class to which the method belongs.&lt;br /&gt;
Another disadvantage that follows from the namespace collision is the case where we have multiple methods with the same name in different modules.In such a case whenever a call to that method is made, the method which belongs to the module that was included last will be called. In the above example, the method call canEat() invokes Animal's canEat() method since Animal was the last included module. The problem with this is that, this takes place silently without any warning. This may result in accidentally inheriting wrong methods.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
In this article, we have discussed about multiple inheritance and means by which it is implemented in different languages. Though multiple inheritance is a useful feature,&lt;br /&gt;
many languages do not support them due to its associated issues. Given its advantages, mixin in ruby seems to be a viable option to achieve multiple inheritance. However mixins too have limitations which should be taken care of during implementation.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
==See Also==&lt;br /&gt;
1. Lucas Carlson and Leonard Richardson. 2006. [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=1212915&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 Ruby Cookbook] (Cookbooks (O'Reilly)). O'Reilly Media, Inc..&lt;br /&gt;
&lt;br /&gt;
2. Herbert Schildt. 2000. Java 2: [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=557816&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 The Complete Reference] (4th ed.). McGraw-Hill Professional.&lt;/div&gt;</summary>
		<author><name>Snallan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:Class_Diagram_-_Example.jpeg&amp;diff=65462</id>
		<title>File:Class Diagram - Example.jpeg</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:Class_Diagram_-_Example.jpeg&amp;diff=65462"/>
		<updated>2012-09-21T17:01:23Z</updated>

		<summary type="html">&lt;p&gt;Snallan: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Snallan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65461</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65461"/>
		<updated>2012-09-21T14:39:23Z</updated>

		<summary type="html">&lt;p&gt;Snallan: /* A Real World Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''1w23:  Multiple Inheritance and Mixins&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
The purpose of this article is to give a brief introduction about multiple inheritance, consider some of its issues and discuss techniques employed by different programming languages to handle such issues. In this relation, the article also compares solutions provided by different programming languages, especially against Ruby mixins, by listing their advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
Multiple Inheritance is a technique of establishing hierarchical relationships within a set of classes, such that a single class can inherit  behaviors from multiple parent classes. Although it might seem useful to include methods from multiple classes, the major issue with multiple inheritance is the ambiguity in referencing the parent class methods,  when more than one parent classes share a method with the same name.This ambiguity is predominant in cases where the inheritance hierarchy is more than one level deep, where the class in the lowermost level inherits from two classes, that have overridden the same method from their parent in different ways.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A Real World Example==&lt;br /&gt;
A known example of multiple inheritance to represent winged animals and mammals, using classes and objects is cited here &amp;lt;ref&amp;gt; http://stackoverflow.com/questions/9359948/multiple-inheritance-whats-a-good-example&amp;lt;/ref&amp;gt;. A Winged animal and a mammal inherit primitive attributes of an animal. While a winged animal can fly; a mammal can breathe and reproduce; they both eat, move and share features generic to animals. A bat being a winged mammal possesses features common to both winged animal and mammal. It follows that class Bat inherits from classes WingedAnimal and Mammal and in turn classes WingedAnimal and Mammal inherit from class Animal.   In reference to this context, let us write our own implementation and explore the issues of multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
[[File:Class_Diagram.png|thumb|right|x400px|alt=Class Diagram for the example.]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;img src=&amp;quot;http://www.gliffy.com/pubdoc/3892521/M.png&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Issues of Multiple Inheritance==&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Due to inheritance, the parent class’s objects get duplicated to child class. As a result, Class Bat duplicates Animal objects through each of WingedAnimal and Mammal classes. When Bat accesses any of the Animal's attributes, the compiler would be ambiguous about determining the correct path to use since two paths are available from Animal to Bat. This problem of multiple inheritance is called diamond problem &amp;lt;ref&amp;gt;http://www.artima.com/weblogs/viewpost.jsp?thread=246488&amp;lt;/ref&amp;gt;. Compiler takes arbitrary decisions during such situations. &amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt; Objects of parent classes can be separately managed by using [http://en.wikipedia.org/wiki/Virtual_method_table virtual tables]as in C++. By explicitly specifying the virtual table pointer, the Bat class can access WingedAnimal and Mammal classes unambiguously. However virtual tables occupy significant portion of memory thereby limiting their extensive usage. &amp;lt;/li&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt; The order by which a child class inherits parent classes also matters. If Bat inherits WingedAnimal prior to Mammal, then WingedAnimal precedes Mammal and common methods are accessed in this order of precedence.  Hence if the Programmer is not aware of such precedence, debugging would be difficult. &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
Programming languages provide different solutions to handle the issues of multiple inheritance. Here we consider the techniques offered by C++, Java, Ruby, Scala and Python.    &lt;br /&gt;
===Multiple Inheritance in C++===&lt;br /&gt;
C++ supports multiple inheritance with the notion of virtual classes and virtual functions. In the example, the Animal class defines virtual function canEat() with WingedAnimal and Mammal classes virtually inheriting the features of Animal. The Bat class can now unambiguously access canEat() method of Animal class, using virtual table pointers of WingedAnimal and Mammal. The example written using C++ virtual functions is shown below.&lt;br /&gt;
&lt;br /&gt;
 class Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void virtual canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class WingedAnimal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Mammal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Bat :public WingedAnimal, public Mammal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }  &lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
  int i = 0;&lt;br /&gt;
  Animal *instances[3];&lt;br /&gt;
  WingedAnimal w;&lt;br /&gt;
  Mammal m;&lt;br /&gt;
  Bat b;&lt;br /&gt;
  instances[0] = &amp;amp;w;&lt;br /&gt;
  instances[1] = &amp;amp;m;&lt;br /&gt;
  instances[2] = &amp;amp;b;&lt;br /&gt;
  while(i&amp;lt;3)&lt;br /&gt;
    instances[i++]-&amp;gt;canEat();&lt;br /&gt;
  return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Java===&lt;br /&gt;
Java doesn’t support multiple inheritance but provides a feature called interfaces to simulate inheritance from multiple classes. Interface is an abstract class which provides a generic template by just declaring variables and methods without implementing them. [http://www.brpreiss.com/books/opus4/html/page611.html Concrete classes] that inherit interfaces are supposed to implement its methods. The example written using Java interfaces is shown below.&lt;br /&gt;
&lt;br /&gt;
 interface Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface WingedAnimal extends Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();  &lt;br /&gt;
   void canFly();  &lt;br /&gt;
 }&lt;br /&gt;
 class ImplementWingedAnimal implements WingedAnimal  &lt;br /&gt;
 {&lt;br /&gt;
    public void canEat() {    &lt;br /&gt;
    }&lt;br /&gt;
    public void canFly() {    &lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Mammal extends Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();&lt;br /&gt;
   void canGiveBirth();&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementMammal implements Mammal&lt;br /&gt;
 {&lt;br /&gt;
    public void canEat() {    &lt;br /&gt;
    }&lt;br /&gt;
    public void canGiveBirth() {    &lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Bat extends WingedAnimal, Mammal&lt;br /&gt;
 {&lt;br /&gt;
    void canEat();    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class ImplementBat implements Bat&lt;br /&gt;
 {  &lt;br /&gt;
    ImplementWingedAnimal w = new ImplementWingedAnimal();&lt;br /&gt;
    ImplementMammal m = new ImplementMammal();     &lt;br /&gt;
    public void canEat() {            &lt;br /&gt;
      m.canEat();  &lt;br /&gt;
      w.canEat();      &lt;br /&gt;
    }&lt;br /&gt;
    public void canFly() {&lt;br /&gt;
    }&lt;br /&gt;
    public void canGiveBirth() {&lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class MultipleInheritance&lt;br /&gt;
 {&lt;br /&gt;
   public static void main(String args[])&lt;br /&gt;
   {&lt;br /&gt;
     ImplementBat b = new ImplementBat ();&lt;br /&gt;
     b.canEat();       &lt;br /&gt;
   } &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Ruby===&lt;br /&gt;
Ruby doesn't support multiple inheritance but provides a similar feature using modules and mixins.  &lt;br /&gt;
====Modules====&lt;br /&gt;
A Module is an encapsulation of methods and variables, although similar in function to a Class, differs from a class in the sense that it cannot be instantiated. Modules are  more dynamic and allow us to organize the code in a more granular way.Each module can add something to the existing class. A module can be thought of an encapsulation of a  subset of the full capabilities of an object.&lt;br /&gt;
An example found in the book ,The Well-Grounded Rubyist &amp;lt;ref&amp;gt;http://www.manning.com/black2/SampleChapter4.pdf [Pg.92] &amp;lt;/ref&amp;gt;, is the stack-like module. The module encapsulates the characteristics of being like a stack,implements the operations that can be performed by a stack but is not the actual Stack class. This allows us to use this module to mix-in  stack-like behaviour into other classes that require them.&lt;br /&gt;
&lt;br /&gt;
====Mixins====&lt;br /&gt;
A Mixin is a concept that allows inclusion of special characteristics into existing classes, so that common features that need to be reused in several classes can be selectively included, thus avoiding implementation of unnecessary functions like in the case of interfaces.&lt;br /&gt;
Mixins allow us to dynamically bind special features defined in modules, into core classes without really making any changes to the objects. This improves flexibility when we want to add plugins and extensions to the existing framework.&amp;lt;ref&amp;gt;http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-frameworks/&amp;lt;/ref&amp;gt;.The source code for the same example implemented using ruby mixins is shown below.&lt;br /&gt;
&lt;br /&gt;
 module Animal&lt;br /&gt;
   def canEat     &lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module WingedAnimal&lt;br /&gt;
   include Animal    &lt;br /&gt;
   def canEat&lt;br /&gt;
   end      &lt;br /&gt;
   def canFly&lt;br /&gt;
   end      &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Mammal&lt;br /&gt;
   include Animal&lt;br /&gt;
   def canEat&lt;br /&gt;
   end&lt;br /&gt;
   def canGiveBirth&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Bat&lt;br /&gt;
   include WingedAnimal&lt;br /&gt;
   include Mammal&lt;br /&gt;
   include Animal  &lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 Bat.new().canEat()&lt;br /&gt;
&lt;br /&gt;
===Multiple inheritance in other languages===&lt;br /&gt;
[http://www.scala-lang.org/ Scala] offers [http://www.scala-lang.org/node/126 traits] that are similar to Java interfaces to support multiple inheritance. Traits define a template with methods and variables which any class can make use of. The difference between interfaces and traits is that traits can implement methods. A Scala class can inherit from any number of traits together with a class in a way to support multiple inheritance. Traits can be mixed in to a class even without inheriting it but just by including it at the time of creating instances similar to extend feature in ruby. &amp;lt;ref&amp;gt;http://joelabrahamsson.com/entry/learning-scala-traits&amp;lt;/ref&amp;gt;. Trait can also extend a class and methods to be overridden are usually prefixed by the keyword override. The language enforces method names to be different across inherited  traits to avoid the issues of multiple inheritance. [http://www.python.org/ Python] supports a limited version of multiple inheritance by extending multiple classes in depth-first left-right order &amp;lt;ref&amp;gt;http://docs.python.org/tutorial/classes.html&amp;lt;/ref&amp;gt;. If a python class D inherits classes A, B, C and if an instance variable or method is not available in the current class, it checks in A and its base classes, then moves to B and its base classes and eventually to C thus moving from left to right. In this way, python avoids the diamond problem by making the search order linear, by which the derived class accesses attributes of its parent classes. In addition python also defines mixins to access reusable features within a class.&lt;br /&gt;
&lt;br /&gt;
==Advantages of mixins==&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Firstly, when a module gets mixed into different classes, each class acquires the same  reference  to the module and hence even dynamic changes to the module gets reflected in the behavior of the classes &amp;lt;ref&amp;gt;http://www.linuxtopia.org/online_books/programming_books/ruby_tutorial/Ruby_Modules_Mixins.html&amp;lt;/ref&amp;gt;. In this way, the module’s code is not just copied to individual classes as in static languages instead its reference is provided. &amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt; Secondly, the mixed in state and behavior can be accessed by the instance of target class just like accessing its own features. &amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt; Thirdly, a class can include any number of modules in a way it can simulate multiple inheritance without having the related issues. &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Questions to Ponder==&lt;br /&gt;
===Do mixins solve all of the problems of multiple inheritance?===&lt;br /&gt;
Undoubtedly mixins solve some of the problems of multiple inheritance. There is no ambiguity in resolving the request to a common method implemented by multiple modules. In that case, the lastly included module is responsible for handling accesses to common methods. Though the order of precedence of included modules matters, it looks clearer than C++.  However languages like Scala and Eiffel implement multiple inheritance far more safely  by explicitly forcing the user to have different method names across inherited classes.&lt;br /&gt;
&lt;br /&gt;
===Are mixins a clear advantage over interfaces?===&lt;br /&gt;
Mixins are more advantageous than interfaces because of their features. Interfaces have some limitations. As seen from the example, the derived class Bat uses the instances of WingedAnimal and Mammal classes (has-a relationship) to access respective attributes. Interface is nice in the sense it provides a generic template that could be implemented differently by different classes.  However the target class has to necessarily implement every method of the interface. This would pose problems to a Class which is interested in implementing specific features of the interface. On the contrary, Mixins eliminate this inconvenience by mixing in the state and behavior of the modules in the target class and allows it to use them as it needs. Hence the target class can just use module’s features selectively or completely instead of implementing by itself. The size of multiple inheritance code using mixins is smaller than interfaces and C++ variants and this is obvious from the example.&lt;br /&gt;
&lt;br /&gt;
===Do mixins have any disadvantages not shared by multiple inheritance or interfaces?===&lt;br /&gt;
Inspite of making the lives of programmers better with effective code reuse and avoiding unnecessary implementation of methods that cause clutter in the code, the concept of Mixins can easily be abused resulting in namespace pollution. This is caused when a class includes multiple modules each of which contains several hundred method implementations. Having hundreds of methods mixed into a single class, makes it difficult to identify the source modules of the methods, thereby causing confusion while implementing these methods, whereas in the case of multiple inheritance we can easily identify the class to which the method belongs.&lt;br /&gt;
Another disadvantage that follows from the namespace collision is the case where we have multiple methods with the same name in different modules.In such a case whenever a call to that method is made, the method which belongs to the module that was included last will be called. In the above example, the method call canEat() invokes Animal's canEat() method since Animal was the last included module. The problem with this is that, this takes place silently without any warning. This may result in accidentally inheriting wrong methods.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
In this article, we have discussed about multiple inheritance and means by which it is implemented in different languages. Though multiple inheritance is a useful feature,&lt;br /&gt;
many languages do not support them due to its associated issues. Given its advantages, mixin in ruby seems to be a viable option to achieve multiple inheritance. However mixins too have limitations which should be taken care of during implementation.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
==See Also==&lt;br /&gt;
1. Lucas Carlson and Leonard Richardson. 2006. [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=1212915&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 Ruby Cookbook] (Cookbooks (O'Reilly)). O'Reilly Media, Inc..&lt;br /&gt;
&lt;br /&gt;
2. Herbert Schildt. 2000. Java 2: [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=557816&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 The Complete Reference] (4th ed.). McGraw-Hill Professional.&lt;/div&gt;</summary>
		<author><name>Snallan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65460</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65460"/>
		<updated>2012-09-21T14:32:49Z</updated>

		<summary type="html">&lt;p&gt;Snallan: /* Are mixins a clear advantage over interfaces? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''1w23:  Multiple Inheritance and Mixins&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
The purpose of this article is to give a brief introduction about multiple inheritance, consider some of its issues and discuss techniques employed by different programming languages to handle such issues. In this relation, the article also compares solutions provided by different programming languages, especially against Ruby mixins, by listing their advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
Multiple Inheritance is a technique of establishing hierarchical relationships within a set of classes, such that a single class can inherit  behaviors from multiple parent classes. Although it might seem useful to include methods from multiple classes, the major issue with multiple inheritance is the ambiguity in referencing the parent class methods,  when more than one parent classes share a method with the same name.This ambiguity is predominant in cases where the inheritance hierarchy is more than one level deep, where the class in the lowermost level inherits from two classes, that have overridden the same method from their parent in different ways.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A Real World Example==&lt;br /&gt;
A known example of multiple inheritance to represent winged animals and mammals, using classes and objects is cited here &amp;lt;ref&amp;gt; http://stackoverflow.com/questions/9359948/multiple-inheritance-whats-a-good-example&amp;lt;/ref&amp;gt;. A Winged animal and a mammal inherit primitive attributes of an animal. While a winged animal can fly; a mammal can breathe and reproduce; they both eat, move and share features generic to animals. A bat being a winged mammal possesses features common to both winged animal and mammal. It follows that class Bat inherits from classes WingedAnimal and Mammal and in turn classes WingedAnimal and Mammal inherit from class Animal.   In reference to this context, let us write our own implementation and explore the issues of multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
[[File:Class_Diagram.png|thumb|right|x400px|alt=Class Diagram for the example.]]&lt;br /&gt;
&lt;br /&gt;
==Issues of Multiple Inheritance==&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Due to inheritance, the parent class’s objects get duplicated to child class. As a result, Class Bat duplicates Animal objects through each of WingedAnimal and Mammal classes. When Bat accesses any of the Animal's attributes, the compiler would be ambiguous about determining the correct path to use since two paths are available from Animal to Bat. This problem of multiple inheritance is called diamond problem &amp;lt;ref&amp;gt;http://www.artima.com/weblogs/viewpost.jsp?thread=246488&amp;lt;/ref&amp;gt;. Compiler takes arbitrary decisions during such situations. &amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt; Objects of parent classes can be separately managed by using [http://en.wikipedia.org/wiki/Virtual_method_table virtual tables]as in C++. By explicitly specifying the virtual table pointer, the Bat class can access WingedAnimal and Mammal classes unambiguously. However virtual tables occupy significant portion of memory thereby limiting their extensive usage. &amp;lt;/li&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt; The order by which a child class inherits parent classes also matters. If Bat inherits WingedAnimal prior to Mammal, then WingedAnimal precedes Mammal and common methods are accessed in this order of precedence.  Hence if the Programmer is not aware of such precedence, debugging would be difficult. &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
Programming languages provide different solutions to handle the issues of multiple inheritance. Here we consider the techniques offered by C++, Java, Ruby, Scala and Python.    &lt;br /&gt;
===Multiple Inheritance in C++===&lt;br /&gt;
C++ supports multiple inheritance with the notion of virtual classes and virtual functions. In the example, the Animal class defines virtual function canEat() with WingedAnimal and Mammal classes virtually inheriting the features of Animal. The Bat class can now unambiguously access canEat() method of Animal class, using virtual table pointers of WingedAnimal and Mammal. The example written using C++ virtual functions is shown below.&lt;br /&gt;
&lt;br /&gt;
 class Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void virtual canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class WingedAnimal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Mammal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Bat :public WingedAnimal, public Mammal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }  &lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
  int i = 0;&lt;br /&gt;
  Animal *instances[3];&lt;br /&gt;
  WingedAnimal w;&lt;br /&gt;
  Mammal m;&lt;br /&gt;
  Bat b;&lt;br /&gt;
  instances[0] = &amp;amp;w;&lt;br /&gt;
  instances[1] = &amp;amp;m;&lt;br /&gt;
  instances[2] = &amp;amp;b;&lt;br /&gt;
  while(i&amp;lt;3)&lt;br /&gt;
    instances[i++]-&amp;gt;canEat();&lt;br /&gt;
  return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Java===&lt;br /&gt;
Java doesn’t support multiple inheritance but provides a feature called interfaces to simulate inheritance from multiple classes. Interface is an abstract class which provides a generic template by just declaring variables and methods without implementing them. [http://www.brpreiss.com/books/opus4/html/page611.html Concrete classes] that inherit interfaces are supposed to implement its methods. The example written using Java interfaces is shown below.&lt;br /&gt;
&lt;br /&gt;
 interface Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface WingedAnimal extends Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();  &lt;br /&gt;
   void canFly();  &lt;br /&gt;
 }&lt;br /&gt;
 class ImplementWingedAnimal implements WingedAnimal  &lt;br /&gt;
 {&lt;br /&gt;
    public void canEat() {    &lt;br /&gt;
    }&lt;br /&gt;
    public void canFly() {    &lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Mammal extends Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();&lt;br /&gt;
   void canGiveBirth();&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementMammal implements Mammal&lt;br /&gt;
 {&lt;br /&gt;
    public void canEat() {    &lt;br /&gt;
    }&lt;br /&gt;
    public void canGiveBirth() {    &lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Bat extends WingedAnimal, Mammal&lt;br /&gt;
 {&lt;br /&gt;
    void canEat();    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class ImplementBat implements Bat&lt;br /&gt;
 {  &lt;br /&gt;
    ImplementWingedAnimal w = new ImplementWingedAnimal();&lt;br /&gt;
    ImplementMammal m = new ImplementMammal();     &lt;br /&gt;
    public void canEat() {            &lt;br /&gt;
      m.canEat();  &lt;br /&gt;
      w.canEat();      &lt;br /&gt;
    }&lt;br /&gt;
    public void canFly() {&lt;br /&gt;
    }&lt;br /&gt;
    public void canGiveBirth() {&lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class MultipleInheritance&lt;br /&gt;
 {&lt;br /&gt;
   public static void main(String args[])&lt;br /&gt;
   {&lt;br /&gt;
     ImplementBat b = new ImplementBat ();&lt;br /&gt;
     b.canEat();       &lt;br /&gt;
   } &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Ruby===&lt;br /&gt;
Ruby doesn't support multiple inheritance but provides a similar feature using modules and mixins.  &lt;br /&gt;
====Modules====&lt;br /&gt;
A Module is an encapsulation of methods and variables, although similar in function to a Class, differs from a class in the sense that it cannot be instantiated. Modules are  more dynamic and allow us to organize the code in a more granular way.Each module can add something to the existing class. A module can be thought of an encapsulation of a  subset of the full capabilities of an object.&lt;br /&gt;
An example found in the book ,The Well-Grounded Rubyist &amp;lt;ref&amp;gt;http://www.manning.com/black2/SampleChapter4.pdf [Pg.92] &amp;lt;/ref&amp;gt;, is the stack-like module. The module encapsulates the characteristics of being like a stack,implements the operations that can be performed by a stack but is not the actual Stack class. This allows us to use this module to mix-in  stack-like behaviour into other classes that require them.&lt;br /&gt;
&lt;br /&gt;
====Mixins====&lt;br /&gt;
A Mixin is a concept that allows inclusion of special characteristics into existing classes, so that common features that need to be reused in several classes can be selectively included, thus avoiding implementation of unnecessary functions like in the case of interfaces.&lt;br /&gt;
Mixins allow us to dynamically bind special features defined in modules, into core classes without really making any changes to the objects. This improves flexibility when we want to add plugins and extensions to the existing framework.&amp;lt;ref&amp;gt;http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-frameworks/&amp;lt;/ref&amp;gt;.The source code for the same example implemented using ruby mixins is shown below.&lt;br /&gt;
&lt;br /&gt;
 module Animal&lt;br /&gt;
   def canEat     &lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module WingedAnimal&lt;br /&gt;
   include Animal    &lt;br /&gt;
   def canEat&lt;br /&gt;
   end      &lt;br /&gt;
   def canFly&lt;br /&gt;
   end      &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Mammal&lt;br /&gt;
   include Animal&lt;br /&gt;
   def canEat&lt;br /&gt;
   end&lt;br /&gt;
   def canGiveBirth&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Bat&lt;br /&gt;
   include WingedAnimal&lt;br /&gt;
   include Mammal&lt;br /&gt;
   include Animal  &lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 Bat.new().canEat()&lt;br /&gt;
&lt;br /&gt;
===Multiple inheritance in other languages===&lt;br /&gt;
[http://www.scala-lang.org/ Scala] offers [http://www.scala-lang.org/node/126 traits] that are similar to Java interfaces to support multiple inheritance. Traits define a template with methods and variables which any class can make use of. The difference between interfaces and traits is that traits can implement methods. A Scala class can inherit from any number of traits together with a class in a way to support multiple inheritance. Traits can be mixed in to a class even without inheriting it but just by including it at the time of creating instances similar to extend feature in ruby. &amp;lt;ref&amp;gt;http://joelabrahamsson.com/entry/learning-scala-traits&amp;lt;/ref&amp;gt;. Trait can also extend a class and methods to be overridden are usually prefixed by the keyword override. The language enforces method names to be different across inherited  traits to avoid the issues of multiple inheritance. [http://www.python.org/ Python] supports a limited version of multiple inheritance by extending multiple classes in depth-first left-right order &amp;lt;ref&amp;gt;http://docs.python.org/tutorial/classes.html&amp;lt;/ref&amp;gt;. If a python class D inherits classes A, B, C and if an instance variable or method is not available in the current class, it checks in A and its base classes, then moves to B and its base classes and eventually to C thus moving from left to right. In this way, python avoids the diamond problem by making the search order linear, by which the derived class accesses attributes of its parent classes. In addition python also defines mixins to access reusable features within a class.&lt;br /&gt;
&lt;br /&gt;
==Advantages of mixins==&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Firstly, when a module gets mixed into different classes, each class acquires the same  reference  to the module and hence even dynamic changes to the module gets reflected in the behavior of the classes &amp;lt;ref&amp;gt;http://www.linuxtopia.org/online_books/programming_books/ruby_tutorial/Ruby_Modules_Mixins.html&amp;lt;/ref&amp;gt;. In this way, the module’s code is not just copied to individual classes as in static languages instead its reference is provided. &amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt; Secondly, the mixed in state and behavior can be accessed by the instance of target class just like accessing its own features. &amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt; Thirdly, a class can include any number of modules in a way it can simulate multiple inheritance without having the related issues. &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Questions to Ponder==&lt;br /&gt;
===Do mixins solve all of the problems of multiple inheritance?===&lt;br /&gt;
Undoubtedly mixins solve some of the problems of multiple inheritance. There is no ambiguity in resolving the request to a common method implemented by multiple modules. In that case, the lastly included module is responsible for handling accesses to common methods. Though the order of precedence of included modules matters, it looks clearer than C++.  However languages like Scala and Eiffel implement multiple inheritance far more safely  by explicitly forcing the user to have different method names across inherited classes.&lt;br /&gt;
&lt;br /&gt;
===Are mixins a clear advantage over interfaces?===&lt;br /&gt;
Mixins are more advantageous than interfaces because of their features. Interfaces have some limitations. As seen from the example, the derived class Bat uses the instances of WingedAnimal and Mammal classes (has-a relationship) to access respective attributes. Interface is nice in the sense it provides a generic template that could be implemented differently by different classes.  However the target class has to necessarily implement every method of the interface. This would pose problems to a Class which is interested in implementing specific features of the interface. On the contrary, Mixins eliminate this inconvenience by mixing in the state and behavior of the modules in the target class and allows it to use them as it needs. Hence the target class can just use module’s features selectively or completely instead of implementing by itself. The size of multiple inheritance code using mixins is smaller than interfaces and C++ variants and this is obvious from the example.&lt;br /&gt;
&lt;br /&gt;
===Do mixins have any disadvantages not shared by multiple inheritance or interfaces?===&lt;br /&gt;
Inspite of making the lives of programmers better with effective code reuse and avoiding unnecessary implementation of methods that cause clutter in the code, the concept of Mixins can easily be abused resulting in namespace pollution. This is caused when a class includes multiple modules each of which contains several hundred method implementations. Having hundreds of methods mixed into a single class, makes it difficult to identify the source modules of the methods, thereby causing confusion while implementing these methods, whereas in the case of multiple inheritance we can easily identify the class to which the method belongs.&lt;br /&gt;
Another disadvantage that follows from the namespace collision is the case where we have multiple methods with the same name in different modules.In such a case whenever a call to that method is made, the method which belongs to the module that was included last will be called. In the above example, the method call canEat() invokes Animal's canEat() method since Animal was the last included module. The problem with this is that, this takes place silently without any warning. This may result in accidentally inheriting wrong methods.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
In this article, we have discussed about multiple inheritance and means by which it is implemented in different languages. Though multiple inheritance is a useful feature,&lt;br /&gt;
many languages do not support them due to its associated issues. Given its advantages, mixin in ruby seems to be a viable option to achieve multiple inheritance. However mixins too have limitations which should be taken care of during implementation.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
==See Also==&lt;br /&gt;
1. Lucas Carlson and Leonard Richardson. 2006. [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=1212915&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 Ruby Cookbook] (Cookbooks (O'Reilly)). O'Reilly Media, Inc..&lt;br /&gt;
&lt;br /&gt;
2. Herbert Schildt. 2000. Java 2: [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=557816&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 The Complete Reference] (4th ed.). McGraw-Hill Professional.&lt;/div&gt;</summary>
		<author><name>Snallan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65459</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65459"/>
		<updated>2012-09-21T14:32:19Z</updated>

		<summary type="html">&lt;p&gt;Snallan: /* Are mixins a clear advantage over interfaces? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''1w23:  Multiple Inheritance and Mixins&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
The purpose of this article is to give a brief introduction about multiple inheritance, consider some of its issues and discuss techniques employed by different programming languages to handle such issues. In this relation, the article also compares solutions provided by different programming languages, especially against Ruby mixins, by listing their advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
Multiple Inheritance is a technique of establishing hierarchical relationships within a set of classes, such that a single class can inherit  behaviors from multiple parent classes. Although it might seem useful to include methods from multiple classes, the major issue with multiple inheritance is the ambiguity in referencing the parent class methods,  when more than one parent classes share a method with the same name.This ambiguity is predominant in cases where the inheritance hierarchy is more than one level deep, where the class in the lowermost level inherits from two classes, that have overridden the same method from their parent in different ways.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A Real World Example==&lt;br /&gt;
A known example of multiple inheritance to represent winged animals and mammals, using classes and objects is cited here &amp;lt;ref&amp;gt; http://stackoverflow.com/questions/9359948/multiple-inheritance-whats-a-good-example&amp;lt;/ref&amp;gt;. A Winged animal and a mammal inherit primitive attributes of an animal. While a winged animal can fly; a mammal can breathe and reproduce; they both eat, move and share features generic to animals. A bat being a winged mammal possesses features common to both winged animal and mammal. It follows that class Bat inherits from classes WingedAnimal and Mammal and in turn classes WingedAnimal and Mammal inherit from class Animal.   In reference to this context, let us write our own implementation and explore the issues of multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
[[File:Class_Diagram.png|thumb|right|x400px|alt=Class Diagram for the example.]]&lt;br /&gt;
&lt;br /&gt;
==Issues of Multiple Inheritance==&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Due to inheritance, the parent class’s objects get duplicated to child class. As a result, Class Bat duplicates Animal objects through each of WingedAnimal and Mammal classes. When Bat accesses any of the Animal's attributes, the compiler would be ambiguous about determining the correct path to use since two paths are available from Animal to Bat. This problem of multiple inheritance is called diamond problem &amp;lt;ref&amp;gt;http://www.artima.com/weblogs/viewpost.jsp?thread=246488&amp;lt;/ref&amp;gt;. Compiler takes arbitrary decisions during such situations. &amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt; Objects of parent classes can be separately managed by using [http://en.wikipedia.org/wiki/Virtual_method_table virtual tables]as in C++. By explicitly specifying the virtual table pointer, the Bat class can access WingedAnimal and Mammal classes unambiguously. However virtual tables occupy significant portion of memory thereby limiting their extensive usage. &amp;lt;/li&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt; The order by which a child class inherits parent classes also matters. If Bat inherits WingedAnimal prior to Mammal, then WingedAnimal precedes Mammal and common methods are accessed in this order of precedence.  Hence if the Programmer is not aware of such precedence, debugging would be difficult. &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
Programming languages provide different solutions to handle the issues of multiple inheritance. Here we consider the techniques offered by C++, Java, Ruby, Scala and Python.    &lt;br /&gt;
===Multiple Inheritance in C++===&lt;br /&gt;
C++ supports multiple inheritance with the notion of virtual classes and virtual functions. In the example, the Animal class defines virtual function canEat() with WingedAnimal and Mammal classes virtually inheriting the features of Animal. The Bat class can now unambiguously access canEat() method of Animal class, using virtual table pointers of WingedAnimal and Mammal. The example written using C++ virtual functions is shown below.&lt;br /&gt;
&lt;br /&gt;
 class Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void virtual canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class WingedAnimal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Mammal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Bat :public WingedAnimal, public Mammal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }  &lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
  int i = 0;&lt;br /&gt;
  Animal *instances[3];&lt;br /&gt;
  WingedAnimal w;&lt;br /&gt;
  Mammal m;&lt;br /&gt;
  Bat b;&lt;br /&gt;
  instances[0] = &amp;amp;w;&lt;br /&gt;
  instances[1] = &amp;amp;m;&lt;br /&gt;
  instances[2] = &amp;amp;b;&lt;br /&gt;
  while(i&amp;lt;3)&lt;br /&gt;
    instances[i++]-&amp;gt;canEat();&lt;br /&gt;
  return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Java===&lt;br /&gt;
Java doesn’t support multiple inheritance but provides a feature called interfaces to simulate inheritance from multiple classes. Interface is an abstract class which provides a generic template by just declaring variables and methods without implementing them. [http://www.brpreiss.com/books/opus4/html/page611.html Concrete classes] that inherit interfaces are supposed to implement its methods. The example written using Java interfaces is shown below.&lt;br /&gt;
&lt;br /&gt;
 interface Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface WingedAnimal extends Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();  &lt;br /&gt;
   void canFly();  &lt;br /&gt;
 }&lt;br /&gt;
 class ImplementWingedAnimal implements WingedAnimal  &lt;br /&gt;
 {&lt;br /&gt;
    public void canEat() {    &lt;br /&gt;
    }&lt;br /&gt;
    public void canFly() {    &lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Mammal extends Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();&lt;br /&gt;
   void canGiveBirth();&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementMammal implements Mammal&lt;br /&gt;
 {&lt;br /&gt;
    public void canEat() {    &lt;br /&gt;
    }&lt;br /&gt;
    public void canGiveBirth() {    &lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Bat extends WingedAnimal, Mammal&lt;br /&gt;
 {&lt;br /&gt;
    void canEat();    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class ImplementBat implements Bat&lt;br /&gt;
 {  &lt;br /&gt;
    ImplementWingedAnimal w = new ImplementWingedAnimal();&lt;br /&gt;
    ImplementMammal m = new ImplementMammal();     &lt;br /&gt;
    public void canEat() {            &lt;br /&gt;
      m.canEat();  &lt;br /&gt;
      w.canEat();      &lt;br /&gt;
    }&lt;br /&gt;
    public void canFly() {&lt;br /&gt;
    }&lt;br /&gt;
    public void canGiveBirth() {&lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class MultipleInheritance&lt;br /&gt;
 {&lt;br /&gt;
   public static void main(String args[])&lt;br /&gt;
   {&lt;br /&gt;
     ImplementBat b = new ImplementBat ();&lt;br /&gt;
     b.canEat();       &lt;br /&gt;
   } &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Ruby===&lt;br /&gt;
Ruby doesn't support multiple inheritance but provides a similar feature using modules and mixins.  &lt;br /&gt;
====Modules====&lt;br /&gt;
A Module is an encapsulation of methods and variables, although similar in function to a Class, differs from a class in the sense that it cannot be instantiated. Modules are  more dynamic and allow us to organize the code in a more granular way.Each module can add something to the existing class. A module can be thought of an encapsulation of a  subset of the full capabilities of an object.&lt;br /&gt;
An example found in the book ,The Well-Grounded Rubyist &amp;lt;ref&amp;gt;http://www.manning.com/black2/SampleChapter4.pdf [Pg.92] &amp;lt;/ref&amp;gt;, is the stack-like module. The module encapsulates the characteristics of being like a stack,implements the operations that can be performed by a stack but is not the actual Stack class. This allows us to use this module to mix-in  stack-like behaviour into other classes that require them.&lt;br /&gt;
&lt;br /&gt;
====Mixins====&lt;br /&gt;
A Mixin is a concept that allows inclusion of special characteristics into existing classes, so that common features that need to be reused in several classes can be selectively included, thus avoiding implementation of unnecessary functions like in the case of interfaces.&lt;br /&gt;
Mixins allow us to dynamically bind special features defined in modules, into core classes without really making any changes to the objects. This improves flexibility when we want to add plugins and extensions to the existing framework.&amp;lt;ref&amp;gt;http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-frameworks/&amp;lt;/ref&amp;gt;.The source code for the same example implemented using ruby mixins is shown below.&lt;br /&gt;
&lt;br /&gt;
 module Animal&lt;br /&gt;
   def canEat     &lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module WingedAnimal&lt;br /&gt;
   include Animal    &lt;br /&gt;
   def canEat&lt;br /&gt;
   end      &lt;br /&gt;
   def canFly&lt;br /&gt;
   end      &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Mammal&lt;br /&gt;
   include Animal&lt;br /&gt;
   def canEat&lt;br /&gt;
   end&lt;br /&gt;
   def canGiveBirth&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Bat&lt;br /&gt;
   include WingedAnimal&lt;br /&gt;
   include Mammal&lt;br /&gt;
   include Animal  &lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 Bat.new().canEat()&lt;br /&gt;
&lt;br /&gt;
===Multiple inheritance in other languages===&lt;br /&gt;
[http://www.scala-lang.org/ Scala] offers [http://www.scala-lang.org/node/126 traits] that are similar to Java interfaces to support multiple inheritance. Traits define a template with methods and variables which any class can make use of. The difference between interfaces and traits is that traits can implement methods. A Scala class can inherit from any number of traits together with a class in a way to support multiple inheritance. Traits can be mixed in to a class even without inheriting it but just by including it at the time of creating instances similar to extend feature in ruby. &amp;lt;ref&amp;gt;http://joelabrahamsson.com/entry/learning-scala-traits&amp;lt;/ref&amp;gt;. Trait can also extend a class and methods to be overridden are usually prefixed by the keyword override. The language enforces method names to be different across inherited  traits to avoid the issues of multiple inheritance. [http://www.python.org/ Python] supports a limited version of multiple inheritance by extending multiple classes in depth-first left-right order &amp;lt;ref&amp;gt;http://docs.python.org/tutorial/classes.html&amp;lt;/ref&amp;gt;. If a python class D inherits classes A, B, C and if an instance variable or method is not available in the current class, it checks in A and its base classes, then moves to B and its base classes and eventually to C thus moving from left to right. In this way, python avoids the diamond problem by making the search order linear, by which the derived class accesses attributes of its parent classes. In addition python also defines mixins to access reusable features within a class.&lt;br /&gt;
&lt;br /&gt;
==Advantages of mixins==&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Firstly, when a module gets mixed into different classes, each class acquires the same  reference  to the module and hence even dynamic changes to the module gets reflected in the behavior of the classes &amp;lt;ref&amp;gt;http://www.linuxtopia.org/online_books/programming_books/ruby_tutorial/Ruby_Modules_Mixins.html&amp;lt;/ref&amp;gt;. In this way, the module’s code is not just copied to individual classes as in static languages instead its reference is provided. &amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt; Secondly, the mixed in state and behavior can be accessed by the instance of target class just like accessing its own features. &amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt; Thirdly, a class can include any number of modules in a way it can simulate multiple inheritance without having the related issues. &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Questions to Ponder==&lt;br /&gt;
===Do mixins solve all of the problems of multiple inheritance?===&lt;br /&gt;
Undoubtedly mixins solve some of the problems of multiple inheritance. There is no ambiguity in resolving the request to a common method implemented by multiple modules. In that case, the lastly included module is responsible for handling accesses to common methods. Though the order of precedence of included modules matters, it looks clearer than C++.  However languages like Scala and Eiffel implement multiple inheritance far more safely  by explicitly forcing the user to have different method names across inherited classes.&lt;br /&gt;
&lt;br /&gt;
===Are mixins a clear advantage over interfaces?===&lt;br /&gt;
Mixins are more advantageous than interfaces because of their features. Interfaces have some limitations. As seen from the example, the derived class Bat uses the instances of WingedAnimal and Mammal classes (has-a relationship) to access respective attributes. Interface is nice in the sense it provides a generic template that could be implemented differently by different classes.  However the target class has to necessarily implement every method of the interface. This would pose problems to a Class which is interested in implementing specific features of the interface. On the contrary, Mixins eliminate this inconvenience by mixing in the state and behavior of the modules in the target class and allows it to use them as it needs. Hence the target class can just use module’s features selectively or completely instead of implementing by itself. The size of multiple inheritance code using mixins is smaller than interfaces and C++ variants which is obvious from the example.&lt;br /&gt;
&lt;br /&gt;
===Do mixins have any disadvantages not shared by multiple inheritance or interfaces?===&lt;br /&gt;
Inspite of making the lives of programmers better with effective code reuse and avoiding unnecessary implementation of methods that cause clutter in the code, the concept of Mixins can easily be abused resulting in namespace pollution. This is caused when a class includes multiple modules each of which contains several hundred method implementations. Having hundreds of methods mixed into a single class, makes it difficult to identify the source modules of the methods, thereby causing confusion while implementing these methods, whereas in the case of multiple inheritance we can easily identify the class to which the method belongs.&lt;br /&gt;
Another disadvantage that follows from the namespace collision is the case where we have multiple methods with the same name in different modules.In such a case whenever a call to that method is made, the method which belongs to the module that was included last will be called. In the above example, the method call canEat() invokes Animal's canEat() method since Animal was the last included module. The problem with this is that, this takes place silently without any warning. This may result in accidentally inheriting wrong methods.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
In this article, we have discussed about multiple inheritance and means by which it is implemented in different languages. Though multiple inheritance is a useful feature,&lt;br /&gt;
many languages do not support them due to its associated issues. Given its advantages, mixin in ruby seems to be a viable option to achieve multiple inheritance. However mixins too have limitations which should be taken care of during implementation.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
==See Also==&lt;br /&gt;
1. Lucas Carlson and Leonard Richardson. 2006. [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=1212915&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 Ruby Cookbook] (Cookbooks (O'Reilly)). O'Reilly Media, Inc..&lt;br /&gt;
&lt;br /&gt;
2. Herbert Schildt. 2000. Java 2: [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=557816&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 The Complete Reference] (4th ed.). McGraw-Hill Professional.&lt;/div&gt;</summary>
		<author><name>Snallan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65458</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65458"/>
		<updated>2012-09-21T14:28:59Z</updated>

		<summary type="html">&lt;p&gt;Snallan: /* Advantages of mixins */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''1w23:  Multiple Inheritance and Mixins&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
The purpose of this article is to give a brief introduction about multiple inheritance, consider some of its issues and discuss techniques employed by different programming languages to handle such issues. In this relation, the article also compares solutions provided by different programming languages, especially against Ruby mixins, by listing their advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
Multiple Inheritance is a technique of establishing hierarchical relationships within a set of classes, such that a single class can inherit  behaviors from multiple parent classes. Although it might seem useful to include methods from multiple classes, the major issue with multiple inheritance is the ambiguity in referencing the parent class methods,  when more than one parent classes share a method with the same name.This ambiguity is predominant in cases where the inheritance hierarchy is more than one level deep, where the class in the lowermost level inherits from two classes, that have overridden the same method from their parent in different ways.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A Real World Example==&lt;br /&gt;
A known example of multiple inheritance to represent winged animals and mammals, using classes and objects is cited here &amp;lt;ref&amp;gt; http://stackoverflow.com/questions/9359948/multiple-inheritance-whats-a-good-example&amp;lt;/ref&amp;gt;. A Winged animal and a mammal inherit primitive attributes of an animal. While a winged animal can fly; a mammal can breathe and reproduce; they both eat, move and share features generic to animals. A bat being a winged mammal possesses features common to both winged animal and mammal. It follows that class Bat inherits from classes WingedAnimal and Mammal and in turn classes WingedAnimal and Mammal inherit from class Animal.   In reference to this context, let us write our own implementation and explore the issues of multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
[[File:Class_Diagram.png|thumb|right|x400px|alt=Class Diagram for the example.]]&lt;br /&gt;
&lt;br /&gt;
==Issues of Multiple Inheritance==&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Due to inheritance, the parent class’s objects get duplicated to child class. As a result, Class Bat duplicates Animal objects through each of WingedAnimal and Mammal classes. When Bat accesses any of the Animal's attributes, the compiler would be ambiguous about determining the correct path to use since two paths are available from Animal to Bat. This problem of multiple inheritance is called diamond problem &amp;lt;ref&amp;gt;http://www.artima.com/weblogs/viewpost.jsp?thread=246488&amp;lt;/ref&amp;gt;. Compiler takes arbitrary decisions during such situations. &amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt; Objects of parent classes can be separately managed by using [http://en.wikipedia.org/wiki/Virtual_method_table virtual tables]as in C++. By explicitly specifying the virtual table pointer, the Bat class can access WingedAnimal and Mammal classes unambiguously. However virtual tables occupy significant portion of memory thereby limiting their extensive usage. &amp;lt;/li&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt; The order by which a child class inherits parent classes also matters. If Bat inherits WingedAnimal prior to Mammal, then WingedAnimal precedes Mammal and common methods are accessed in this order of precedence.  Hence if the Programmer is not aware of such precedence, debugging would be difficult. &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
Programming languages provide different solutions to handle the issues of multiple inheritance. Here we consider the techniques offered by C++, Java, Ruby, Scala and Python.    &lt;br /&gt;
===Multiple Inheritance in C++===&lt;br /&gt;
C++ supports multiple inheritance with the notion of virtual classes and virtual functions. In the example, the Animal class defines virtual function canEat() with WingedAnimal and Mammal classes virtually inheriting the features of Animal. The Bat class can now unambiguously access canEat() method of Animal class, using virtual table pointers of WingedAnimal and Mammal. The example written using C++ virtual functions is shown below.&lt;br /&gt;
&lt;br /&gt;
 class Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void virtual canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class WingedAnimal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Mammal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Bat :public WingedAnimal, public Mammal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }  &lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
  int i = 0;&lt;br /&gt;
  Animal *instances[3];&lt;br /&gt;
  WingedAnimal w;&lt;br /&gt;
  Mammal m;&lt;br /&gt;
  Bat b;&lt;br /&gt;
  instances[0] = &amp;amp;w;&lt;br /&gt;
  instances[1] = &amp;amp;m;&lt;br /&gt;
  instances[2] = &amp;amp;b;&lt;br /&gt;
  while(i&amp;lt;3)&lt;br /&gt;
    instances[i++]-&amp;gt;canEat();&lt;br /&gt;
  return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Java===&lt;br /&gt;
Java doesn’t support multiple inheritance but provides a feature called interfaces to simulate inheritance from multiple classes. Interface is an abstract class which provides a generic template by just declaring variables and methods without implementing them. [http://www.brpreiss.com/books/opus4/html/page611.html Concrete classes] that inherit interfaces are supposed to implement its methods. The example written using Java interfaces is shown below.&lt;br /&gt;
&lt;br /&gt;
 interface Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface WingedAnimal extends Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();  &lt;br /&gt;
   void canFly();  &lt;br /&gt;
 }&lt;br /&gt;
 class ImplementWingedAnimal implements WingedAnimal  &lt;br /&gt;
 {&lt;br /&gt;
    public void canEat() {    &lt;br /&gt;
    }&lt;br /&gt;
    public void canFly() {    &lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Mammal extends Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();&lt;br /&gt;
   void canGiveBirth();&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementMammal implements Mammal&lt;br /&gt;
 {&lt;br /&gt;
    public void canEat() {    &lt;br /&gt;
    }&lt;br /&gt;
    public void canGiveBirth() {    &lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Bat extends WingedAnimal, Mammal&lt;br /&gt;
 {&lt;br /&gt;
    void canEat();    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class ImplementBat implements Bat&lt;br /&gt;
 {  &lt;br /&gt;
    ImplementWingedAnimal w = new ImplementWingedAnimal();&lt;br /&gt;
    ImplementMammal m = new ImplementMammal();     &lt;br /&gt;
    public void canEat() {            &lt;br /&gt;
      m.canEat();  &lt;br /&gt;
      w.canEat();      &lt;br /&gt;
    }&lt;br /&gt;
    public void canFly() {&lt;br /&gt;
    }&lt;br /&gt;
    public void canGiveBirth() {&lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class MultipleInheritance&lt;br /&gt;
 {&lt;br /&gt;
   public static void main(String args[])&lt;br /&gt;
   {&lt;br /&gt;
     ImplementBat b = new ImplementBat ();&lt;br /&gt;
     b.canEat();       &lt;br /&gt;
   } &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Ruby===&lt;br /&gt;
Ruby doesn't support multiple inheritance but provides a similar feature using modules and mixins.  &lt;br /&gt;
====Modules====&lt;br /&gt;
A Module is an encapsulation of methods and variables, although similar in function to a Class, differs from a class in the sense that it cannot be instantiated. Modules are  more dynamic and allow us to organize the code in a more granular way.Each module can add something to the existing class. A module can be thought of an encapsulation of a  subset of the full capabilities of an object.&lt;br /&gt;
An example found in the book ,The Well-Grounded Rubyist &amp;lt;ref&amp;gt;http://www.manning.com/black2/SampleChapter4.pdf [Pg.92] &amp;lt;/ref&amp;gt;, is the stack-like module. The module encapsulates the characteristics of being like a stack,implements the operations that can be performed by a stack but is not the actual Stack class. This allows us to use this module to mix-in  stack-like behaviour into other classes that require them.&lt;br /&gt;
&lt;br /&gt;
====Mixins====&lt;br /&gt;
A Mixin is a concept that allows inclusion of special characteristics into existing classes, so that common features that need to be reused in several classes can be selectively included, thus avoiding implementation of unnecessary functions like in the case of interfaces.&lt;br /&gt;
Mixins allow us to dynamically bind special features defined in modules, into core classes without really making any changes to the objects. This improves flexibility when we want to add plugins and extensions to the existing framework.&amp;lt;ref&amp;gt;http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-frameworks/&amp;lt;/ref&amp;gt;.The source code for the same example implemented using ruby mixins is shown below.&lt;br /&gt;
&lt;br /&gt;
 module Animal&lt;br /&gt;
   def canEat     &lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module WingedAnimal&lt;br /&gt;
   include Animal    &lt;br /&gt;
   def canEat&lt;br /&gt;
   end      &lt;br /&gt;
   def canFly&lt;br /&gt;
   end      &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Mammal&lt;br /&gt;
   include Animal&lt;br /&gt;
   def canEat&lt;br /&gt;
   end&lt;br /&gt;
   def canGiveBirth&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Bat&lt;br /&gt;
   include WingedAnimal&lt;br /&gt;
   include Mammal&lt;br /&gt;
   include Animal  &lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 Bat.new().canEat()&lt;br /&gt;
&lt;br /&gt;
===Multiple inheritance in other languages===&lt;br /&gt;
[http://www.scala-lang.org/ Scala] offers [http://www.scala-lang.org/node/126 traits] that are similar to Java interfaces to support multiple inheritance. Traits define a template with methods and variables which any class can make use of. The difference between interfaces and traits is that traits can implement methods. A Scala class can inherit from any number of traits together with a class in a way to support multiple inheritance. Traits can be mixed in to a class even without inheriting it but just by including it at the time of creating instances similar to extend feature in ruby. &amp;lt;ref&amp;gt;http://joelabrahamsson.com/entry/learning-scala-traits&amp;lt;/ref&amp;gt;. Trait can also extend a class and methods to be overridden are usually prefixed by the keyword override. The language enforces method names to be different across inherited  traits to avoid the issues of multiple inheritance. [http://www.python.org/ Python] supports a limited version of multiple inheritance by extending multiple classes in depth-first left-right order &amp;lt;ref&amp;gt;http://docs.python.org/tutorial/classes.html&amp;lt;/ref&amp;gt;. If a python class D inherits classes A, B, C and if an instance variable or method is not available in the current class, it checks in A and its base classes, then moves to B and its base classes and eventually to C thus moving from left to right. In this way, python avoids the diamond problem by making the search order linear, by which the derived class accesses attributes of its parent classes. In addition python also defines mixins to access reusable features within a class.&lt;br /&gt;
&lt;br /&gt;
==Advantages of mixins==&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Firstly, when a module gets mixed into different classes, each class acquires the same  reference  to the module and hence even dynamic changes to the module gets reflected in the behavior of the classes &amp;lt;ref&amp;gt;http://www.linuxtopia.org/online_books/programming_books/ruby_tutorial/Ruby_Modules_Mixins.html&amp;lt;/ref&amp;gt;. In this way, the module’s code is not just copied to individual classes as in static languages instead its reference is provided. &amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt; Secondly, the mixed in state and behavior can be accessed by the instance of target class just like accessing its own features. &amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt; Thirdly, a class can include any number of modules in a way it can simulate multiple inheritance without having the related issues. &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Questions to Ponder==&lt;br /&gt;
===Do mixins solve all of the problems of multiple inheritance?===&lt;br /&gt;
Undoubtedly mixins solve some of the problems of multiple inheritance. There is no ambiguity in resolving the request to a common method implemented by multiple modules. In that case, the lastly included module is responsible for handling accesses to common methods. Though the order of precedence of included modules matters, it looks clearer than C++.  However languages like Scala and Eiffel implement multiple inheritance far more safely  by explicitly forcing the user to have different method names across inherited classes.&lt;br /&gt;
&lt;br /&gt;
===Are mixins a clear advantage over interfaces?===&lt;br /&gt;
Mixins are more advantageous than interfaces because of their features. Interfaces have some limitations. As seen from programs, the derived class Allrounder uses the instances of Batsman and Bowler classes (has-a relationship) to access respective attributes. Interface is nice in the sense it provides a generic template that could be implemented differently by different classes.  However the target class has to necessarily implement every method of the interface. This would pose problems to a Class which is interested in implementing specific features of the interface. On the contrary, Mixins eliminate this inconvenience by mixing in the state and behavior of the modules in the target class and allows a it to use them as it needs. Hence the target class can just use module’s features selectively or completely instead of implementing by itself. The size of multiple inheritance code using mixins is smaller than interfaces and C++ variants.&lt;br /&gt;
&lt;br /&gt;
===Do mixins have any disadvantages not shared by multiple inheritance or interfaces?===&lt;br /&gt;
Inspite of making the lives of programmers better with effective code reuse and avoiding unnecessary implementation of methods that cause clutter in the code, the concept of Mixins can easily be abused resulting in namespace pollution. This is caused when a class includes multiple modules each of which contains several hundred method implementations. Having hundreds of methods mixed into a single class, makes it difficult to identify the source modules of the methods, thereby causing confusion while implementing these methods, whereas in the case of multiple inheritance we can easily identify the class to which the method belongs.&lt;br /&gt;
Another disadvantage that follows from the namespace collision is the case where we have multiple methods with the same name in different modules.In such a case whenever a call to that method is made, the method which belongs to the module that was included last will be called. In the above example, the method call canEat() invokes Animal's canEat() method since Animal was the last included module. The problem with this is that, this takes place silently without any warning. This may result in accidentally inheriting wrong methods.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
In this article, we have discussed about multiple inheritance and means by which it is implemented in different languages. Though multiple inheritance is a useful feature,&lt;br /&gt;
many languages do not support them due to its associated issues. Given its advantages, mixin in ruby seems to be a viable option to achieve multiple inheritance. However mixins too have limitations which should be taken care of during implementation.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
==See Also==&lt;br /&gt;
1. Lucas Carlson and Leonard Richardson. 2006. [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=1212915&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 Ruby Cookbook] (Cookbooks (O'Reilly)). O'Reilly Media, Inc..&lt;br /&gt;
&lt;br /&gt;
2. Herbert Schildt. 2000. Java 2: [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=557816&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 The Complete Reference] (4th ed.). McGraw-Hill Professional.&lt;/div&gt;</summary>
		<author><name>Snallan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65457</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65457"/>
		<updated>2012-09-21T14:28:00Z</updated>

		<summary type="html">&lt;p&gt;Snallan: /* Multiple inheritance in other languages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''1w23:  Multiple Inheritance and Mixins&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
The purpose of this article is to give a brief introduction about multiple inheritance, consider some of its issues and discuss techniques employed by different programming languages to handle such issues. In this relation, the article also compares solutions provided by different programming languages, especially against Ruby mixins, by listing their advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
Multiple Inheritance is a technique of establishing hierarchical relationships within a set of classes, such that a single class can inherit  behaviors from multiple parent classes. Although it might seem useful to include methods from multiple classes, the major issue with multiple inheritance is the ambiguity in referencing the parent class methods,  when more than one parent classes share a method with the same name.This ambiguity is predominant in cases where the inheritance hierarchy is more than one level deep, where the class in the lowermost level inherits from two classes, that have overridden the same method from their parent in different ways.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A Real World Example==&lt;br /&gt;
A known example of multiple inheritance to represent winged animals and mammals, using classes and objects is cited here &amp;lt;ref&amp;gt; http://stackoverflow.com/questions/9359948/multiple-inheritance-whats-a-good-example&amp;lt;/ref&amp;gt;. A Winged animal and a mammal inherit primitive attributes of an animal. While a winged animal can fly; a mammal can breathe and reproduce; they both eat, move and share features generic to animals. A bat being a winged mammal possesses features common to both winged animal and mammal. It follows that class Bat inherits from classes WingedAnimal and Mammal and in turn classes WingedAnimal and Mammal inherit from class Animal.   In reference to this context, let us write our own implementation and explore the issues of multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
[[File:Class_Diagram.png|thumb|right|x400px|alt=Class Diagram for the example.]]&lt;br /&gt;
&lt;br /&gt;
==Issues of Multiple Inheritance==&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Due to inheritance, the parent class’s objects get duplicated to child class. As a result, Class Bat duplicates Animal objects through each of WingedAnimal and Mammal classes. When Bat accesses any of the Animal's attributes, the compiler would be ambiguous about determining the correct path to use since two paths are available from Animal to Bat. This problem of multiple inheritance is called diamond problem &amp;lt;ref&amp;gt;http://www.artima.com/weblogs/viewpost.jsp?thread=246488&amp;lt;/ref&amp;gt;. Compiler takes arbitrary decisions during such situations. &amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt; Objects of parent classes can be separately managed by using [http://en.wikipedia.org/wiki/Virtual_method_table virtual tables]as in C++. By explicitly specifying the virtual table pointer, the Bat class can access WingedAnimal and Mammal classes unambiguously. However virtual tables occupy significant portion of memory thereby limiting their extensive usage. &amp;lt;/li&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt; The order by which a child class inherits parent classes also matters. If Bat inherits WingedAnimal prior to Mammal, then WingedAnimal precedes Mammal and common methods are accessed in this order of precedence.  Hence if the Programmer is not aware of such precedence, debugging would be difficult. &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
Programming languages provide different solutions to handle the issues of multiple inheritance. Here we consider the techniques offered by C++, Java, Ruby, Scala and Python.    &lt;br /&gt;
===Multiple Inheritance in C++===&lt;br /&gt;
C++ supports multiple inheritance with the notion of virtual classes and virtual functions. In the example, the Animal class defines virtual function canEat() with WingedAnimal and Mammal classes virtually inheriting the features of Animal. The Bat class can now unambiguously access canEat() method of Animal class, using virtual table pointers of WingedAnimal and Mammal. The example written using C++ virtual functions is shown below.&lt;br /&gt;
&lt;br /&gt;
 class Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void virtual canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class WingedAnimal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Mammal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Bat :public WingedAnimal, public Mammal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }  &lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
  int i = 0;&lt;br /&gt;
  Animal *instances[3];&lt;br /&gt;
  WingedAnimal w;&lt;br /&gt;
  Mammal m;&lt;br /&gt;
  Bat b;&lt;br /&gt;
  instances[0] = &amp;amp;w;&lt;br /&gt;
  instances[1] = &amp;amp;m;&lt;br /&gt;
  instances[2] = &amp;amp;b;&lt;br /&gt;
  while(i&amp;lt;3)&lt;br /&gt;
    instances[i++]-&amp;gt;canEat();&lt;br /&gt;
  return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Java===&lt;br /&gt;
Java doesn’t support multiple inheritance but provides a feature called interfaces to simulate inheritance from multiple classes. Interface is an abstract class which provides a generic template by just declaring variables and methods without implementing them. [http://www.brpreiss.com/books/opus4/html/page611.html Concrete classes] that inherit interfaces are supposed to implement its methods. The example written using Java interfaces is shown below.&lt;br /&gt;
&lt;br /&gt;
 interface Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface WingedAnimal extends Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();  &lt;br /&gt;
   void canFly();  &lt;br /&gt;
 }&lt;br /&gt;
 class ImplementWingedAnimal implements WingedAnimal  &lt;br /&gt;
 {&lt;br /&gt;
    public void canEat() {    &lt;br /&gt;
    }&lt;br /&gt;
    public void canFly() {    &lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Mammal extends Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();&lt;br /&gt;
   void canGiveBirth();&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementMammal implements Mammal&lt;br /&gt;
 {&lt;br /&gt;
    public void canEat() {    &lt;br /&gt;
    }&lt;br /&gt;
    public void canGiveBirth() {    &lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Bat extends WingedAnimal, Mammal&lt;br /&gt;
 {&lt;br /&gt;
    void canEat();    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class ImplementBat implements Bat&lt;br /&gt;
 {  &lt;br /&gt;
    ImplementWingedAnimal w = new ImplementWingedAnimal();&lt;br /&gt;
    ImplementMammal m = new ImplementMammal();     &lt;br /&gt;
    public void canEat() {            &lt;br /&gt;
      m.canEat();  &lt;br /&gt;
      w.canEat();      &lt;br /&gt;
    }&lt;br /&gt;
    public void canFly() {&lt;br /&gt;
    }&lt;br /&gt;
    public void canGiveBirth() {&lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class MultipleInheritance&lt;br /&gt;
 {&lt;br /&gt;
   public static void main(String args[])&lt;br /&gt;
   {&lt;br /&gt;
     ImplementBat b = new ImplementBat ();&lt;br /&gt;
     b.canEat();       &lt;br /&gt;
   } &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Ruby===&lt;br /&gt;
Ruby doesn't support multiple inheritance but provides a similar feature using modules and mixins.  &lt;br /&gt;
====Modules====&lt;br /&gt;
A Module is an encapsulation of methods and variables, although similar in function to a Class, differs from a class in the sense that it cannot be instantiated. Modules are  more dynamic and allow us to organize the code in a more granular way.Each module can add something to the existing class. A module can be thought of an encapsulation of a  subset of the full capabilities of an object.&lt;br /&gt;
An example found in the book ,The Well-Grounded Rubyist &amp;lt;ref&amp;gt;http://www.manning.com/black2/SampleChapter4.pdf [Pg.92] &amp;lt;/ref&amp;gt;, is the stack-like module. The module encapsulates the characteristics of being like a stack,implements the operations that can be performed by a stack but is not the actual Stack class. This allows us to use this module to mix-in  stack-like behaviour into other classes that require them.&lt;br /&gt;
&lt;br /&gt;
====Mixins====&lt;br /&gt;
A Mixin is a concept that allows inclusion of special characteristics into existing classes, so that common features that need to be reused in several classes can be selectively included, thus avoiding implementation of unnecessary functions like in the case of interfaces.&lt;br /&gt;
Mixins allow us to dynamically bind special features defined in modules, into core classes without really making any changes to the objects. This improves flexibility when we want to add plugins and extensions to the existing framework.&amp;lt;ref&amp;gt;http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-frameworks/&amp;lt;/ref&amp;gt;.The source code for the same example implemented using ruby mixins is shown below.&lt;br /&gt;
&lt;br /&gt;
 module Animal&lt;br /&gt;
   def canEat     &lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module WingedAnimal&lt;br /&gt;
   include Animal    &lt;br /&gt;
   def canEat&lt;br /&gt;
   end      &lt;br /&gt;
   def canFly&lt;br /&gt;
   end      &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Mammal&lt;br /&gt;
   include Animal&lt;br /&gt;
   def canEat&lt;br /&gt;
   end&lt;br /&gt;
   def canGiveBirth&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Bat&lt;br /&gt;
   include WingedAnimal&lt;br /&gt;
   include Mammal&lt;br /&gt;
   include Animal  &lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 Bat.new().canEat()&lt;br /&gt;
&lt;br /&gt;
===Multiple inheritance in other languages===&lt;br /&gt;
[http://www.scala-lang.org/ Scala] offers [http://www.scala-lang.org/node/126 traits] that are similar to Java interfaces to support multiple inheritance. Traits define a template with methods and variables which any class can make use of. The difference between interfaces and traits is that traits can implement methods. A Scala class can inherit from any number of traits together with a class in a way to support multiple inheritance. Traits can be mixed in to a class even without inheriting it but just by including it at the time of creating instances similar to extend feature in ruby. &amp;lt;ref&amp;gt;http://joelabrahamsson.com/entry/learning-scala-traits&amp;lt;/ref&amp;gt;. Trait can also extend a class and methods to be overridden are usually prefixed by the keyword override. The language enforces method names to be different across inherited  traits to avoid the issues of multiple inheritance. [http://www.python.org/ Python] supports a limited version of multiple inheritance by extending multiple classes in depth-first left-right order &amp;lt;ref&amp;gt;http://docs.python.org/tutorial/classes.html&amp;lt;/ref&amp;gt;. If a python class D inherits classes A, B, C and if an instance variable or method is not available in the current class, it checks in A and its base classes, then moves to B and its base classes and eventually to C thus moving from left to right. In this way, python avoids the diamond problem by making the search order linear, by which the derived class accesses attributes of its parent classes. In addition python also defines mixins to access reusable features within a class.&lt;br /&gt;
&lt;br /&gt;
==Advantages of mixins==&lt;br /&gt;
1. Firstly, when a module gets mixed into different classes, each class acquires the same  reference  to the module and hence even dynamic changes to the module gets reflected in the behavior of the classes &amp;lt;ref&amp;gt;http://www.linuxtopia.org/online_books/programming_books/ruby_tutorial/Ruby_Modules_Mixins.html&amp;lt;/ref&amp;gt;. In this way, the module’s code is not just copied to individual classes as in static languages instead its reference is provided. &lt;br /&gt;
&lt;br /&gt;
2. Secondly, the mixed in state and behavior can be accessed by the instance of target class just like accessing its own features. &lt;br /&gt;
&lt;br /&gt;
3. Thirdly, a class can include any number of modules in a way it can simulate multiple inheritance without having the related issues.&lt;br /&gt;
&lt;br /&gt;
==Questions to Ponder==&lt;br /&gt;
===Do mixins solve all of the problems of multiple inheritance?===&lt;br /&gt;
Undoubtedly mixins solve some of the problems of multiple inheritance. There is no ambiguity in resolving the request to a common method implemented by multiple modules. In that case, the lastly included module is responsible for handling accesses to common methods. Though the order of precedence of included modules matters, it looks clearer than C++.  However languages like Scala and Eiffel implement multiple inheritance far more safely  by explicitly forcing the user to have different method names across inherited classes.&lt;br /&gt;
&lt;br /&gt;
===Are mixins a clear advantage over interfaces?===&lt;br /&gt;
Mixins are more advantageous than interfaces because of their features. Interfaces have some limitations. As seen from programs, the derived class Allrounder uses the instances of Batsman and Bowler classes (has-a relationship) to access respective attributes. Interface is nice in the sense it provides a generic template that could be implemented differently by different classes.  However the target class has to necessarily implement every method of the interface. This would pose problems to a Class which is interested in implementing specific features of the interface. On the contrary, Mixins eliminate this inconvenience by mixing in the state and behavior of the modules in the target class and allows a it to use them as it needs. Hence the target class can just use module’s features selectively or completely instead of implementing by itself. The size of multiple inheritance code using mixins is smaller than interfaces and C++ variants.&lt;br /&gt;
&lt;br /&gt;
===Do mixins have any disadvantages not shared by multiple inheritance or interfaces?===&lt;br /&gt;
Inspite of making the lives of programmers better with effective code reuse and avoiding unnecessary implementation of methods that cause clutter in the code, the concept of Mixins can easily be abused resulting in namespace pollution. This is caused when a class includes multiple modules each of which contains several hundred method implementations. Having hundreds of methods mixed into a single class, makes it difficult to identify the source modules of the methods, thereby causing confusion while implementing these methods, whereas in the case of multiple inheritance we can easily identify the class to which the method belongs.&lt;br /&gt;
Another disadvantage that follows from the namespace collision is the case where we have multiple methods with the same name in different modules.In such a case whenever a call to that method is made, the method which belongs to the module that was included last will be called. In the above example, the method call canEat() invokes Animal's canEat() method since Animal was the last included module. The problem with this is that, this takes place silently without any warning. This may result in accidentally inheriting wrong methods.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
In this article, we have discussed about multiple inheritance and means by which it is implemented in different languages. Though multiple inheritance is a useful feature,&lt;br /&gt;
many languages do not support them due to its associated issues. Given its advantages, mixin in ruby seems to be a viable option to achieve multiple inheritance. However mixins too have limitations which should be taken care of during implementation.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
==See Also==&lt;br /&gt;
1. Lucas Carlson and Leonard Richardson. 2006. [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=1212915&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 Ruby Cookbook] (Cookbooks (O'Reilly)). O'Reilly Media, Inc..&lt;br /&gt;
&lt;br /&gt;
2. Herbert Schildt. 2000. Java 2: [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=557816&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 The Complete Reference] (4th ed.). McGraw-Hill Professional.&lt;/div&gt;</summary>
		<author><name>Snallan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65456</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65456"/>
		<updated>2012-09-21T14:26:43Z</updated>

		<summary type="html">&lt;p&gt;Snallan: /* Multiple inheritance in other languages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''1w23:  Multiple Inheritance and Mixins&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
The purpose of this article is to give a brief introduction about multiple inheritance, consider some of its issues and discuss techniques employed by different programming languages to handle such issues. In this relation, the article also compares solutions provided by different programming languages, especially against Ruby mixins, by listing their advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
Multiple Inheritance is a technique of establishing hierarchical relationships within a set of classes, such that a single class can inherit  behaviors from multiple parent classes. Although it might seem useful to include methods from multiple classes, the major issue with multiple inheritance is the ambiguity in referencing the parent class methods,  when more than one parent classes share a method with the same name.This ambiguity is predominant in cases where the inheritance hierarchy is more than one level deep, where the class in the lowermost level inherits from two classes, that have overridden the same method from their parent in different ways.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A Real World Example==&lt;br /&gt;
A known example of multiple inheritance to represent winged animals and mammals, using classes and objects is cited here &amp;lt;ref&amp;gt; http://stackoverflow.com/questions/9359948/multiple-inheritance-whats-a-good-example&amp;lt;/ref&amp;gt;. A Winged animal and a mammal inherit primitive attributes of an animal. While a winged animal can fly; a mammal can breathe and reproduce; they both eat, move and share features generic to animals. A bat being a winged mammal possesses features common to both winged animal and mammal. It follows that class Bat inherits from classes WingedAnimal and Mammal and in turn classes WingedAnimal and Mammal inherit from class Animal.   In reference to this context, let us write our own implementation and explore the issues of multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
[[File:Class_Diagram.png|thumb|right|x400px|alt=Class Diagram for the example.]]&lt;br /&gt;
&lt;br /&gt;
==Issues of Multiple Inheritance==&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Due to inheritance, the parent class’s objects get duplicated to child class. As a result, Class Bat duplicates Animal objects through each of WingedAnimal and Mammal classes. When Bat accesses any of the Animal's attributes, the compiler would be ambiguous about determining the correct path to use since two paths are available from Animal to Bat. This problem of multiple inheritance is called diamond problem &amp;lt;ref&amp;gt;http://www.artima.com/weblogs/viewpost.jsp?thread=246488&amp;lt;/ref&amp;gt;. Compiler takes arbitrary decisions during such situations. &amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt; Objects of parent classes can be separately managed by using [http://en.wikipedia.org/wiki/Virtual_method_table virtual tables]as in C++. By explicitly specifying the virtual table pointer, the Bat class can access WingedAnimal and Mammal classes unambiguously. However virtual tables occupy significant portion of memory thereby limiting their extensive usage. &amp;lt;/li&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt; The order by which a child class inherits parent classes also matters. If Bat inherits WingedAnimal prior to Mammal, then WingedAnimal precedes Mammal and common methods are accessed in this order of precedence.  Hence if the Programmer is not aware of such precedence, debugging would be difficult. &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
Programming languages provide different solutions to handle the issues of multiple inheritance. Here we consider the techniques offered by C++, Java, Ruby, Scala and Python.    &lt;br /&gt;
===Multiple Inheritance in C++===&lt;br /&gt;
C++ supports multiple inheritance with the notion of virtual classes and virtual functions. In the example, the Animal class defines virtual function canEat() with WingedAnimal and Mammal classes virtually inheriting the features of Animal. The Bat class can now unambiguously access canEat() method of Animal class, using virtual table pointers of WingedAnimal and Mammal. The example written using C++ virtual functions is shown below.&lt;br /&gt;
&lt;br /&gt;
 class Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void virtual canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class WingedAnimal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Mammal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Bat :public WingedAnimal, public Mammal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }  &lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
  int i = 0;&lt;br /&gt;
  Animal *instances[3];&lt;br /&gt;
  WingedAnimal w;&lt;br /&gt;
  Mammal m;&lt;br /&gt;
  Bat b;&lt;br /&gt;
  instances[0] = &amp;amp;w;&lt;br /&gt;
  instances[1] = &amp;amp;m;&lt;br /&gt;
  instances[2] = &amp;amp;b;&lt;br /&gt;
  while(i&amp;lt;3)&lt;br /&gt;
    instances[i++]-&amp;gt;canEat();&lt;br /&gt;
  return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Java===&lt;br /&gt;
Java doesn’t support multiple inheritance but provides a feature called interfaces to simulate inheritance from multiple classes. Interface is an abstract class which provides a generic template by just declaring variables and methods without implementing them. [http://www.brpreiss.com/books/opus4/html/page611.html Concrete classes] that inherit interfaces are supposed to implement its methods. The example written using Java interfaces is shown below.&lt;br /&gt;
&lt;br /&gt;
 interface Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface WingedAnimal extends Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();  &lt;br /&gt;
   void canFly();  &lt;br /&gt;
 }&lt;br /&gt;
 class ImplementWingedAnimal implements WingedAnimal  &lt;br /&gt;
 {&lt;br /&gt;
    public void canEat() {    &lt;br /&gt;
    }&lt;br /&gt;
    public void canFly() {    &lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Mammal extends Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();&lt;br /&gt;
   void canGiveBirth();&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementMammal implements Mammal&lt;br /&gt;
 {&lt;br /&gt;
    public void canEat() {    &lt;br /&gt;
    }&lt;br /&gt;
    public void canGiveBirth() {    &lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Bat extends WingedAnimal, Mammal&lt;br /&gt;
 {&lt;br /&gt;
    void canEat();    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class ImplementBat implements Bat&lt;br /&gt;
 {  &lt;br /&gt;
    ImplementWingedAnimal w = new ImplementWingedAnimal();&lt;br /&gt;
    ImplementMammal m = new ImplementMammal();     &lt;br /&gt;
    public void canEat() {            &lt;br /&gt;
      m.canEat();  &lt;br /&gt;
      w.canEat();      &lt;br /&gt;
    }&lt;br /&gt;
    public void canFly() {&lt;br /&gt;
    }&lt;br /&gt;
    public void canGiveBirth() {&lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class MultipleInheritance&lt;br /&gt;
 {&lt;br /&gt;
   public static void main(String args[])&lt;br /&gt;
   {&lt;br /&gt;
     ImplementBat b = new ImplementBat ();&lt;br /&gt;
     b.canEat();       &lt;br /&gt;
   } &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Ruby===&lt;br /&gt;
Ruby doesn't support multiple inheritance but provides a similar feature using modules and mixins.  &lt;br /&gt;
====Modules====&lt;br /&gt;
A Module is an encapsulation of methods and variables, although similar in function to a Class, differs from a class in the sense that it cannot be instantiated. Modules are  more dynamic and allow us to organize the code in a more granular way.Each module can add something to the existing class. A module can be thought of an encapsulation of a  subset of the full capabilities of an object.&lt;br /&gt;
An example found in the book ,The Well-Grounded Rubyist &amp;lt;ref&amp;gt;http://www.manning.com/black2/SampleChapter4.pdf [Pg.92] &amp;lt;/ref&amp;gt;, is the stack-like module. The module encapsulates the characteristics of being like a stack,implements the operations that can be performed by a stack but is not the actual Stack class. This allows us to use this module to mix-in  stack-like behaviour into other classes that require them.&lt;br /&gt;
&lt;br /&gt;
====Mixins====&lt;br /&gt;
A Mixin is a concept that allows inclusion of special characteristics into existing classes, so that common features that need to be reused in several classes can be selectively included, thus avoiding implementation of unnecessary functions like in the case of interfaces.&lt;br /&gt;
Mixins allow us to dynamically bind special features defined in modules, into core classes without really making any changes to the objects. This improves flexibility when we want to add plugins and extensions to the existing framework.&amp;lt;ref&amp;gt;http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-frameworks/&amp;lt;/ref&amp;gt;.The source code for the same example implemented using ruby mixins is shown below.&lt;br /&gt;
&lt;br /&gt;
 module Animal&lt;br /&gt;
   def canEat     &lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module WingedAnimal&lt;br /&gt;
   include Animal    &lt;br /&gt;
   def canEat&lt;br /&gt;
   end      &lt;br /&gt;
   def canFly&lt;br /&gt;
   end      &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Mammal&lt;br /&gt;
   include Animal&lt;br /&gt;
   def canEat&lt;br /&gt;
   end&lt;br /&gt;
   def canGiveBirth&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Bat&lt;br /&gt;
   include WingedAnimal&lt;br /&gt;
   include Mammal&lt;br /&gt;
   include Animal  &lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 Bat.new().canEat()&lt;br /&gt;
&lt;br /&gt;
===Multiple inheritance in other languages===&lt;br /&gt;
[http://www.scala-lang.org/ Scala] offers [http://www.scala-lang.org/node/126 traits] that are similar to Java interfaces to support multiple inheritance. Traits define a template with methods and variables which any class can make use of. The difference between interfaces and traits is that traits can implement methods. A Scala class can inherit from any number of traits together with a class in a way to support multiple inheritance. Traits can be mixed in to a class even without inheriting it but just by including it at the time of creating instances similar to extend feature in ruby. &amp;lt;ref&amp;gt;http://joelabrahamsson.com/entry/learning-scala-traits&amp;lt;/ref&amp;gt;. Trait can also extend a class and methods to be overridden are usually prefixed by the keyword override. The language enforces method names to be different across inherited  traits to avoid the issues of multiple inheritance. [http://www.python.org/ Python] supports a limited version of multiple inheritance by extending multiple classes in depth-first left-right order &amp;lt;ref&amp;gt;http://docs.python.org/tutorial/classes.html&amp;lt;/ref&amp;gt;. If a python class D inherits classes A, B, C and if an instance variable or method is not available in the current class, it checks in A and its base classes, then moves to B and its base classes and eventually to C thus moving from left to right. In this way, python avoids the diamond problem by making the search order linear by which the derived class accesses attributes of its parent classes. In addition python also defines mixins to access reusable features within a class.&lt;br /&gt;
&lt;br /&gt;
==Advantages of mixins==&lt;br /&gt;
1. Firstly, when a module gets mixed into different classes, each class acquires the same  reference  to the module and hence even dynamic changes to the module gets reflected in the behavior of the classes &amp;lt;ref&amp;gt;http://www.linuxtopia.org/online_books/programming_books/ruby_tutorial/Ruby_Modules_Mixins.html&amp;lt;/ref&amp;gt;. In this way, the module’s code is not just copied to individual classes as in static languages instead its reference is provided. &lt;br /&gt;
&lt;br /&gt;
2. Secondly, the mixed in state and behavior can be accessed by the instance of target class just like accessing its own features. &lt;br /&gt;
&lt;br /&gt;
3. Thirdly, a class can include any number of modules in a way it can simulate multiple inheritance without having the related issues.&lt;br /&gt;
&lt;br /&gt;
==Questions to Ponder==&lt;br /&gt;
===Do mixins solve all of the problems of multiple inheritance?===&lt;br /&gt;
Undoubtedly mixins solve some of the problems of multiple inheritance. There is no ambiguity in resolving the request to a common method implemented by multiple modules. In that case, the lastly included module is responsible for handling accesses to common methods. Though the order of precedence of included modules matters, it looks clearer than C++.  However languages like Scala and Eiffel implement multiple inheritance far more safely  by explicitly forcing the user to have different method names across inherited classes.&lt;br /&gt;
&lt;br /&gt;
===Are mixins a clear advantage over interfaces?===&lt;br /&gt;
Mixins are more advantageous than interfaces because of their features. Interfaces have some limitations. As seen from programs, the derived class Allrounder uses the instances of Batsman and Bowler classes (has-a relationship) to access respective attributes. Interface is nice in the sense it provides a generic template that could be implemented differently by different classes.  However the target class has to necessarily implement every method of the interface. This would pose problems to a Class which is interested in implementing specific features of the interface. On the contrary, Mixins eliminate this inconvenience by mixing in the state and behavior of the modules in the target class and allows a it to use them as it needs. Hence the target class can just use module’s features selectively or completely instead of implementing by itself. The size of multiple inheritance code using mixins is smaller than interfaces and C++ variants.&lt;br /&gt;
&lt;br /&gt;
===Do mixins have any disadvantages not shared by multiple inheritance or interfaces?===&lt;br /&gt;
Inspite of making the lives of programmers better with effective code reuse and avoiding unnecessary implementation of methods that cause clutter in the code, the concept of Mixins can easily be abused resulting in namespace pollution. This is caused when a class includes multiple modules each of which contains several hundred method implementations. Having hundreds of methods mixed into a single class, makes it difficult to identify the source modules of the methods, thereby causing confusion while implementing these methods, whereas in the case of multiple inheritance we can easily identify the class to which the method belongs.&lt;br /&gt;
Another disadvantage that follows from the namespace collision is the case where we have multiple methods with the same name in different modules.In such a case whenever a call to that method is made, the method which belongs to the module that was included last will be called. In the above example, the method call canEat() invokes Animal's canEat() method since Animal was the last included module. The problem with this is that, this takes place silently without any warning. This may result in accidentally inheriting wrong methods.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
In this article, we have discussed about multiple inheritance and means by which it is implemented in different languages. Though multiple inheritance is a useful feature,&lt;br /&gt;
many languages do not support them due to its associated issues. Given its advantages, mixin in ruby seems to be a viable option to achieve multiple inheritance. However mixins too have limitations which should be taken care of during implementation.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
==See Also==&lt;br /&gt;
1. Lucas Carlson and Leonard Richardson. 2006. [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=1212915&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 Ruby Cookbook] (Cookbooks (O'Reilly)). O'Reilly Media, Inc..&lt;br /&gt;
&lt;br /&gt;
2. Herbert Schildt. 2000. Java 2: [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=557816&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 The Complete Reference] (4th ed.). McGraw-Hill Professional.&lt;/div&gt;</summary>
		<author><name>Snallan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65455</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65455"/>
		<updated>2012-09-21T14:24:49Z</updated>

		<summary type="html">&lt;p&gt;Snallan: /* Multiple inheritance in other languages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''1w23:  Multiple Inheritance and Mixins&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
The purpose of this article is to give a brief introduction about multiple inheritance, consider some of its issues and discuss techniques employed by different programming languages to handle such issues. In this relation, the article also compares solutions provided by different programming languages, especially against Ruby mixins, by listing their advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
Multiple Inheritance is a technique of establishing hierarchical relationships within a set of classes, such that a single class can inherit  behaviors from multiple parent classes. Although it might seem useful to include methods from multiple classes, the major issue with multiple inheritance is the ambiguity in referencing the parent class methods,  when more than one parent classes share a method with the same name.This ambiguity is predominant in cases where the inheritance hierarchy is more than one level deep, where the class in the lowermost level inherits from two classes, that have overridden the same method from their parent in different ways.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A Real World Example==&lt;br /&gt;
A known example of multiple inheritance to represent winged animals and mammals, using classes and objects is cited here &amp;lt;ref&amp;gt; http://stackoverflow.com/questions/9359948/multiple-inheritance-whats-a-good-example&amp;lt;/ref&amp;gt;. A Winged animal and a mammal inherit primitive attributes of an animal. While a winged animal can fly; a mammal can breathe and reproduce; they both eat, move and share features generic to animals. A bat being a winged mammal possesses features common to both winged animal and mammal. It follows that class Bat inherits from classes WingedAnimal and Mammal and in turn classes WingedAnimal and Mammal inherit from class Animal.   In reference to this context, let us write our own implementation and explore the issues of multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
[[File:Class_Diagram.png|thumb|right|x400px|alt=Class Diagram for the example.]]&lt;br /&gt;
&lt;br /&gt;
==Issues of Multiple Inheritance==&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Due to inheritance, the parent class’s objects get duplicated to child class. As a result, Class Bat duplicates Animal objects through each of WingedAnimal and Mammal classes. When Bat accesses any of the Animal's attributes, the compiler would be ambiguous about determining the correct path to use since two paths are available from Animal to Bat. This problem of multiple inheritance is called diamond problem &amp;lt;ref&amp;gt;http://www.artima.com/weblogs/viewpost.jsp?thread=246488&amp;lt;/ref&amp;gt;. Compiler takes arbitrary decisions during such situations. &amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt; Objects of parent classes can be separately managed by using [http://en.wikipedia.org/wiki/Virtual_method_table virtual tables]as in C++. By explicitly specifying the virtual table pointer, the Bat class can access WingedAnimal and Mammal classes unambiguously. However virtual tables occupy significant portion of memory thereby limiting their extensive usage. &amp;lt;/li&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt; The order by which a child class inherits parent classes also matters. If Bat inherits WingedAnimal prior to Mammal, then WingedAnimal precedes Mammal and common methods are accessed in this order of precedence.  Hence if the Programmer is not aware of such precedence, debugging would be difficult. &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
Programming languages provide different solutions to handle the issues of multiple inheritance. Here we consider the techniques offered by C++, Java, Ruby, Scala and Python.    &lt;br /&gt;
===Multiple Inheritance in C++===&lt;br /&gt;
C++ supports multiple inheritance with the notion of virtual classes and virtual functions. In the example, the Animal class defines virtual function canEat() with WingedAnimal and Mammal classes virtually inheriting the features of Animal. The Bat class can now unambiguously access canEat() method of Animal class, using virtual table pointers of WingedAnimal and Mammal. The example written using C++ virtual functions is shown below.&lt;br /&gt;
&lt;br /&gt;
 class Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void virtual canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class WingedAnimal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Mammal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Bat :public WingedAnimal, public Mammal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }  &lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
  int i = 0;&lt;br /&gt;
  Animal *instances[3];&lt;br /&gt;
  WingedAnimal w;&lt;br /&gt;
  Mammal m;&lt;br /&gt;
  Bat b;&lt;br /&gt;
  instances[0] = &amp;amp;w;&lt;br /&gt;
  instances[1] = &amp;amp;m;&lt;br /&gt;
  instances[2] = &amp;amp;b;&lt;br /&gt;
  while(i&amp;lt;3)&lt;br /&gt;
    instances[i++]-&amp;gt;canEat();&lt;br /&gt;
  return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Java===&lt;br /&gt;
Java doesn’t support multiple inheritance but provides a feature called interfaces to simulate inheritance from multiple classes. Interface is an abstract class which provides a generic template by just declaring variables and methods without implementing them. [http://www.brpreiss.com/books/opus4/html/page611.html Concrete classes] that inherit interfaces are supposed to implement its methods. The example written using Java interfaces is shown below.&lt;br /&gt;
&lt;br /&gt;
 interface Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface WingedAnimal extends Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();  &lt;br /&gt;
   void canFly();  &lt;br /&gt;
 }&lt;br /&gt;
 class ImplementWingedAnimal implements WingedAnimal  &lt;br /&gt;
 {&lt;br /&gt;
    public void canEat() {    &lt;br /&gt;
    }&lt;br /&gt;
    public void canFly() {    &lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Mammal extends Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();&lt;br /&gt;
   void canGiveBirth();&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementMammal implements Mammal&lt;br /&gt;
 {&lt;br /&gt;
    public void canEat() {    &lt;br /&gt;
    }&lt;br /&gt;
    public void canGiveBirth() {    &lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Bat extends WingedAnimal, Mammal&lt;br /&gt;
 {&lt;br /&gt;
    void canEat();    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class ImplementBat implements Bat&lt;br /&gt;
 {  &lt;br /&gt;
    ImplementWingedAnimal w = new ImplementWingedAnimal();&lt;br /&gt;
    ImplementMammal m = new ImplementMammal();     &lt;br /&gt;
    public void canEat() {            &lt;br /&gt;
      m.canEat();  &lt;br /&gt;
      w.canEat();      &lt;br /&gt;
    }&lt;br /&gt;
    public void canFly() {&lt;br /&gt;
    }&lt;br /&gt;
    public void canGiveBirth() {&lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class MultipleInheritance&lt;br /&gt;
 {&lt;br /&gt;
   public static void main(String args[])&lt;br /&gt;
   {&lt;br /&gt;
     ImplementBat b = new ImplementBat ();&lt;br /&gt;
     b.canEat();       &lt;br /&gt;
   } &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Ruby===&lt;br /&gt;
Ruby doesn't support multiple inheritance but provides a similar feature using modules and mixins.  &lt;br /&gt;
====Modules====&lt;br /&gt;
A Module is an encapsulation of methods and variables, although similar in function to a Class, differs from a class in the sense that it cannot be instantiated. Modules are  more dynamic and allow us to organize the code in a more granular way.Each module can add something to the existing class. A module can be thought of an encapsulation of a  subset of the full capabilities of an object.&lt;br /&gt;
An example found in the book ,The Well-Grounded Rubyist &amp;lt;ref&amp;gt;http://www.manning.com/black2/SampleChapter4.pdf [Pg.92] &amp;lt;/ref&amp;gt;, is the stack-like module. The module encapsulates the characteristics of being like a stack,implements the operations that can be performed by a stack but is not the actual Stack class. This allows us to use this module to mix-in  stack-like behaviour into other classes that require them.&lt;br /&gt;
&lt;br /&gt;
====Mixins====&lt;br /&gt;
A Mixin is a concept that allows inclusion of special characteristics into existing classes, so that common features that need to be reused in several classes can be selectively included, thus avoiding implementation of unnecessary functions like in the case of interfaces.&lt;br /&gt;
Mixins allow us to dynamically bind special features defined in modules, into core classes without really making any changes to the objects. This improves flexibility when we want to add plugins and extensions to the existing framework.&amp;lt;ref&amp;gt;http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-frameworks/&amp;lt;/ref&amp;gt;.The source code for the same example implemented using ruby mixins is shown below.&lt;br /&gt;
&lt;br /&gt;
 module Animal&lt;br /&gt;
   def canEat     &lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module WingedAnimal&lt;br /&gt;
   include Animal    &lt;br /&gt;
   def canEat&lt;br /&gt;
   end      &lt;br /&gt;
   def canFly&lt;br /&gt;
   end      &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Mammal&lt;br /&gt;
   include Animal&lt;br /&gt;
   def canEat&lt;br /&gt;
   end&lt;br /&gt;
   def canGiveBirth&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Bat&lt;br /&gt;
   include WingedAnimal&lt;br /&gt;
   include Mammal&lt;br /&gt;
   include Animal  &lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 Bat.new().canEat()&lt;br /&gt;
&lt;br /&gt;
===Multiple inheritance in other languages===&lt;br /&gt;
[http://www.scala-lang.org/ Scala] offers [http://www.scala-lang.org/node/126 traits] that are similar to Java interfaces to support multiple inheritance. Traits define a template with methods and variables which any class can make use of. The difference between interfaces and traits is that traits can implement methods. A Scala class can inherit from any number of traits together with a class in way to support multiple inheritance. Traits can be mixed in to a class even without inheriting it but just by including it at the time of creating instances. &amp;lt;ref&amp;gt;http://joelabrahamsson.com/entry/learning-scala-traits&amp;lt;/ref&amp;gt;. Trait can also extend a class and methods to be overridden are usually prefixed by the keyword override. The language enforces method names to be different across inherited  traits to avoid the issues of multiple inheritance. [http://www.python.org/ Python] supports a limited version of multiple inheritance by extending multiple classes in depth-first left-right order &amp;lt;ref&amp;gt;http://docs.python.org/tutorial/classes.html&amp;lt;/ref&amp;gt;. If a python class D inherits classes A, B, C and if an instance variable or method is not available in the current class, it checks in A and its base classes, then moves to B and its base classes and eventually to C thus moving from left to right. In this way, python avoids the diamond problem by making the search order linear by which the derived class accesses attributes of its parent classes. In addition python also defines mixins to access reusable features within a class.&lt;br /&gt;
&lt;br /&gt;
==Advantages of mixins==&lt;br /&gt;
1. Firstly, when a module gets mixed into different classes, each class acquires the same  reference  to the module and hence even dynamic changes to the module gets reflected in the behavior of the classes &amp;lt;ref&amp;gt;http://www.linuxtopia.org/online_books/programming_books/ruby_tutorial/Ruby_Modules_Mixins.html&amp;lt;/ref&amp;gt;. In this way, the module’s code is not just copied to individual classes as in static languages instead its reference is provided. &lt;br /&gt;
&lt;br /&gt;
2. Secondly, the mixed in state and behavior can be accessed by the instance of target class just like accessing its own features. &lt;br /&gt;
&lt;br /&gt;
3. Thirdly, a class can include any number of modules in a way it can simulate multiple inheritance without having the related issues.&lt;br /&gt;
&lt;br /&gt;
==Questions to Ponder==&lt;br /&gt;
===Do mixins solve all of the problems of multiple inheritance?===&lt;br /&gt;
Undoubtedly mixins solve some of the problems of multiple inheritance. There is no ambiguity in resolving the request to a common method implemented by multiple modules. In that case, the lastly included module is responsible for handling accesses to common methods. Though the order of precedence of included modules matters, it looks clearer than C++.  However languages like Scala and Eiffel implement multiple inheritance far more safely  by explicitly forcing the user to have different method names across inherited classes.&lt;br /&gt;
&lt;br /&gt;
===Are mixins a clear advantage over interfaces?===&lt;br /&gt;
Mixins are more advantageous than interfaces because of their features. Interfaces have some limitations. As seen from programs, the derived class Allrounder uses the instances of Batsman and Bowler classes (has-a relationship) to access respective attributes. Interface is nice in the sense it provides a generic template that could be implemented differently by different classes.  However the target class has to necessarily implement every method of the interface. This would pose problems to a Class which is interested in implementing specific features of the interface. On the contrary, Mixins eliminate this inconvenience by mixing in the state and behavior of the modules in the target class and allows a it to use them as it needs. Hence the target class can just use module’s features selectively or completely instead of implementing by itself. The size of multiple inheritance code using mixins is smaller than interfaces and C++ variants.&lt;br /&gt;
&lt;br /&gt;
===Do mixins have any disadvantages not shared by multiple inheritance or interfaces?===&lt;br /&gt;
Inspite of making the lives of programmers better with effective code reuse and avoiding unnecessary implementation of methods that cause clutter in the code, the concept of Mixins can easily be abused resulting in namespace pollution. This is caused when a class includes multiple modules each of which contains several hundred method implementations. Having hundreds of methods mixed into a single class, makes it difficult to identify the source modules of the methods, thereby causing confusion while implementing these methods, whereas in the case of multiple inheritance we can easily identify the class to which the method belongs.&lt;br /&gt;
Another disadvantage that follows from the namespace collision is the case where we have multiple methods with the same name in different modules.In such a case whenever a call to that method is made, the method which belongs to the module that was included last will be called. In the above example, the method call canEat() invokes Animal's canEat() method since Animal was the last included module. The problem with this is that, this takes place silently without any warning. This may result in accidentally inheriting wrong methods.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
In this article, we have discussed about multiple inheritance and means by which it is implemented in different languages. Though multiple inheritance is a useful feature,&lt;br /&gt;
many languages do not support them due to its associated issues. Given its advantages, mixin in ruby seems to be a viable option to achieve multiple inheritance. However mixins too have limitations which should be taken care of during implementation.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
==See Also==&lt;br /&gt;
1. Lucas Carlson and Leonard Richardson. 2006. [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=1212915&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 Ruby Cookbook] (Cookbooks (O'Reilly)). O'Reilly Media, Inc..&lt;br /&gt;
&lt;br /&gt;
2. Herbert Schildt. 2000. Java 2: [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=557816&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 The Complete Reference] (4th ed.). McGraw-Hill Professional.&lt;/div&gt;</summary>
		<author><name>Snallan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65454</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65454"/>
		<updated>2012-09-21T14:22:37Z</updated>

		<summary type="html">&lt;p&gt;Snallan: /* Multiple Inheritance in C++ */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''1w23:  Multiple Inheritance and Mixins&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
The purpose of this article is to give a brief introduction about multiple inheritance, consider some of its issues and discuss techniques employed by different programming languages to handle such issues. In this relation, the article also compares solutions provided by different programming languages, especially against Ruby mixins, by listing their advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
Multiple Inheritance is a technique of establishing hierarchical relationships within a set of classes, such that a single class can inherit  behaviors from multiple parent classes. Although it might seem useful to include methods from multiple classes, the major issue with multiple inheritance is the ambiguity in referencing the parent class methods,  when more than one parent classes share a method with the same name.This ambiguity is predominant in cases where the inheritance hierarchy is more than one level deep, where the class in the lowermost level inherits from two classes, that have overridden the same method from their parent in different ways.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A Real World Example==&lt;br /&gt;
A known example of multiple inheritance to represent winged animals and mammals, using classes and objects is cited here &amp;lt;ref&amp;gt; http://stackoverflow.com/questions/9359948/multiple-inheritance-whats-a-good-example&amp;lt;/ref&amp;gt;. A Winged animal and a mammal inherit primitive attributes of an animal. While a winged animal can fly; a mammal can breathe and reproduce; they both eat, move and share features generic to animals. A bat being a winged mammal possesses features common to both winged animal and mammal. It follows that class Bat inherits from classes WingedAnimal and Mammal and in turn classes WingedAnimal and Mammal inherit from class Animal.   In reference to this context, let us write our own implementation and explore the issues of multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
[[File:Class_Diagram.png|thumb|right|x400px|alt=Class Diagram for the example.]]&lt;br /&gt;
&lt;br /&gt;
==Issues of Multiple Inheritance==&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Due to inheritance, the parent class’s objects get duplicated to child class. As a result, Class Bat duplicates Animal objects through each of WingedAnimal and Mammal classes. When Bat accesses any of the Animal's attributes, the compiler would be ambiguous about determining the correct path to use since two paths are available from Animal to Bat. This problem of multiple inheritance is called diamond problem &amp;lt;ref&amp;gt;http://www.artima.com/weblogs/viewpost.jsp?thread=246488&amp;lt;/ref&amp;gt;. Compiler takes arbitrary decisions during such situations. &amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt; Objects of parent classes can be separately managed by using [http://en.wikipedia.org/wiki/Virtual_method_table virtual tables]as in C++. By explicitly specifying the virtual table pointer, the Bat class can access WingedAnimal and Mammal classes unambiguously. However virtual tables occupy significant portion of memory thereby limiting their extensive usage. &amp;lt;/li&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt; The order by which a child class inherits parent classes also matters. If Bat inherits WingedAnimal prior to Mammal, then WingedAnimal precedes Mammal and common methods are accessed in this order of precedence.  Hence if the Programmer is not aware of such precedence, debugging would be difficult. &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
Programming languages provide different solutions to handle the issues of multiple inheritance. Here we consider the techniques offered by C++, Java, Ruby, Scala and Python.    &lt;br /&gt;
===Multiple Inheritance in C++===&lt;br /&gt;
C++ supports multiple inheritance with the notion of virtual classes and virtual functions. In the example, the Animal class defines virtual function canEat() with WingedAnimal and Mammal classes virtually inheriting the features of Animal. The Bat class can now unambiguously access canEat() method of Animal class, using virtual table pointers of WingedAnimal and Mammal. The example written using C++ virtual functions is shown below.&lt;br /&gt;
&lt;br /&gt;
 class Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void virtual canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class WingedAnimal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Mammal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Bat :public WingedAnimal, public Mammal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }  &lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
  int i = 0;&lt;br /&gt;
  Animal *instances[3];&lt;br /&gt;
  WingedAnimal w;&lt;br /&gt;
  Mammal m;&lt;br /&gt;
  Bat b;&lt;br /&gt;
  instances[0] = &amp;amp;w;&lt;br /&gt;
  instances[1] = &amp;amp;m;&lt;br /&gt;
  instances[2] = &amp;amp;b;&lt;br /&gt;
  while(i&amp;lt;3)&lt;br /&gt;
    instances[i++]-&amp;gt;canEat();&lt;br /&gt;
  return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Java===&lt;br /&gt;
Java doesn’t support multiple inheritance but provides a feature called interfaces to simulate inheritance from multiple classes. Interface is an abstract class which provides a generic template by just declaring variables and methods without implementing them. [http://www.brpreiss.com/books/opus4/html/page611.html Concrete classes] that inherit interfaces are supposed to implement its methods. The example written using Java interfaces is shown below.&lt;br /&gt;
&lt;br /&gt;
 interface Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface WingedAnimal extends Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();  &lt;br /&gt;
   void canFly();  &lt;br /&gt;
 }&lt;br /&gt;
 class ImplementWingedAnimal implements WingedAnimal  &lt;br /&gt;
 {&lt;br /&gt;
    public void canEat() {    &lt;br /&gt;
    }&lt;br /&gt;
    public void canFly() {    &lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Mammal extends Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();&lt;br /&gt;
   void canGiveBirth();&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementMammal implements Mammal&lt;br /&gt;
 {&lt;br /&gt;
    public void canEat() {    &lt;br /&gt;
    }&lt;br /&gt;
    public void canGiveBirth() {    &lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Bat extends WingedAnimal, Mammal&lt;br /&gt;
 {&lt;br /&gt;
    void canEat();    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class ImplementBat implements Bat&lt;br /&gt;
 {  &lt;br /&gt;
    ImplementWingedAnimal w = new ImplementWingedAnimal();&lt;br /&gt;
    ImplementMammal m = new ImplementMammal();     &lt;br /&gt;
    public void canEat() {            &lt;br /&gt;
      m.canEat();  &lt;br /&gt;
      w.canEat();      &lt;br /&gt;
    }&lt;br /&gt;
    public void canFly() {&lt;br /&gt;
    }&lt;br /&gt;
    public void canGiveBirth() {&lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class MultipleInheritance&lt;br /&gt;
 {&lt;br /&gt;
   public static void main(String args[])&lt;br /&gt;
   {&lt;br /&gt;
     ImplementBat b = new ImplementBat ();&lt;br /&gt;
     b.canEat();       &lt;br /&gt;
   } &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Ruby===&lt;br /&gt;
Ruby doesn't support multiple inheritance but provides a similar feature using modules and mixins.  &lt;br /&gt;
====Modules====&lt;br /&gt;
A Module is an encapsulation of methods and variables, although similar in function to a Class, differs from a class in the sense that it cannot be instantiated. Modules are  more dynamic and allow us to organize the code in a more granular way.Each module can add something to the existing class. A module can be thought of an encapsulation of a  subset of the full capabilities of an object.&lt;br /&gt;
An example found in the book ,The Well-Grounded Rubyist &amp;lt;ref&amp;gt;http://www.manning.com/black2/SampleChapter4.pdf [Pg.92] &amp;lt;/ref&amp;gt;, is the stack-like module. The module encapsulates the characteristics of being like a stack,implements the operations that can be performed by a stack but is not the actual Stack class. This allows us to use this module to mix-in  stack-like behaviour into other classes that require them.&lt;br /&gt;
&lt;br /&gt;
====Mixins====&lt;br /&gt;
A Mixin is a concept that allows inclusion of special characteristics into existing classes, so that common features that need to be reused in several classes can be selectively included, thus avoiding implementation of unnecessary functions like in the case of interfaces.&lt;br /&gt;
Mixins allow us to dynamically bind special features defined in modules, into core classes without really making any changes to the objects. This improves flexibility when we want to add plugins and extensions to the existing framework.&amp;lt;ref&amp;gt;http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-frameworks/&amp;lt;/ref&amp;gt;.The source code for the same example implemented using ruby mixins is shown below.&lt;br /&gt;
&lt;br /&gt;
 module Animal&lt;br /&gt;
   def canEat     &lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module WingedAnimal&lt;br /&gt;
   include Animal    &lt;br /&gt;
   def canEat&lt;br /&gt;
   end      &lt;br /&gt;
   def canFly&lt;br /&gt;
   end      &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Mammal&lt;br /&gt;
   include Animal&lt;br /&gt;
   def canEat&lt;br /&gt;
   end&lt;br /&gt;
   def canGiveBirth&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Bat&lt;br /&gt;
   include WingedAnimal&lt;br /&gt;
   include Mammal&lt;br /&gt;
   include Animal  &lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 Bat.new().canEat()&lt;br /&gt;
&lt;br /&gt;
===Multiple inheritance in other languages===&lt;br /&gt;
[http://www.scala-lang.org/ Scala] offers traits that are similar to Java interfaces to support multiple inheritance. Traits define a template with methods and variables which any class can make use of. The difference between interfaces and traits is that traits can implement methods. A Scala class can inherit from any number of traits together with a class in way to support multiple inheritance. Traits can be mixed in to a class even without inheriting it but just by including it at the time of creating instances. &amp;lt;ref&amp;gt;http://joelabrahamsson.com/entry/learning-scala-traits&amp;lt;/ref&amp;gt;. Trait can also extend a class and methods to be overridden are usually prefixed by the keyword override. The language enforces method names to be different across inherited  traits to avoid the issues of multiple inheritance. [http://www.python.org/ Python] supports a limited version of multiple inheritance by extending multiple classes in depth-first left-right order &amp;lt;ref&amp;gt;http://docs.python.org/tutorial/classes.html&amp;lt;/ref&amp;gt;. If a python class D inherits classes A, B, C and if an instance variable or method is not available in the current class, it checks in A and its base classes, then moves to B and its base classes and eventually to C thus moving from left to right. In this way, python avoids the diamond problem by making the search order linear by which the derived class accesses attributes of its parent classes. In addition python also defines mixins to access reusable features within a class.&lt;br /&gt;
&lt;br /&gt;
==Advantages of mixins==&lt;br /&gt;
1. Firstly, when a module gets mixed into different classes, each class acquires the same  reference  to the module and hence even dynamic changes to the module gets reflected in the behavior of the classes &amp;lt;ref&amp;gt;http://www.linuxtopia.org/online_books/programming_books/ruby_tutorial/Ruby_Modules_Mixins.html&amp;lt;/ref&amp;gt;. In this way, the module’s code is not just copied to individual classes as in static languages instead its reference is provided. &lt;br /&gt;
&lt;br /&gt;
2. Secondly, the mixed in state and behavior can be accessed by the instance of target class just like accessing its own features. &lt;br /&gt;
&lt;br /&gt;
3. Thirdly, a class can include any number of modules in a way it can simulate multiple inheritance without having the related issues.&lt;br /&gt;
&lt;br /&gt;
==Questions to Ponder==&lt;br /&gt;
===Do mixins solve all of the problems of multiple inheritance?===&lt;br /&gt;
Undoubtedly mixins solve some of the problems of multiple inheritance. There is no ambiguity in resolving the request to a common method implemented by multiple modules. In that case, the lastly included module is responsible for handling accesses to common methods. Though the order of precedence of included modules matters, it looks clearer than C++.  However languages like Scala and Eiffel implement multiple inheritance far more safely  by explicitly forcing the user to have different method names across inherited classes.&lt;br /&gt;
&lt;br /&gt;
===Are mixins a clear advantage over interfaces?===&lt;br /&gt;
Mixins are more advantageous than interfaces because of their features. Interfaces have some limitations. As seen from programs, the derived class Allrounder uses the instances of Batsman and Bowler classes (has-a relationship) to access respective attributes. Interface is nice in the sense it provides a generic template that could be implemented differently by different classes.  However the target class has to necessarily implement every method of the interface. This would pose problems to a Class which is interested in implementing specific features of the interface. On the contrary, Mixins eliminate this inconvenience by mixing in the state and behavior of the modules in the target class and allows a it to use them as it needs. Hence the target class can just use module’s features selectively or completely instead of implementing by itself. The size of multiple inheritance code using mixins is smaller than interfaces and C++ variants.&lt;br /&gt;
&lt;br /&gt;
===Do mixins have any disadvantages not shared by multiple inheritance or interfaces?===&lt;br /&gt;
Inspite of making the lives of programmers better with effective code reuse and avoiding unnecessary implementation of methods that cause clutter in the code, the concept of Mixins can easily be abused resulting in namespace pollution. This is caused when a class includes multiple modules each of which contains several hundred method implementations. Having hundreds of methods mixed into a single class, makes it difficult to identify the source modules of the methods, thereby causing confusion while implementing these methods, whereas in the case of multiple inheritance we can easily identify the class to which the method belongs.&lt;br /&gt;
Another disadvantage that follows from the namespace collision is the case where we have multiple methods with the same name in different modules.In such a case whenever a call to that method is made, the method which belongs to the module that was included last will be called. In the above example, the method call canEat() invokes Animal's canEat() method since Animal was the last included module. The problem with this is that, this takes place silently without any warning. This may result in accidentally inheriting wrong methods.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
In this article, we have discussed about multiple inheritance and means by which it is implemented in different languages. Though multiple inheritance is a useful feature,&lt;br /&gt;
many languages do not support them due to its associated issues. Given its advantages, mixin in ruby seems to be a viable option to achieve multiple inheritance. However mixins too have limitations which should be taken care of during implementation.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
==See Also==&lt;br /&gt;
1. Lucas Carlson and Leonard Richardson. 2006. [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=1212915&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 Ruby Cookbook] (Cookbooks (O'Reilly)). O'Reilly Media, Inc..&lt;br /&gt;
&lt;br /&gt;
2. Herbert Schildt. 2000. Java 2: [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=557816&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 The Complete Reference] (4th ed.). McGraw-Hill Professional.&lt;/div&gt;</summary>
		<author><name>Snallan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65453</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65453"/>
		<updated>2012-09-21T14:20:59Z</updated>

		<summary type="html">&lt;p&gt;Snallan: /* Issues of Multiple Inheritance */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''1w23:  Multiple Inheritance and Mixins&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
The purpose of this article is to give a brief introduction about multiple inheritance, consider some of its issues and discuss techniques employed by different programming languages to handle such issues. In this relation, the article also compares solutions provided by different programming languages, especially against Ruby mixins, by listing their advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
Multiple Inheritance is a technique of establishing hierarchical relationships within a set of classes, such that a single class can inherit  behaviors from multiple parent classes. Although it might seem useful to include methods from multiple classes, the major issue with multiple inheritance is the ambiguity in referencing the parent class methods,  when more than one parent classes share a method with the same name.This ambiguity is predominant in cases where the inheritance hierarchy is more than one level deep, where the class in the lowermost level inherits from two classes, that have overridden the same method from their parent in different ways.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A Real World Example==&lt;br /&gt;
A known example of multiple inheritance to represent winged animals and mammals, using classes and objects is cited here &amp;lt;ref&amp;gt; http://stackoverflow.com/questions/9359948/multiple-inheritance-whats-a-good-example&amp;lt;/ref&amp;gt;. A Winged animal and a mammal inherit primitive attributes of an animal. While a winged animal can fly; a mammal can breathe and reproduce; they both eat, move and share features generic to animals. A bat being a winged mammal possesses features common to both winged animal and mammal. It follows that class Bat inherits from classes WingedAnimal and Mammal and in turn classes WingedAnimal and Mammal inherit from class Animal.   In reference to this context, let us write our own implementation and explore the issues of multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
[[File:Class_Diagram.png|thumb|right|x400px|alt=Class Diagram for the example.]]&lt;br /&gt;
&lt;br /&gt;
==Issues of Multiple Inheritance==&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Due to inheritance, the parent class’s objects get duplicated to child class. As a result, Class Bat duplicates Animal objects through each of WingedAnimal and Mammal classes. When Bat accesses any of the Animal's attributes, the compiler would be ambiguous about determining the correct path to use since two paths are available from Animal to Bat. This problem of multiple inheritance is called diamond problem &amp;lt;ref&amp;gt;http://www.artima.com/weblogs/viewpost.jsp?thread=246488&amp;lt;/ref&amp;gt;. Compiler takes arbitrary decisions during such situations. &amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt; Objects of parent classes can be separately managed by using [http://en.wikipedia.org/wiki/Virtual_method_table virtual tables]as in C++. By explicitly specifying the virtual table pointer, the Bat class can access WingedAnimal and Mammal classes unambiguously. However virtual tables occupy significant portion of memory thereby limiting their extensive usage. &amp;lt;/li&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt; The order by which a child class inherits parent classes also matters. If Bat inherits WingedAnimal prior to Mammal, then WingedAnimal precedes Mammal and common methods are accessed in this order of precedence.  Hence if the Programmer is not aware of such precedence, debugging would be difficult. &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
Programming languages provide different solutions to handle the issues of multiple inheritance. Here we consider the techniques offered by C++, Java, Ruby, Scala and Python.    &lt;br /&gt;
===Multiple Inheritance in C++===&lt;br /&gt;
C++ supports multiple inheritance with the notion of virtual classes and virtual functions. In the example, the Animal class defines virtual functions with WingedAnimal and Mammal classes virtually inheriting the features of Animal. The Bat class can now unambiguously access canEat() method of Animal class, using virtual table pointers of WingedAnimal and Mammal. The example written using C++ virtual functions is shown below.&lt;br /&gt;
&lt;br /&gt;
 class Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void virtual canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class WingedAnimal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Mammal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Bat :public WingedAnimal, public Mammal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }  &lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
  int i = 0;&lt;br /&gt;
  Animal *instances[3];&lt;br /&gt;
  WingedAnimal w;&lt;br /&gt;
  Mammal m;&lt;br /&gt;
  Bat b;&lt;br /&gt;
  instances[0] = &amp;amp;w;&lt;br /&gt;
  instances[1] = &amp;amp;m;&lt;br /&gt;
  instances[2] = &amp;amp;b;&lt;br /&gt;
  while(i&amp;lt;3)&lt;br /&gt;
    instances[i++]-&amp;gt;canEat();&lt;br /&gt;
  return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Java===&lt;br /&gt;
Java doesn’t support multiple inheritance but provides a feature called interfaces to simulate inheritance from multiple classes. Interface is an abstract class which provides a generic template by just declaring variables and methods without implementing them. [http://www.brpreiss.com/books/opus4/html/page611.html Concrete classes] that inherit interfaces are supposed to implement its methods. The example written using Java interfaces is shown below.&lt;br /&gt;
&lt;br /&gt;
 interface Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface WingedAnimal extends Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();  &lt;br /&gt;
   void canFly();  &lt;br /&gt;
 }&lt;br /&gt;
 class ImplementWingedAnimal implements WingedAnimal  &lt;br /&gt;
 {&lt;br /&gt;
    public void canEat() {    &lt;br /&gt;
    }&lt;br /&gt;
    public void canFly() {    &lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Mammal extends Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();&lt;br /&gt;
   void canGiveBirth();&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementMammal implements Mammal&lt;br /&gt;
 {&lt;br /&gt;
    public void canEat() {    &lt;br /&gt;
    }&lt;br /&gt;
    public void canGiveBirth() {    &lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Bat extends WingedAnimal, Mammal&lt;br /&gt;
 {&lt;br /&gt;
    void canEat();    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class ImplementBat implements Bat&lt;br /&gt;
 {  &lt;br /&gt;
    ImplementWingedAnimal w = new ImplementWingedAnimal();&lt;br /&gt;
    ImplementMammal m = new ImplementMammal();     &lt;br /&gt;
    public void canEat() {            &lt;br /&gt;
      m.canEat();  &lt;br /&gt;
      w.canEat();      &lt;br /&gt;
    }&lt;br /&gt;
    public void canFly() {&lt;br /&gt;
    }&lt;br /&gt;
    public void canGiveBirth() {&lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class MultipleInheritance&lt;br /&gt;
 {&lt;br /&gt;
   public static void main(String args[])&lt;br /&gt;
   {&lt;br /&gt;
     ImplementBat b = new ImplementBat ();&lt;br /&gt;
     b.canEat();       &lt;br /&gt;
   } &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Ruby===&lt;br /&gt;
Ruby doesn't support multiple inheritance but provides a similar feature using modules and mixins.  &lt;br /&gt;
====Modules====&lt;br /&gt;
A Module is an encapsulation of methods and variables, although similar in function to a Class, differs from a class in the sense that it cannot be instantiated. Modules are  more dynamic and allow us to organize the code in a more granular way.Each module can add something to the existing class. A module can be thought of an encapsulation of a  subset of the full capabilities of an object.&lt;br /&gt;
An example found in the book ,The Well-Grounded Rubyist &amp;lt;ref&amp;gt;http://www.manning.com/black2/SampleChapter4.pdf [Pg.92] &amp;lt;/ref&amp;gt;, is the stack-like module. The module encapsulates the characteristics of being like a stack,implements the operations that can be performed by a stack but is not the actual Stack class. This allows us to use this module to mix-in  stack-like behaviour into other classes that require them.&lt;br /&gt;
&lt;br /&gt;
====Mixins====&lt;br /&gt;
A Mixin is a concept that allows inclusion of special characteristics into existing classes, so that common features that need to be reused in several classes can be selectively included, thus avoiding implementation of unnecessary functions like in the case of interfaces.&lt;br /&gt;
Mixins allow us to dynamically bind special features defined in modules, into core classes without really making any changes to the objects. This improves flexibility when we want to add plugins and extensions to the existing framework.&amp;lt;ref&amp;gt;http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-frameworks/&amp;lt;/ref&amp;gt;.The source code for the same example implemented using ruby mixins is shown below.&lt;br /&gt;
&lt;br /&gt;
 module Animal&lt;br /&gt;
   def canEat     &lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module WingedAnimal&lt;br /&gt;
   include Animal    &lt;br /&gt;
   def canEat&lt;br /&gt;
   end      &lt;br /&gt;
   def canFly&lt;br /&gt;
   end      &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Mammal&lt;br /&gt;
   include Animal&lt;br /&gt;
   def canEat&lt;br /&gt;
   end&lt;br /&gt;
   def canGiveBirth&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Bat&lt;br /&gt;
   include WingedAnimal&lt;br /&gt;
   include Mammal&lt;br /&gt;
   include Animal  &lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 Bat.new().canEat()&lt;br /&gt;
&lt;br /&gt;
===Multiple inheritance in other languages===&lt;br /&gt;
[http://www.scala-lang.org/ Scala] offers traits that are similar to Java interfaces to support multiple inheritance. Traits define a template with methods and variables which any class can make use of. The difference between interfaces and traits is that traits can implement methods. A Scala class can inherit from any number of traits together with a class in way to support multiple inheritance. Traits can be mixed in to a class even without inheriting it but just by including it at the time of creating instances. &amp;lt;ref&amp;gt;http://joelabrahamsson.com/entry/learning-scala-traits&amp;lt;/ref&amp;gt;. Trait can also extend a class and methods to be overridden are usually prefixed by the keyword override. The language enforces method names to be different across inherited  traits to avoid the issues of multiple inheritance. [http://www.python.org/ Python] supports a limited version of multiple inheritance by extending multiple classes in depth-first left-right order &amp;lt;ref&amp;gt;http://docs.python.org/tutorial/classes.html&amp;lt;/ref&amp;gt;. If a python class D inherits classes A, B, C and if an instance variable or method is not available in the current class, it checks in A and its base classes, then moves to B and its base classes and eventually to C thus moving from left to right. In this way, python avoids the diamond problem by making the search order linear by which the derived class accesses attributes of its parent classes. In addition python also defines mixins to access reusable features within a class.&lt;br /&gt;
&lt;br /&gt;
==Advantages of mixins==&lt;br /&gt;
1. Firstly, when a module gets mixed into different classes, each class acquires the same  reference  to the module and hence even dynamic changes to the module gets reflected in the behavior of the classes &amp;lt;ref&amp;gt;http://www.linuxtopia.org/online_books/programming_books/ruby_tutorial/Ruby_Modules_Mixins.html&amp;lt;/ref&amp;gt;. In this way, the module’s code is not just copied to individual classes as in static languages instead its reference is provided. &lt;br /&gt;
&lt;br /&gt;
2. Secondly, the mixed in state and behavior can be accessed by the instance of target class just like accessing its own features. &lt;br /&gt;
&lt;br /&gt;
3. Thirdly, a class can include any number of modules in a way it can simulate multiple inheritance without having the related issues.&lt;br /&gt;
&lt;br /&gt;
==Questions to Ponder==&lt;br /&gt;
===Do mixins solve all of the problems of multiple inheritance?===&lt;br /&gt;
Undoubtedly mixins solve some of the problems of multiple inheritance. There is no ambiguity in resolving the request to a common method implemented by multiple modules. In that case, the lastly included module is responsible for handling accesses to common methods. Though the order of precedence of included modules matters, it looks clearer than C++.  However languages like Scala and Eiffel implement multiple inheritance far more safely  by explicitly forcing the user to have different method names across inherited classes.&lt;br /&gt;
&lt;br /&gt;
===Are mixins a clear advantage over interfaces?===&lt;br /&gt;
Mixins are more advantageous than interfaces because of their features. Interfaces have some limitations. As seen from programs, the derived class Allrounder uses the instances of Batsman and Bowler classes (has-a relationship) to access respective attributes. Interface is nice in the sense it provides a generic template that could be implemented differently by different classes.  However the target class has to necessarily implement every method of the interface. This would pose problems to a Class which is interested in implementing specific features of the interface. On the contrary, Mixins eliminate this inconvenience by mixing in the state and behavior of the modules in the target class and allows a it to use them as it needs. Hence the target class can just use module’s features selectively or completely instead of implementing by itself. The size of multiple inheritance code using mixins is smaller than interfaces and C++ variants.&lt;br /&gt;
&lt;br /&gt;
===Do mixins have any disadvantages not shared by multiple inheritance or interfaces?===&lt;br /&gt;
Inspite of making the lives of programmers better with effective code reuse and avoiding unnecessary implementation of methods that cause clutter in the code, the concept of Mixins can easily be abused resulting in namespace pollution. This is caused when a class includes multiple modules each of which contains several hundred method implementations. Having hundreds of methods mixed into a single class, makes it difficult to identify the source modules of the methods, thereby causing confusion while implementing these methods, whereas in the case of multiple inheritance we can easily identify the class to which the method belongs.&lt;br /&gt;
Another disadvantage that follows from the namespace collision is the case where we have multiple methods with the same name in different modules.In such a case whenever a call to that method is made, the method which belongs to the module that was included last will be called. In the above example, the method call canEat() invokes Animal's canEat() method since Animal was the last included module. The problem with this is that, this takes place silently without any warning. This may result in accidentally inheriting wrong methods.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
In this article, we have discussed about multiple inheritance and means by which it is implemented in different languages. Though multiple inheritance is a useful feature,&lt;br /&gt;
many languages do not support them due to its associated issues. Given its advantages, mixin in ruby seems to be a viable option to achieve multiple inheritance. However mixins too have limitations which should be taken care of during implementation.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
==See Also==&lt;br /&gt;
1. Lucas Carlson and Leonard Richardson. 2006. [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=1212915&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 Ruby Cookbook] (Cookbooks (O'Reilly)). O'Reilly Media, Inc..&lt;br /&gt;
&lt;br /&gt;
2. Herbert Schildt. 2000. Java 2: [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=557816&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 The Complete Reference] (4th ed.). McGraw-Hill Professional.&lt;/div&gt;</summary>
		<author><name>Snallan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65452</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65452"/>
		<updated>2012-09-21T14:17:38Z</updated>

		<summary type="html">&lt;p&gt;Snallan: /* A Real World Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''1w23:  Multiple Inheritance and Mixins&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
The purpose of this article is to give a brief introduction about multiple inheritance, consider some of its issues and discuss techniques employed by different programming languages to handle such issues. In this relation, the article also compares solutions provided by different programming languages, especially against Ruby mixins, by listing their advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
Multiple Inheritance is a technique of establishing hierarchical relationships within a set of classes, such that a single class can inherit  behaviors from multiple parent classes. Although it might seem useful to include methods from multiple classes, the major issue with multiple inheritance is the ambiguity in referencing the parent class methods,  when more than one parent classes share a method with the same name.This ambiguity is predominant in cases where the inheritance hierarchy is more than one level deep, where the class in the lowermost level inherits from two classes, that have overridden the same method from their parent in different ways.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A Real World Example==&lt;br /&gt;
A known example of multiple inheritance to represent winged animals and mammals, using classes and objects is cited here &amp;lt;ref&amp;gt; http://stackoverflow.com/questions/9359948/multiple-inheritance-whats-a-good-example&amp;lt;/ref&amp;gt;. A Winged animal and a mammal inherit primitive attributes of an animal. While a winged animal can fly; a mammal can breathe and reproduce; they both eat, move and share features generic to animals. A bat being a winged mammal possesses features common to both winged animal and mammal. It follows that class Bat inherits from classes WingedAnimal and Mammal and in turn classes WingedAnimal and Mammal inherit from class Animal.   In reference to this context, let us write our own implementation and explore the issues of multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
[[File:Class_Diagram.png|thumb|right|x400px|alt=Class Diagram for the example.]]&lt;br /&gt;
&lt;br /&gt;
==Issues of Multiple Inheritance==&lt;br /&gt;
1. Due to inheritance, the parent class’s objects get duplicated to child class. As a result, Class Bat duplicates Animal objects through each of WingedAnimal and Mammal classes. When Bat accesses any of the Animal's attributes, the compiler would be ambiguous about determining the correct path to use since two paths are available from Animal to Bat. This problem of multiple inheritance is called diamond problem &amp;lt;ref&amp;gt;http://www.artima.com/weblogs/viewpost.jsp?thread=246488&amp;lt;/ref&amp;gt;. Compiler takes arbitrary decisions during such situations. &lt;br /&gt;
&lt;br /&gt;
2.Objects of parent classes can be separately managed by using [http://en.wikipedia.org/wiki/Virtual_method_table virtual tables]as in C++. By explicitly specifying the virtual table pointer, the Bat class can access WingedAnimal and Mammal classes unambiguously. However virtual tables occupy significant portion of memory thereby limiting their extensive usage.  &lt;br /&gt;
&lt;br /&gt;
3. The order by which a child class inherits parent classes also matters. If Bat inherits WingedAnimal prior to Mammal, then WingedAnimal precedes Mammal and common methods are accessed in this order of precedence.  Hence if the Programmer is not aware of such precedence, debugging would be difficult.&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
Programming languages provide different solutions to handle the issues of multiple inheritance. Here we consider the techniques offered by C++, Java, Ruby, Scala and Python.    &lt;br /&gt;
===Multiple Inheritance in C++===&lt;br /&gt;
C++ supports multiple inheritance with the notion of virtual classes and virtual functions. In the example, the Animal class defines virtual functions with WingedAnimal and Mammal classes virtually inheriting the features of Animal. The Bat class can now unambiguously access canEat() method of Animal class, using virtual table pointers of WingedAnimal and Mammal. The example written using C++ virtual functions is shown below.&lt;br /&gt;
&lt;br /&gt;
 class Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void virtual canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class WingedAnimal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Mammal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Bat :public WingedAnimal, public Mammal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }  &lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
  int i = 0;&lt;br /&gt;
  Animal *instances[3];&lt;br /&gt;
  WingedAnimal w;&lt;br /&gt;
  Mammal m;&lt;br /&gt;
  Bat b;&lt;br /&gt;
  instances[0] = &amp;amp;w;&lt;br /&gt;
  instances[1] = &amp;amp;m;&lt;br /&gt;
  instances[2] = &amp;amp;b;&lt;br /&gt;
  while(i&amp;lt;3)&lt;br /&gt;
    instances[i++]-&amp;gt;canEat();&lt;br /&gt;
  return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Java===&lt;br /&gt;
Java doesn’t support multiple inheritance but provides a feature called interfaces to simulate inheritance from multiple classes. Interface is an abstract class which provides a generic template by just declaring variables and methods without implementing them. [http://www.brpreiss.com/books/opus4/html/page611.html Concrete classes] that inherit interfaces are supposed to implement its methods. The example written using Java interfaces is shown below.&lt;br /&gt;
&lt;br /&gt;
 interface Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface WingedAnimal extends Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();  &lt;br /&gt;
   void canFly();  &lt;br /&gt;
 }&lt;br /&gt;
 class ImplementWingedAnimal implements WingedAnimal  &lt;br /&gt;
 {&lt;br /&gt;
    public void canEat() {    &lt;br /&gt;
    }&lt;br /&gt;
    public void canFly() {    &lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Mammal extends Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();&lt;br /&gt;
   void canGiveBirth();&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementMammal implements Mammal&lt;br /&gt;
 {&lt;br /&gt;
    public void canEat() {    &lt;br /&gt;
    }&lt;br /&gt;
    public void canGiveBirth() {    &lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Bat extends WingedAnimal, Mammal&lt;br /&gt;
 {&lt;br /&gt;
    void canEat();    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class ImplementBat implements Bat&lt;br /&gt;
 {  &lt;br /&gt;
    ImplementWingedAnimal w = new ImplementWingedAnimal();&lt;br /&gt;
    ImplementMammal m = new ImplementMammal();     &lt;br /&gt;
    public void canEat() {            &lt;br /&gt;
      m.canEat();  &lt;br /&gt;
      w.canEat();      &lt;br /&gt;
    }&lt;br /&gt;
    public void canFly() {&lt;br /&gt;
    }&lt;br /&gt;
    public void canGiveBirth() {&lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class MultipleInheritance&lt;br /&gt;
 {&lt;br /&gt;
   public static void main(String args[])&lt;br /&gt;
   {&lt;br /&gt;
     ImplementBat b = new ImplementBat ();&lt;br /&gt;
     b.canEat();       &lt;br /&gt;
   } &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Ruby===&lt;br /&gt;
Ruby doesn't support multiple inheritance but provides a similar feature using modules and mixins.  &lt;br /&gt;
====Modules====&lt;br /&gt;
A Module is an encapsulation of methods and variables, although similar in function to a Class, differs from a class in the sense that it cannot be instantiated. Modules are  more dynamic and allow us to organize the code in a more granular way.Each module can add something to the existing class. A module can be thought of an encapsulation of a  subset of the full capabilities of an object.&lt;br /&gt;
An example found in the book ,The Well-Grounded Rubyist &amp;lt;ref&amp;gt;http://www.manning.com/black2/SampleChapter4.pdf [Pg.92] &amp;lt;/ref&amp;gt;, is the stack-like module. The module encapsulates the characteristics of being like a stack,implements the operations that can be performed by a stack but is not the actual Stack class. This allows us to use this module to mix-in  stack-like behaviour into other classes that require them.&lt;br /&gt;
&lt;br /&gt;
====Mixins====&lt;br /&gt;
A Mixin is a concept that allows inclusion of special characteristics into existing classes, so that common features that need to be reused in several classes can be selectively included, thus avoiding implementation of unnecessary functions like in the case of interfaces.&lt;br /&gt;
Mixins allow us to dynamically bind special features defined in modules, into core classes without really making any changes to the objects. This improves flexibility when we want to add plugins and extensions to the existing framework.&amp;lt;ref&amp;gt;http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-frameworks/&amp;lt;/ref&amp;gt;.The source code for the same example implemented using ruby mixins is shown below.&lt;br /&gt;
&lt;br /&gt;
 module Animal&lt;br /&gt;
   def canEat     &lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module WingedAnimal&lt;br /&gt;
   include Animal    &lt;br /&gt;
   def canEat&lt;br /&gt;
   end      &lt;br /&gt;
   def canFly&lt;br /&gt;
   end      &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Mammal&lt;br /&gt;
   include Animal&lt;br /&gt;
   def canEat&lt;br /&gt;
   end&lt;br /&gt;
   def canGiveBirth&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Bat&lt;br /&gt;
   include WingedAnimal&lt;br /&gt;
   include Mammal&lt;br /&gt;
   include Animal  &lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 Bat.new().canEat()&lt;br /&gt;
&lt;br /&gt;
===Multiple inheritance in other languages===&lt;br /&gt;
[http://www.scala-lang.org/ Scala] offers traits that are similar to Java interfaces to support multiple inheritance. Traits define a template with methods and variables which any class can make use of. The difference between interfaces and traits is that traits can implement methods. A Scala class can inherit from any number of traits together with a class in way to support multiple inheritance. Traits can be mixed in to a class even without inheriting it but just by including it at the time of creating instances. &amp;lt;ref&amp;gt;http://joelabrahamsson.com/entry/learning-scala-traits&amp;lt;/ref&amp;gt;. Trait can also extend a class and methods to be overridden are usually prefixed by the keyword override. The language enforces method names to be different across inherited  traits to avoid the issues of multiple inheritance. [http://www.python.org/ Python] supports a limited version of multiple inheritance by extending multiple classes in depth-first left-right order &amp;lt;ref&amp;gt;http://docs.python.org/tutorial/classes.html&amp;lt;/ref&amp;gt;. If a python class D inherits classes A, B, C and if an instance variable or method is not available in the current class, it checks in A and its base classes, then moves to B and its base classes and eventually to C thus moving from left to right. In this way, python avoids the diamond problem by making the search order linear by which the derived class accesses attributes of its parent classes. In addition python also defines mixins to access reusable features within a class.&lt;br /&gt;
&lt;br /&gt;
==Advantages of mixins==&lt;br /&gt;
1. Firstly, when a module gets mixed into different classes, each class acquires the same  reference  to the module and hence even dynamic changes to the module gets reflected in the behavior of the classes &amp;lt;ref&amp;gt;http://www.linuxtopia.org/online_books/programming_books/ruby_tutorial/Ruby_Modules_Mixins.html&amp;lt;/ref&amp;gt;. In this way, the module’s code is not just copied to individual classes as in static languages instead its reference is provided. &lt;br /&gt;
&lt;br /&gt;
2. Secondly, the mixed in state and behavior can be accessed by the instance of target class just like accessing its own features. &lt;br /&gt;
&lt;br /&gt;
3. Thirdly, a class can include any number of modules in a way it can simulate multiple inheritance without having the related issues.&lt;br /&gt;
&lt;br /&gt;
==Questions to Ponder==&lt;br /&gt;
===Do mixins solve all of the problems of multiple inheritance?===&lt;br /&gt;
Undoubtedly mixins solve some of the problems of multiple inheritance. There is no ambiguity in resolving the request to a common method implemented by multiple modules. In that case, the lastly included module is responsible for handling accesses to common methods. Though the order of precedence of included modules matters, it looks clearer than C++.  However languages like Scala and Eiffel implement multiple inheritance far more safely  by explicitly forcing the user to have different method names across inherited classes.&lt;br /&gt;
&lt;br /&gt;
===Are mixins a clear advantage over interfaces?===&lt;br /&gt;
Mixins are more advantageous than interfaces because of their features. Interfaces have some limitations. As seen from programs, the derived class Allrounder uses the instances of Batsman and Bowler classes (has-a relationship) to access respective attributes. Interface is nice in the sense it provides a generic template that could be implemented differently by different classes.  However the target class has to necessarily implement every method of the interface. This would pose problems to a Class which is interested in implementing specific features of the interface. On the contrary, Mixins eliminate this inconvenience by mixing in the state and behavior of the modules in the target class and allows a it to use them as it needs. Hence the target class can just use module’s features selectively or completely instead of implementing by itself. The size of multiple inheritance code using mixins is smaller than interfaces and C++ variants.&lt;br /&gt;
&lt;br /&gt;
===Do mixins have any disadvantages not shared by multiple inheritance or interfaces?===&lt;br /&gt;
Inspite of making the lives of programmers better with effective code reuse and avoiding unnecessary implementation of methods that cause clutter in the code, the concept of Mixins can easily be abused resulting in namespace pollution. This is caused when a class includes multiple modules each of which contains several hundred method implementations. Having hundreds of methods mixed into a single class, makes it difficult to identify the source modules of the methods, thereby causing confusion while implementing these methods, whereas in the case of multiple inheritance we can easily identify the class to which the method belongs.&lt;br /&gt;
Another disadvantage that follows from the namespace collision is the case where we have multiple methods with the same name in different modules.In such a case whenever a call to that method is made, the method which belongs to the module that was included last will be called. In the above example, the method call canEat() invokes Animal's canEat() method since Animal was the last included module. The problem with this is that, this takes place silently without any warning. This may result in accidentally inheriting wrong methods.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
In this article, we have discussed about multiple inheritance and means by which it is implemented in different languages. Though multiple inheritance is a useful feature,&lt;br /&gt;
many languages do not support them due to its associated issues. Given its advantages, mixin in ruby seems to be a viable option to achieve multiple inheritance. However mixins too have limitations which should be taken care of during implementation.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
==See Also==&lt;br /&gt;
1. Lucas Carlson and Leonard Richardson. 2006. [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=1212915&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 Ruby Cookbook] (Cookbooks (O'Reilly)). O'Reilly Media, Inc..&lt;br /&gt;
&lt;br /&gt;
2. Herbert Schildt. 2000. Java 2: [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=557816&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 The Complete Reference] (4th ed.). McGraw-Hill Professional.&lt;/div&gt;</summary>
		<author><name>Snallan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65451</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65451"/>
		<updated>2012-09-21T14:16:54Z</updated>

		<summary type="html">&lt;p&gt;Snallan: /* A Real World Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''1w23:  Multiple Inheritance and Mixins&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
The purpose of this article is to give a brief introduction about multiple inheritance, consider some of its issues and discuss techniques employed by different programming languages to handle such issues. In this relation, the article also compares solutions provided by different programming languages, especially against Ruby mixins, by listing their advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
Multiple Inheritance is a technique of establishing hierarchical relationships within a set of classes, such that a single class can inherit  behaviors from multiple parent classes. Although it might seem useful to include methods from multiple classes, the major issue with multiple inheritance is the ambiguity in referencing the parent class methods,  when more than one parent classes share a method with the same name.This ambiguity is predominant in cases where the inheritance hierarchy is more than one level deep, where the class in the lowermost level inherits from two classes, that have overridden the same method from their parent in different ways.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A Real World Example==&lt;br /&gt;
A known example of multiple inheritance to represent winged animals and mammals, using classes and objects is cited here &amp;lt;ref&amp;gt; http://stackoverflow.com/questions/9359948/multiple-inheritance-whats-a-good-example&amp;lt;/ref&amp;gt;. A Winged animal and a mammal inherit primitive attributes of an animal. While a winged animal can fly; a mammal can breathe and reproduce; they both eat, move and share features generic to animals. A bat being a winged mammal possesses features common to both winged animal and mammal. It follows that class Bat inherits from classes WingedAnimal and Mammal and in turn classes WingedAnimal and Mammal inherit from class Animal.   In reference to this context, let us give our own implementation and explore the issues of multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
[[File:Class_Diagram.png|thumb|right|x400px|alt=Class Diagram for the example.]]&lt;br /&gt;
&lt;br /&gt;
==Issues of Multiple Inheritance==&lt;br /&gt;
1. Due to inheritance, the parent class’s objects get duplicated to child class. As a result, Class Bat duplicates Animal objects through each of WingedAnimal and Mammal classes. When Bat accesses any of the Animal's attributes, the compiler would be ambiguous about determining the correct path to use since two paths are available from Animal to Bat. This problem of multiple inheritance is called diamond problem &amp;lt;ref&amp;gt;http://www.artima.com/weblogs/viewpost.jsp?thread=246488&amp;lt;/ref&amp;gt;. Compiler takes arbitrary decisions during such situations. &lt;br /&gt;
&lt;br /&gt;
2.Objects of parent classes can be separately managed by using [http://en.wikipedia.org/wiki/Virtual_method_table virtual tables]as in C++. By explicitly specifying the virtual table pointer, the Bat class can access WingedAnimal and Mammal classes unambiguously. However virtual tables occupy significant portion of memory thereby limiting their extensive usage.  &lt;br /&gt;
&lt;br /&gt;
3. The order by which a child class inherits parent classes also matters. If Bat inherits WingedAnimal prior to Mammal, then WingedAnimal precedes Mammal and common methods are accessed in this order of precedence.  Hence if the Programmer is not aware of such precedence, debugging would be difficult.&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
Programming languages provide different solutions to handle the issues of multiple inheritance. Here we consider the techniques offered by C++, Java, Ruby, Scala and Python.    &lt;br /&gt;
===Multiple Inheritance in C++===&lt;br /&gt;
C++ supports multiple inheritance with the notion of virtual classes and virtual functions. In the example, the Animal class defines virtual functions with WingedAnimal and Mammal classes virtually inheriting the features of Animal. The Bat class can now unambiguously access canEat() method of Animal class, using virtual table pointers of WingedAnimal and Mammal. The example written using C++ virtual functions is shown below.&lt;br /&gt;
&lt;br /&gt;
 class Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void virtual canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class WingedAnimal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Mammal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Bat :public WingedAnimal, public Mammal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }  &lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
  int i = 0;&lt;br /&gt;
  Animal *instances[3];&lt;br /&gt;
  WingedAnimal w;&lt;br /&gt;
  Mammal m;&lt;br /&gt;
  Bat b;&lt;br /&gt;
  instances[0] = &amp;amp;w;&lt;br /&gt;
  instances[1] = &amp;amp;m;&lt;br /&gt;
  instances[2] = &amp;amp;b;&lt;br /&gt;
  while(i&amp;lt;3)&lt;br /&gt;
    instances[i++]-&amp;gt;canEat();&lt;br /&gt;
  return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Java===&lt;br /&gt;
Java doesn’t support multiple inheritance but provides a feature called interfaces to simulate inheritance from multiple classes. Interface is an abstract class which provides a generic template by just declaring variables and methods without implementing them. [http://www.brpreiss.com/books/opus4/html/page611.html Concrete classes] that inherit interfaces are supposed to implement its methods. The example written using Java interfaces is shown below.&lt;br /&gt;
&lt;br /&gt;
 interface Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface WingedAnimal extends Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();  &lt;br /&gt;
   void canFly();  &lt;br /&gt;
 }&lt;br /&gt;
 class ImplementWingedAnimal implements WingedAnimal  &lt;br /&gt;
 {&lt;br /&gt;
    public void canEat() {    &lt;br /&gt;
    }&lt;br /&gt;
    public void canFly() {    &lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Mammal extends Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();&lt;br /&gt;
   void canGiveBirth();&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementMammal implements Mammal&lt;br /&gt;
 {&lt;br /&gt;
    public void canEat() {    &lt;br /&gt;
    }&lt;br /&gt;
    public void canGiveBirth() {    &lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Bat extends WingedAnimal, Mammal&lt;br /&gt;
 {&lt;br /&gt;
    void canEat();    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class ImplementBat implements Bat&lt;br /&gt;
 {  &lt;br /&gt;
    ImplementWingedAnimal w = new ImplementWingedAnimal();&lt;br /&gt;
    ImplementMammal m = new ImplementMammal();     &lt;br /&gt;
    public void canEat() {            &lt;br /&gt;
      m.canEat();  &lt;br /&gt;
      w.canEat();      &lt;br /&gt;
    }&lt;br /&gt;
    public void canFly() {&lt;br /&gt;
    }&lt;br /&gt;
    public void canGiveBirth() {&lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class MultipleInheritance&lt;br /&gt;
 {&lt;br /&gt;
   public static void main(String args[])&lt;br /&gt;
   {&lt;br /&gt;
     ImplementBat b = new ImplementBat ();&lt;br /&gt;
     b.canEat();       &lt;br /&gt;
   } &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Ruby===&lt;br /&gt;
Ruby doesn't support multiple inheritance but provides a similar feature using modules and mixins.  &lt;br /&gt;
====Modules====&lt;br /&gt;
A Module is an encapsulation of methods and variables, although similar in function to a Class, differs from a class in the sense that it cannot be instantiated. Modules are  more dynamic and allow us to organize the code in a more granular way.Each module can add something to the existing class. A module can be thought of an encapsulation of a  subset of the full capabilities of an object.&lt;br /&gt;
An example found in the book ,The Well-Grounded Rubyist &amp;lt;ref&amp;gt;http://www.manning.com/black2/SampleChapter4.pdf [Pg.92] &amp;lt;/ref&amp;gt;, is the stack-like module. The module encapsulates the characteristics of being like a stack,implements the operations that can be performed by a stack but is not the actual Stack class. This allows us to use this module to mix-in  stack-like behaviour into other classes that require them.&lt;br /&gt;
&lt;br /&gt;
====Mixins====&lt;br /&gt;
A Mixin is a concept that allows inclusion of special characteristics into existing classes, so that common features that need to be reused in several classes can be selectively included, thus avoiding implementation of unnecessary functions like in the case of interfaces.&lt;br /&gt;
Mixins allow us to dynamically bind special features defined in modules, into core classes without really making any changes to the objects. This improves flexibility when we want to add plugins and extensions to the existing framework.&amp;lt;ref&amp;gt;http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-frameworks/&amp;lt;/ref&amp;gt;.The source code for the same example implemented using ruby mixins is shown below.&lt;br /&gt;
&lt;br /&gt;
 module Animal&lt;br /&gt;
   def canEat     &lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module WingedAnimal&lt;br /&gt;
   include Animal    &lt;br /&gt;
   def canEat&lt;br /&gt;
   end      &lt;br /&gt;
   def canFly&lt;br /&gt;
   end      &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Mammal&lt;br /&gt;
   include Animal&lt;br /&gt;
   def canEat&lt;br /&gt;
   end&lt;br /&gt;
   def canGiveBirth&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Bat&lt;br /&gt;
   include WingedAnimal&lt;br /&gt;
   include Mammal&lt;br /&gt;
   include Animal  &lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 Bat.new().canEat()&lt;br /&gt;
&lt;br /&gt;
===Multiple inheritance in other languages===&lt;br /&gt;
[http://www.scala-lang.org/ Scala] offers traits that are similar to Java interfaces to support multiple inheritance. Traits define a template with methods and variables which any class can make use of. The difference between interfaces and traits is that traits can implement methods. A Scala class can inherit from any number of traits together with a class in way to support multiple inheritance. Traits can be mixed in to a class even without inheriting it but just by including it at the time of creating instances. &amp;lt;ref&amp;gt;http://joelabrahamsson.com/entry/learning-scala-traits&amp;lt;/ref&amp;gt;. Trait can also extend a class and methods to be overridden are usually prefixed by the keyword override. The language enforces method names to be different across inherited  traits to avoid the issues of multiple inheritance. [http://www.python.org/ Python] supports a limited version of multiple inheritance by extending multiple classes in depth-first left-right order &amp;lt;ref&amp;gt;http://docs.python.org/tutorial/classes.html&amp;lt;/ref&amp;gt;. If a python class D inherits classes A, B, C and if an instance variable or method is not available in the current class, it checks in A and its base classes, then moves to B and its base classes and eventually to C thus moving from left to right. In this way, python avoids the diamond problem by making the search order linear by which the derived class accesses attributes of its parent classes. In addition python also defines mixins to access reusable features within a class.&lt;br /&gt;
&lt;br /&gt;
==Advantages of mixins==&lt;br /&gt;
1. Firstly, when a module gets mixed into different classes, each class acquires the same  reference  to the module and hence even dynamic changes to the module gets reflected in the behavior of the classes &amp;lt;ref&amp;gt;http://www.linuxtopia.org/online_books/programming_books/ruby_tutorial/Ruby_Modules_Mixins.html&amp;lt;/ref&amp;gt;. In this way, the module’s code is not just copied to individual classes as in static languages instead its reference is provided. &lt;br /&gt;
&lt;br /&gt;
2. Secondly, the mixed in state and behavior can be accessed by the instance of target class just like accessing its own features. &lt;br /&gt;
&lt;br /&gt;
3. Thirdly, a class can include any number of modules in a way it can simulate multiple inheritance without having the related issues.&lt;br /&gt;
&lt;br /&gt;
==Questions to Ponder==&lt;br /&gt;
===Do mixins solve all of the problems of multiple inheritance?===&lt;br /&gt;
Undoubtedly mixins solve some of the problems of multiple inheritance. There is no ambiguity in resolving the request to a common method implemented by multiple modules. In that case, the lastly included module is responsible for handling accesses to common methods. Though the order of precedence of included modules matters, it looks clearer than C++.  However languages like Scala and Eiffel implement multiple inheritance far more safely  by explicitly forcing the user to have different method names across inherited classes.&lt;br /&gt;
&lt;br /&gt;
===Are mixins a clear advantage over interfaces?===&lt;br /&gt;
Mixins are more advantageous than interfaces because of their features. Interfaces have some limitations. As seen from programs, the derived class Allrounder uses the instances of Batsman and Bowler classes (has-a relationship) to access respective attributes. Interface is nice in the sense it provides a generic template that could be implemented differently by different classes.  However the target class has to necessarily implement every method of the interface. This would pose problems to a Class which is interested in implementing specific features of the interface. On the contrary, Mixins eliminate this inconvenience by mixing in the state and behavior of the modules in the target class and allows a it to use them as it needs. Hence the target class can just use module’s features selectively or completely instead of implementing by itself. The size of multiple inheritance code using mixins is smaller than interfaces and C++ variants.&lt;br /&gt;
&lt;br /&gt;
===Do mixins have any disadvantages not shared by multiple inheritance or interfaces?===&lt;br /&gt;
Inspite of making the lives of programmers better with effective code reuse and avoiding unnecessary implementation of methods that cause clutter in the code, the concept of Mixins can easily be abused resulting in namespace pollution. This is caused when a class includes multiple modules each of which contains several hundred method implementations. Having hundreds of methods mixed into a single class, makes it difficult to identify the source modules of the methods, thereby causing confusion while implementing these methods, whereas in the case of multiple inheritance we can easily identify the class to which the method belongs.&lt;br /&gt;
Another disadvantage that follows from the namespace collision is the case where we have multiple methods with the same name in different modules.In such a case whenever a call to that method is made, the method which belongs to the module that was included last will be called. In the above example, the method call canEat() invokes Animal's canEat() method since Animal was the last included module. The problem with this is that, this takes place silently without any warning. This may result in accidentally inheriting wrong methods.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
In this article, we have discussed about multiple inheritance and means by which it is implemented in different languages. Though multiple inheritance is a useful feature,&lt;br /&gt;
many languages do not support them due to its associated issues. Given its advantages, mixin in ruby seems to be a viable option to achieve multiple inheritance. However mixins too have limitations which should be taken care of during implementation.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
==See Also==&lt;br /&gt;
1. Lucas Carlson and Leonard Richardson. 2006. [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=1212915&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 Ruby Cookbook] (Cookbooks (O'Reilly)). O'Reilly Media, Inc..&lt;br /&gt;
&lt;br /&gt;
2. Herbert Schildt. 2000. Java 2: [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=557816&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 The Complete Reference] (4th ed.). McGraw-Hill Professional.&lt;/div&gt;</summary>
		<author><name>Snallan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65450</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65450"/>
		<updated>2012-09-21T14:16:26Z</updated>

		<summary type="html">&lt;p&gt;Snallan: /* A Real World Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''1w23:  Multiple Inheritance and Mixins&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
The purpose of this article is to give a brief introduction about multiple inheritance, consider some of its issues and discuss techniques employed by different programming languages to handle such issues. In this relation, the article also compares solutions provided by different programming languages, especially against Ruby mixins, by listing their advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
Multiple Inheritance is a technique of establishing hierarchical relationships within a set of classes, such that a single class can inherit  behaviors from multiple parent classes. Although it might seem useful to include methods from multiple classes, the major issue with multiple inheritance is the ambiguity in referencing the parent class methods,  when more than one parent classes share a method with the same name.This ambiguity is predominant in cases where the inheritance hierarchy is more than one level deep, where the class in the lowermost level inherits from two classes, that have overridden the same method from their parent in different ways.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A Real World Example==&lt;br /&gt;
A known example of multiple inheritance to represent winged animals and mammals, using classes and objects is cited here, &amp;lt;ref&amp;gt; http://stackoverflow.com/questions/9359948/multiple-inheritance-whats-a-good-example&amp;lt;/ref&amp;gt; for the sake of understanding. A Winged animal and a mammal inherit primitive attributes of an animal. While a winged animal can fly; a mammal can breathe and reproduce; they both eat, move and share features generic to animals. A bat being a winged mammal possesses features common to both winged animal and mammal. It follows that class Bat inherits from classes WingedAnimal and Mammal and in turn classes WingedAnimal and Mammal inherit from class Animal.   In reference to this context, let us give our own implementation and explore the issues of multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
[[File:Class_Diagram.png|thumb|right|x400px|alt=Class Diagram for the example.]]&lt;br /&gt;
&lt;br /&gt;
==Issues of Multiple Inheritance==&lt;br /&gt;
1. Due to inheritance, the parent class’s objects get duplicated to child class. As a result, Class Bat duplicates Animal objects through each of WingedAnimal and Mammal classes. When Bat accesses any of the Animal's attributes, the compiler would be ambiguous about determining the correct path to use since two paths are available from Animal to Bat. This problem of multiple inheritance is called diamond problem &amp;lt;ref&amp;gt;http://www.artima.com/weblogs/viewpost.jsp?thread=246488&amp;lt;/ref&amp;gt;. Compiler takes arbitrary decisions during such situations. &lt;br /&gt;
&lt;br /&gt;
2.Objects of parent classes can be separately managed by using [http://en.wikipedia.org/wiki/Virtual_method_table virtual tables]as in C++. By explicitly specifying the virtual table pointer, the Bat class can access WingedAnimal and Mammal classes unambiguously. However virtual tables occupy significant portion of memory thereby limiting their extensive usage.  &lt;br /&gt;
&lt;br /&gt;
3. The order by which a child class inherits parent classes also matters. If Bat inherits WingedAnimal prior to Mammal, then WingedAnimal precedes Mammal and common methods are accessed in this order of precedence.  Hence if the Programmer is not aware of such precedence, debugging would be difficult.&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
Programming languages provide different solutions to handle the issues of multiple inheritance. Here we consider the techniques offered by C++, Java, Ruby, Scala and Python.    &lt;br /&gt;
===Multiple Inheritance in C++===&lt;br /&gt;
C++ supports multiple inheritance with the notion of virtual classes and virtual functions. In the example, the Animal class defines virtual functions with WingedAnimal and Mammal classes virtually inheriting the features of Animal. The Bat class can now unambiguously access canEat() method of Animal class, using virtual table pointers of WingedAnimal and Mammal. The example written using C++ virtual functions is shown below.&lt;br /&gt;
&lt;br /&gt;
 class Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void virtual canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class WingedAnimal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Mammal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Bat :public WingedAnimal, public Mammal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }  &lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
  int i = 0;&lt;br /&gt;
  Animal *instances[3];&lt;br /&gt;
  WingedAnimal w;&lt;br /&gt;
  Mammal m;&lt;br /&gt;
  Bat b;&lt;br /&gt;
  instances[0] = &amp;amp;w;&lt;br /&gt;
  instances[1] = &amp;amp;m;&lt;br /&gt;
  instances[2] = &amp;amp;b;&lt;br /&gt;
  while(i&amp;lt;3)&lt;br /&gt;
    instances[i++]-&amp;gt;canEat();&lt;br /&gt;
  return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Java===&lt;br /&gt;
Java doesn’t support multiple inheritance but provides a feature called interfaces to simulate inheritance from multiple classes. Interface is an abstract class which provides a generic template by just declaring variables and methods without implementing them. [http://www.brpreiss.com/books/opus4/html/page611.html Concrete classes] that inherit interfaces are supposed to implement its methods. The example written using Java interfaces is shown below.&lt;br /&gt;
&lt;br /&gt;
 interface Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface WingedAnimal extends Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();  &lt;br /&gt;
   void canFly();  &lt;br /&gt;
 }&lt;br /&gt;
 class ImplementWingedAnimal implements WingedAnimal  &lt;br /&gt;
 {&lt;br /&gt;
    public void canEat() {    &lt;br /&gt;
    }&lt;br /&gt;
    public void canFly() {    &lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Mammal extends Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();&lt;br /&gt;
   void canGiveBirth();&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementMammal implements Mammal&lt;br /&gt;
 {&lt;br /&gt;
    public void canEat() {    &lt;br /&gt;
    }&lt;br /&gt;
    public void canGiveBirth() {    &lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Bat extends WingedAnimal, Mammal&lt;br /&gt;
 {&lt;br /&gt;
    void canEat();    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class ImplementBat implements Bat&lt;br /&gt;
 {  &lt;br /&gt;
    ImplementWingedAnimal w = new ImplementWingedAnimal();&lt;br /&gt;
    ImplementMammal m = new ImplementMammal();     &lt;br /&gt;
    public void canEat() {            &lt;br /&gt;
      m.canEat();  &lt;br /&gt;
      w.canEat();      &lt;br /&gt;
    }&lt;br /&gt;
    public void canFly() {&lt;br /&gt;
    }&lt;br /&gt;
    public void canGiveBirth() {&lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class MultipleInheritance&lt;br /&gt;
 {&lt;br /&gt;
   public static void main(String args[])&lt;br /&gt;
   {&lt;br /&gt;
     ImplementBat b = new ImplementBat ();&lt;br /&gt;
     b.canEat();       &lt;br /&gt;
   } &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Ruby===&lt;br /&gt;
Ruby doesn't support multiple inheritance but provides a similar feature using modules and mixins.  &lt;br /&gt;
====Modules====&lt;br /&gt;
A Module is an encapsulation of methods and variables, although similar in function to a Class, differs from a class in the sense that it cannot be instantiated. Modules are  more dynamic and allow us to organize the code in a more granular way.Each module can add something to the existing class. A module can be thought of an encapsulation of a  subset of the full capabilities of an object.&lt;br /&gt;
An example found in the book ,The Well-Grounded Rubyist &amp;lt;ref&amp;gt;http://www.manning.com/black2/SampleChapter4.pdf [Pg.92] &amp;lt;/ref&amp;gt;, is the stack-like module. The module encapsulates the characteristics of being like a stack,implements the operations that can be performed by a stack but is not the actual Stack class. This allows us to use this module to mix-in  stack-like behaviour into other classes that require them.&lt;br /&gt;
&lt;br /&gt;
====Mixins====&lt;br /&gt;
A Mixin is a concept that allows inclusion of special characteristics into existing classes, so that common features that need to be reused in several classes can be selectively included, thus avoiding implementation of unnecessary functions like in the case of interfaces.&lt;br /&gt;
Mixins allow us to dynamically bind special features defined in modules, into core classes without really making any changes to the objects. This improves flexibility when we want to add plugins and extensions to the existing framework.&amp;lt;ref&amp;gt;http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-frameworks/&amp;lt;/ref&amp;gt;.The source code for the same example implemented using ruby mixins is shown below.&lt;br /&gt;
&lt;br /&gt;
 module Animal&lt;br /&gt;
   def canEat     &lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module WingedAnimal&lt;br /&gt;
   include Animal    &lt;br /&gt;
   def canEat&lt;br /&gt;
   end      &lt;br /&gt;
   def canFly&lt;br /&gt;
   end      &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Mammal&lt;br /&gt;
   include Animal&lt;br /&gt;
   def canEat&lt;br /&gt;
   end&lt;br /&gt;
   def canGiveBirth&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Bat&lt;br /&gt;
   include WingedAnimal&lt;br /&gt;
   include Mammal&lt;br /&gt;
   include Animal  &lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 Bat.new().canEat()&lt;br /&gt;
&lt;br /&gt;
===Multiple inheritance in other languages===&lt;br /&gt;
[http://www.scala-lang.org/ Scala] offers traits that are similar to Java interfaces to support multiple inheritance. Traits define a template with methods and variables which any class can make use of. The difference between interfaces and traits is that traits can implement methods. A Scala class can inherit from any number of traits together with a class in way to support multiple inheritance. Traits can be mixed in to a class even without inheriting it but just by including it at the time of creating instances. &amp;lt;ref&amp;gt;http://joelabrahamsson.com/entry/learning-scala-traits&amp;lt;/ref&amp;gt;. Trait can also extend a class and methods to be overridden are usually prefixed by the keyword override. The language enforces method names to be different across inherited  traits to avoid the issues of multiple inheritance. [http://www.python.org/ Python] supports a limited version of multiple inheritance by extending multiple classes in depth-first left-right order &amp;lt;ref&amp;gt;http://docs.python.org/tutorial/classes.html&amp;lt;/ref&amp;gt;. If a python class D inherits classes A, B, C and if an instance variable or method is not available in the current class, it checks in A and its base classes, then moves to B and its base classes and eventually to C thus moving from left to right. In this way, python avoids the diamond problem by making the search order linear by which the derived class accesses attributes of its parent classes. In addition python also defines mixins to access reusable features within a class.&lt;br /&gt;
&lt;br /&gt;
==Advantages of mixins==&lt;br /&gt;
1. Firstly, when a module gets mixed into different classes, each class acquires the same  reference  to the module and hence even dynamic changes to the module gets reflected in the behavior of the classes &amp;lt;ref&amp;gt;http://www.linuxtopia.org/online_books/programming_books/ruby_tutorial/Ruby_Modules_Mixins.html&amp;lt;/ref&amp;gt;. In this way, the module’s code is not just copied to individual classes as in static languages instead its reference is provided. &lt;br /&gt;
&lt;br /&gt;
2. Secondly, the mixed in state and behavior can be accessed by the instance of target class just like accessing its own features. &lt;br /&gt;
&lt;br /&gt;
3. Thirdly, a class can include any number of modules in a way it can simulate multiple inheritance without having the related issues.&lt;br /&gt;
&lt;br /&gt;
==Questions to Ponder==&lt;br /&gt;
===Do mixins solve all of the problems of multiple inheritance?===&lt;br /&gt;
Undoubtedly mixins solve some of the problems of multiple inheritance. There is no ambiguity in resolving the request to a common method implemented by multiple modules. In that case, the lastly included module is responsible for handling accesses to common methods. Though the order of precedence of included modules matters, it looks clearer than C++.  However languages like Scala and Eiffel implement multiple inheritance far more safely  by explicitly forcing the user to have different method names across inherited classes.&lt;br /&gt;
&lt;br /&gt;
===Are mixins a clear advantage over interfaces?===&lt;br /&gt;
Mixins are more advantageous than interfaces because of their features. Interfaces have some limitations. As seen from programs, the derived class Allrounder uses the instances of Batsman and Bowler classes (has-a relationship) to access respective attributes. Interface is nice in the sense it provides a generic template that could be implemented differently by different classes.  However the target class has to necessarily implement every method of the interface. This would pose problems to a Class which is interested in implementing specific features of the interface. On the contrary, Mixins eliminate this inconvenience by mixing in the state and behavior of the modules in the target class and allows a it to use them as it needs. Hence the target class can just use module’s features selectively or completely instead of implementing by itself. The size of multiple inheritance code using mixins is smaller than interfaces and C++ variants.&lt;br /&gt;
&lt;br /&gt;
===Do mixins have any disadvantages not shared by multiple inheritance or interfaces?===&lt;br /&gt;
Inspite of making the lives of programmers better with effective code reuse and avoiding unnecessary implementation of methods that cause clutter in the code, the concept of Mixins can easily be abused resulting in namespace pollution. This is caused when a class includes multiple modules each of which contains several hundred method implementations. Having hundreds of methods mixed into a single class, makes it difficult to identify the source modules of the methods, thereby causing confusion while implementing these methods, whereas in the case of multiple inheritance we can easily identify the class to which the method belongs.&lt;br /&gt;
Another disadvantage that follows from the namespace collision is the case where we have multiple methods with the same name in different modules.In such a case whenever a call to that method is made, the method which belongs to the module that was included last will be called. In the above example, the method call canEat() invokes Animal's canEat() method since Animal was the last included module. The problem with this is that, this takes place silently without any warning. This may result in accidentally inheriting wrong methods.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
In this article, we have discussed about multiple inheritance and means by which it is implemented in different languages. Though multiple inheritance is a useful feature,&lt;br /&gt;
many languages do not support them due to its associated issues. Given its advantages, mixin in ruby seems to be a viable option to achieve multiple inheritance. However mixins too have limitations which should be taken care of during implementation.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
==See Also==&lt;br /&gt;
1. Lucas Carlson and Leonard Richardson. 2006. [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=1212915&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 Ruby Cookbook] (Cookbooks (O'Reilly)). O'Reilly Media, Inc..&lt;br /&gt;
&lt;br /&gt;
2. Herbert Schildt. 2000. Java 2: [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=557816&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 The Complete Reference] (4th ed.). McGraw-Hill Professional.&lt;/div&gt;</summary>
		<author><name>Snallan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65449</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65449"/>
		<updated>2012-09-21T14:15:56Z</updated>

		<summary type="html">&lt;p&gt;Snallan: /* A Real World Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''1w23:  Multiple Inheritance and Mixins&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
The purpose of this article is to give a brief introduction about multiple inheritance, consider some of its issues and discuss techniques employed by different programming languages to handle such issues. In this relation, the article also compares solutions provided by different programming languages, especially against Ruby mixins, by listing their advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
Multiple Inheritance is a technique of establishing hierarchical relationships within a set of classes, such that a single class can inherit  behaviors from multiple parent classes. Although it might seem useful to include methods from multiple classes, the major issue with multiple inheritance is the ambiguity in referencing the parent class methods,  when more than one parent classes share a method with the same name.This ambiguity is predominant in cases where the inheritance hierarchy is more than one level deep, where the class in the lowermost level inherits from two classes, that have overridden the same method from their parent in different ways.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A Real World Example==&lt;br /&gt;
A known example of multiple inheritance to represent winged animals and mammals using classes and objects is cited here &amp;lt;ref&amp;gt; http://stackoverflow.com/questions/9359948/multiple-inheritance-whats-a-good-example&amp;lt;/ref&amp;gt; for the sake of understanding. A Winged animal and a mammal inherit primitive attributes of an animal. While a winged animal can fly; a mammal can breathe and reproduce; they both eat, move and share features generic to animals. A bat being a winged mammal possesses features common to both winged animal and mammal. It follows that class Bat inherits from classes WingedAnimal and Mammal and in turn classes WingedAnimal and Mammal inherit from class Animal.   In reference to this context, let us give our own implementation and explore the issues of multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
[[File:Class_Diagram.png|thumb|right|x400px|alt=Class Diagram for the example.]]&lt;br /&gt;
&lt;br /&gt;
==Issues of Multiple Inheritance==&lt;br /&gt;
1. Due to inheritance, the parent class’s objects get duplicated to child class. As a result, Class Bat duplicates Animal objects through each of WingedAnimal and Mammal classes. When Bat accesses any of the Animal's attributes, the compiler would be ambiguous about determining the correct path to use since two paths are available from Animal to Bat. This problem of multiple inheritance is called diamond problem &amp;lt;ref&amp;gt;http://www.artima.com/weblogs/viewpost.jsp?thread=246488&amp;lt;/ref&amp;gt;. Compiler takes arbitrary decisions during such situations. &lt;br /&gt;
&lt;br /&gt;
2.Objects of parent classes can be separately managed by using [http://en.wikipedia.org/wiki/Virtual_method_table virtual tables]as in C++. By explicitly specifying the virtual table pointer, the Bat class can access WingedAnimal and Mammal classes unambiguously. However virtual tables occupy significant portion of memory thereby limiting their extensive usage.  &lt;br /&gt;
&lt;br /&gt;
3. The order by which a child class inherits parent classes also matters. If Bat inherits WingedAnimal prior to Mammal, then WingedAnimal precedes Mammal and common methods are accessed in this order of precedence.  Hence if the Programmer is not aware of such precedence, debugging would be difficult.&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
Programming languages provide different solutions to handle the issues of multiple inheritance. Here we consider the techniques offered by C++, Java, Ruby, Scala and Python.    &lt;br /&gt;
===Multiple Inheritance in C++===&lt;br /&gt;
C++ supports multiple inheritance with the notion of virtual classes and virtual functions. In the example, the Animal class defines virtual functions with WingedAnimal and Mammal classes virtually inheriting the features of Animal. The Bat class can now unambiguously access canEat() method of Animal class, using virtual table pointers of WingedAnimal and Mammal. The example written using C++ virtual functions is shown below.&lt;br /&gt;
&lt;br /&gt;
 class Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void virtual canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class WingedAnimal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Mammal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Bat :public WingedAnimal, public Mammal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }  &lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
  int i = 0;&lt;br /&gt;
  Animal *instances[3];&lt;br /&gt;
  WingedAnimal w;&lt;br /&gt;
  Mammal m;&lt;br /&gt;
  Bat b;&lt;br /&gt;
  instances[0] = &amp;amp;w;&lt;br /&gt;
  instances[1] = &amp;amp;m;&lt;br /&gt;
  instances[2] = &amp;amp;b;&lt;br /&gt;
  while(i&amp;lt;3)&lt;br /&gt;
    instances[i++]-&amp;gt;canEat();&lt;br /&gt;
  return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Java===&lt;br /&gt;
Java doesn’t support multiple inheritance but provides a feature called interfaces to simulate inheritance from multiple classes. Interface is an abstract class which provides a generic template by just declaring variables and methods without implementing them. [http://www.brpreiss.com/books/opus4/html/page611.html Concrete classes] that inherit interfaces are supposed to implement its methods. The example written using Java interfaces is shown below.&lt;br /&gt;
&lt;br /&gt;
 interface Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface WingedAnimal extends Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();  &lt;br /&gt;
   void canFly();  &lt;br /&gt;
 }&lt;br /&gt;
 class ImplementWingedAnimal implements WingedAnimal  &lt;br /&gt;
 {&lt;br /&gt;
    public void canEat() {    &lt;br /&gt;
    }&lt;br /&gt;
    public void canFly() {    &lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Mammal extends Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();&lt;br /&gt;
   void canGiveBirth();&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementMammal implements Mammal&lt;br /&gt;
 {&lt;br /&gt;
    public void canEat() {    &lt;br /&gt;
    }&lt;br /&gt;
    public void canGiveBirth() {    &lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Bat extends WingedAnimal, Mammal&lt;br /&gt;
 {&lt;br /&gt;
    void canEat();    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class ImplementBat implements Bat&lt;br /&gt;
 {  &lt;br /&gt;
    ImplementWingedAnimal w = new ImplementWingedAnimal();&lt;br /&gt;
    ImplementMammal m = new ImplementMammal();     &lt;br /&gt;
    public void canEat() {            &lt;br /&gt;
      m.canEat();  &lt;br /&gt;
      w.canEat();      &lt;br /&gt;
    }&lt;br /&gt;
    public void canFly() {&lt;br /&gt;
    }&lt;br /&gt;
    public void canGiveBirth() {&lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class MultipleInheritance&lt;br /&gt;
 {&lt;br /&gt;
   public static void main(String args[])&lt;br /&gt;
   {&lt;br /&gt;
     ImplementBat b = new ImplementBat ();&lt;br /&gt;
     b.canEat();       &lt;br /&gt;
   } &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Ruby===&lt;br /&gt;
Ruby doesn't support multiple inheritance but provides a similar feature using modules and mixins.  &lt;br /&gt;
====Modules====&lt;br /&gt;
A Module is an encapsulation of methods and variables, although similar in function to a Class, differs from a class in the sense that it cannot be instantiated. Modules are  more dynamic and allow us to organize the code in a more granular way.Each module can add something to the existing class. A module can be thought of an encapsulation of a  subset of the full capabilities of an object.&lt;br /&gt;
An example found in the book ,The Well-Grounded Rubyist &amp;lt;ref&amp;gt;http://www.manning.com/black2/SampleChapter4.pdf [Pg.92] &amp;lt;/ref&amp;gt;, is the stack-like module. The module encapsulates the characteristics of being like a stack,implements the operations that can be performed by a stack but is not the actual Stack class. This allows us to use this module to mix-in  stack-like behaviour into other classes that require them.&lt;br /&gt;
&lt;br /&gt;
====Mixins====&lt;br /&gt;
A Mixin is a concept that allows inclusion of special characteristics into existing classes, so that common features that need to be reused in several classes can be selectively included, thus avoiding implementation of unnecessary functions like in the case of interfaces.&lt;br /&gt;
Mixins allow us to dynamically bind special features defined in modules, into core classes without really making any changes to the objects. This improves flexibility when we want to add plugins and extensions to the existing framework.&amp;lt;ref&amp;gt;http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-frameworks/&amp;lt;/ref&amp;gt;.The source code for the same example implemented using ruby mixins is shown below.&lt;br /&gt;
&lt;br /&gt;
 module Animal&lt;br /&gt;
   def canEat     &lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module WingedAnimal&lt;br /&gt;
   include Animal    &lt;br /&gt;
   def canEat&lt;br /&gt;
   end      &lt;br /&gt;
   def canFly&lt;br /&gt;
   end      &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Mammal&lt;br /&gt;
   include Animal&lt;br /&gt;
   def canEat&lt;br /&gt;
   end&lt;br /&gt;
   def canGiveBirth&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Bat&lt;br /&gt;
   include WingedAnimal&lt;br /&gt;
   include Mammal&lt;br /&gt;
   include Animal  &lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 Bat.new().canEat()&lt;br /&gt;
&lt;br /&gt;
===Multiple inheritance in other languages===&lt;br /&gt;
[http://www.scala-lang.org/ Scala] offers traits that are similar to Java interfaces to support multiple inheritance. Traits define a template with methods and variables which any class can make use of. The difference between interfaces and traits is that traits can implement methods. A Scala class can inherit from any number of traits together with a class in way to support multiple inheritance. Traits can be mixed in to a class even without inheriting it but just by including it at the time of creating instances. &amp;lt;ref&amp;gt;http://joelabrahamsson.com/entry/learning-scala-traits&amp;lt;/ref&amp;gt;. Trait can also extend a class and methods to be overridden are usually prefixed by the keyword override. The language enforces method names to be different across inherited  traits to avoid the issues of multiple inheritance. [http://www.python.org/ Python] supports a limited version of multiple inheritance by extending multiple classes in depth-first left-right order &amp;lt;ref&amp;gt;http://docs.python.org/tutorial/classes.html&amp;lt;/ref&amp;gt;. If a python class D inherits classes A, B, C and if an instance variable or method is not available in the current class, it checks in A and its base classes, then moves to B and its base classes and eventually to C thus moving from left to right. In this way, python avoids the diamond problem by making the search order linear by which the derived class accesses attributes of its parent classes. In addition python also defines mixins to access reusable features within a class.&lt;br /&gt;
&lt;br /&gt;
==Advantages of mixins==&lt;br /&gt;
1. Firstly, when a module gets mixed into different classes, each class acquires the same  reference  to the module and hence even dynamic changes to the module gets reflected in the behavior of the classes &amp;lt;ref&amp;gt;http://www.linuxtopia.org/online_books/programming_books/ruby_tutorial/Ruby_Modules_Mixins.html&amp;lt;/ref&amp;gt;. In this way, the module’s code is not just copied to individual classes as in static languages instead its reference is provided. &lt;br /&gt;
&lt;br /&gt;
2. Secondly, the mixed in state and behavior can be accessed by the instance of target class just like accessing its own features. &lt;br /&gt;
&lt;br /&gt;
3. Thirdly, a class can include any number of modules in a way it can simulate multiple inheritance without having the related issues.&lt;br /&gt;
&lt;br /&gt;
==Questions to Ponder==&lt;br /&gt;
===Do mixins solve all of the problems of multiple inheritance?===&lt;br /&gt;
Undoubtedly mixins solve some of the problems of multiple inheritance. There is no ambiguity in resolving the request to a common method implemented by multiple modules. In that case, the lastly included module is responsible for handling accesses to common methods. Though the order of precedence of included modules matters, it looks clearer than C++.  However languages like Scala and Eiffel implement multiple inheritance far more safely  by explicitly forcing the user to have different method names across inherited classes.&lt;br /&gt;
&lt;br /&gt;
===Are mixins a clear advantage over interfaces?===&lt;br /&gt;
Mixins are more advantageous than interfaces because of their features. Interfaces have some limitations. As seen from programs, the derived class Allrounder uses the instances of Batsman and Bowler classes (has-a relationship) to access respective attributes. Interface is nice in the sense it provides a generic template that could be implemented differently by different classes.  However the target class has to necessarily implement every method of the interface. This would pose problems to a Class which is interested in implementing specific features of the interface. On the contrary, Mixins eliminate this inconvenience by mixing in the state and behavior of the modules in the target class and allows a it to use them as it needs. Hence the target class can just use module’s features selectively or completely instead of implementing by itself. The size of multiple inheritance code using mixins is smaller than interfaces and C++ variants.&lt;br /&gt;
&lt;br /&gt;
===Do mixins have any disadvantages not shared by multiple inheritance or interfaces?===&lt;br /&gt;
Inspite of making the lives of programmers better with effective code reuse and avoiding unnecessary implementation of methods that cause clutter in the code, the concept of Mixins can easily be abused resulting in namespace pollution. This is caused when a class includes multiple modules each of which contains several hundred method implementations. Having hundreds of methods mixed into a single class, makes it difficult to identify the source modules of the methods, thereby causing confusion while implementing these methods, whereas in the case of multiple inheritance we can easily identify the class to which the method belongs.&lt;br /&gt;
Another disadvantage that follows from the namespace collision is the case where we have multiple methods with the same name in different modules.In such a case whenever a call to that method is made, the method which belongs to the module that was included last will be called. In the above example, the method call canEat() invokes Animal's canEat() method since Animal was the last included module. The problem with this is that, this takes place silently without any warning. This may result in accidentally inheriting wrong methods.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
In this article, we have discussed about multiple inheritance and means by which it is implemented in different languages. Though multiple inheritance is a useful feature,&lt;br /&gt;
many languages do not support them due to its associated issues. Given its advantages, mixin in ruby seems to be a viable option to achieve multiple inheritance. However mixins too have limitations which should be taken care of during implementation.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
==See Also==&lt;br /&gt;
1. Lucas Carlson and Leonard Richardson. 2006. [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=1212915&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 Ruby Cookbook] (Cookbooks (O'Reilly)). O'Reilly Media, Inc..&lt;br /&gt;
&lt;br /&gt;
2. Herbert Schildt. 2000. Java 2: [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=557816&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 The Complete Reference] (4th ed.). McGraw-Hill Professional.&lt;/div&gt;</summary>
		<author><name>Snallan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65448</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65448"/>
		<updated>2012-09-21T14:15:20Z</updated>

		<summary type="html">&lt;p&gt;Snallan: /* A Real World Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''1w23:  Multiple Inheritance and Mixins&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
The purpose of this article is to give a brief introduction about multiple inheritance, consider some of its issues and discuss techniques employed by different programming languages to handle such issues. In this relation, the article also compares solutions provided by different programming languages, especially against Ruby mixins, by listing their advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
Multiple Inheritance is a technique of establishing hierarchical relationships within a set of classes, such that a single class can inherit  behaviors from multiple parent classes. Although it might seem useful to include methods from multiple classes, the major issue with multiple inheritance is the ambiguity in referencing the parent class methods,  when more than one parent classes share a method with the same name.This ambiguity is predominant in cases where the inheritance hierarchy is more than one level deep, where the class in the lowermost level inherits from two classes, that have overridden the same method from their parent in different ways.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A Real World Example==&lt;br /&gt;
A known example of multiple inheritance to represent animals using classes and objects is cited here &amp;lt;ref&amp;gt; http://stackoverflow.com/questions/9359948/multiple-inheritance-whats-a-good-example&amp;lt;/ref&amp;gt; for the sake of understanding. A Winged animal and a mammal inherit primitive attributes of an animal. While a winged animal can fly; a mammal can breathe and reproduce; they both eat, move and share features generic to animals. A bat being a winged mammal possesses features common to both winged animal and mammal. It follows that class Bat inherits from classes WingedAnimal and Mammal and in turn classes WingedAnimal and Mammal inherit from class Animal.   In reference to this context, let us give our own implementation and explore the issues of multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
[[File:Class_Diagram.png|thumb|right|x400px|alt=Class Diagram for the example.]]&lt;br /&gt;
&lt;br /&gt;
==Issues of Multiple Inheritance==&lt;br /&gt;
1. Due to inheritance, the parent class’s objects get duplicated to child class. As a result, Class Bat duplicates Animal objects through each of WingedAnimal and Mammal classes. When Bat accesses any of the Animal's attributes, the compiler would be ambiguous about determining the correct path to use since two paths are available from Animal to Bat. This problem of multiple inheritance is called diamond problem &amp;lt;ref&amp;gt;http://www.artima.com/weblogs/viewpost.jsp?thread=246488&amp;lt;/ref&amp;gt;. Compiler takes arbitrary decisions during such situations. &lt;br /&gt;
&lt;br /&gt;
2.Objects of parent classes can be separately managed by using [http://en.wikipedia.org/wiki/Virtual_method_table virtual tables]as in C++. By explicitly specifying the virtual table pointer, the Bat class can access WingedAnimal and Mammal classes unambiguously. However virtual tables occupy significant portion of memory thereby limiting their extensive usage.  &lt;br /&gt;
&lt;br /&gt;
3. The order by which a child class inherits parent classes also matters. If Bat inherits WingedAnimal prior to Mammal, then WingedAnimal precedes Mammal and common methods are accessed in this order of precedence.  Hence if the Programmer is not aware of such precedence, debugging would be difficult.&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
Programming languages provide different solutions to handle the issues of multiple inheritance. Here we consider the techniques offered by C++, Java, Ruby, Scala and Python.    &lt;br /&gt;
===Multiple Inheritance in C++===&lt;br /&gt;
C++ supports multiple inheritance with the notion of virtual classes and virtual functions. In the example, the Animal class defines virtual functions with WingedAnimal and Mammal classes virtually inheriting the features of Animal. The Bat class can now unambiguously access canEat() method of Animal class, using virtual table pointers of WingedAnimal and Mammal. The example written using C++ virtual functions is shown below.&lt;br /&gt;
&lt;br /&gt;
 class Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void virtual canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class WingedAnimal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Mammal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Bat :public WingedAnimal, public Mammal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }  &lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
  int i = 0;&lt;br /&gt;
  Animal *instances[3];&lt;br /&gt;
  WingedAnimal w;&lt;br /&gt;
  Mammal m;&lt;br /&gt;
  Bat b;&lt;br /&gt;
  instances[0] = &amp;amp;w;&lt;br /&gt;
  instances[1] = &amp;amp;m;&lt;br /&gt;
  instances[2] = &amp;amp;b;&lt;br /&gt;
  while(i&amp;lt;3)&lt;br /&gt;
    instances[i++]-&amp;gt;canEat();&lt;br /&gt;
  return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Java===&lt;br /&gt;
Java doesn’t support multiple inheritance but provides a feature called interfaces to simulate inheritance from multiple classes. Interface is an abstract class which provides a generic template by just declaring variables and methods without implementing them. [http://www.brpreiss.com/books/opus4/html/page611.html Concrete classes] that inherit interfaces are supposed to implement its methods. The example written using Java interfaces is shown below.&lt;br /&gt;
&lt;br /&gt;
 interface Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface WingedAnimal extends Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();  &lt;br /&gt;
   void canFly();  &lt;br /&gt;
 }&lt;br /&gt;
 class ImplementWingedAnimal implements WingedAnimal  &lt;br /&gt;
 {&lt;br /&gt;
    public void canEat() {    &lt;br /&gt;
    }&lt;br /&gt;
    public void canFly() {    &lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Mammal extends Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();&lt;br /&gt;
   void canGiveBirth();&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementMammal implements Mammal&lt;br /&gt;
 {&lt;br /&gt;
    public void canEat() {    &lt;br /&gt;
    }&lt;br /&gt;
    public void canGiveBirth() {    &lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Bat extends WingedAnimal, Mammal&lt;br /&gt;
 {&lt;br /&gt;
    void canEat();    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class ImplementBat implements Bat&lt;br /&gt;
 {  &lt;br /&gt;
    ImplementWingedAnimal w = new ImplementWingedAnimal();&lt;br /&gt;
    ImplementMammal m = new ImplementMammal();     &lt;br /&gt;
    public void canEat() {            &lt;br /&gt;
      m.canEat();  &lt;br /&gt;
      w.canEat();      &lt;br /&gt;
    }&lt;br /&gt;
    public void canFly() {&lt;br /&gt;
    }&lt;br /&gt;
    public void canGiveBirth() {&lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class MultipleInheritance&lt;br /&gt;
 {&lt;br /&gt;
   public static void main(String args[])&lt;br /&gt;
   {&lt;br /&gt;
     ImplementBat b = new ImplementBat ();&lt;br /&gt;
     b.canEat();       &lt;br /&gt;
   } &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Ruby===&lt;br /&gt;
Ruby doesn't support multiple inheritance but provides a similar feature using modules and mixins.  &lt;br /&gt;
====Modules====&lt;br /&gt;
A Module is an encapsulation of methods and variables, although similar in function to a Class, differs from a class in the sense that it cannot be instantiated. Modules are  more dynamic and allow us to organize the code in a more granular way.Each module can add something to the existing class. A module can be thought of an encapsulation of a  subset of the full capabilities of an object.&lt;br /&gt;
An example found in the book ,The Well-Grounded Rubyist &amp;lt;ref&amp;gt;http://www.manning.com/black2/SampleChapter4.pdf [Pg.92] &amp;lt;/ref&amp;gt;, is the stack-like module. The module encapsulates the characteristics of being like a stack,implements the operations that can be performed by a stack but is not the actual Stack class. This allows us to use this module to mix-in  stack-like behaviour into other classes that require them.&lt;br /&gt;
&lt;br /&gt;
====Mixins====&lt;br /&gt;
A Mixin is a concept that allows inclusion of special characteristics into existing classes, so that common features that need to be reused in several classes can be selectively included, thus avoiding implementation of unnecessary functions like in the case of interfaces.&lt;br /&gt;
Mixins allow us to dynamically bind special features defined in modules, into core classes without really making any changes to the objects. This improves flexibility when we want to add plugins and extensions to the existing framework.&amp;lt;ref&amp;gt;http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-frameworks/&amp;lt;/ref&amp;gt;.The source code for the same example implemented using ruby mixins is shown below.&lt;br /&gt;
&lt;br /&gt;
 module Animal&lt;br /&gt;
   def canEat     &lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module WingedAnimal&lt;br /&gt;
   include Animal    &lt;br /&gt;
   def canEat&lt;br /&gt;
   end      &lt;br /&gt;
   def canFly&lt;br /&gt;
   end      &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Mammal&lt;br /&gt;
   include Animal&lt;br /&gt;
   def canEat&lt;br /&gt;
   end&lt;br /&gt;
   def canGiveBirth&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Bat&lt;br /&gt;
   include WingedAnimal&lt;br /&gt;
   include Mammal&lt;br /&gt;
   include Animal  &lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 Bat.new().canEat()&lt;br /&gt;
&lt;br /&gt;
===Multiple inheritance in other languages===&lt;br /&gt;
[http://www.scala-lang.org/ Scala] offers traits that are similar to Java interfaces to support multiple inheritance. Traits define a template with methods and variables which any class can make use of. The difference between interfaces and traits is that traits can implement methods. A Scala class can inherit from any number of traits together with a class in way to support multiple inheritance. Traits can be mixed in to a class even without inheriting it but just by including it at the time of creating instances. &amp;lt;ref&amp;gt;http://joelabrahamsson.com/entry/learning-scala-traits&amp;lt;/ref&amp;gt;. Trait can also extend a class and methods to be overridden are usually prefixed by the keyword override. The language enforces method names to be different across inherited  traits to avoid the issues of multiple inheritance. [http://www.python.org/ Python] supports a limited version of multiple inheritance by extending multiple classes in depth-first left-right order &amp;lt;ref&amp;gt;http://docs.python.org/tutorial/classes.html&amp;lt;/ref&amp;gt;. If a python class D inherits classes A, B, C and if an instance variable or method is not available in the current class, it checks in A and its base classes, then moves to B and its base classes and eventually to C thus moving from left to right. In this way, python avoids the diamond problem by making the search order linear by which the derived class accesses attributes of its parent classes. In addition python also defines mixins to access reusable features within a class.&lt;br /&gt;
&lt;br /&gt;
==Advantages of mixins==&lt;br /&gt;
1. Firstly, when a module gets mixed into different classes, each class acquires the same  reference  to the module and hence even dynamic changes to the module gets reflected in the behavior of the classes &amp;lt;ref&amp;gt;http://www.linuxtopia.org/online_books/programming_books/ruby_tutorial/Ruby_Modules_Mixins.html&amp;lt;/ref&amp;gt;. In this way, the module’s code is not just copied to individual classes as in static languages instead its reference is provided. &lt;br /&gt;
&lt;br /&gt;
2. Secondly, the mixed in state and behavior can be accessed by the instance of target class just like accessing its own features. &lt;br /&gt;
&lt;br /&gt;
3. Thirdly, a class can include any number of modules in a way it can simulate multiple inheritance without having the related issues.&lt;br /&gt;
&lt;br /&gt;
==Questions to Ponder==&lt;br /&gt;
===Do mixins solve all of the problems of multiple inheritance?===&lt;br /&gt;
Undoubtedly mixins solve some of the problems of multiple inheritance. There is no ambiguity in resolving the request to a common method implemented by multiple modules. In that case, the lastly included module is responsible for handling accesses to common methods. Though the order of precedence of included modules matters, it looks clearer than C++.  However languages like Scala and Eiffel implement multiple inheritance far more safely  by explicitly forcing the user to have different method names across inherited classes.&lt;br /&gt;
&lt;br /&gt;
===Are mixins a clear advantage over interfaces?===&lt;br /&gt;
Mixins are more advantageous than interfaces because of their features. Interfaces have some limitations. As seen from programs, the derived class Allrounder uses the instances of Batsman and Bowler classes (has-a relationship) to access respective attributes. Interface is nice in the sense it provides a generic template that could be implemented differently by different classes.  However the target class has to necessarily implement every method of the interface. This would pose problems to a Class which is interested in implementing specific features of the interface. On the contrary, Mixins eliminate this inconvenience by mixing in the state and behavior of the modules in the target class and allows a it to use them as it needs. Hence the target class can just use module’s features selectively or completely instead of implementing by itself. The size of multiple inheritance code using mixins is smaller than interfaces and C++ variants.&lt;br /&gt;
&lt;br /&gt;
===Do mixins have any disadvantages not shared by multiple inheritance or interfaces?===&lt;br /&gt;
Inspite of making the lives of programmers better with effective code reuse and avoiding unnecessary implementation of methods that cause clutter in the code, the concept of Mixins can easily be abused resulting in namespace pollution. This is caused when a class includes multiple modules each of which contains several hundred method implementations. Having hundreds of methods mixed into a single class, makes it difficult to identify the source modules of the methods, thereby causing confusion while implementing these methods, whereas in the case of multiple inheritance we can easily identify the class to which the method belongs.&lt;br /&gt;
Another disadvantage that follows from the namespace collision is the case where we have multiple methods with the same name in different modules.In such a case whenever a call to that method is made, the method which belongs to the module that was included last will be called. In the above example, the method call canEat() invokes Animal's canEat() method since Animal was the last included module. The problem with this is that, this takes place silently without any warning. This may result in accidentally inheriting wrong methods.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
In this article, we have discussed about multiple inheritance and means by which it is implemented in different languages. Though multiple inheritance is a useful feature,&lt;br /&gt;
many languages do not support them due to its associated issues. Given its advantages, mixin in ruby seems to be a viable option to achieve multiple inheritance. However mixins too have limitations which should be taken care of during implementation.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
==See Also==&lt;br /&gt;
1. Lucas Carlson and Leonard Richardson. 2006. [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=1212915&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 Ruby Cookbook] (Cookbooks (O'Reilly)). O'Reilly Media, Inc..&lt;br /&gt;
&lt;br /&gt;
2. Herbert Schildt. 2000. Java 2: [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=557816&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 The Complete Reference] (4th ed.). McGraw-Hill Professional.&lt;/div&gt;</summary>
		<author><name>Snallan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65447</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65447"/>
		<updated>2012-09-21T14:14:50Z</updated>

		<summary type="html">&lt;p&gt;Snallan: /* A Real World Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''1w23:  Multiple Inheritance and Mixins&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
The purpose of this article is to give a brief introduction about multiple inheritance, consider some of its issues and discuss techniques employed by different programming languages to handle such issues. In this relation, the article also compares solutions provided by different programming languages, especially against Ruby mixins, by listing their advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
Multiple Inheritance is a technique of establishing hierarchical relationships within a set of classes, such that a single class can inherit  behaviors from multiple parent classes. Although it might seem useful to include methods from multiple classes, the major issue with multiple inheritance is the ambiguity in referencing the parent class methods,  when more than one parent classes share a method with the same name.This ambiguity is predominant in cases where the inheritance hierarchy is more than one level deep, where the class in the lowermost level inherits from two classes, that have overridden the same method from their parent in different ways.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A Real World Example==&lt;br /&gt;
A known example of multiple inheritance to represent animals using classes and objects is cited here for the sake of understanding &amp;lt;ref&amp;gt; http://stackoverflow.com/questions/9359948/multiple-inheritance-whats-a-good-example&amp;lt;/ref&amp;gt;. A Winged animal and a mammal inherit primitive attributes of an animal. While a winged animal can fly; a mammal can breathe and reproduce; they both eat, move and share features generic to animals. A bat being a winged mammal possesses features common to both winged animal and mammal. It follows that class Bat inherits from classes WingedAnimal and Mammal and in turn classes WingedAnimal and Mammal inherit from class Animal.   In reference to this context, let us give our own implementation and explore the issues of multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
[[File:Class_Diagram.png|thumb|right|x400px|alt=Class Diagram for the example.]]&lt;br /&gt;
&lt;br /&gt;
==Issues of Multiple Inheritance==&lt;br /&gt;
1. Due to inheritance, the parent class’s objects get duplicated to child class. As a result, Class Bat duplicates Animal objects through each of WingedAnimal and Mammal classes. When Bat accesses any of the Animal's attributes, the compiler would be ambiguous about determining the correct path to use since two paths are available from Animal to Bat. This problem of multiple inheritance is called diamond problem &amp;lt;ref&amp;gt;http://www.artima.com/weblogs/viewpost.jsp?thread=246488&amp;lt;/ref&amp;gt;. Compiler takes arbitrary decisions during such situations. &lt;br /&gt;
&lt;br /&gt;
2.Objects of parent classes can be separately managed by using [http://en.wikipedia.org/wiki/Virtual_method_table virtual tables]as in C++. By explicitly specifying the virtual table pointer, the Bat class can access WingedAnimal and Mammal classes unambiguously. However virtual tables occupy significant portion of memory thereby limiting their extensive usage.  &lt;br /&gt;
&lt;br /&gt;
3. The order by which a child class inherits parent classes also matters. If Bat inherits WingedAnimal prior to Mammal, then WingedAnimal precedes Mammal and common methods are accessed in this order of precedence.  Hence if the Programmer is not aware of such precedence, debugging would be difficult.&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
Programming languages provide different solutions to handle the issues of multiple inheritance. Here we consider the techniques offered by C++, Java, Ruby, Scala and Python.    &lt;br /&gt;
===Multiple Inheritance in C++===&lt;br /&gt;
C++ supports multiple inheritance with the notion of virtual classes and virtual functions. In the example, the Animal class defines virtual functions with WingedAnimal and Mammal classes virtually inheriting the features of Animal. The Bat class can now unambiguously access canEat() method of Animal class, using virtual table pointers of WingedAnimal and Mammal. The example written using C++ virtual functions is shown below.&lt;br /&gt;
&lt;br /&gt;
 class Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void virtual canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class WingedAnimal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Mammal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Bat :public WingedAnimal, public Mammal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }  &lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
  int i = 0;&lt;br /&gt;
  Animal *instances[3];&lt;br /&gt;
  WingedAnimal w;&lt;br /&gt;
  Mammal m;&lt;br /&gt;
  Bat b;&lt;br /&gt;
  instances[0] = &amp;amp;w;&lt;br /&gt;
  instances[1] = &amp;amp;m;&lt;br /&gt;
  instances[2] = &amp;amp;b;&lt;br /&gt;
  while(i&amp;lt;3)&lt;br /&gt;
    instances[i++]-&amp;gt;canEat();&lt;br /&gt;
  return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Java===&lt;br /&gt;
Java doesn’t support multiple inheritance but provides a feature called interfaces to simulate inheritance from multiple classes. Interface is an abstract class which provides a generic template by just declaring variables and methods without implementing them. [http://www.brpreiss.com/books/opus4/html/page611.html Concrete classes] that inherit interfaces are supposed to implement its methods. The example written using Java interfaces is shown below.&lt;br /&gt;
&lt;br /&gt;
 interface Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface WingedAnimal extends Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();  &lt;br /&gt;
   void canFly();  &lt;br /&gt;
 }&lt;br /&gt;
 class ImplementWingedAnimal implements WingedAnimal  &lt;br /&gt;
 {&lt;br /&gt;
    public void canEat() {    &lt;br /&gt;
    }&lt;br /&gt;
    public void canFly() {    &lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Mammal extends Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();&lt;br /&gt;
   void canGiveBirth();&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementMammal implements Mammal&lt;br /&gt;
 {&lt;br /&gt;
    public void canEat() {    &lt;br /&gt;
    }&lt;br /&gt;
    public void canGiveBirth() {    &lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Bat extends WingedAnimal, Mammal&lt;br /&gt;
 {&lt;br /&gt;
    void canEat();    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class ImplementBat implements Bat&lt;br /&gt;
 {  &lt;br /&gt;
    ImplementWingedAnimal w = new ImplementWingedAnimal();&lt;br /&gt;
    ImplementMammal m = new ImplementMammal();     &lt;br /&gt;
    public void canEat() {            &lt;br /&gt;
      m.canEat();  &lt;br /&gt;
      w.canEat();      &lt;br /&gt;
    }&lt;br /&gt;
    public void canFly() {&lt;br /&gt;
    }&lt;br /&gt;
    public void canGiveBirth() {&lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class MultipleInheritance&lt;br /&gt;
 {&lt;br /&gt;
   public static void main(String args[])&lt;br /&gt;
   {&lt;br /&gt;
     ImplementBat b = new ImplementBat ();&lt;br /&gt;
     b.canEat();       &lt;br /&gt;
   } &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Ruby===&lt;br /&gt;
Ruby doesn't support multiple inheritance but provides a similar feature using modules and mixins.  &lt;br /&gt;
====Modules====&lt;br /&gt;
A Module is an encapsulation of methods and variables, although similar in function to a Class, differs from a class in the sense that it cannot be instantiated. Modules are  more dynamic and allow us to organize the code in a more granular way.Each module can add something to the existing class. A module can be thought of an encapsulation of a  subset of the full capabilities of an object.&lt;br /&gt;
An example found in the book ,The Well-Grounded Rubyist &amp;lt;ref&amp;gt;http://www.manning.com/black2/SampleChapter4.pdf [Pg.92] &amp;lt;/ref&amp;gt;, is the stack-like module. The module encapsulates the characteristics of being like a stack,implements the operations that can be performed by a stack but is not the actual Stack class. This allows us to use this module to mix-in  stack-like behaviour into other classes that require them.&lt;br /&gt;
&lt;br /&gt;
====Mixins====&lt;br /&gt;
A Mixin is a concept that allows inclusion of special characteristics into existing classes, so that common features that need to be reused in several classes can be selectively included, thus avoiding implementation of unnecessary functions like in the case of interfaces.&lt;br /&gt;
Mixins allow us to dynamically bind special features defined in modules, into core classes without really making any changes to the objects. This improves flexibility when we want to add plugins and extensions to the existing framework.&amp;lt;ref&amp;gt;http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-frameworks/&amp;lt;/ref&amp;gt;.The source code for the same example implemented using ruby mixins is shown below.&lt;br /&gt;
&lt;br /&gt;
 module Animal&lt;br /&gt;
   def canEat     &lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module WingedAnimal&lt;br /&gt;
   include Animal    &lt;br /&gt;
   def canEat&lt;br /&gt;
   end      &lt;br /&gt;
   def canFly&lt;br /&gt;
   end      &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Mammal&lt;br /&gt;
   include Animal&lt;br /&gt;
   def canEat&lt;br /&gt;
   end&lt;br /&gt;
   def canGiveBirth&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Bat&lt;br /&gt;
   include WingedAnimal&lt;br /&gt;
   include Mammal&lt;br /&gt;
   include Animal  &lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 Bat.new().canEat()&lt;br /&gt;
&lt;br /&gt;
===Multiple inheritance in other languages===&lt;br /&gt;
[http://www.scala-lang.org/ Scala] offers traits that are similar to Java interfaces to support multiple inheritance. Traits define a template with methods and variables which any class can make use of. The difference between interfaces and traits is that traits can implement methods. A Scala class can inherit from any number of traits together with a class in way to support multiple inheritance. Traits can be mixed in to a class even without inheriting it but just by including it at the time of creating instances. &amp;lt;ref&amp;gt;http://joelabrahamsson.com/entry/learning-scala-traits&amp;lt;/ref&amp;gt;. Trait can also extend a class and methods to be overridden are usually prefixed by the keyword override. The language enforces method names to be different across inherited  traits to avoid the issues of multiple inheritance. [http://www.python.org/ Python] supports a limited version of multiple inheritance by extending multiple classes in depth-first left-right order &amp;lt;ref&amp;gt;http://docs.python.org/tutorial/classes.html&amp;lt;/ref&amp;gt;. If a python class D inherits classes A, B, C and if an instance variable or method is not available in the current class, it checks in A and its base classes, then moves to B and its base classes and eventually to C thus moving from left to right. In this way, python avoids the diamond problem by making the search order linear by which the derived class accesses attributes of its parent classes. In addition python also defines mixins to access reusable features within a class.&lt;br /&gt;
&lt;br /&gt;
==Advantages of mixins==&lt;br /&gt;
1. Firstly, when a module gets mixed into different classes, each class acquires the same  reference  to the module and hence even dynamic changes to the module gets reflected in the behavior of the classes &amp;lt;ref&amp;gt;http://www.linuxtopia.org/online_books/programming_books/ruby_tutorial/Ruby_Modules_Mixins.html&amp;lt;/ref&amp;gt;. In this way, the module’s code is not just copied to individual classes as in static languages instead its reference is provided. &lt;br /&gt;
&lt;br /&gt;
2. Secondly, the mixed in state and behavior can be accessed by the instance of target class just like accessing its own features. &lt;br /&gt;
&lt;br /&gt;
3. Thirdly, a class can include any number of modules in a way it can simulate multiple inheritance without having the related issues.&lt;br /&gt;
&lt;br /&gt;
==Questions to Ponder==&lt;br /&gt;
===Do mixins solve all of the problems of multiple inheritance?===&lt;br /&gt;
Undoubtedly mixins solve some of the problems of multiple inheritance. There is no ambiguity in resolving the request to a common method implemented by multiple modules. In that case, the lastly included module is responsible for handling accesses to common methods. Though the order of precedence of included modules matters, it looks clearer than C++.  However languages like Scala and Eiffel implement multiple inheritance far more safely  by explicitly forcing the user to have different method names across inherited classes.&lt;br /&gt;
&lt;br /&gt;
===Are mixins a clear advantage over interfaces?===&lt;br /&gt;
Mixins are more advantageous than interfaces because of their features. Interfaces have some limitations. As seen from programs, the derived class Allrounder uses the instances of Batsman and Bowler classes (has-a relationship) to access respective attributes. Interface is nice in the sense it provides a generic template that could be implemented differently by different classes.  However the target class has to necessarily implement every method of the interface. This would pose problems to a Class which is interested in implementing specific features of the interface. On the contrary, Mixins eliminate this inconvenience by mixing in the state and behavior of the modules in the target class and allows a it to use them as it needs. Hence the target class can just use module’s features selectively or completely instead of implementing by itself. The size of multiple inheritance code using mixins is smaller than interfaces and C++ variants.&lt;br /&gt;
&lt;br /&gt;
===Do mixins have any disadvantages not shared by multiple inheritance or interfaces?===&lt;br /&gt;
Inspite of making the lives of programmers better with effective code reuse and avoiding unnecessary implementation of methods that cause clutter in the code, the concept of Mixins can easily be abused resulting in namespace pollution. This is caused when a class includes multiple modules each of which contains several hundred method implementations. Having hundreds of methods mixed into a single class, makes it difficult to identify the source modules of the methods, thereby causing confusion while implementing these methods, whereas in the case of multiple inheritance we can easily identify the class to which the method belongs.&lt;br /&gt;
Another disadvantage that follows from the namespace collision is the case where we have multiple methods with the same name in different modules.In such a case whenever a call to that method is made, the method which belongs to the module that was included last will be called. In the above example, the method call canEat() invokes Animal's canEat() method since Animal was the last included module. The problem with this is that, this takes place silently without any warning. This may result in accidentally inheriting wrong methods.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
In this article, we have discussed about multiple inheritance and means by which it is implemented in different languages. Though multiple inheritance is a useful feature,&lt;br /&gt;
many languages do not support them due to its associated issues. Given its advantages, mixin in ruby seems to be a viable option to achieve multiple inheritance. However mixins too have limitations which should be taken care of during implementation.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
==See Also==&lt;br /&gt;
1. Lucas Carlson and Leonard Richardson. 2006. [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=1212915&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 Ruby Cookbook] (Cookbooks (O'Reilly)). O'Reilly Media, Inc..&lt;br /&gt;
&lt;br /&gt;
2. Herbert Schildt. 2000. Java 2: [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=557816&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 The Complete Reference] (4th ed.). McGraw-Hill Professional.&lt;/div&gt;</summary>
		<author><name>Snallan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65446</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65446"/>
		<updated>2012-09-21T14:14:37Z</updated>

		<summary type="html">&lt;p&gt;Snallan: /* A Real World Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''1w23:  Multiple Inheritance and Mixins&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
The purpose of this article is to give a brief introduction about multiple inheritance, consider some of its issues and discuss techniques employed by different programming languages to handle such issues. In this relation, the article also compares solutions provided by different programming languages, especially against Ruby mixins, by listing their advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
Multiple Inheritance is a technique of establishing hierarchical relationships within a set of classes, such that a single class can inherit  behaviors from multiple parent classes. Although it might seem useful to include methods from multiple classes, the major issue with multiple inheritance is the ambiguity in referencing the parent class methods,  when more than one parent classes share a method with the same name.This ambiguity is predominant in cases where the inheritance hierarchy is more than one level deep, where the class in the lowermost level inherits from two classes, that have overridden the same method from their parent in different ways.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A Real World Example==&lt;br /&gt;
A known example of multiple inheritance to represent animals using classes and objects is cited her for the sake of understanding &amp;lt;ref&amp;gt; http://stackoverflow.com/questions/9359948/multiple-inheritance-whats-a-good-example&amp;lt;/ref&amp;gt;. A Winged animal and a mammal inherit primitive attributes of an animal. While a winged animal can fly; a mammal can breathe and reproduce; they both eat, move and share features generic to animals. A bat being a winged mammal possesses features common to both winged animal and mammal. It follows that class Bat inherits from classes WingedAnimal and Mammal and in turn classes WingedAnimal and Mammal inherit from class Animal.   In reference to this context, let us give our own implementation and explore the issues of multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
[[File:Class_Diagram.png|thumb|right|x400px|alt=Class Diagram for the example.]]&lt;br /&gt;
&lt;br /&gt;
==Issues of Multiple Inheritance==&lt;br /&gt;
1. Due to inheritance, the parent class’s objects get duplicated to child class. As a result, Class Bat duplicates Animal objects through each of WingedAnimal and Mammal classes. When Bat accesses any of the Animal's attributes, the compiler would be ambiguous about determining the correct path to use since two paths are available from Animal to Bat. This problem of multiple inheritance is called diamond problem &amp;lt;ref&amp;gt;http://www.artima.com/weblogs/viewpost.jsp?thread=246488&amp;lt;/ref&amp;gt;. Compiler takes arbitrary decisions during such situations. &lt;br /&gt;
&lt;br /&gt;
2.Objects of parent classes can be separately managed by using [http://en.wikipedia.org/wiki/Virtual_method_table virtual tables]as in C++. By explicitly specifying the virtual table pointer, the Bat class can access WingedAnimal and Mammal classes unambiguously. However virtual tables occupy significant portion of memory thereby limiting their extensive usage.  &lt;br /&gt;
&lt;br /&gt;
3. The order by which a child class inherits parent classes also matters. If Bat inherits WingedAnimal prior to Mammal, then WingedAnimal precedes Mammal and common methods are accessed in this order of precedence.  Hence if the Programmer is not aware of such precedence, debugging would be difficult.&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
Programming languages provide different solutions to handle the issues of multiple inheritance. Here we consider the techniques offered by C++, Java, Ruby, Scala and Python.    &lt;br /&gt;
===Multiple Inheritance in C++===&lt;br /&gt;
C++ supports multiple inheritance with the notion of virtual classes and virtual functions. In the example, the Animal class defines virtual functions with WingedAnimal and Mammal classes virtually inheriting the features of Animal. The Bat class can now unambiguously access canEat() method of Animal class, using virtual table pointers of WingedAnimal and Mammal. The example written using C++ virtual functions is shown below.&lt;br /&gt;
&lt;br /&gt;
 class Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void virtual canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class WingedAnimal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Mammal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Bat :public WingedAnimal, public Mammal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }  &lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
  int i = 0;&lt;br /&gt;
  Animal *instances[3];&lt;br /&gt;
  WingedAnimal w;&lt;br /&gt;
  Mammal m;&lt;br /&gt;
  Bat b;&lt;br /&gt;
  instances[0] = &amp;amp;w;&lt;br /&gt;
  instances[1] = &amp;amp;m;&lt;br /&gt;
  instances[2] = &amp;amp;b;&lt;br /&gt;
  while(i&amp;lt;3)&lt;br /&gt;
    instances[i++]-&amp;gt;canEat();&lt;br /&gt;
  return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Java===&lt;br /&gt;
Java doesn’t support multiple inheritance but provides a feature called interfaces to simulate inheritance from multiple classes. Interface is an abstract class which provides a generic template by just declaring variables and methods without implementing them. [http://www.brpreiss.com/books/opus4/html/page611.html Concrete classes] that inherit interfaces are supposed to implement its methods. The example written using Java interfaces is shown below.&lt;br /&gt;
&lt;br /&gt;
 interface Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface WingedAnimal extends Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();  &lt;br /&gt;
   void canFly();  &lt;br /&gt;
 }&lt;br /&gt;
 class ImplementWingedAnimal implements WingedAnimal  &lt;br /&gt;
 {&lt;br /&gt;
    public void canEat() {    &lt;br /&gt;
    }&lt;br /&gt;
    public void canFly() {    &lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Mammal extends Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();&lt;br /&gt;
   void canGiveBirth();&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementMammal implements Mammal&lt;br /&gt;
 {&lt;br /&gt;
    public void canEat() {    &lt;br /&gt;
    }&lt;br /&gt;
    public void canGiveBirth() {    &lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Bat extends WingedAnimal, Mammal&lt;br /&gt;
 {&lt;br /&gt;
    void canEat();    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class ImplementBat implements Bat&lt;br /&gt;
 {  &lt;br /&gt;
    ImplementWingedAnimal w = new ImplementWingedAnimal();&lt;br /&gt;
    ImplementMammal m = new ImplementMammal();     &lt;br /&gt;
    public void canEat() {            &lt;br /&gt;
      m.canEat();  &lt;br /&gt;
      w.canEat();      &lt;br /&gt;
    }&lt;br /&gt;
    public void canFly() {&lt;br /&gt;
    }&lt;br /&gt;
    public void canGiveBirth() {&lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class MultipleInheritance&lt;br /&gt;
 {&lt;br /&gt;
   public static void main(String args[])&lt;br /&gt;
   {&lt;br /&gt;
     ImplementBat b = new ImplementBat ();&lt;br /&gt;
     b.canEat();       &lt;br /&gt;
   } &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Ruby===&lt;br /&gt;
Ruby doesn't support multiple inheritance but provides a similar feature using modules and mixins.  &lt;br /&gt;
====Modules====&lt;br /&gt;
A Module is an encapsulation of methods and variables, although similar in function to a Class, differs from a class in the sense that it cannot be instantiated. Modules are  more dynamic and allow us to organize the code in a more granular way.Each module can add something to the existing class. A module can be thought of an encapsulation of a  subset of the full capabilities of an object.&lt;br /&gt;
An example found in the book ,The Well-Grounded Rubyist &amp;lt;ref&amp;gt;http://www.manning.com/black2/SampleChapter4.pdf [Pg.92] &amp;lt;/ref&amp;gt;, is the stack-like module. The module encapsulates the characteristics of being like a stack,implements the operations that can be performed by a stack but is not the actual Stack class. This allows us to use this module to mix-in  stack-like behaviour into other classes that require them.&lt;br /&gt;
&lt;br /&gt;
====Mixins====&lt;br /&gt;
A Mixin is a concept that allows inclusion of special characteristics into existing classes, so that common features that need to be reused in several classes can be selectively included, thus avoiding implementation of unnecessary functions like in the case of interfaces.&lt;br /&gt;
Mixins allow us to dynamically bind special features defined in modules, into core classes without really making any changes to the objects. This improves flexibility when we want to add plugins and extensions to the existing framework.&amp;lt;ref&amp;gt;http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-frameworks/&amp;lt;/ref&amp;gt;.The source code for the same example implemented using ruby mixins is shown below.&lt;br /&gt;
&lt;br /&gt;
 module Animal&lt;br /&gt;
   def canEat     &lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module WingedAnimal&lt;br /&gt;
   include Animal    &lt;br /&gt;
   def canEat&lt;br /&gt;
   end      &lt;br /&gt;
   def canFly&lt;br /&gt;
   end      &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Mammal&lt;br /&gt;
   include Animal&lt;br /&gt;
   def canEat&lt;br /&gt;
   end&lt;br /&gt;
   def canGiveBirth&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Bat&lt;br /&gt;
   include WingedAnimal&lt;br /&gt;
   include Mammal&lt;br /&gt;
   include Animal  &lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 Bat.new().canEat()&lt;br /&gt;
&lt;br /&gt;
===Multiple inheritance in other languages===&lt;br /&gt;
[http://www.scala-lang.org/ Scala] offers traits that are similar to Java interfaces to support multiple inheritance. Traits define a template with methods and variables which any class can make use of. The difference between interfaces and traits is that traits can implement methods. A Scala class can inherit from any number of traits together with a class in way to support multiple inheritance. Traits can be mixed in to a class even without inheriting it but just by including it at the time of creating instances. &amp;lt;ref&amp;gt;http://joelabrahamsson.com/entry/learning-scala-traits&amp;lt;/ref&amp;gt;. Trait can also extend a class and methods to be overridden are usually prefixed by the keyword override. The language enforces method names to be different across inherited  traits to avoid the issues of multiple inheritance. [http://www.python.org/ Python] supports a limited version of multiple inheritance by extending multiple classes in depth-first left-right order &amp;lt;ref&amp;gt;http://docs.python.org/tutorial/classes.html&amp;lt;/ref&amp;gt;. If a python class D inherits classes A, B, C and if an instance variable or method is not available in the current class, it checks in A and its base classes, then moves to B and its base classes and eventually to C thus moving from left to right. In this way, python avoids the diamond problem by making the search order linear by which the derived class accesses attributes of its parent classes. In addition python also defines mixins to access reusable features within a class.&lt;br /&gt;
&lt;br /&gt;
==Advantages of mixins==&lt;br /&gt;
1. Firstly, when a module gets mixed into different classes, each class acquires the same  reference  to the module and hence even dynamic changes to the module gets reflected in the behavior of the classes &amp;lt;ref&amp;gt;http://www.linuxtopia.org/online_books/programming_books/ruby_tutorial/Ruby_Modules_Mixins.html&amp;lt;/ref&amp;gt;. In this way, the module’s code is not just copied to individual classes as in static languages instead its reference is provided. &lt;br /&gt;
&lt;br /&gt;
2. Secondly, the mixed in state and behavior can be accessed by the instance of target class just like accessing its own features. &lt;br /&gt;
&lt;br /&gt;
3. Thirdly, a class can include any number of modules in a way it can simulate multiple inheritance without having the related issues.&lt;br /&gt;
&lt;br /&gt;
==Questions to Ponder==&lt;br /&gt;
===Do mixins solve all of the problems of multiple inheritance?===&lt;br /&gt;
Undoubtedly mixins solve some of the problems of multiple inheritance. There is no ambiguity in resolving the request to a common method implemented by multiple modules. In that case, the lastly included module is responsible for handling accesses to common methods. Though the order of precedence of included modules matters, it looks clearer than C++.  However languages like Scala and Eiffel implement multiple inheritance far more safely  by explicitly forcing the user to have different method names across inherited classes.&lt;br /&gt;
&lt;br /&gt;
===Are mixins a clear advantage over interfaces?===&lt;br /&gt;
Mixins are more advantageous than interfaces because of their features. Interfaces have some limitations. As seen from programs, the derived class Allrounder uses the instances of Batsman and Bowler classes (has-a relationship) to access respective attributes. Interface is nice in the sense it provides a generic template that could be implemented differently by different classes.  However the target class has to necessarily implement every method of the interface. This would pose problems to a Class which is interested in implementing specific features of the interface. On the contrary, Mixins eliminate this inconvenience by mixing in the state and behavior of the modules in the target class and allows a it to use them as it needs. Hence the target class can just use module’s features selectively or completely instead of implementing by itself. The size of multiple inheritance code using mixins is smaller than interfaces and C++ variants.&lt;br /&gt;
&lt;br /&gt;
===Do mixins have any disadvantages not shared by multiple inheritance or interfaces?===&lt;br /&gt;
Inspite of making the lives of programmers better with effective code reuse and avoiding unnecessary implementation of methods that cause clutter in the code, the concept of Mixins can easily be abused resulting in namespace pollution. This is caused when a class includes multiple modules each of which contains several hundred method implementations. Having hundreds of methods mixed into a single class, makes it difficult to identify the source modules of the methods, thereby causing confusion while implementing these methods, whereas in the case of multiple inheritance we can easily identify the class to which the method belongs.&lt;br /&gt;
Another disadvantage that follows from the namespace collision is the case where we have multiple methods with the same name in different modules.In such a case whenever a call to that method is made, the method which belongs to the module that was included last will be called. In the above example, the method call canEat() invokes Animal's canEat() method since Animal was the last included module. The problem with this is that, this takes place silently without any warning. This may result in accidentally inheriting wrong methods.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
In this article, we have discussed about multiple inheritance and means by which it is implemented in different languages. Though multiple inheritance is a useful feature,&lt;br /&gt;
many languages do not support them due to its associated issues. Given its advantages, mixin in ruby seems to be a viable option to achieve multiple inheritance. However mixins too have limitations which should be taken care of during implementation.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
==See Also==&lt;br /&gt;
1. Lucas Carlson and Leonard Richardson. 2006. [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=1212915&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 Ruby Cookbook] (Cookbooks (O'Reilly)). O'Reilly Media, Inc..&lt;br /&gt;
&lt;br /&gt;
2. Herbert Schildt. 2000. Java 2: [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=557816&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 The Complete Reference] (4th ed.). McGraw-Hill Professional.&lt;/div&gt;</summary>
		<author><name>Snallan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65445</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65445"/>
		<updated>2012-09-21T14:01:08Z</updated>

		<summary type="html">&lt;p&gt;Snallan: /* Do mixins have any disadvantages not shared by multiple inheritance or interfaces? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''1w23:  Multiple Inheritance and Mixins&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
The purpose of this article is to give a brief introduction about multiple inheritance, consider some of its issues and discuss techniques employed by different programming languages to handle such issues. In this relation, the article also compares solutions provided by different programming languages, especially against Ruby mixins, by listing their advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
Multiple Inheritance is a technique of establishing hierarchical relationships within a set of classes, such that a single class can inherit  behaviors from multiple parent classes. Although it might seem useful to include methods from multiple classes, the major issue with multiple inheritance is the ambiguity in referencing the parent class methods,  when more than one parent classes share a method with the same name.This ambiguity is predominant in cases where the inheritance hierarchy is more than one level deep, where the class in the lowermost level inherits from two classes, that have overridden the same method from their parent in different ways.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A Real World Example==&lt;br /&gt;
A known example of multiple inheritance to represent animals using classes and objects is cited for the sake of understanding &amp;lt;ref&amp;gt; http://stackoverflow.com/questions/9359948/multiple-inheritance-whats-a-good-example&amp;lt;/ref&amp;gt;. A Winged animal and a mammal inherit primitive attributes of an animal. While a winged animal can fly; a mammal can breathe and reproduce; they both eat, move and share features generic to animals. A bat being a winged mammal possesses features common to both winged animal and mammal. It follows that class Bat inherits from classes WingedAnimal and Mammal and in turn classes WingedAnimal and Mammal inherit from class Animal.   In reference to this context, let us give our own implementation and explore the issues of multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
[[File:Class_Diagram.png|thumb|right|x400px|alt=Class Diagram for the example.]]&lt;br /&gt;
&lt;br /&gt;
==Issues of Multiple Inheritance==&lt;br /&gt;
1. Due to inheritance, the parent class’s objects get duplicated to child class. As a result, Class Bat duplicates Animal objects through each of WingedAnimal and Mammal classes. When Bat accesses any of the Animal's attributes, the compiler would be ambiguous about determining the correct path to use since two paths are available from Animal to Bat. This problem of multiple inheritance is called diamond problem &amp;lt;ref&amp;gt;http://www.artima.com/weblogs/viewpost.jsp?thread=246488&amp;lt;/ref&amp;gt;. Compiler takes arbitrary decisions during such situations. &lt;br /&gt;
&lt;br /&gt;
2.Objects of parent classes can be separately managed by using [http://en.wikipedia.org/wiki/Virtual_method_table virtual tables]as in C++. By explicitly specifying the virtual table pointer, the Bat class can access WingedAnimal and Mammal classes unambiguously. However virtual tables occupy significant portion of memory thereby limiting their extensive usage.  &lt;br /&gt;
&lt;br /&gt;
3. The order by which a child class inherits parent classes also matters. If Bat inherits WingedAnimal prior to Mammal, then WingedAnimal precedes Mammal and common methods are accessed in this order of precedence.  Hence if the Programmer is not aware of such precedence, debugging would be difficult.&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
Programming languages provide different solutions to handle the issues of multiple inheritance. Here we consider the techniques offered by C++, Java, Ruby, Scala and Python.    &lt;br /&gt;
===Multiple Inheritance in C++===&lt;br /&gt;
C++ supports multiple inheritance with the notion of virtual classes and virtual functions. In the example, the Animal class defines virtual functions with WingedAnimal and Mammal classes virtually inheriting the features of Animal. The Bat class can now unambiguously access canEat() method of Animal class, using virtual table pointers of WingedAnimal and Mammal. The example written using C++ virtual functions is shown below.&lt;br /&gt;
&lt;br /&gt;
 class Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void virtual canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class WingedAnimal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Mammal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Bat :public WingedAnimal, public Mammal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }  &lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
  int i = 0;&lt;br /&gt;
  Animal *instances[3];&lt;br /&gt;
  WingedAnimal w;&lt;br /&gt;
  Mammal m;&lt;br /&gt;
  Bat b;&lt;br /&gt;
  instances[0] = &amp;amp;w;&lt;br /&gt;
  instances[1] = &amp;amp;m;&lt;br /&gt;
  instances[2] = &amp;amp;b;&lt;br /&gt;
  while(i&amp;lt;3)&lt;br /&gt;
    instances[i++]-&amp;gt;canEat();&lt;br /&gt;
  return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Java===&lt;br /&gt;
Java doesn’t support multiple inheritance but provides a feature called interfaces to simulate inheritance from multiple classes. Interface is an abstract class which provides a generic template by just declaring variables and methods without implementing them. [http://www.brpreiss.com/books/opus4/html/page611.html Concrete classes] that inherit interfaces are supposed to implement its methods. The example written using Java interfaces is shown below.&lt;br /&gt;
&lt;br /&gt;
 interface Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface WingedAnimal extends Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();  &lt;br /&gt;
   void canFly();  &lt;br /&gt;
 }&lt;br /&gt;
 class ImplementWingedAnimal implements WingedAnimal  &lt;br /&gt;
 {&lt;br /&gt;
    public void canEat() {    &lt;br /&gt;
    }&lt;br /&gt;
    public void canFly() {    &lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Mammal extends Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();&lt;br /&gt;
   void canGiveBirth();&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementMammal implements Mammal&lt;br /&gt;
 {&lt;br /&gt;
    public void canEat() {    &lt;br /&gt;
    }&lt;br /&gt;
    public void canGiveBirth() {    &lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Bat extends WingedAnimal, Mammal&lt;br /&gt;
 {&lt;br /&gt;
    void canEat();    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class ImplementBat implements Bat&lt;br /&gt;
 {  &lt;br /&gt;
    ImplementWingedAnimal w = new ImplementWingedAnimal();&lt;br /&gt;
    ImplementMammal m = new ImplementMammal();     &lt;br /&gt;
    public void canEat() {            &lt;br /&gt;
      m.canEat();  &lt;br /&gt;
      w.canEat();      &lt;br /&gt;
    }&lt;br /&gt;
    public void canFly() {&lt;br /&gt;
    }&lt;br /&gt;
    public void canGiveBirth() {&lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class MultipleInheritance&lt;br /&gt;
 {&lt;br /&gt;
   public static void main(String args[])&lt;br /&gt;
   {&lt;br /&gt;
     ImplementBat b = new ImplementBat ();&lt;br /&gt;
     b.canEat();       &lt;br /&gt;
   } &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Ruby===&lt;br /&gt;
Ruby doesn't support multiple inheritance but provides a similar feature using modules and mixins.  &lt;br /&gt;
====Modules====&lt;br /&gt;
A Module is an encapsulation of methods and variables, although similar in function to a Class, differs from a class in the sense that it cannot be instantiated. Modules are  more dynamic and allow us to organize the code in a more granular way.Each module can add something to the existing class. A module can be thought of an encapsulation of a  subset of the full capabilities of an object.&lt;br /&gt;
An example found in the book ,The Well-Grounded Rubyist &amp;lt;ref&amp;gt;http://www.manning.com/black2/SampleChapter4.pdf [Pg.92] &amp;lt;/ref&amp;gt;, is the stack-like module. The module encapsulates the characteristics of being like a stack,implements the operations that can be performed by a stack but is not the actual Stack class. This allows us to use this module to mix-in  stack-like behaviour into other classes that require them.&lt;br /&gt;
&lt;br /&gt;
====Mixins====&lt;br /&gt;
A Mixin is a concept that allows inclusion of special characteristics into existing classes, so that common features that need to be reused in several classes can be selectively included, thus avoiding implementation of unnecessary functions like in the case of interfaces.&lt;br /&gt;
Mixins allow us to dynamically bind special features defined in modules, into core classes without really making any changes to the objects. This improves flexibility when we want to add plugins and extensions to the existing framework.&amp;lt;ref&amp;gt;http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-frameworks/&amp;lt;/ref&amp;gt;.The source code for the same example implemented using ruby mixins is shown below.&lt;br /&gt;
&lt;br /&gt;
 module Animal&lt;br /&gt;
   def canEat     &lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module WingedAnimal&lt;br /&gt;
   include Animal    &lt;br /&gt;
   def canEat&lt;br /&gt;
   end      &lt;br /&gt;
   def canFly&lt;br /&gt;
   end      &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Mammal&lt;br /&gt;
   include Animal&lt;br /&gt;
   def canEat&lt;br /&gt;
   end&lt;br /&gt;
   def canGiveBirth&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Bat&lt;br /&gt;
   include WingedAnimal&lt;br /&gt;
   include Mammal&lt;br /&gt;
   include Animal  &lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 Bat.new().canEat()&lt;br /&gt;
&lt;br /&gt;
===Multiple inheritance in other languages===&lt;br /&gt;
[http://www.scala-lang.org/ Scala] offers traits that are similar to Java interfaces to support multiple inheritance. Traits define a template with methods and variables which any class can make use of. The difference between interfaces and traits is that traits can implement methods. A Scala class can inherit from any number of traits together with a class in way to support multiple inheritance. Traits can be mixed in to a class even without inheriting it but just by including it at the time of creating instances. &amp;lt;ref&amp;gt;http://joelabrahamsson.com/entry/learning-scala-traits&amp;lt;/ref&amp;gt;. Trait can also extend a class and methods to be overridden are usually prefixed by the keyword override. The language enforces method names to be different across inherited  traits to avoid the issues of multiple inheritance. [http://www.python.org/ Python] supports a limited version of multiple inheritance by extending multiple classes in depth-first left-right order &amp;lt;ref&amp;gt;http://docs.python.org/tutorial/classes.html&amp;lt;/ref&amp;gt;. If a python class D inherits classes A, B, C and if an instance variable or method is not available in the current class, it checks in A and its base classes, then moves to B and its base classes and eventually to C thus moving from left to right. In this way, python avoids the diamond problem by making the search order linear by which the derived class accesses attributes of its parent classes. In addition python also defines mixins to access reusable features within a class.&lt;br /&gt;
&lt;br /&gt;
==Advantages of mixins==&lt;br /&gt;
1. Firstly, when a module gets mixed into different classes, each class acquires the same  reference  to the module and hence even dynamic changes to the module gets reflected in the behavior of the classes &amp;lt;ref&amp;gt;http://www.linuxtopia.org/online_books/programming_books/ruby_tutorial/Ruby_Modules_Mixins.html&amp;lt;/ref&amp;gt;. In this way, the module’s code is not just copied to individual classes as in static languages instead its reference is provided. &lt;br /&gt;
&lt;br /&gt;
2. Secondly, the mixed in state and behavior can be accessed by the instance of target class just like accessing its own features. &lt;br /&gt;
&lt;br /&gt;
3. Thirdly, a class can include any number of modules in a way it can simulate multiple inheritance without having the related issues.&lt;br /&gt;
&lt;br /&gt;
==Questions to Ponder==&lt;br /&gt;
===Do mixins solve all of the problems of multiple inheritance?===&lt;br /&gt;
Undoubtedly mixins solve some of the problems of multiple inheritance. There is no ambiguity in resolving the request to a common method implemented by multiple modules. In that case, the lastly included module is responsible for handling accesses to common methods. Though the order of precedence of included modules matters, it looks clearer than C++.  However languages like Scala and Eiffel implement multiple inheritance far more safely  by explicitly forcing the user to have different method names across inherited classes.&lt;br /&gt;
&lt;br /&gt;
===Are mixins a clear advantage over interfaces?===&lt;br /&gt;
Mixins are more advantageous than interfaces because of their features. Interfaces have some limitations. As seen from programs, the derived class Allrounder uses the instances of Batsman and Bowler classes (has-a relationship) to access respective attributes. Interface is nice in the sense it provides a generic template that could be implemented differently by different classes.  However the target class has to necessarily implement every method of the interface. This would pose problems to a Class which is interested in implementing specific features of the interface. On the contrary, Mixins eliminate this inconvenience by mixing in the state and behavior of the modules in the target class and allows a it to use them as it needs. Hence the target class can just use module’s features selectively or completely instead of implementing by itself. The size of multiple inheritance code using mixins is smaller than interfaces and C++ variants.&lt;br /&gt;
&lt;br /&gt;
===Do mixins have any disadvantages not shared by multiple inheritance or interfaces?===&lt;br /&gt;
Inspite of making the lives of programmers better with effective code reuse and avoiding unnecessary implementation of methods that cause clutter in the code, the concept of Mixins can easily be abused resulting in namespace pollution. This is caused when a class includes multiple modules each of which contains several hundred method implementations. Having hundreds of methods mixed into a single class, makes it difficult to identify the source modules of the methods, thereby causing confusion while implementing these methods, whereas in the case of multiple inheritance we can easily identify the class to which the method belongs.&lt;br /&gt;
Another disadvantage that follows from the namespace collision is the case where we have multiple methods with the same name in different modules.In such a case whenever a call to that method is made, the method which belongs to the module that was included last will be called. In the above example, the method call canEat() invokes Animal's canEat() method since Animal was the last included module. The problem with this is that, this takes place silently without any warning. This may result in accidentally inheriting wrong methods.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
In this article, we have discussed about multiple inheritance and means by which it is implemented in different languages. Though multiple inheritance is a useful feature,&lt;br /&gt;
many languages do not support them due to its associated issues. Given its advantages, mixin in ruby seems to be a viable option to achieve multiple inheritance. However mixins too have limitations which should be taken care of during implementation.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
==See Also==&lt;br /&gt;
1. Lucas Carlson and Leonard Richardson. 2006. [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=1212915&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 Ruby Cookbook] (Cookbooks (O'Reilly)). O'Reilly Media, Inc..&lt;br /&gt;
&lt;br /&gt;
2. Herbert Schildt. 2000. Java 2: [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=557816&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 The Complete Reference] (4th ed.). McGraw-Hill Professional.&lt;/div&gt;</summary>
		<author><name>Snallan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65444</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65444"/>
		<updated>2012-09-21T13:59:51Z</updated>

		<summary type="html">&lt;p&gt;Snallan: /* Do mixins have any disadvantages not shared by multiple inheritance or interfaces? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''1w23:  Multiple Inheritance and Mixins&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
The purpose of this article is to give a brief introduction about multiple inheritance, consider some of its issues and discuss techniques employed by different programming languages to handle such issues. In this relation, the article also compares solutions provided by different programming languages, especially against Ruby mixins, by listing their advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
Multiple Inheritance is a technique of establishing hierarchical relationships within a set of classes, such that a single class can inherit  behaviors from multiple parent classes. Although it might seem useful to include methods from multiple classes, the major issue with multiple inheritance is the ambiguity in referencing the parent class methods,  when more than one parent classes share a method with the same name.This ambiguity is predominant in cases where the inheritance hierarchy is more than one level deep, where the class in the lowermost level inherits from two classes, that have overridden the same method from their parent in different ways.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A Real World Example==&lt;br /&gt;
A known example of multiple inheritance to represent animals using classes and objects is cited for the sake of understanding &amp;lt;ref&amp;gt; http://stackoverflow.com/questions/9359948/multiple-inheritance-whats-a-good-example&amp;lt;/ref&amp;gt;. A Winged animal and a mammal inherit primitive attributes of an animal. While a winged animal can fly; a mammal can breathe and reproduce; they both eat, move and share features generic to animals. A bat being a winged mammal possesses features common to both winged animal and mammal. It follows that class Bat inherits from classes WingedAnimal and Mammal and in turn classes WingedAnimal and Mammal inherit from class Animal.   In reference to this context, let us give our own implementation and explore the issues of multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
[[File:Class_Diagram.png|thumb|right|x400px|alt=Class Diagram for the example.]]&lt;br /&gt;
&lt;br /&gt;
==Issues of Multiple Inheritance==&lt;br /&gt;
1. Due to inheritance, the parent class’s objects get duplicated to child class. As a result, Class Bat duplicates Animal objects through each of WingedAnimal and Mammal classes. When Bat accesses any of the Animal's attributes, the compiler would be ambiguous about determining the correct path to use since two paths are available from Animal to Bat. This problem of multiple inheritance is called diamond problem &amp;lt;ref&amp;gt;http://www.artima.com/weblogs/viewpost.jsp?thread=246488&amp;lt;/ref&amp;gt;. Compiler takes arbitrary decisions during such situations. &lt;br /&gt;
&lt;br /&gt;
2.Objects of parent classes can be separately managed by using [http://en.wikipedia.org/wiki/Virtual_method_table virtual tables]as in C++. By explicitly specifying the virtual table pointer, the Bat class can access WingedAnimal and Mammal classes unambiguously. However virtual tables occupy significant portion of memory thereby limiting their extensive usage.  &lt;br /&gt;
&lt;br /&gt;
3. The order by which a child class inherits parent classes also matters. If Bat inherits WingedAnimal prior to Mammal, then WingedAnimal precedes Mammal and common methods are accessed in this order of precedence.  Hence if the Programmer is not aware of such precedence, debugging would be difficult.&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
Programming languages provide different solutions to handle the issues of multiple inheritance. Here we consider the techniques offered by C++, Java, Ruby, Scala and Python.    &lt;br /&gt;
===Multiple Inheritance in C++===&lt;br /&gt;
C++ supports multiple inheritance with the notion of virtual classes and virtual functions. In the example, the Animal class defines virtual functions with WingedAnimal and Mammal classes virtually inheriting the features of Animal. The Bat class can now unambiguously access canEat() method of Animal class, using virtual table pointers of WingedAnimal and Mammal. The example written using C++ virtual functions is shown below.&lt;br /&gt;
&lt;br /&gt;
 class Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void virtual canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class WingedAnimal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Mammal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Bat :public WingedAnimal, public Mammal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }  &lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
  int i = 0;&lt;br /&gt;
  Animal *instances[3];&lt;br /&gt;
  WingedAnimal w;&lt;br /&gt;
  Mammal m;&lt;br /&gt;
  Bat b;&lt;br /&gt;
  instances[0] = &amp;amp;w;&lt;br /&gt;
  instances[1] = &amp;amp;m;&lt;br /&gt;
  instances[2] = &amp;amp;b;&lt;br /&gt;
  while(i&amp;lt;3)&lt;br /&gt;
    instances[i++]-&amp;gt;canEat();&lt;br /&gt;
  return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Java===&lt;br /&gt;
Java doesn’t support multiple inheritance but provides a feature called interfaces to simulate inheritance from multiple classes. Interface is an abstract class which provides a generic template by just declaring variables and methods without implementing them. [http://www.brpreiss.com/books/opus4/html/page611.html Concrete classes] that inherit interfaces are supposed to implement its methods. The example written using Java interfaces is shown below.&lt;br /&gt;
&lt;br /&gt;
 interface Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface WingedAnimal extends Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();  &lt;br /&gt;
   void canFly();  &lt;br /&gt;
 }&lt;br /&gt;
 class ImplementWingedAnimal implements WingedAnimal  &lt;br /&gt;
 {&lt;br /&gt;
    public void canEat() {    &lt;br /&gt;
    }&lt;br /&gt;
    public void canFly() {    &lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Mammal extends Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();&lt;br /&gt;
   void canGiveBirth();&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementMammal implements Mammal&lt;br /&gt;
 {&lt;br /&gt;
    public void canEat() {    &lt;br /&gt;
    }&lt;br /&gt;
    public void canGiveBirth() {    &lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Bat extends WingedAnimal, Mammal&lt;br /&gt;
 {&lt;br /&gt;
    void canEat();    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class ImplementBat implements Bat&lt;br /&gt;
 {  &lt;br /&gt;
    ImplementWingedAnimal w = new ImplementWingedAnimal();&lt;br /&gt;
    ImplementMammal m = new ImplementMammal();     &lt;br /&gt;
    public void canEat() {            &lt;br /&gt;
      m.canEat();  &lt;br /&gt;
      w.canEat();      &lt;br /&gt;
    }&lt;br /&gt;
    public void canFly() {&lt;br /&gt;
    }&lt;br /&gt;
    public void canGiveBirth() {&lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class MultipleInheritance&lt;br /&gt;
 {&lt;br /&gt;
   public static void main(String args[])&lt;br /&gt;
   {&lt;br /&gt;
     ImplementBat b = new ImplementBat ();&lt;br /&gt;
     b.canEat();       &lt;br /&gt;
   } &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Ruby===&lt;br /&gt;
Ruby doesn't support multiple inheritance but provides a similar feature using modules and mixins.  &lt;br /&gt;
====Modules====&lt;br /&gt;
A Module is an encapsulation of methods and variables, although similar in function to a Class, differs from a class in the sense that it cannot be instantiated. Modules are  more dynamic and allow us to organize the code in a more granular way.Each module can add something to the existing class. A module can be thought of an encapsulation of a  subset of the full capabilities of an object.&lt;br /&gt;
An example found in the book ,The Well-Grounded Rubyist &amp;lt;ref&amp;gt;http://www.manning.com/black2/SampleChapter4.pdf [Pg.92] &amp;lt;/ref&amp;gt;, is the stack-like module. The module encapsulates the characteristics of being like a stack,implements the operations that can be performed by a stack but is not the actual Stack class. This allows us to use this module to mix-in  stack-like behaviour into other classes that require them.&lt;br /&gt;
&lt;br /&gt;
====Mixins====&lt;br /&gt;
A Mixin is a concept that allows inclusion of special characteristics into existing classes, so that common features that need to be reused in several classes can be selectively included, thus avoiding implementation of unnecessary functions like in the case of interfaces.&lt;br /&gt;
Mixins allow us to dynamically bind special features defined in modules, into core classes without really making any changes to the objects. This improves flexibility when we want to add plugins and extensions to the existing framework.&amp;lt;ref&amp;gt;http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-frameworks/&amp;lt;/ref&amp;gt;.The source code for the same example implemented using ruby mixins is shown below.&lt;br /&gt;
&lt;br /&gt;
 module Animal&lt;br /&gt;
   def canEat     &lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module WingedAnimal&lt;br /&gt;
   include Animal    &lt;br /&gt;
   def canEat&lt;br /&gt;
   end      &lt;br /&gt;
   def canFly&lt;br /&gt;
   end      &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Mammal&lt;br /&gt;
   include Animal&lt;br /&gt;
   def canEat&lt;br /&gt;
   end&lt;br /&gt;
   def canGiveBirth&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Bat&lt;br /&gt;
   include WingedAnimal&lt;br /&gt;
   include Mammal&lt;br /&gt;
   include Animal  &lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 Bat.new().canEat()&lt;br /&gt;
&lt;br /&gt;
===Multiple inheritance in other languages===&lt;br /&gt;
[http://www.scala-lang.org/ Scala] offers traits that are similar to Java interfaces to support multiple inheritance. Traits define a template with methods and variables which any class can make use of. The difference between interfaces and traits is that traits can implement methods. A Scala class can inherit from any number of traits together with a class in way to support multiple inheritance. Traits can be mixed in to a class even without inheriting it but just by including it at the time of creating instances. &amp;lt;ref&amp;gt;http://joelabrahamsson.com/entry/learning-scala-traits&amp;lt;/ref&amp;gt;. Trait can also extend a class and methods to be overridden are usually prefixed by the keyword override. The language enforces method names to be different across inherited  traits to avoid the issues of multiple inheritance. [http://www.python.org/ Python] supports a limited version of multiple inheritance by extending multiple classes in depth-first left-right order &amp;lt;ref&amp;gt;http://docs.python.org/tutorial/classes.html&amp;lt;/ref&amp;gt;. If a python class D inherits classes A, B, C and if an instance variable or method is not available in the current class, it checks in A and its base classes, then moves to B and its base classes and eventually to C thus moving from left to right. In this way, python avoids the diamond problem by making the search order linear by which the derived class accesses attributes of its parent classes. In addition python also defines mixins to access reusable features within a class.&lt;br /&gt;
&lt;br /&gt;
==Advantages of mixins==&lt;br /&gt;
1. Firstly, when a module gets mixed into different classes, each class acquires the same  reference  to the module and hence even dynamic changes to the module gets reflected in the behavior of the classes &amp;lt;ref&amp;gt;http://www.linuxtopia.org/online_books/programming_books/ruby_tutorial/Ruby_Modules_Mixins.html&amp;lt;/ref&amp;gt;. In this way, the module’s code is not just copied to individual classes as in static languages instead its reference is provided. &lt;br /&gt;
&lt;br /&gt;
2. Secondly, the mixed in state and behavior can be accessed by the instance of target class just like accessing its own features. &lt;br /&gt;
&lt;br /&gt;
3. Thirdly, a class can include any number of modules in a way it can simulate multiple inheritance without having the related issues.&lt;br /&gt;
&lt;br /&gt;
==Questions to Ponder==&lt;br /&gt;
===Do mixins solve all of the problems of multiple inheritance?===&lt;br /&gt;
Undoubtedly mixins solve some of the problems of multiple inheritance. There is no ambiguity in resolving the request to a common method implemented by multiple modules. In that case, the lastly included module is responsible for handling accesses to common methods. Though the order of precedence of included modules matters, it looks clearer than C++.  However languages like Scala and Eiffel implement multiple inheritance far more safely  by explicitly forcing the user to have different method names across inherited classes.&lt;br /&gt;
&lt;br /&gt;
===Are mixins a clear advantage over interfaces?===&lt;br /&gt;
Mixins are more advantageous than interfaces because of their features. Interfaces have some limitations. As seen from programs, the derived class Allrounder uses the instances of Batsman and Bowler classes (has-a relationship) to access respective attributes. Interface is nice in the sense it provides a generic template that could be implemented differently by different classes.  However the target class has to necessarily implement every method of the interface. This would pose problems to a Class which is interested in implementing specific features of the interface. On the contrary, Mixins eliminate this inconvenience by mixing in the state and behavior of the modules in the target class and allows a it to use them as it needs. Hence the target class can just use module’s features selectively or completely instead of implementing by itself. The size of multiple inheritance code using mixins is smaller than interfaces and C++ variants.&lt;br /&gt;
&lt;br /&gt;
===Do mixins have any disadvantages not shared by multiple inheritance or interfaces?===&lt;br /&gt;
Inspite of making the lives of programmers better with effective code reuse and avoiding unnecessary implementation of methods that cause clutter in the code, the concept of Mixins can easily be abused resulting in namespace pollution. This is caused when a class includes multiple modules each of which contains several hundred method implementations. Having hundreds of methods mixed into a single class, makes it difficult to identify the source modules of the methods, thereby causing confusion while implementing these methods, whereas in the case of multiple inheritance we can easily identify the class to which the method belongs.&lt;br /&gt;
Another disadvantage that follows from the namespace collision is the case where we have multiple methods with the same name in different modules.In such a case whenever a call to that method is made, the method which belongs to the module that was included last will be called. In the above example, the method call canEat() invokes Animal's canEat() method since Animal was the last included module. The problem with this is that, this takes place silently without any warning. This may result in accidental overriding of wrong methods.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
In this article, we have discussed about multiple inheritance and means by which it is implemented in different languages. Though multiple inheritance is a useful feature,&lt;br /&gt;
many languages do not support them due to its associated issues. Given its advantages, mixin in ruby seems to be a viable option to achieve multiple inheritance. However mixins too have limitations which should be taken care of during implementation.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
==See Also==&lt;br /&gt;
1. Lucas Carlson and Leonard Richardson. 2006. [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=1212915&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 Ruby Cookbook] (Cookbooks (O'Reilly)). O'Reilly Media, Inc..&lt;br /&gt;
&lt;br /&gt;
2. Herbert Schildt. 2000. Java 2: [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=557816&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 The Complete Reference] (4th ed.). McGraw-Hill Professional.&lt;/div&gt;</summary>
		<author><name>Snallan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65443</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65443"/>
		<updated>2012-09-21T13:58:29Z</updated>

		<summary type="html">&lt;p&gt;Snallan: /* Do mixins have any disadvantages not shared by multiple inheritance or interfaces? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''1w23:  Multiple Inheritance and Mixins&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
The purpose of this article is to give a brief introduction about multiple inheritance, consider some of its issues and discuss techniques employed by different programming languages to handle such issues. In this relation, the article also compares solutions provided by different programming languages, especially against Ruby mixins, by listing their advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
Multiple Inheritance is a technique of establishing hierarchical relationships within a set of classes, such that a single class can inherit  behaviors from multiple parent classes. Although it might seem useful to include methods from multiple classes, the major issue with multiple inheritance is the ambiguity in referencing the parent class methods,  when more than one parent classes share a method with the same name.This ambiguity is predominant in cases where the inheritance hierarchy is more than one level deep, where the class in the lowermost level inherits from two classes, that have overridden the same method from their parent in different ways.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A Real World Example==&lt;br /&gt;
A known example of multiple inheritance to represent animals using classes and objects is cited for the sake of understanding &amp;lt;ref&amp;gt; http://stackoverflow.com/questions/9359948/multiple-inheritance-whats-a-good-example&amp;lt;/ref&amp;gt;. A Winged animal and a mammal inherit primitive attributes of an animal. While a winged animal can fly; a mammal can breathe and reproduce; they both eat, move and share features generic to animals. A bat being a winged mammal possesses features common to both winged animal and mammal. It follows that class Bat inherits from classes WingedAnimal and Mammal and in turn classes WingedAnimal and Mammal inherit from class Animal.   In reference to this context, let us give our own implementation and explore the issues of multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
[[File:Class_Diagram.png|thumb|right|x400px|alt=Class Diagram for the example.]]&lt;br /&gt;
&lt;br /&gt;
==Issues of Multiple Inheritance==&lt;br /&gt;
1. Due to inheritance, the parent class’s objects get duplicated to child class. As a result, Class Bat duplicates Animal objects through each of WingedAnimal and Mammal classes. When Bat accesses any of the Animal's attributes, the compiler would be ambiguous about determining the correct path to use since two paths are available from Animal to Bat. This problem of multiple inheritance is called diamond problem &amp;lt;ref&amp;gt;http://www.artima.com/weblogs/viewpost.jsp?thread=246488&amp;lt;/ref&amp;gt;. Compiler takes arbitrary decisions during such situations. &lt;br /&gt;
&lt;br /&gt;
2.Objects of parent classes can be separately managed by using [http://en.wikipedia.org/wiki/Virtual_method_table virtual tables]as in C++. By explicitly specifying the virtual table pointer, the Bat class can access WingedAnimal and Mammal classes unambiguously. However virtual tables occupy significant portion of memory thereby limiting their extensive usage.  &lt;br /&gt;
&lt;br /&gt;
3. The order by which a child class inherits parent classes also matters. If Bat inherits WingedAnimal prior to Mammal, then WingedAnimal precedes Mammal and common methods are accessed in this order of precedence.  Hence if the Programmer is not aware of such precedence, debugging would be difficult.&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
Programming languages provide different solutions to handle the issues of multiple inheritance. Here we consider the techniques offered by C++, Java, Ruby, Scala and Python.    &lt;br /&gt;
===Multiple Inheritance in C++===&lt;br /&gt;
C++ supports multiple inheritance with the notion of virtual classes and virtual functions. In the example, the Animal class defines virtual functions with WingedAnimal and Mammal classes virtually inheriting the features of Animal. The Bat class can now unambiguously access canEat() method of Animal class, using virtual table pointers of WingedAnimal and Mammal. The example written using C++ virtual functions is shown below.&lt;br /&gt;
&lt;br /&gt;
 class Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void virtual canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class WingedAnimal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Mammal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Bat :public WingedAnimal, public Mammal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }  &lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
  int i = 0;&lt;br /&gt;
  Animal *instances[3];&lt;br /&gt;
  WingedAnimal w;&lt;br /&gt;
  Mammal m;&lt;br /&gt;
  Bat b;&lt;br /&gt;
  instances[0] = &amp;amp;w;&lt;br /&gt;
  instances[1] = &amp;amp;m;&lt;br /&gt;
  instances[2] = &amp;amp;b;&lt;br /&gt;
  while(i&amp;lt;3)&lt;br /&gt;
    instances[i++]-&amp;gt;canEat();&lt;br /&gt;
  return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Java===&lt;br /&gt;
Java doesn’t support multiple inheritance but provides a feature called interfaces to simulate inheritance from multiple classes. Interface is an abstract class which provides a generic template by just declaring variables and methods without implementing them. [http://www.brpreiss.com/books/opus4/html/page611.html Concrete classes] that inherit interfaces are supposed to implement its methods. The example written using Java interfaces is shown below.&lt;br /&gt;
&lt;br /&gt;
 interface Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface WingedAnimal extends Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();  &lt;br /&gt;
   void canFly();  &lt;br /&gt;
 }&lt;br /&gt;
 class ImplementWingedAnimal implements WingedAnimal  &lt;br /&gt;
 {&lt;br /&gt;
    public void canEat() {    &lt;br /&gt;
    }&lt;br /&gt;
    public void canFly() {    &lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Mammal extends Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();&lt;br /&gt;
   void canGiveBirth();&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementMammal implements Mammal&lt;br /&gt;
 {&lt;br /&gt;
    public void canEat() {    &lt;br /&gt;
    }&lt;br /&gt;
    public void canGiveBirth() {    &lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Bat extends WingedAnimal, Mammal&lt;br /&gt;
 {&lt;br /&gt;
    void canEat();    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class ImplementBat implements Bat&lt;br /&gt;
 {  &lt;br /&gt;
    ImplementWingedAnimal w = new ImplementWingedAnimal();&lt;br /&gt;
    ImplementMammal m = new ImplementMammal();     &lt;br /&gt;
    public void canEat() {            &lt;br /&gt;
      m.canEat();  &lt;br /&gt;
      w.canEat();      &lt;br /&gt;
    }&lt;br /&gt;
    public void canFly() {&lt;br /&gt;
    }&lt;br /&gt;
    public void canGiveBirth() {&lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class MultipleInheritance&lt;br /&gt;
 {&lt;br /&gt;
   public static void main(String args[])&lt;br /&gt;
   {&lt;br /&gt;
     ImplementBat b = new ImplementBat ();&lt;br /&gt;
     b.canEat();       &lt;br /&gt;
   } &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Ruby===&lt;br /&gt;
Ruby doesn't support multiple inheritance but provides a similar feature using modules and mixins.  &lt;br /&gt;
====Modules====&lt;br /&gt;
A Module is an encapsulation of methods and variables, although similar in function to a Class, differs from a class in the sense that it cannot be instantiated. Modules are  more dynamic and allow us to organize the code in a more granular way.Each module can add something to the existing class. A module can be thought of an encapsulation of a  subset of the full capabilities of an object.&lt;br /&gt;
An example found in the book ,The Well-Grounded Rubyist &amp;lt;ref&amp;gt;http://www.manning.com/black2/SampleChapter4.pdf [Pg.92] &amp;lt;/ref&amp;gt;, is the stack-like module. The module encapsulates the characteristics of being like a stack,implements the operations that can be performed by a stack but is not the actual Stack class. This allows us to use this module to mix-in  stack-like behaviour into other classes that require them.&lt;br /&gt;
&lt;br /&gt;
====Mixins====&lt;br /&gt;
A Mixin is a concept that allows inclusion of special characteristics into existing classes, so that common features that need to be reused in several classes can be selectively included, thus avoiding implementation of unnecessary functions like in the case of interfaces.&lt;br /&gt;
Mixins allow us to dynamically bind special features defined in modules, into core classes without really making any changes to the objects. This improves flexibility when we want to add plugins and extensions to the existing framework.&amp;lt;ref&amp;gt;http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-frameworks/&amp;lt;/ref&amp;gt;.The source code for the same example implemented using ruby mixins is shown below.&lt;br /&gt;
&lt;br /&gt;
 module Animal&lt;br /&gt;
   def canEat     &lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module WingedAnimal&lt;br /&gt;
   include Animal    &lt;br /&gt;
   def canEat&lt;br /&gt;
   end      &lt;br /&gt;
   def canFly&lt;br /&gt;
   end      &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Mammal&lt;br /&gt;
   include Animal&lt;br /&gt;
   def canEat&lt;br /&gt;
   end&lt;br /&gt;
   def canGiveBirth&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Bat&lt;br /&gt;
   include WingedAnimal&lt;br /&gt;
   include Mammal&lt;br /&gt;
   include Animal  &lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 Bat.new().canEat()&lt;br /&gt;
&lt;br /&gt;
===Multiple inheritance in other languages===&lt;br /&gt;
[http://www.scala-lang.org/ Scala] offers traits that are similar to Java interfaces to support multiple inheritance. Traits define a template with methods and variables which any class can make use of. The difference between interfaces and traits is that traits can implement methods. A Scala class can inherit from any number of traits together with a class in way to support multiple inheritance. Traits can be mixed in to a class even without inheriting it but just by including it at the time of creating instances. &amp;lt;ref&amp;gt;http://joelabrahamsson.com/entry/learning-scala-traits&amp;lt;/ref&amp;gt;. Trait can also extend a class and methods to be overridden are usually prefixed by the keyword override. The language enforces method names to be different across inherited  traits to avoid the issues of multiple inheritance. [http://www.python.org/ Python] supports a limited version of multiple inheritance by extending multiple classes in depth-first left-right order &amp;lt;ref&amp;gt;http://docs.python.org/tutorial/classes.html&amp;lt;/ref&amp;gt;. If a python class D inherits classes A, B, C and if an instance variable or method is not available in the current class, it checks in A and its base classes, then moves to B and its base classes and eventually to C thus moving from left to right. In this way, python avoids the diamond problem by making the search order linear by which the derived class accesses attributes of its parent classes. In addition python also defines mixins to access reusable features within a class.&lt;br /&gt;
&lt;br /&gt;
==Advantages of mixins==&lt;br /&gt;
1. Firstly, when a module gets mixed into different classes, each class acquires the same  reference  to the module and hence even dynamic changes to the module gets reflected in the behavior of the classes &amp;lt;ref&amp;gt;http://www.linuxtopia.org/online_books/programming_books/ruby_tutorial/Ruby_Modules_Mixins.html&amp;lt;/ref&amp;gt;. In this way, the module’s code is not just copied to individual classes as in static languages instead its reference is provided. &lt;br /&gt;
&lt;br /&gt;
2. Secondly, the mixed in state and behavior can be accessed by the instance of target class just like accessing its own features. &lt;br /&gt;
&lt;br /&gt;
3. Thirdly, a class can include any number of modules in a way it can simulate multiple inheritance without having the related issues.&lt;br /&gt;
&lt;br /&gt;
==Questions to Ponder==&lt;br /&gt;
===Do mixins solve all of the problems of multiple inheritance?===&lt;br /&gt;
Undoubtedly mixins solve some of the problems of multiple inheritance. There is no ambiguity in resolving the request to a common method implemented by multiple modules. In that case, the lastly included module is responsible for handling accesses to common methods. Though the order of precedence of included modules matters, it looks clearer than C++.  However languages like Scala and Eiffel implement multiple inheritance far more safely  by explicitly forcing the user to have different method names across inherited classes.&lt;br /&gt;
&lt;br /&gt;
===Are mixins a clear advantage over interfaces?===&lt;br /&gt;
Mixins are more advantageous than interfaces because of their features. Interfaces have some limitations. As seen from programs, the derived class Allrounder uses the instances of Batsman and Bowler classes (has-a relationship) to access respective attributes. Interface is nice in the sense it provides a generic template that could be implemented differently by different classes.  However the target class has to necessarily implement every method of the interface. This would pose problems to a Class which is interested in implementing specific features of the interface. On the contrary, Mixins eliminate this inconvenience by mixing in the state and behavior of the modules in the target class and allows a it to use them as it needs. Hence the target class can just use module’s features selectively or completely instead of implementing by itself. The size of multiple inheritance code using mixins is smaller than interfaces and C++ variants.&lt;br /&gt;
&lt;br /&gt;
===Do mixins have any disadvantages not shared by multiple inheritance or interfaces?===&lt;br /&gt;
Inspite of making the lives of programmers better with effective code reuse and avoiding unnecessary implementation of methods that cause clutter in the code, the concept of Mixins can easily be abused resulting in namespace pollution. This is caused when a class includes multiple modules each of which contains several hundred method implementations. Having hundreds of methods mixed into a single class, makes it difficult to identify the source modules of the methods, thereby causing confusion while implementing these methods, whereas in the case of multiple inheritance we can easily identify the class to which the method belongs.&lt;br /&gt;
Another disadvantage that follows from the namespace collision is the case where we have multiple methods with the same name in different modules.In such a case whenever a call to that method is made, the method which belongs to the module that was included last will be called. In the above example, the method call canEat() invokes Mammal's callEat() method. The problem with this is that, this takes place silently without any warning. This may result in accidental overriding of wrong methods.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
In this article, we have discussed about multiple inheritance and means by which it is implemented in different languages. Though multiple inheritance is a useful feature,&lt;br /&gt;
many languages do not support them due to its associated issues. Given its advantages, mixin in ruby seems to be a viable option to achieve multiple inheritance. However mixins too have limitations which should be taken care of during implementation.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
==See Also==&lt;br /&gt;
1. Lucas Carlson and Leonard Richardson. 2006. [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=1212915&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 Ruby Cookbook] (Cookbooks (O'Reilly)). O'Reilly Media, Inc..&lt;br /&gt;
&lt;br /&gt;
2. Herbert Schildt. 2000. Java 2: [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=557816&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 The Complete Reference] (4th ed.). McGraw-Hill Professional.&lt;/div&gt;</summary>
		<author><name>Snallan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65442</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65442"/>
		<updated>2012-09-21T13:55:37Z</updated>

		<summary type="html">&lt;p&gt;Snallan: /* Multiple inheritance in other languages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''1w23:  Multiple Inheritance and Mixins&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
The purpose of this article is to give a brief introduction about multiple inheritance, consider some of its issues and discuss techniques employed by different programming languages to handle such issues. In this relation, the article also compares solutions provided by different programming languages, especially against Ruby mixins, by listing their advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
Multiple Inheritance is a technique of establishing hierarchical relationships within a set of classes, such that a single class can inherit  behaviors from multiple parent classes. Although it might seem useful to include methods from multiple classes, the major issue with multiple inheritance is the ambiguity in referencing the parent class methods,  when more than one parent classes share a method with the same name.This ambiguity is predominant in cases where the inheritance hierarchy is more than one level deep, where the class in the lowermost level inherits from two classes, that have overridden the same method from their parent in different ways.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A Real World Example==&lt;br /&gt;
A known example of multiple inheritance to represent animals using classes and objects is cited for the sake of understanding &amp;lt;ref&amp;gt; http://stackoverflow.com/questions/9359948/multiple-inheritance-whats-a-good-example&amp;lt;/ref&amp;gt;. A Winged animal and a mammal inherit primitive attributes of an animal. While a winged animal can fly; a mammal can breathe and reproduce; they both eat, move and share features generic to animals. A bat being a winged mammal possesses features common to both winged animal and mammal. It follows that class Bat inherits from classes WingedAnimal and Mammal and in turn classes WingedAnimal and Mammal inherit from class Animal.   In reference to this context, let us give our own implementation and explore the issues of multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
[[File:Class_Diagram.png|thumb|right|x400px|alt=Class Diagram for the example.]]&lt;br /&gt;
&lt;br /&gt;
==Issues of Multiple Inheritance==&lt;br /&gt;
1. Due to inheritance, the parent class’s objects get duplicated to child class. As a result, Class Bat duplicates Animal objects through each of WingedAnimal and Mammal classes. When Bat accesses any of the Animal's attributes, the compiler would be ambiguous about determining the correct path to use since two paths are available from Animal to Bat. This problem of multiple inheritance is called diamond problem &amp;lt;ref&amp;gt;http://www.artima.com/weblogs/viewpost.jsp?thread=246488&amp;lt;/ref&amp;gt;. Compiler takes arbitrary decisions during such situations. &lt;br /&gt;
&lt;br /&gt;
2.Objects of parent classes can be separately managed by using [http://en.wikipedia.org/wiki/Virtual_method_table virtual tables]as in C++. By explicitly specifying the virtual table pointer, the Bat class can access WingedAnimal and Mammal classes unambiguously. However virtual tables occupy significant portion of memory thereby limiting their extensive usage.  &lt;br /&gt;
&lt;br /&gt;
3. The order by which a child class inherits parent classes also matters. If Bat inherits WingedAnimal prior to Mammal, then WingedAnimal precedes Mammal and common methods are accessed in this order of precedence.  Hence if the Programmer is not aware of such precedence, debugging would be difficult.&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
Programming languages provide different solutions to handle the issues of multiple inheritance. Here we consider the techniques offered by C++, Java, Ruby, Scala and Python.    &lt;br /&gt;
===Multiple Inheritance in C++===&lt;br /&gt;
C++ supports multiple inheritance with the notion of virtual classes and virtual functions. In the example, the Animal class defines virtual functions with WingedAnimal and Mammal classes virtually inheriting the features of Animal. The Bat class can now unambiguously access canEat() method of Animal class, using virtual table pointers of WingedAnimal and Mammal. The example written using C++ virtual functions is shown below.&lt;br /&gt;
&lt;br /&gt;
 class Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void virtual canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class WingedAnimal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Mammal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Bat :public WingedAnimal, public Mammal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }  &lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
  int i = 0;&lt;br /&gt;
  Animal *instances[3];&lt;br /&gt;
  WingedAnimal w;&lt;br /&gt;
  Mammal m;&lt;br /&gt;
  Bat b;&lt;br /&gt;
  instances[0] = &amp;amp;w;&lt;br /&gt;
  instances[1] = &amp;amp;m;&lt;br /&gt;
  instances[2] = &amp;amp;b;&lt;br /&gt;
  while(i&amp;lt;3)&lt;br /&gt;
    instances[i++]-&amp;gt;canEat();&lt;br /&gt;
  return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Java===&lt;br /&gt;
Java doesn’t support multiple inheritance but provides a feature called interfaces to simulate inheritance from multiple classes. Interface is an abstract class which provides a generic template by just declaring variables and methods without implementing them. [http://www.brpreiss.com/books/opus4/html/page611.html Concrete classes] that inherit interfaces are supposed to implement its methods. The example written using Java interfaces is shown below.&lt;br /&gt;
&lt;br /&gt;
 interface Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface WingedAnimal extends Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();  &lt;br /&gt;
   void canFly();  &lt;br /&gt;
 }&lt;br /&gt;
 class ImplementWingedAnimal implements WingedAnimal  &lt;br /&gt;
 {&lt;br /&gt;
    public void canEat() {    &lt;br /&gt;
    }&lt;br /&gt;
    public void canFly() {    &lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Mammal extends Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();&lt;br /&gt;
   void canGiveBirth();&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementMammal implements Mammal&lt;br /&gt;
 {&lt;br /&gt;
    public void canEat() {    &lt;br /&gt;
    }&lt;br /&gt;
    public void canGiveBirth() {    &lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Bat extends WingedAnimal, Mammal&lt;br /&gt;
 {&lt;br /&gt;
    void canEat();    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class ImplementBat implements Bat&lt;br /&gt;
 {  &lt;br /&gt;
    ImplementWingedAnimal w = new ImplementWingedAnimal();&lt;br /&gt;
    ImplementMammal m = new ImplementMammal();     &lt;br /&gt;
    public void canEat() {            &lt;br /&gt;
      m.canEat();  &lt;br /&gt;
      w.canEat();      &lt;br /&gt;
    }&lt;br /&gt;
    public void canFly() {&lt;br /&gt;
    }&lt;br /&gt;
    public void canGiveBirth() {&lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class MultipleInheritance&lt;br /&gt;
 {&lt;br /&gt;
   public static void main(String args[])&lt;br /&gt;
   {&lt;br /&gt;
     ImplementBat b = new ImplementBat ();&lt;br /&gt;
     b.canEat();       &lt;br /&gt;
   } &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Ruby===&lt;br /&gt;
Ruby doesn't support multiple inheritance but provides a similar feature using modules and mixins.  &lt;br /&gt;
====Modules====&lt;br /&gt;
A Module is an encapsulation of methods and variables, although similar in function to a Class, differs from a class in the sense that it cannot be instantiated. Modules are  more dynamic and allow us to organize the code in a more granular way.Each module can add something to the existing class. A module can be thought of an encapsulation of a  subset of the full capabilities of an object.&lt;br /&gt;
An example found in the book ,The Well-Grounded Rubyist &amp;lt;ref&amp;gt;http://www.manning.com/black2/SampleChapter4.pdf [Pg.92] &amp;lt;/ref&amp;gt;, is the stack-like module. The module encapsulates the characteristics of being like a stack,implements the operations that can be performed by a stack but is not the actual Stack class. This allows us to use this module to mix-in  stack-like behaviour into other classes that require them.&lt;br /&gt;
&lt;br /&gt;
====Mixins====&lt;br /&gt;
A Mixin is a concept that allows inclusion of special characteristics into existing classes, so that common features that need to be reused in several classes can be selectively included, thus avoiding implementation of unnecessary functions like in the case of interfaces.&lt;br /&gt;
Mixins allow us to dynamically bind special features defined in modules, into core classes without really making any changes to the objects. This improves flexibility when we want to add plugins and extensions to the existing framework.&amp;lt;ref&amp;gt;http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-frameworks/&amp;lt;/ref&amp;gt;.The source code for the same example implemented using ruby mixins is shown below.&lt;br /&gt;
&lt;br /&gt;
 module Animal&lt;br /&gt;
   def canEat     &lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module WingedAnimal&lt;br /&gt;
   include Animal    &lt;br /&gt;
   def canEat&lt;br /&gt;
   end      &lt;br /&gt;
   def canFly&lt;br /&gt;
   end      &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Mammal&lt;br /&gt;
   include Animal&lt;br /&gt;
   def canEat&lt;br /&gt;
   end&lt;br /&gt;
   def canGiveBirth&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Bat&lt;br /&gt;
   include WingedAnimal&lt;br /&gt;
   include Mammal&lt;br /&gt;
   include Animal  &lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 Bat.new().canEat()&lt;br /&gt;
&lt;br /&gt;
===Multiple inheritance in other languages===&lt;br /&gt;
[http://www.scala-lang.org/ Scala] offers traits that are similar to Java interfaces to support multiple inheritance. Traits define a template with methods and variables which any class can make use of. The difference between interfaces and traits is that traits can implement methods. A Scala class can inherit from any number of traits together with a class in way to support multiple inheritance. Traits can be mixed in to a class even without inheriting it but just by including it at the time of creating instances. &amp;lt;ref&amp;gt;http://joelabrahamsson.com/entry/learning-scala-traits&amp;lt;/ref&amp;gt;. Trait can also extend a class and methods to be overridden are usually prefixed by the keyword override. The language enforces method names to be different across inherited  traits to avoid the issues of multiple inheritance. [http://www.python.org/ Python] supports a limited version of multiple inheritance by extending multiple classes in depth-first left-right order &amp;lt;ref&amp;gt;http://docs.python.org/tutorial/classes.html&amp;lt;/ref&amp;gt;. If a python class D inherits classes A, B, C and if an instance variable or method is not available in the current class, it checks in A and its base classes, then moves to B and its base classes and eventually to C thus moving from left to right. In this way, python avoids the diamond problem by making the search order linear by which the derived class accesses attributes of its parent classes. In addition python also defines mixins to access reusable features within a class.&lt;br /&gt;
&lt;br /&gt;
==Advantages of mixins==&lt;br /&gt;
1. Firstly, when a module gets mixed into different classes, each class acquires the same  reference  to the module and hence even dynamic changes to the module gets reflected in the behavior of the classes &amp;lt;ref&amp;gt;http://www.linuxtopia.org/online_books/programming_books/ruby_tutorial/Ruby_Modules_Mixins.html&amp;lt;/ref&amp;gt;. In this way, the module’s code is not just copied to individual classes as in static languages instead its reference is provided. &lt;br /&gt;
&lt;br /&gt;
2. Secondly, the mixed in state and behavior can be accessed by the instance of target class just like accessing its own features. &lt;br /&gt;
&lt;br /&gt;
3. Thirdly, a class can include any number of modules in a way it can simulate multiple inheritance without having the related issues.&lt;br /&gt;
&lt;br /&gt;
==Questions to Ponder==&lt;br /&gt;
===Do mixins solve all of the problems of multiple inheritance?===&lt;br /&gt;
Undoubtedly mixins solve some of the problems of multiple inheritance. There is no ambiguity in resolving the request to a common method implemented by multiple modules. In that case, the lastly included module is responsible for handling accesses to common methods. Though the order of precedence of included modules matters, it looks clearer than C++.  However languages like Scala and Eiffel implement multiple inheritance far more safely  by explicitly forcing the user to have different method names across inherited classes.&lt;br /&gt;
&lt;br /&gt;
===Are mixins a clear advantage over interfaces?===&lt;br /&gt;
Mixins are more advantageous than interfaces because of their features. Interfaces have some limitations. As seen from programs, the derived class Allrounder uses the instances of Batsman and Bowler classes (has-a relationship) to access respective attributes. Interface is nice in the sense it provides a generic template that could be implemented differently by different classes.  However the target class has to necessarily implement every method of the interface. This would pose problems to a Class which is interested in implementing specific features of the interface. On the contrary, Mixins eliminate this inconvenience by mixing in the state and behavior of the modules in the target class and allows a it to use them as it needs. Hence the target class can just use module’s features selectively or completely instead of implementing by itself. The size of multiple inheritance code using mixins is smaller than interfaces and C++ variants.&lt;br /&gt;
&lt;br /&gt;
===Do mixins have any disadvantages not shared by multiple inheritance or interfaces?===&lt;br /&gt;
Inspite of making the lives of programmers better with effective code reuse and avoiding unnecessary implementation of methods that cause clutter in the code, the concept of Mixins can easily be abused resulting in namespace pollution. This is caused when a class includes multiple modules each of which contains several hundred method implementations. Having hundreds of methods mixed into a single class, makes it difficult to identify the source modules of the methods, thereby causing confusion while implementing these methods, whereas in the case of multiple inheritance we can easily identify the class to which the method belongs.&lt;br /&gt;
Another disadvantage that follows from the namespace collision is the case where we have multiple methods with the same name in different modules.In such a case whenever a call to that method is made, the method which belongs to the module that was included last will be called. The problem with this is that, this takes place silently without any warning. This may result in accidental overriding of wrong methods.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
In this article, we have discussed about multiple inheritance and means by which it is implemented in different languages. Though multiple inheritance is a useful feature,&lt;br /&gt;
many languages do not support them due to its associated issues. Given its advantages, mixin in ruby seems to be a viable option to achieve multiple inheritance. However mixins too have limitations which should be taken care of during implementation.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
==See Also==&lt;br /&gt;
1. Lucas Carlson and Leonard Richardson. 2006. [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=1212915&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 Ruby Cookbook] (Cookbooks (O'Reilly)). O'Reilly Media, Inc..&lt;br /&gt;
&lt;br /&gt;
2. Herbert Schildt. 2000. Java 2: [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=557816&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 The Complete Reference] (4th ed.). McGraw-Hill Professional.&lt;/div&gt;</summary>
		<author><name>Snallan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65441</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65441"/>
		<updated>2012-09-21T13:53:44Z</updated>

		<summary type="html">&lt;p&gt;Snallan: /* Mixins */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''1w23:  Multiple Inheritance and Mixins&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
The purpose of this article is to give a brief introduction about multiple inheritance, consider some of its issues and discuss techniques employed by different programming languages to handle such issues. In this relation, the article also compares solutions provided by different programming languages, especially against Ruby mixins, by listing their advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
Multiple Inheritance is a technique of establishing hierarchical relationships within a set of classes, such that a single class can inherit  behaviors from multiple parent classes. Although it might seem useful to include methods from multiple classes, the major issue with multiple inheritance is the ambiguity in referencing the parent class methods,  when more than one parent classes share a method with the same name.This ambiguity is predominant in cases where the inheritance hierarchy is more than one level deep, where the class in the lowermost level inherits from two classes, that have overridden the same method from their parent in different ways.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A Real World Example==&lt;br /&gt;
A known example of multiple inheritance to represent animals using classes and objects is cited for the sake of understanding &amp;lt;ref&amp;gt; http://stackoverflow.com/questions/9359948/multiple-inheritance-whats-a-good-example&amp;lt;/ref&amp;gt;. A Winged animal and a mammal inherit primitive attributes of an animal. While a winged animal can fly; a mammal can breathe and reproduce; they both eat, move and share features generic to animals. A bat being a winged mammal possesses features common to both winged animal and mammal. It follows that class Bat inherits from classes WingedAnimal and Mammal and in turn classes WingedAnimal and Mammal inherit from class Animal.   In reference to this context, let us give our own implementation and explore the issues of multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
[[File:Class_Diagram.png|thumb|right|x400px|alt=Class Diagram for the example.]]&lt;br /&gt;
&lt;br /&gt;
==Issues of Multiple Inheritance==&lt;br /&gt;
1. Due to inheritance, the parent class’s objects get duplicated to child class. As a result, Class Bat duplicates Animal objects through each of WingedAnimal and Mammal classes. When Bat accesses any of the Animal's attributes, the compiler would be ambiguous about determining the correct path to use since two paths are available from Animal to Bat. This problem of multiple inheritance is called diamond problem &amp;lt;ref&amp;gt;http://www.artima.com/weblogs/viewpost.jsp?thread=246488&amp;lt;/ref&amp;gt;. Compiler takes arbitrary decisions during such situations. &lt;br /&gt;
&lt;br /&gt;
2.Objects of parent classes can be separately managed by using [http://en.wikipedia.org/wiki/Virtual_method_table virtual tables]as in C++. By explicitly specifying the virtual table pointer, the Bat class can access WingedAnimal and Mammal classes unambiguously. However virtual tables occupy significant portion of memory thereby limiting their extensive usage.  &lt;br /&gt;
&lt;br /&gt;
3. The order by which a child class inherits parent classes also matters. If Bat inherits WingedAnimal prior to Mammal, then WingedAnimal precedes Mammal and common methods are accessed in this order of precedence.  Hence if the Programmer is not aware of such precedence, debugging would be difficult.&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
Programming languages provide different solutions to handle the issues of multiple inheritance. Here we consider the techniques offered by C++, Java, Ruby, Scala and Python.    &lt;br /&gt;
===Multiple Inheritance in C++===&lt;br /&gt;
C++ supports multiple inheritance with the notion of virtual classes and virtual functions. In the example, the Animal class defines virtual functions with WingedAnimal and Mammal classes virtually inheriting the features of Animal. The Bat class can now unambiguously access canEat() method of Animal class, using virtual table pointers of WingedAnimal and Mammal. The example written using C++ virtual functions is shown below.&lt;br /&gt;
&lt;br /&gt;
 class Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void virtual canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class WingedAnimal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Mammal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Bat :public WingedAnimal, public Mammal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }  &lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
  int i = 0;&lt;br /&gt;
  Animal *instances[3];&lt;br /&gt;
  WingedAnimal w;&lt;br /&gt;
  Mammal m;&lt;br /&gt;
  Bat b;&lt;br /&gt;
  instances[0] = &amp;amp;w;&lt;br /&gt;
  instances[1] = &amp;amp;m;&lt;br /&gt;
  instances[2] = &amp;amp;b;&lt;br /&gt;
  while(i&amp;lt;3)&lt;br /&gt;
    instances[i++]-&amp;gt;canEat();&lt;br /&gt;
  return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Java===&lt;br /&gt;
Java doesn’t support multiple inheritance but provides a feature called interfaces to simulate inheritance from multiple classes. Interface is an abstract class which provides a generic template by just declaring variables and methods without implementing them. [http://www.brpreiss.com/books/opus4/html/page611.html Concrete classes] that inherit interfaces are supposed to implement its methods. The example written using Java interfaces is shown below.&lt;br /&gt;
&lt;br /&gt;
 interface Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface WingedAnimal extends Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();  &lt;br /&gt;
   void canFly();  &lt;br /&gt;
 }&lt;br /&gt;
 class ImplementWingedAnimal implements WingedAnimal  &lt;br /&gt;
 {&lt;br /&gt;
    public void canEat() {    &lt;br /&gt;
    }&lt;br /&gt;
    public void canFly() {    &lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Mammal extends Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();&lt;br /&gt;
   void canGiveBirth();&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementMammal implements Mammal&lt;br /&gt;
 {&lt;br /&gt;
    public void canEat() {    &lt;br /&gt;
    }&lt;br /&gt;
    public void canGiveBirth() {    &lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Bat extends WingedAnimal, Mammal&lt;br /&gt;
 {&lt;br /&gt;
    void canEat();    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class ImplementBat implements Bat&lt;br /&gt;
 {  &lt;br /&gt;
    ImplementWingedAnimal w = new ImplementWingedAnimal();&lt;br /&gt;
    ImplementMammal m = new ImplementMammal();     &lt;br /&gt;
    public void canEat() {            &lt;br /&gt;
      m.canEat();  &lt;br /&gt;
      w.canEat();      &lt;br /&gt;
    }&lt;br /&gt;
    public void canFly() {&lt;br /&gt;
    }&lt;br /&gt;
    public void canGiveBirth() {&lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class MultipleInheritance&lt;br /&gt;
 {&lt;br /&gt;
   public static void main(String args[])&lt;br /&gt;
   {&lt;br /&gt;
     ImplementBat b = new ImplementBat ();&lt;br /&gt;
     b.canEat();       &lt;br /&gt;
   } &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Ruby===&lt;br /&gt;
Ruby doesn't support multiple inheritance but provides a similar feature using modules and mixins.  &lt;br /&gt;
====Modules====&lt;br /&gt;
A Module is an encapsulation of methods and variables, although similar in function to a Class, differs from a class in the sense that it cannot be instantiated. Modules are  more dynamic and allow us to organize the code in a more granular way.Each module can add something to the existing class. A module can be thought of an encapsulation of a  subset of the full capabilities of an object.&lt;br /&gt;
An example found in the book ,The Well-Grounded Rubyist &amp;lt;ref&amp;gt;http://www.manning.com/black2/SampleChapter4.pdf [Pg.92] &amp;lt;/ref&amp;gt;, is the stack-like module. The module encapsulates the characteristics of being like a stack,implements the operations that can be performed by a stack but is not the actual Stack class. This allows us to use this module to mix-in  stack-like behaviour into other classes that require them.&lt;br /&gt;
&lt;br /&gt;
====Mixins====&lt;br /&gt;
A Mixin is a concept that allows inclusion of special characteristics into existing classes, so that common features that need to be reused in several classes can be selectively included, thus avoiding implementation of unnecessary functions like in the case of interfaces.&lt;br /&gt;
Mixins allow us to dynamically bind special features defined in modules, into core classes without really making any changes to the objects. This improves flexibility when we want to add plugins and extensions to the existing framework.&amp;lt;ref&amp;gt;http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-frameworks/&amp;lt;/ref&amp;gt;.The source code for the same example implemented using ruby mixins is shown below.&lt;br /&gt;
&lt;br /&gt;
 module Animal&lt;br /&gt;
   def canEat     &lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module WingedAnimal&lt;br /&gt;
   include Animal    &lt;br /&gt;
   def canEat&lt;br /&gt;
   end      &lt;br /&gt;
   def canFly&lt;br /&gt;
   end      &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Mammal&lt;br /&gt;
   include Animal&lt;br /&gt;
   def canEat&lt;br /&gt;
   end&lt;br /&gt;
   def canGiveBirth&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Bat&lt;br /&gt;
   include WingedAnimal&lt;br /&gt;
   include Mammal&lt;br /&gt;
   include Animal  &lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 Bat.new().canEat()&lt;br /&gt;
&lt;br /&gt;
===Multiple inheritance in other languages===&lt;br /&gt;
Scala offers traits that are similar to Java interfaces to support multiple inheritance. Traits define a template with methods and variables which any class can make use of. The difference between interfaces and traits is that traits can implement methods. A Scala class can inherit from any number of traits together with a class in way to support multiple inheritance. Traits can be mixed in to a class even without inheriting it but just by including it at the time of creating instances. &amp;lt;ref&amp;gt;http://joelabrahamsson.com/entry/learning-scala-traits&amp;lt;/ref&amp;gt;. Trait can also extend a class and methods to be overridden are usually prefixed by the keyword override. The language enforces method names to be different across inherited  traits to avoid the issues of multiple inheritance. Python supports a limited version of multiple inheritance by extending multiple classes in depth-first left-right order &amp;lt;ref&amp;gt;http://docs.python.org/tutorial/classes.html&amp;lt;/ref&amp;gt;. If a python class D inherits classes A, B, C and if an instance variable or method is not available in the current class, it checks in A and its base classes, then moves to B and its base classes and eventually to C thus moving from left to right. In this way, python avoids the diamond problem by making the search order linear by which the derived class accesses attributes of its parent classes. In addition python also defines mixins to access reusable features within a class.&lt;br /&gt;
&lt;br /&gt;
==Advantages of mixins==&lt;br /&gt;
1. Firstly, when a module gets mixed into different classes, each class acquires the same  reference  to the module and hence even dynamic changes to the module gets reflected in the behavior of the classes &amp;lt;ref&amp;gt;http://www.linuxtopia.org/online_books/programming_books/ruby_tutorial/Ruby_Modules_Mixins.html&amp;lt;/ref&amp;gt;. In this way, the module’s code is not just copied to individual classes as in static languages instead its reference is provided. &lt;br /&gt;
&lt;br /&gt;
2. Secondly, the mixed in state and behavior can be accessed by the instance of target class just like accessing its own features. &lt;br /&gt;
&lt;br /&gt;
3. Thirdly, a class can include any number of modules in a way it can simulate multiple inheritance without having the related issues.&lt;br /&gt;
&lt;br /&gt;
==Questions to Ponder==&lt;br /&gt;
===Do mixins solve all of the problems of multiple inheritance?===&lt;br /&gt;
Undoubtedly mixins solve some of the problems of multiple inheritance. There is no ambiguity in resolving the request to a common method implemented by multiple modules. In that case, the lastly included module is responsible for handling accesses to common methods. Though the order of precedence of included modules matters, it looks clearer than C++.  However languages like Scala and Eiffel implement multiple inheritance far more safely  by explicitly forcing the user to have different method names across inherited classes.&lt;br /&gt;
&lt;br /&gt;
===Are mixins a clear advantage over interfaces?===&lt;br /&gt;
Mixins are more advantageous than interfaces because of their features. Interfaces have some limitations. As seen from programs, the derived class Allrounder uses the instances of Batsman and Bowler classes (has-a relationship) to access respective attributes. Interface is nice in the sense it provides a generic template that could be implemented differently by different classes.  However the target class has to necessarily implement every method of the interface. This would pose problems to a Class which is interested in implementing specific features of the interface. On the contrary, Mixins eliminate this inconvenience by mixing in the state and behavior of the modules in the target class and allows a it to use them as it needs. Hence the target class can just use module’s features selectively or completely instead of implementing by itself. The size of multiple inheritance code using mixins is smaller than interfaces and C++ variants.&lt;br /&gt;
&lt;br /&gt;
===Do mixins have any disadvantages not shared by multiple inheritance or interfaces?===&lt;br /&gt;
Inspite of making the lives of programmers better with effective code reuse and avoiding unnecessary implementation of methods that cause clutter in the code, the concept of Mixins can easily be abused resulting in namespace pollution. This is caused when a class includes multiple modules each of which contains several hundred method implementations. Having hundreds of methods mixed into a single class, makes it difficult to identify the source modules of the methods, thereby causing confusion while implementing these methods, whereas in the case of multiple inheritance we can easily identify the class to which the method belongs.&lt;br /&gt;
Another disadvantage that follows from the namespace collision is the case where we have multiple methods with the same name in different modules.In such a case whenever a call to that method is made, the method which belongs to the module that was included last will be called. The problem with this is that, this takes place silently without any warning. This may result in accidental overriding of wrong methods.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
In this article, we have discussed about multiple inheritance and means by which it is implemented in different languages. Though multiple inheritance is a useful feature,&lt;br /&gt;
many languages do not support them due to its associated issues. Given its advantages, mixin in ruby seems to be a viable option to achieve multiple inheritance. However mixins too have limitations which should be taken care of during implementation.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
==See Also==&lt;br /&gt;
1. Lucas Carlson and Leonard Richardson. 2006. [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=1212915&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 Ruby Cookbook] (Cookbooks (O'Reilly)). O'Reilly Media, Inc..&lt;br /&gt;
&lt;br /&gt;
2. Herbert Schildt. 2000. Java 2: [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=557816&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 The Complete Reference] (4th ed.). McGraw-Hill Professional.&lt;/div&gt;</summary>
		<author><name>Snallan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65440</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65440"/>
		<updated>2012-09-21T13:43:43Z</updated>

		<summary type="html">&lt;p&gt;Snallan: /* Multiple Inheritance in Java */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''1w23:  Multiple Inheritance and Mixins&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
The purpose of this article is to give a brief introduction about multiple inheritance, consider some of its issues and discuss techniques employed by different programming languages to handle such issues. In this relation, the article also compares solutions provided by different programming languages, especially against Ruby mixins, by listing their advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
Multiple Inheritance is a technique of establishing hierarchical relationships within a set of classes, such that a single class can inherit  behaviors from multiple parent classes. Although it might seem useful to include methods from multiple classes, the major issue with multiple inheritance is the ambiguity in referencing the parent class methods,  when more than one parent classes share a method with the same name.This ambiguity is predominant in cases where the inheritance hierarchy is more than one level deep, where the class in the lowermost level inherits from two classes, that have overridden the same method from their parent in different ways.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A Real World Example==&lt;br /&gt;
A known example of multiple inheritance to represent animals using classes and objects is cited for the sake of understanding &amp;lt;ref&amp;gt; http://stackoverflow.com/questions/9359948/multiple-inheritance-whats-a-good-example&amp;lt;/ref&amp;gt;. A Winged animal and a mammal inherit primitive attributes of an animal. While a winged animal can fly; a mammal can breathe and reproduce; they both eat, move and share features generic to animals. A bat being a winged mammal possesses features common to both winged animal and mammal. It follows that class Bat inherits from classes WingedAnimal and Mammal and in turn classes WingedAnimal and Mammal inherit from class Animal.   In reference to this context, let us give our own implementation and explore the issues of multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
[[File:Class_Diagram.png|thumb|right|x400px|alt=Class Diagram for the example.]]&lt;br /&gt;
&lt;br /&gt;
==Issues of Multiple Inheritance==&lt;br /&gt;
1. Due to inheritance, the parent class’s objects get duplicated to child class. As a result, Class Bat duplicates Animal objects through each of WingedAnimal and Mammal classes. When Bat accesses any of the Animal's attributes, the compiler would be ambiguous about determining the correct path to use since two paths are available from Animal to Bat. This problem of multiple inheritance is called diamond problem &amp;lt;ref&amp;gt;http://www.artima.com/weblogs/viewpost.jsp?thread=246488&amp;lt;/ref&amp;gt;. Compiler takes arbitrary decisions during such situations. &lt;br /&gt;
&lt;br /&gt;
2.Objects of parent classes can be separately managed by using [http://en.wikipedia.org/wiki/Virtual_method_table virtual tables]as in C++. By explicitly specifying the virtual table pointer, the Bat class can access WingedAnimal and Mammal classes unambiguously. However virtual tables occupy significant portion of memory thereby limiting their extensive usage.  &lt;br /&gt;
&lt;br /&gt;
3. The order by which a child class inherits parent classes also matters. If Bat inherits WingedAnimal prior to Mammal, then WingedAnimal precedes Mammal and common methods are accessed in this order of precedence.  Hence if the Programmer is not aware of such precedence, debugging would be difficult.&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
Programming languages provide different solutions to handle the issues of multiple inheritance. Here we consider the techniques offered by C++, Java, Ruby, Scala and Python.    &lt;br /&gt;
===Multiple Inheritance in C++===&lt;br /&gt;
C++ supports multiple inheritance with the notion of virtual classes and virtual functions. In the example, the Animal class defines virtual functions with WingedAnimal and Mammal classes virtually inheriting the features of Animal. The Bat class can now unambiguously access canEat() method of Animal class, using virtual table pointers of WingedAnimal and Mammal. The example written using C++ virtual functions is shown below.&lt;br /&gt;
&lt;br /&gt;
 class Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void virtual canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class WingedAnimal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Mammal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Bat :public WingedAnimal, public Mammal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }  &lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
  int i = 0;&lt;br /&gt;
  Animal *instances[3];&lt;br /&gt;
  WingedAnimal w;&lt;br /&gt;
  Mammal m;&lt;br /&gt;
  Bat b;&lt;br /&gt;
  instances[0] = &amp;amp;w;&lt;br /&gt;
  instances[1] = &amp;amp;m;&lt;br /&gt;
  instances[2] = &amp;amp;b;&lt;br /&gt;
  while(i&amp;lt;3)&lt;br /&gt;
    instances[i++]-&amp;gt;canEat();&lt;br /&gt;
  return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Java===&lt;br /&gt;
Java doesn’t support multiple inheritance but provides a feature called interfaces to simulate inheritance from multiple classes. Interface is an abstract class which provides a generic template by just declaring variables and methods without implementing them. [http://www.brpreiss.com/books/opus4/html/page611.html Concrete classes] that inherit interfaces are supposed to implement its methods. The example written using Java interfaces is shown below.&lt;br /&gt;
&lt;br /&gt;
 interface Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface WingedAnimal extends Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();  &lt;br /&gt;
   void canFly();  &lt;br /&gt;
 }&lt;br /&gt;
 class ImplementWingedAnimal implements WingedAnimal  &lt;br /&gt;
 {&lt;br /&gt;
    public void canEat() {    &lt;br /&gt;
    }&lt;br /&gt;
    public void canFly() {    &lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Mammal extends Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();&lt;br /&gt;
   void canGiveBirth();&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementMammal implements Mammal&lt;br /&gt;
 {&lt;br /&gt;
    public void canEat() {    &lt;br /&gt;
    }&lt;br /&gt;
    public void canGiveBirth() {    &lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Bat extends WingedAnimal, Mammal&lt;br /&gt;
 {&lt;br /&gt;
    void canEat();    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class ImplementBat implements Bat&lt;br /&gt;
 {  &lt;br /&gt;
    ImplementWingedAnimal w = new ImplementWingedAnimal();&lt;br /&gt;
    ImplementMammal m = new ImplementMammal();     &lt;br /&gt;
    public void canEat() {            &lt;br /&gt;
      m.canEat();  &lt;br /&gt;
      w.canEat();      &lt;br /&gt;
    }&lt;br /&gt;
    public void canFly() {&lt;br /&gt;
    }&lt;br /&gt;
    public void canGiveBirth() {&lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class MultipleInheritance&lt;br /&gt;
 {&lt;br /&gt;
   public static void main(String args[])&lt;br /&gt;
   {&lt;br /&gt;
     ImplementBat b = new ImplementBat ();&lt;br /&gt;
     b.canEat();       &lt;br /&gt;
   } &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Ruby===&lt;br /&gt;
Ruby doesn't support multiple inheritance but provides a similar feature using modules and mixins.  &lt;br /&gt;
====Modules====&lt;br /&gt;
A Module is an encapsulation of methods and variables, although similar in function to a Class, differs from a class in the sense that it cannot be instantiated. Modules are  more dynamic and allow us to organize the code in a more granular way.Each module can add something to the existing class. A module can be thought of an encapsulation of a  subset of the full capabilities of an object.&lt;br /&gt;
An example found in the book ,The Well-Grounded Rubyist &amp;lt;ref&amp;gt;http://www.manning.com/black2/SampleChapter4.pdf [Pg.92] &amp;lt;/ref&amp;gt;, is the stack-like module. The module encapsulates the characteristics of being like a stack,implements the operations that can be performed by a stack but is not the actual Stack class. This allows us to use this module to mix-in  stack-like behaviour into other classes that require them.&lt;br /&gt;
&lt;br /&gt;
====Mixins====&lt;br /&gt;
A Mixin is a concept that allows inclusion of special characteristics into existing classes, so that common features that need to be reused in several classes can be selectively included, thus avoiding implementation of unnecessary functions like in the case of interfaces.&lt;br /&gt;
Mixins allow us to dynamically bind special features defined in modules, into core classes without really making any changes to the objects. This improves flexibility when we want to add plugins and extensions to the existing framework.&amp;lt;ref&amp;gt;http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-frameworks/&amp;lt;/ref&amp;gt;.The source code for the same example implemented using ruby mixins is shown below.&lt;br /&gt;
&lt;br /&gt;
 module Cricketer&lt;br /&gt;
   def statistics&lt;br /&gt;
     puts &amp;quot;Cricketer's statistics include number of matches, average etc.&amp;quot;&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Batsman&lt;br /&gt;
   include Cricketer&lt;br /&gt;
   def statistics&lt;br /&gt;
     puts &amp;quot;Batsman's statistics include batting average, number of 100's &amp;amp; 50's etc.&amp;quot;&lt;br /&gt;
   end    &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Bowler&lt;br /&gt;
   include Cricketer&lt;br /&gt;
   def statistics&lt;br /&gt;
      puts &amp;quot;Bowler's statistics include number of wickets, bowling average, economy etc.&amp;quot;&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class AllRounder&lt;br /&gt;
   include Batsman&lt;br /&gt;
   include Bowler&lt;br /&gt;
   include Cricketer&lt;br /&gt;
   # Other implementation&lt;br /&gt;
 end&lt;br /&gt;
 AllRounder.new().statistics()&lt;br /&gt;
&lt;br /&gt;
===Multiple inheritance in other languages===&lt;br /&gt;
Scala offers traits that are similar to Java interfaces to support multiple inheritance. Traits define a template with methods and variables which any class can make use of. The difference between interfaces and traits is that traits can implement methods. A Scala class can inherit from any number of traits together with a class in way to support multiple inheritance. Traits can be mixed in to a class even without inheriting it but just by including it at the time of creating instances. &amp;lt;ref&amp;gt;http://joelabrahamsson.com/entry/learning-scala-traits&amp;lt;/ref&amp;gt;. Trait can also extend a class and methods to be overridden are usually prefixed by the keyword override. The language enforces method names to be different across inherited  traits to avoid the issues of multiple inheritance. Python supports a limited version of multiple inheritance by extending multiple classes in depth-first left-right order &amp;lt;ref&amp;gt;http://docs.python.org/tutorial/classes.html&amp;lt;/ref&amp;gt;. If a python class D inherits classes A, B, C and if an instance variable or method is not available in the current class, it checks in A and its base classes, then moves to B and its base classes and eventually to C thus moving from left to right. In this way, python avoids the diamond problem by making the search order linear by which the derived class accesses attributes of its parent classes. In addition python also defines mixins to access reusable features within a class.&lt;br /&gt;
&lt;br /&gt;
==Advantages of mixins==&lt;br /&gt;
1. Firstly, when a module gets mixed into different classes, each class acquires the same  reference  to the module and hence even dynamic changes to the module gets reflected in the behavior of the classes &amp;lt;ref&amp;gt;http://www.linuxtopia.org/online_books/programming_books/ruby_tutorial/Ruby_Modules_Mixins.html&amp;lt;/ref&amp;gt;. In this way, the module’s code is not just copied to individual classes as in static languages instead its reference is provided. &lt;br /&gt;
&lt;br /&gt;
2. Secondly, the mixed in state and behavior can be accessed by the instance of target class just like accessing its own features. &lt;br /&gt;
&lt;br /&gt;
3. Thirdly, a class can include any number of modules in a way it can simulate multiple inheritance without having the related issues.&lt;br /&gt;
&lt;br /&gt;
==Questions to Ponder==&lt;br /&gt;
===Do mixins solve all of the problems of multiple inheritance?===&lt;br /&gt;
Undoubtedly mixins solve some of the problems of multiple inheritance. There is no ambiguity in resolving the request to a common method implemented by multiple modules. In that case, the lastly included module is responsible for handling accesses to common methods. Though the order of precedence of included modules matters, it looks clearer than C++.  However languages like Scala and Eiffel implement multiple inheritance far more safely  by explicitly forcing the user to have different method names across inherited classes.&lt;br /&gt;
&lt;br /&gt;
===Are mixins a clear advantage over interfaces?===&lt;br /&gt;
Mixins are more advantageous than interfaces because of their features. Interfaces have some limitations. As seen from programs, the derived class Allrounder uses the instances of Batsman and Bowler classes (has-a relationship) to access respective attributes. Interface is nice in the sense it provides a generic template that could be implemented differently by different classes.  However the target class has to necessarily implement every method of the interface. This would pose problems to a Class which is interested in implementing specific features of the interface. On the contrary, Mixins eliminate this inconvenience by mixing in the state and behavior of the modules in the target class and allows a it to use them as it needs. Hence the target class can just use module’s features selectively or completely instead of implementing by itself. The size of multiple inheritance code using mixins is smaller than interfaces and C++ variants.&lt;br /&gt;
&lt;br /&gt;
===Do mixins have any disadvantages not shared by multiple inheritance or interfaces?===&lt;br /&gt;
Inspite of making the lives of programmers better with effective code reuse and avoiding unnecessary implementation of methods that cause clutter in the code, the concept of Mixins can easily be abused resulting in namespace pollution. This is caused when a class includes multiple modules each of which contains several hundred method implementations. Having hundreds of methods mixed into a single class, makes it difficult to identify the source modules of the methods, thereby causing confusion while implementing these methods, whereas in the case of multiple inheritance we can easily identify the class to which the method belongs.&lt;br /&gt;
Another disadvantage that follows from the namespace collision is the case where we have multiple methods with the same name in different modules.In such a case whenever a call to that method is made, the method which belongs to the module that was included last will be called. The problem with this is that, this takes place silently without any warning. This may result in accidental overriding of wrong methods.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
In this article, we have discussed about multiple inheritance and means by which it is implemented in different languages. Though multiple inheritance is a useful feature,&lt;br /&gt;
many languages do not support them due to its associated issues. Given its advantages, mixin in ruby seems to be a viable option to achieve multiple inheritance. However mixins too have limitations which should be taken care of during implementation.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
==See Also==&lt;br /&gt;
1. Lucas Carlson and Leonard Richardson. 2006. [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=1212915&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 Ruby Cookbook] (Cookbooks (O'Reilly)). O'Reilly Media, Inc..&lt;br /&gt;
&lt;br /&gt;
2. Herbert Schildt. 2000. Java 2: [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=557816&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 The Complete Reference] (4th ed.). McGraw-Hill Professional.&lt;/div&gt;</summary>
		<author><name>Snallan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65439</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65439"/>
		<updated>2012-09-21T13:43:17Z</updated>

		<summary type="html">&lt;p&gt;Snallan: /* Multiple Inheritance in Java */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''1w23:  Multiple Inheritance and Mixins&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
The purpose of this article is to give a brief introduction about multiple inheritance, consider some of its issues and discuss techniques employed by different programming languages to handle such issues. In this relation, the article also compares solutions provided by different programming languages, especially against Ruby mixins, by listing their advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
Multiple Inheritance is a technique of establishing hierarchical relationships within a set of classes, such that a single class can inherit  behaviors from multiple parent classes. Although it might seem useful to include methods from multiple classes, the major issue with multiple inheritance is the ambiguity in referencing the parent class methods,  when more than one parent classes share a method with the same name.This ambiguity is predominant in cases where the inheritance hierarchy is more than one level deep, where the class in the lowermost level inherits from two classes, that have overridden the same method from their parent in different ways.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A Real World Example==&lt;br /&gt;
A known example of multiple inheritance to represent animals using classes and objects is cited for the sake of understanding &amp;lt;ref&amp;gt; http://stackoverflow.com/questions/9359948/multiple-inheritance-whats-a-good-example&amp;lt;/ref&amp;gt;. A Winged animal and a mammal inherit primitive attributes of an animal. While a winged animal can fly; a mammal can breathe and reproduce; they both eat, move and share features generic to animals. A bat being a winged mammal possesses features common to both winged animal and mammal. It follows that class Bat inherits from classes WingedAnimal and Mammal and in turn classes WingedAnimal and Mammal inherit from class Animal.   In reference to this context, let us give our own implementation and explore the issues of multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
[[File:Class_Diagram.png|thumb|right|x400px|alt=Class Diagram for the example.]]&lt;br /&gt;
&lt;br /&gt;
==Issues of Multiple Inheritance==&lt;br /&gt;
1. Due to inheritance, the parent class’s objects get duplicated to child class. As a result, Class Bat duplicates Animal objects through each of WingedAnimal and Mammal classes. When Bat accesses any of the Animal's attributes, the compiler would be ambiguous about determining the correct path to use since two paths are available from Animal to Bat. This problem of multiple inheritance is called diamond problem &amp;lt;ref&amp;gt;http://www.artima.com/weblogs/viewpost.jsp?thread=246488&amp;lt;/ref&amp;gt;. Compiler takes arbitrary decisions during such situations. &lt;br /&gt;
&lt;br /&gt;
2.Objects of parent classes can be separately managed by using [http://en.wikipedia.org/wiki/Virtual_method_table virtual tables]as in C++. By explicitly specifying the virtual table pointer, the Bat class can access WingedAnimal and Mammal classes unambiguously. However virtual tables occupy significant portion of memory thereby limiting their extensive usage.  &lt;br /&gt;
&lt;br /&gt;
3. The order by which a child class inherits parent classes also matters. If Bat inherits WingedAnimal prior to Mammal, then WingedAnimal precedes Mammal and common methods are accessed in this order of precedence.  Hence if the Programmer is not aware of such precedence, debugging would be difficult.&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
Programming languages provide different solutions to handle the issues of multiple inheritance. Here we consider the techniques offered by C++, Java, Ruby, Scala and Python.    &lt;br /&gt;
===Multiple Inheritance in C++===&lt;br /&gt;
C++ supports multiple inheritance with the notion of virtual classes and virtual functions. In the example, the Animal class defines virtual functions with WingedAnimal and Mammal classes virtually inheriting the features of Animal. The Bat class can now unambiguously access canEat() method of Animal class, using virtual table pointers of WingedAnimal and Mammal. The example written using C++ virtual functions is shown below.&lt;br /&gt;
&lt;br /&gt;
 class Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void virtual canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class WingedAnimal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Mammal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Bat :public WingedAnimal, public Mammal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }  &lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
  int i = 0;&lt;br /&gt;
  Animal *instances[3];&lt;br /&gt;
  WingedAnimal w;&lt;br /&gt;
  Mammal m;&lt;br /&gt;
  Bat b;&lt;br /&gt;
  instances[0] = &amp;amp;w;&lt;br /&gt;
  instances[1] = &amp;amp;m;&lt;br /&gt;
  instances[2] = &amp;amp;b;&lt;br /&gt;
  while(i&amp;lt;3)&lt;br /&gt;
    instances[i++]-&amp;gt;canEat();&lt;br /&gt;
  return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Java===&lt;br /&gt;
Java doesn’t support multiple inheritance but provides a feature called interfaces to simulate inheritance from multiple classes. Interface is an abstract class which provides a generic template by just declaring variables and methods without implementing them. [http://www.brpreiss.com/books/opus4/html/page611.html]Concrete classes that inherit interfaces are supposed to implement its methods. The example written using Java interfaces is shown below.&lt;br /&gt;
&lt;br /&gt;
 interface Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface WingedAnimal extends Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();  &lt;br /&gt;
   void canFly();  &lt;br /&gt;
 }&lt;br /&gt;
 class ImplementWingedAnimal implements WingedAnimal  &lt;br /&gt;
 {&lt;br /&gt;
    public void canEat() {    &lt;br /&gt;
    }&lt;br /&gt;
    public void canFly() {    &lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Mammal extends Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();&lt;br /&gt;
   void canGiveBirth();&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementMammal implements Mammal&lt;br /&gt;
 {&lt;br /&gt;
    public void canEat() {    &lt;br /&gt;
    }&lt;br /&gt;
    public void canGiveBirth() {    &lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Bat extends WingedAnimal, Mammal&lt;br /&gt;
 {&lt;br /&gt;
    void canEat();    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class ImplementBat implements Bat&lt;br /&gt;
 {  &lt;br /&gt;
    ImplementWingedAnimal w = new ImplementWingedAnimal();&lt;br /&gt;
    ImplementMammal m = new ImplementMammal();     &lt;br /&gt;
    public void canEat() {            &lt;br /&gt;
      m.canEat();  &lt;br /&gt;
      w.canEat();      &lt;br /&gt;
    }&lt;br /&gt;
    public void canFly() {&lt;br /&gt;
    }&lt;br /&gt;
    public void canGiveBirth() {&lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class MultipleInheritance&lt;br /&gt;
 {&lt;br /&gt;
   public static void main(String args[])&lt;br /&gt;
   {&lt;br /&gt;
     ImplementBat b = new ImplementBat ();&lt;br /&gt;
     b.canEat();       &lt;br /&gt;
   } &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Ruby===&lt;br /&gt;
Ruby doesn't support multiple inheritance but provides a similar feature using modules and mixins.  &lt;br /&gt;
====Modules====&lt;br /&gt;
A Module is an encapsulation of methods and variables, although similar in function to a Class, differs from a class in the sense that it cannot be instantiated. Modules are  more dynamic and allow us to organize the code in a more granular way.Each module can add something to the existing class. A module can be thought of an encapsulation of a  subset of the full capabilities of an object.&lt;br /&gt;
An example found in the book ,The Well-Grounded Rubyist &amp;lt;ref&amp;gt;http://www.manning.com/black2/SampleChapter4.pdf [Pg.92] &amp;lt;/ref&amp;gt;, is the stack-like module. The module encapsulates the characteristics of being like a stack,implements the operations that can be performed by a stack but is not the actual Stack class. This allows us to use this module to mix-in  stack-like behaviour into other classes that require them.&lt;br /&gt;
&lt;br /&gt;
====Mixins====&lt;br /&gt;
A Mixin is a concept that allows inclusion of special characteristics into existing classes, so that common features that need to be reused in several classes can be selectively included, thus avoiding implementation of unnecessary functions like in the case of interfaces.&lt;br /&gt;
Mixins allow us to dynamically bind special features defined in modules, into core classes without really making any changes to the objects. This improves flexibility when we want to add plugins and extensions to the existing framework.&amp;lt;ref&amp;gt;http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-frameworks/&amp;lt;/ref&amp;gt;.The source code for the same example implemented using ruby mixins is shown below.&lt;br /&gt;
&lt;br /&gt;
 module Cricketer&lt;br /&gt;
   def statistics&lt;br /&gt;
     puts &amp;quot;Cricketer's statistics include number of matches, average etc.&amp;quot;&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Batsman&lt;br /&gt;
   include Cricketer&lt;br /&gt;
   def statistics&lt;br /&gt;
     puts &amp;quot;Batsman's statistics include batting average, number of 100's &amp;amp; 50's etc.&amp;quot;&lt;br /&gt;
   end    &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Bowler&lt;br /&gt;
   include Cricketer&lt;br /&gt;
   def statistics&lt;br /&gt;
      puts &amp;quot;Bowler's statistics include number of wickets, bowling average, economy etc.&amp;quot;&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class AllRounder&lt;br /&gt;
   include Batsman&lt;br /&gt;
   include Bowler&lt;br /&gt;
   include Cricketer&lt;br /&gt;
   # Other implementation&lt;br /&gt;
 end&lt;br /&gt;
 AllRounder.new().statistics()&lt;br /&gt;
&lt;br /&gt;
===Multiple inheritance in other languages===&lt;br /&gt;
Scala offers traits that are similar to Java interfaces to support multiple inheritance. Traits define a template with methods and variables which any class can make use of. The difference between interfaces and traits is that traits can implement methods. A Scala class can inherit from any number of traits together with a class in way to support multiple inheritance. Traits can be mixed in to a class even without inheriting it but just by including it at the time of creating instances. &amp;lt;ref&amp;gt;http://joelabrahamsson.com/entry/learning-scala-traits&amp;lt;/ref&amp;gt;. Trait can also extend a class and methods to be overridden are usually prefixed by the keyword override. The language enforces method names to be different across inherited  traits to avoid the issues of multiple inheritance. Python supports a limited version of multiple inheritance by extending multiple classes in depth-first left-right order &amp;lt;ref&amp;gt;http://docs.python.org/tutorial/classes.html&amp;lt;/ref&amp;gt;. If a python class D inherits classes A, B, C and if an instance variable or method is not available in the current class, it checks in A and its base classes, then moves to B and its base classes and eventually to C thus moving from left to right. In this way, python avoids the diamond problem by making the search order linear by which the derived class accesses attributes of its parent classes. In addition python also defines mixins to access reusable features within a class.&lt;br /&gt;
&lt;br /&gt;
==Advantages of mixins==&lt;br /&gt;
1. Firstly, when a module gets mixed into different classes, each class acquires the same  reference  to the module and hence even dynamic changes to the module gets reflected in the behavior of the classes &amp;lt;ref&amp;gt;http://www.linuxtopia.org/online_books/programming_books/ruby_tutorial/Ruby_Modules_Mixins.html&amp;lt;/ref&amp;gt;. In this way, the module’s code is not just copied to individual classes as in static languages instead its reference is provided. &lt;br /&gt;
&lt;br /&gt;
2. Secondly, the mixed in state and behavior can be accessed by the instance of target class just like accessing its own features. &lt;br /&gt;
&lt;br /&gt;
3. Thirdly, a class can include any number of modules in a way it can simulate multiple inheritance without having the related issues.&lt;br /&gt;
&lt;br /&gt;
==Questions to Ponder==&lt;br /&gt;
===Do mixins solve all of the problems of multiple inheritance?===&lt;br /&gt;
Undoubtedly mixins solve some of the problems of multiple inheritance. There is no ambiguity in resolving the request to a common method implemented by multiple modules. In that case, the lastly included module is responsible for handling accesses to common methods. Though the order of precedence of included modules matters, it looks clearer than C++.  However languages like Scala and Eiffel implement multiple inheritance far more safely  by explicitly forcing the user to have different method names across inherited classes.&lt;br /&gt;
&lt;br /&gt;
===Are mixins a clear advantage over interfaces?===&lt;br /&gt;
Mixins are more advantageous than interfaces because of their features. Interfaces have some limitations. As seen from programs, the derived class Allrounder uses the instances of Batsman and Bowler classes (has-a relationship) to access respective attributes. Interface is nice in the sense it provides a generic template that could be implemented differently by different classes.  However the target class has to necessarily implement every method of the interface. This would pose problems to a Class which is interested in implementing specific features of the interface. On the contrary, Mixins eliminate this inconvenience by mixing in the state and behavior of the modules in the target class and allows a it to use them as it needs. Hence the target class can just use module’s features selectively or completely instead of implementing by itself. The size of multiple inheritance code using mixins is smaller than interfaces and C++ variants.&lt;br /&gt;
&lt;br /&gt;
===Do mixins have any disadvantages not shared by multiple inheritance or interfaces?===&lt;br /&gt;
Inspite of making the lives of programmers better with effective code reuse and avoiding unnecessary implementation of methods that cause clutter in the code, the concept of Mixins can easily be abused resulting in namespace pollution. This is caused when a class includes multiple modules each of which contains several hundred method implementations. Having hundreds of methods mixed into a single class, makes it difficult to identify the source modules of the methods, thereby causing confusion while implementing these methods, whereas in the case of multiple inheritance we can easily identify the class to which the method belongs.&lt;br /&gt;
Another disadvantage that follows from the namespace collision is the case where we have multiple methods with the same name in different modules.In such a case whenever a call to that method is made, the method which belongs to the module that was included last will be called. The problem with this is that, this takes place silently without any warning. This may result in accidental overriding of wrong methods.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
In this article, we have discussed about multiple inheritance and means by which it is implemented in different languages. Though multiple inheritance is a useful feature,&lt;br /&gt;
many languages do not support them due to its associated issues. Given its advantages, mixin in ruby seems to be a viable option to achieve multiple inheritance. However mixins too have limitations which should be taken care of during implementation.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
==See Also==&lt;br /&gt;
1. Lucas Carlson and Leonard Richardson. 2006. [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=1212915&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 Ruby Cookbook] (Cookbooks (O'Reilly)). O'Reilly Media, Inc..&lt;br /&gt;
&lt;br /&gt;
2. Herbert Schildt. 2000. Java 2: [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=557816&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 The Complete Reference] (4th ed.). McGraw-Hill Professional.&lt;/div&gt;</summary>
		<author><name>Snallan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65438</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65438"/>
		<updated>2012-09-21T13:42:08Z</updated>

		<summary type="html">&lt;p&gt;Snallan: /* Multiple Inheritance in Java */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''1w23:  Multiple Inheritance and Mixins&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
The purpose of this article is to give a brief introduction about multiple inheritance, consider some of its issues and discuss techniques employed by different programming languages to handle such issues. In this relation, the article also compares solutions provided by different programming languages, especially against Ruby mixins, by listing their advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
Multiple Inheritance is a technique of establishing hierarchical relationships within a set of classes, such that a single class can inherit  behaviors from multiple parent classes. Although it might seem useful to include methods from multiple classes, the major issue with multiple inheritance is the ambiguity in referencing the parent class methods,  when more than one parent classes share a method with the same name.This ambiguity is predominant in cases where the inheritance hierarchy is more than one level deep, where the class in the lowermost level inherits from two classes, that have overridden the same method from their parent in different ways.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A Real World Example==&lt;br /&gt;
A known example of multiple inheritance to represent animals using classes and objects is cited for the sake of understanding &amp;lt;ref&amp;gt; http://stackoverflow.com/questions/9359948/multiple-inheritance-whats-a-good-example&amp;lt;/ref&amp;gt;. A Winged animal and a mammal inherit primitive attributes of an animal. While a winged animal can fly; a mammal can breathe and reproduce; they both eat, move and share features generic to animals. A bat being a winged mammal possesses features common to both winged animal and mammal. It follows that class Bat inherits from classes WingedAnimal and Mammal and in turn classes WingedAnimal and Mammal inherit from class Animal.   In reference to this context, let us give our own implementation and explore the issues of multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
[[File:Class_Diagram.png|thumb|right|x400px|alt=Class Diagram for the example.]]&lt;br /&gt;
&lt;br /&gt;
==Issues of Multiple Inheritance==&lt;br /&gt;
1. Due to inheritance, the parent class’s objects get duplicated to child class. As a result, Class Bat duplicates Animal objects through each of WingedAnimal and Mammal classes. When Bat accesses any of the Animal's attributes, the compiler would be ambiguous about determining the correct path to use since two paths are available from Animal to Bat. This problem of multiple inheritance is called diamond problem &amp;lt;ref&amp;gt;http://www.artima.com/weblogs/viewpost.jsp?thread=246488&amp;lt;/ref&amp;gt;. Compiler takes arbitrary decisions during such situations. &lt;br /&gt;
&lt;br /&gt;
2.Objects of parent classes can be separately managed by using [http://en.wikipedia.org/wiki/Virtual_method_table virtual tables]as in C++. By explicitly specifying the virtual table pointer, the Bat class can access WingedAnimal and Mammal classes unambiguously. However virtual tables occupy significant portion of memory thereby limiting their extensive usage.  &lt;br /&gt;
&lt;br /&gt;
3. The order by which a child class inherits parent classes also matters. If Bat inherits WingedAnimal prior to Mammal, then WingedAnimal precedes Mammal and common methods are accessed in this order of precedence.  Hence if the Programmer is not aware of such precedence, debugging would be difficult.&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
Programming languages provide different solutions to handle the issues of multiple inheritance. Here we consider the techniques offered by C++, Java, Ruby, Scala and Python.    &lt;br /&gt;
===Multiple Inheritance in C++===&lt;br /&gt;
C++ supports multiple inheritance with the notion of virtual classes and virtual functions. In the example, the Animal class defines virtual functions with WingedAnimal and Mammal classes virtually inheriting the features of Animal. The Bat class can now unambiguously access canEat() method of Animal class, using virtual table pointers of WingedAnimal and Mammal. The example written using C++ virtual functions is shown below.&lt;br /&gt;
&lt;br /&gt;
 class Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void virtual canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class WingedAnimal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Mammal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Bat :public WingedAnimal, public Mammal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }  &lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
  int i = 0;&lt;br /&gt;
  Animal *instances[3];&lt;br /&gt;
  WingedAnimal w;&lt;br /&gt;
  Mammal m;&lt;br /&gt;
  Bat b;&lt;br /&gt;
  instances[0] = &amp;amp;w;&lt;br /&gt;
  instances[1] = &amp;amp;m;&lt;br /&gt;
  instances[2] = &amp;amp;b;&lt;br /&gt;
  while(i&amp;lt;3)&lt;br /&gt;
    instances[i++]-&amp;gt;canEat();&lt;br /&gt;
  return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Java===&lt;br /&gt;
Java doesn’t support multiple inheritance but provides a feature called interfaces to simulate inheritance from multiple classes. Interface is an abstract class which provides a generic template by just declaring variables and methods without implementing them. Concrete classes that inherit interfaces are supposed to implement its methods. The example written using Java interfaces is shown below.&lt;br /&gt;
&lt;br /&gt;
 interface Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface WingedAnimal extends Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();  &lt;br /&gt;
   void canFly();  &lt;br /&gt;
 }&lt;br /&gt;
 class ImplementWingedAnimal implements WingedAnimal  &lt;br /&gt;
 {&lt;br /&gt;
    public void canEat() {    &lt;br /&gt;
    }&lt;br /&gt;
    public void canFly() {    &lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Mammal extends Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();&lt;br /&gt;
   void canGiveBirth();&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementMammal implements Mammal&lt;br /&gt;
 {&lt;br /&gt;
    public void canEat() {    &lt;br /&gt;
    }&lt;br /&gt;
    public void canGiveBirth() {    &lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Bat extends WingedAnimal, Mammal&lt;br /&gt;
 {&lt;br /&gt;
    void canEat();    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class ImplementBat implements Bat&lt;br /&gt;
 {  &lt;br /&gt;
    ImplementWingedAnimal w = new ImplementWingedAnimal();&lt;br /&gt;
    ImplementMammal m = new ImplementMammal();     &lt;br /&gt;
    public void canEat() {            &lt;br /&gt;
      m.canEat();  &lt;br /&gt;
      w.canEat();      &lt;br /&gt;
    }&lt;br /&gt;
    public void canFly() {&lt;br /&gt;
    }&lt;br /&gt;
    public void canGiveBirth() {&lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class MultipleInheritance&lt;br /&gt;
 {&lt;br /&gt;
   public static void main(String args[])&lt;br /&gt;
   {&lt;br /&gt;
     ImplementBat b = new ImplementBat ();&lt;br /&gt;
     b.canEat();       &lt;br /&gt;
   } &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Ruby===&lt;br /&gt;
Ruby doesn't support multiple inheritance but provides a similar feature using modules and mixins.  &lt;br /&gt;
====Modules====&lt;br /&gt;
A Module is an encapsulation of methods and variables, although similar in function to a Class, differs from a class in the sense that it cannot be instantiated. Modules are  more dynamic and allow us to organize the code in a more granular way.Each module can add something to the existing class. A module can be thought of an encapsulation of a  subset of the full capabilities of an object.&lt;br /&gt;
An example found in the book ,The Well-Grounded Rubyist &amp;lt;ref&amp;gt;http://www.manning.com/black2/SampleChapter4.pdf [Pg.92] &amp;lt;/ref&amp;gt;, is the stack-like module. The module encapsulates the characteristics of being like a stack,implements the operations that can be performed by a stack but is not the actual Stack class. This allows us to use this module to mix-in  stack-like behaviour into other classes that require them.&lt;br /&gt;
&lt;br /&gt;
====Mixins====&lt;br /&gt;
A Mixin is a concept that allows inclusion of special characteristics into existing classes, so that common features that need to be reused in several classes can be selectively included, thus avoiding implementation of unnecessary functions like in the case of interfaces.&lt;br /&gt;
Mixins allow us to dynamically bind special features defined in modules, into core classes without really making any changes to the objects. This improves flexibility when we want to add plugins and extensions to the existing framework.&amp;lt;ref&amp;gt;http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-frameworks/&amp;lt;/ref&amp;gt;.The source code for the same example implemented using ruby mixins is shown below.&lt;br /&gt;
&lt;br /&gt;
 module Cricketer&lt;br /&gt;
   def statistics&lt;br /&gt;
     puts &amp;quot;Cricketer's statistics include number of matches, average etc.&amp;quot;&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Batsman&lt;br /&gt;
   include Cricketer&lt;br /&gt;
   def statistics&lt;br /&gt;
     puts &amp;quot;Batsman's statistics include batting average, number of 100's &amp;amp; 50's etc.&amp;quot;&lt;br /&gt;
   end    &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Bowler&lt;br /&gt;
   include Cricketer&lt;br /&gt;
   def statistics&lt;br /&gt;
      puts &amp;quot;Bowler's statistics include number of wickets, bowling average, economy etc.&amp;quot;&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class AllRounder&lt;br /&gt;
   include Batsman&lt;br /&gt;
   include Bowler&lt;br /&gt;
   include Cricketer&lt;br /&gt;
   # Other implementation&lt;br /&gt;
 end&lt;br /&gt;
 AllRounder.new().statistics()&lt;br /&gt;
&lt;br /&gt;
===Multiple inheritance in other languages===&lt;br /&gt;
Scala offers traits that are similar to Java interfaces to support multiple inheritance. Traits define a template with methods and variables which any class can make use of. The difference between interfaces and traits is that traits can implement methods. A Scala class can inherit from any number of traits together with a class in way to support multiple inheritance. Traits can be mixed in to a class even without inheriting it but just by including it at the time of creating instances. &amp;lt;ref&amp;gt;http://joelabrahamsson.com/entry/learning-scala-traits&amp;lt;/ref&amp;gt;. Trait can also extend a class and methods to be overridden are usually prefixed by the keyword override. The language enforces method names to be different across inherited  traits to avoid the issues of multiple inheritance. Python supports a limited version of multiple inheritance by extending multiple classes in depth-first left-right order &amp;lt;ref&amp;gt;http://docs.python.org/tutorial/classes.html&amp;lt;/ref&amp;gt;. If a python class D inherits classes A, B, C and if an instance variable or method is not available in the current class, it checks in A and its base classes, then moves to B and its base classes and eventually to C thus moving from left to right. In this way, python avoids the diamond problem by making the search order linear by which the derived class accesses attributes of its parent classes. In addition python also defines mixins to access reusable features within a class.&lt;br /&gt;
&lt;br /&gt;
==Advantages of mixins==&lt;br /&gt;
1. Firstly, when a module gets mixed into different classes, each class acquires the same  reference  to the module and hence even dynamic changes to the module gets reflected in the behavior of the classes &amp;lt;ref&amp;gt;http://www.linuxtopia.org/online_books/programming_books/ruby_tutorial/Ruby_Modules_Mixins.html&amp;lt;/ref&amp;gt;. In this way, the module’s code is not just copied to individual classes as in static languages instead its reference is provided. &lt;br /&gt;
&lt;br /&gt;
2. Secondly, the mixed in state and behavior can be accessed by the instance of target class just like accessing its own features. &lt;br /&gt;
&lt;br /&gt;
3. Thirdly, a class can include any number of modules in a way it can simulate multiple inheritance without having the related issues.&lt;br /&gt;
&lt;br /&gt;
==Questions to Ponder==&lt;br /&gt;
===Do mixins solve all of the problems of multiple inheritance?===&lt;br /&gt;
Undoubtedly mixins solve some of the problems of multiple inheritance. There is no ambiguity in resolving the request to a common method implemented by multiple modules. In that case, the lastly included module is responsible for handling accesses to common methods. Though the order of precedence of included modules matters, it looks clearer than C++.  However languages like Scala and Eiffel implement multiple inheritance far more safely  by explicitly forcing the user to have different method names across inherited classes.&lt;br /&gt;
&lt;br /&gt;
===Are mixins a clear advantage over interfaces?===&lt;br /&gt;
Mixins are more advantageous than interfaces because of their features. Interfaces have some limitations. As seen from programs, the derived class Allrounder uses the instances of Batsman and Bowler classes (has-a relationship) to access respective attributes. Interface is nice in the sense it provides a generic template that could be implemented differently by different classes.  However the target class has to necessarily implement every method of the interface. This would pose problems to a Class which is interested in implementing specific features of the interface. On the contrary, Mixins eliminate this inconvenience by mixing in the state and behavior of the modules in the target class and allows a it to use them as it needs. Hence the target class can just use module’s features selectively or completely instead of implementing by itself. The size of multiple inheritance code using mixins is smaller than interfaces and C++ variants.&lt;br /&gt;
&lt;br /&gt;
===Do mixins have any disadvantages not shared by multiple inheritance or interfaces?===&lt;br /&gt;
Inspite of making the lives of programmers better with effective code reuse and avoiding unnecessary implementation of methods that cause clutter in the code, the concept of Mixins can easily be abused resulting in namespace pollution. This is caused when a class includes multiple modules each of which contains several hundred method implementations. Having hundreds of methods mixed into a single class, makes it difficult to identify the source modules of the methods, thereby causing confusion while implementing these methods, whereas in the case of multiple inheritance we can easily identify the class to which the method belongs.&lt;br /&gt;
Another disadvantage that follows from the namespace collision is the case where we have multiple methods with the same name in different modules.In such a case whenever a call to that method is made, the method which belongs to the module that was included last will be called. The problem with this is that, this takes place silently without any warning. This may result in accidental overriding of wrong methods.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
In this article, we have discussed about multiple inheritance and means by which it is implemented in different languages. Though multiple inheritance is a useful feature,&lt;br /&gt;
many languages do not support them due to its associated issues. Given its advantages, mixin in ruby seems to be a viable option to achieve multiple inheritance. However mixins too have limitations which should be taken care of during implementation.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
==See Also==&lt;br /&gt;
1. Lucas Carlson and Leonard Richardson. 2006. [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=1212915&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 Ruby Cookbook] (Cookbooks (O'Reilly)). O'Reilly Media, Inc..&lt;br /&gt;
&lt;br /&gt;
2. Herbert Schildt. 2000. Java 2: [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=557816&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 The Complete Reference] (4th ed.). McGraw-Hill Professional.&lt;/div&gt;</summary>
		<author><name>Snallan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65437</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65437"/>
		<updated>2012-09-21T13:41:37Z</updated>

		<summary type="html">&lt;p&gt;Snallan: /* Multiple Inheritance in Java */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''1w23:  Multiple Inheritance and Mixins&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
The purpose of this article is to give a brief introduction about multiple inheritance, consider some of its issues and discuss techniques employed by different programming languages to handle such issues. In this relation, the article also compares solutions provided by different programming languages, especially against Ruby mixins, by listing their advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
Multiple Inheritance is a technique of establishing hierarchical relationships within a set of classes, such that a single class can inherit  behaviors from multiple parent classes. Although it might seem useful to include methods from multiple classes, the major issue with multiple inheritance is the ambiguity in referencing the parent class methods,  when more than one parent classes share a method with the same name.This ambiguity is predominant in cases where the inheritance hierarchy is more than one level deep, where the class in the lowermost level inherits from two classes, that have overridden the same method from their parent in different ways.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A Real World Example==&lt;br /&gt;
A known example of multiple inheritance to represent animals using classes and objects is cited for the sake of understanding &amp;lt;ref&amp;gt; http://stackoverflow.com/questions/9359948/multiple-inheritance-whats-a-good-example&amp;lt;/ref&amp;gt;. A Winged animal and a mammal inherit primitive attributes of an animal. While a winged animal can fly; a mammal can breathe and reproduce; they both eat, move and share features generic to animals. A bat being a winged mammal possesses features common to both winged animal and mammal. It follows that class Bat inherits from classes WingedAnimal and Mammal and in turn classes WingedAnimal and Mammal inherit from class Animal.   In reference to this context, let us give our own implementation and explore the issues of multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
[[File:Class_Diagram.png|thumb|right|x400px|alt=Class Diagram for the example.]]&lt;br /&gt;
&lt;br /&gt;
==Issues of Multiple Inheritance==&lt;br /&gt;
1. Due to inheritance, the parent class’s objects get duplicated to child class. As a result, Class Bat duplicates Animal objects through each of WingedAnimal and Mammal classes. When Bat accesses any of the Animal's attributes, the compiler would be ambiguous about determining the correct path to use since two paths are available from Animal to Bat. This problem of multiple inheritance is called diamond problem &amp;lt;ref&amp;gt;http://www.artima.com/weblogs/viewpost.jsp?thread=246488&amp;lt;/ref&amp;gt;. Compiler takes arbitrary decisions during such situations. &lt;br /&gt;
&lt;br /&gt;
2.Objects of parent classes can be separately managed by using [http://en.wikipedia.org/wiki/Virtual_method_table virtual tables]as in C++. By explicitly specifying the virtual table pointer, the Bat class can access WingedAnimal and Mammal classes unambiguously. However virtual tables occupy significant portion of memory thereby limiting their extensive usage.  &lt;br /&gt;
&lt;br /&gt;
3. The order by which a child class inherits parent classes also matters. If Bat inherits WingedAnimal prior to Mammal, then WingedAnimal precedes Mammal and common methods are accessed in this order of precedence.  Hence if the Programmer is not aware of such precedence, debugging would be difficult.&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
Programming languages provide different solutions to handle the issues of multiple inheritance. Here we consider the techniques offered by C++, Java, Ruby, Scala and Python.    &lt;br /&gt;
===Multiple Inheritance in C++===&lt;br /&gt;
C++ supports multiple inheritance with the notion of virtual classes and virtual functions. In the example, the Animal class defines virtual functions with WingedAnimal and Mammal classes virtually inheriting the features of Animal. The Bat class can now unambiguously access canEat() method of Animal class, using virtual table pointers of WingedAnimal and Mammal. The example written using C++ virtual functions is shown below.&lt;br /&gt;
&lt;br /&gt;
 class Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void virtual canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class WingedAnimal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Mammal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Bat :public WingedAnimal, public Mammal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }  &lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
  int i = 0;&lt;br /&gt;
  Animal *instances[3];&lt;br /&gt;
  WingedAnimal w;&lt;br /&gt;
  Mammal m;&lt;br /&gt;
  Bat b;&lt;br /&gt;
  instances[0] = &amp;amp;w;&lt;br /&gt;
  instances[1] = &amp;amp;m;&lt;br /&gt;
  instances[2] = &amp;amp;b;&lt;br /&gt;
  while(i&amp;lt;3)&lt;br /&gt;
    instances[i++]-&amp;gt;canEat();&lt;br /&gt;
  return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Java===&lt;br /&gt;
Java doesn’t support multiple inheritance but provides a feature called interfaces to resemble inheritance from multiple classes. Interface is an abstract class which provides a generic template by just declaring variables and methods without implementing them. Concrete classes that inherit interfaces are supposed to implement its methods. The example written using Java interfaces is shown below.&lt;br /&gt;
&lt;br /&gt;
 interface Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface WingedAnimal extends Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();  &lt;br /&gt;
   void canFly();  &lt;br /&gt;
 }&lt;br /&gt;
 class ImplementWingedAnimal implements WingedAnimal  &lt;br /&gt;
 {&lt;br /&gt;
    public void canEat() {    &lt;br /&gt;
    }&lt;br /&gt;
    public void canFly() {    &lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Mammal extends Animal&lt;br /&gt;
 {&lt;br /&gt;
   void canEat();&lt;br /&gt;
   void canGiveBirth();&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementMammal implements Mammal&lt;br /&gt;
 {&lt;br /&gt;
    public void canEat() {    &lt;br /&gt;
    }&lt;br /&gt;
    public void canGiveBirth() {    &lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Bat extends WingedAnimal, Mammal&lt;br /&gt;
 {&lt;br /&gt;
    void canEat();    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class ImplementBat implements Bat&lt;br /&gt;
 {  &lt;br /&gt;
    ImplementWingedAnimal w = new ImplementWingedAnimal();&lt;br /&gt;
    ImplementMammal m = new ImplementMammal();     &lt;br /&gt;
    public void canEat() {            &lt;br /&gt;
      m.canEat();  &lt;br /&gt;
      w.canEat();      &lt;br /&gt;
    }&lt;br /&gt;
    public void canFly() {&lt;br /&gt;
    }&lt;br /&gt;
    public void canGiveBirth() {&lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class MultipleInheritance&lt;br /&gt;
 {&lt;br /&gt;
   public static void main(String args[])&lt;br /&gt;
   {&lt;br /&gt;
     ImplementBat b = new ImplementBat ();&lt;br /&gt;
     b.canEat();       &lt;br /&gt;
   } &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Ruby===&lt;br /&gt;
Ruby doesn't support multiple inheritance but provides a similar feature using modules and mixins.  &lt;br /&gt;
====Modules====&lt;br /&gt;
A Module is an encapsulation of methods and variables, although similar in function to a Class, differs from a class in the sense that it cannot be instantiated. Modules are  more dynamic and allow us to organize the code in a more granular way.Each module can add something to the existing class. A module can be thought of an encapsulation of a  subset of the full capabilities of an object.&lt;br /&gt;
An example found in the book ,The Well-Grounded Rubyist &amp;lt;ref&amp;gt;http://www.manning.com/black2/SampleChapter4.pdf [Pg.92] &amp;lt;/ref&amp;gt;, is the stack-like module. The module encapsulates the characteristics of being like a stack,implements the operations that can be performed by a stack but is not the actual Stack class. This allows us to use this module to mix-in  stack-like behaviour into other classes that require them.&lt;br /&gt;
&lt;br /&gt;
====Mixins====&lt;br /&gt;
A Mixin is a concept that allows inclusion of special characteristics into existing classes, so that common features that need to be reused in several classes can be selectively included, thus avoiding implementation of unnecessary functions like in the case of interfaces.&lt;br /&gt;
Mixins allow us to dynamically bind special features defined in modules, into core classes without really making any changes to the objects. This improves flexibility when we want to add plugins and extensions to the existing framework.&amp;lt;ref&amp;gt;http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-frameworks/&amp;lt;/ref&amp;gt;.The source code for the same example implemented using ruby mixins is shown below.&lt;br /&gt;
&lt;br /&gt;
 module Cricketer&lt;br /&gt;
   def statistics&lt;br /&gt;
     puts &amp;quot;Cricketer's statistics include number of matches, average etc.&amp;quot;&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Batsman&lt;br /&gt;
   include Cricketer&lt;br /&gt;
   def statistics&lt;br /&gt;
     puts &amp;quot;Batsman's statistics include batting average, number of 100's &amp;amp; 50's etc.&amp;quot;&lt;br /&gt;
   end    &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Bowler&lt;br /&gt;
   include Cricketer&lt;br /&gt;
   def statistics&lt;br /&gt;
      puts &amp;quot;Bowler's statistics include number of wickets, bowling average, economy etc.&amp;quot;&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class AllRounder&lt;br /&gt;
   include Batsman&lt;br /&gt;
   include Bowler&lt;br /&gt;
   include Cricketer&lt;br /&gt;
   # Other implementation&lt;br /&gt;
 end&lt;br /&gt;
 AllRounder.new().statistics()&lt;br /&gt;
&lt;br /&gt;
===Multiple inheritance in other languages===&lt;br /&gt;
Scala offers traits that are similar to Java interfaces to support multiple inheritance. Traits define a template with methods and variables which any class can make use of. The difference between interfaces and traits is that traits can implement methods. A Scala class can inherit from any number of traits together with a class in way to support multiple inheritance. Traits can be mixed in to a class even without inheriting it but just by including it at the time of creating instances. &amp;lt;ref&amp;gt;http://joelabrahamsson.com/entry/learning-scala-traits&amp;lt;/ref&amp;gt;. Trait can also extend a class and methods to be overridden are usually prefixed by the keyword override. The language enforces method names to be different across inherited  traits to avoid the issues of multiple inheritance. Python supports a limited version of multiple inheritance by extending multiple classes in depth-first left-right order &amp;lt;ref&amp;gt;http://docs.python.org/tutorial/classes.html&amp;lt;/ref&amp;gt;. If a python class D inherits classes A, B, C and if an instance variable or method is not available in the current class, it checks in A and its base classes, then moves to B and its base classes and eventually to C thus moving from left to right. In this way, python avoids the diamond problem by making the search order linear by which the derived class accesses attributes of its parent classes. In addition python also defines mixins to access reusable features within a class.&lt;br /&gt;
&lt;br /&gt;
==Advantages of mixins==&lt;br /&gt;
1. Firstly, when a module gets mixed into different classes, each class acquires the same  reference  to the module and hence even dynamic changes to the module gets reflected in the behavior of the classes &amp;lt;ref&amp;gt;http://www.linuxtopia.org/online_books/programming_books/ruby_tutorial/Ruby_Modules_Mixins.html&amp;lt;/ref&amp;gt;. In this way, the module’s code is not just copied to individual classes as in static languages instead its reference is provided. &lt;br /&gt;
&lt;br /&gt;
2. Secondly, the mixed in state and behavior can be accessed by the instance of target class just like accessing its own features. &lt;br /&gt;
&lt;br /&gt;
3. Thirdly, a class can include any number of modules in a way it can simulate multiple inheritance without having the related issues.&lt;br /&gt;
&lt;br /&gt;
==Questions to Ponder==&lt;br /&gt;
===Do mixins solve all of the problems of multiple inheritance?===&lt;br /&gt;
Undoubtedly mixins solve some of the problems of multiple inheritance. There is no ambiguity in resolving the request to a common method implemented by multiple modules. In that case, the lastly included module is responsible for handling accesses to common methods. Though the order of precedence of included modules matters, it looks clearer than C++.  However languages like Scala and Eiffel implement multiple inheritance far more safely  by explicitly forcing the user to have different method names across inherited classes.&lt;br /&gt;
&lt;br /&gt;
===Are mixins a clear advantage over interfaces?===&lt;br /&gt;
Mixins are more advantageous than interfaces because of their features. Interfaces have some limitations. As seen from programs, the derived class Allrounder uses the instances of Batsman and Bowler classes (has-a relationship) to access respective attributes. Interface is nice in the sense it provides a generic template that could be implemented differently by different classes.  However the target class has to necessarily implement every method of the interface. This would pose problems to a Class which is interested in implementing specific features of the interface. On the contrary, Mixins eliminate this inconvenience by mixing in the state and behavior of the modules in the target class and allows a it to use them as it needs. Hence the target class can just use module’s features selectively or completely instead of implementing by itself. The size of multiple inheritance code using mixins is smaller than interfaces and C++ variants.&lt;br /&gt;
&lt;br /&gt;
===Do mixins have any disadvantages not shared by multiple inheritance or interfaces?===&lt;br /&gt;
Inspite of making the lives of programmers better with effective code reuse and avoiding unnecessary implementation of methods that cause clutter in the code, the concept of Mixins can easily be abused resulting in namespace pollution. This is caused when a class includes multiple modules each of which contains several hundred method implementations. Having hundreds of methods mixed into a single class, makes it difficult to identify the source modules of the methods, thereby causing confusion while implementing these methods, whereas in the case of multiple inheritance we can easily identify the class to which the method belongs.&lt;br /&gt;
Another disadvantage that follows from the namespace collision is the case where we have multiple methods with the same name in different modules.In such a case whenever a call to that method is made, the method which belongs to the module that was included last will be called. The problem with this is that, this takes place silently without any warning. This may result in accidental overriding of wrong methods.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
In this article, we have discussed about multiple inheritance and means by which it is implemented in different languages. Though multiple inheritance is a useful feature,&lt;br /&gt;
many languages do not support them due to its associated issues. Given its advantages, mixin in ruby seems to be a viable option to achieve multiple inheritance. However mixins too have limitations which should be taken care of during implementation.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
==See Also==&lt;br /&gt;
1. Lucas Carlson and Leonard Richardson. 2006. [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=1212915&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 Ruby Cookbook] (Cookbooks (O'Reilly)). O'Reilly Media, Inc..&lt;br /&gt;
&lt;br /&gt;
2. Herbert Schildt. 2000. Java 2: [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=557816&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 The Complete Reference] (4th ed.). McGraw-Hill Professional.&lt;/div&gt;</summary>
		<author><name>Snallan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65436</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65436"/>
		<updated>2012-09-21T13:19:51Z</updated>

		<summary type="html">&lt;p&gt;Snallan: /* Multiple Inheritance in C++ */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''1w23:  Multiple Inheritance and Mixins&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
The purpose of this article is to give a brief introduction about multiple inheritance, consider some of its issues and discuss techniques employed by different programming languages to handle such issues. In this relation, the article also compares solutions provided by different programming languages, especially against Ruby mixins, by listing their advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
Multiple Inheritance is a technique of establishing hierarchical relationships within a set of classes, such that a single class can inherit  behaviors from multiple parent classes. Although it might seem useful to include methods from multiple classes, the major issue with multiple inheritance is the ambiguity in referencing the parent class methods,  when more than one parent classes share a method with the same name.This ambiguity is predominant in cases where the inheritance hierarchy is more than one level deep, where the class in the lowermost level inherits from two classes, that have overridden the same method from their parent in different ways.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A Real World Example==&lt;br /&gt;
A known example of multiple inheritance to represent animals using classes and objects is cited for the sake of understanding &amp;lt;ref&amp;gt; http://stackoverflow.com/questions/9359948/multiple-inheritance-whats-a-good-example&amp;lt;/ref&amp;gt;. A Winged animal and a mammal inherit primitive attributes of an animal. While a winged animal can fly; a mammal can breathe and reproduce; they both eat, move and share features generic to animals. A bat being a winged mammal possesses features common to both winged animal and mammal. It follows that class Bat inherits from classes WingedAnimal and Mammal and in turn classes WingedAnimal and Mammal inherit from class Animal.   In reference to this context, let us give our own implementation and explore the issues of multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
[[File:Class_Diagram.png|thumb|right|x400px|alt=Class Diagram for the example.]]&lt;br /&gt;
&lt;br /&gt;
==Issues of Multiple Inheritance==&lt;br /&gt;
1. Due to inheritance, the parent class’s objects get duplicated to child class. As a result, Class Bat duplicates Animal objects through each of WingedAnimal and Mammal classes. When Bat accesses any of the Animal's attributes, the compiler would be ambiguous about determining the correct path to use since two paths are available from Animal to Bat. This problem of multiple inheritance is called diamond problem &amp;lt;ref&amp;gt;http://www.artima.com/weblogs/viewpost.jsp?thread=246488&amp;lt;/ref&amp;gt;. Compiler takes arbitrary decisions during such situations. &lt;br /&gt;
&lt;br /&gt;
2.Objects of parent classes can be separately managed by using [http://en.wikipedia.org/wiki/Virtual_method_table virtual tables]as in C++. By explicitly specifying the virtual table pointer, the Bat class can access WingedAnimal and Mammal classes unambiguously. However virtual tables occupy significant portion of memory thereby limiting their extensive usage.  &lt;br /&gt;
&lt;br /&gt;
3. The order by which a child class inherits parent classes also matters. If Bat inherits WingedAnimal prior to Mammal, then WingedAnimal precedes Mammal and common methods are accessed in this order of precedence.  Hence if the Programmer is not aware of such precedence, debugging would be difficult.&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
Programming languages provide different solutions to handle the issues of multiple inheritance. Here we consider the techniques offered by C++, Java, Ruby, Scala and Python.    &lt;br /&gt;
===Multiple Inheritance in C++===&lt;br /&gt;
C++ supports multiple inheritance with the notion of virtual classes and virtual functions. In the example, the Animal class defines virtual functions with WingedAnimal and Mammal classes virtually inheriting the features of Animal. The Bat class can now unambiguously access canEat() method of Animal class, using virtual table pointers of WingedAnimal and Mammal. The example written using C++ virtual functions is shown below.&lt;br /&gt;
&lt;br /&gt;
 class Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void virtual canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class WingedAnimal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Mammal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Bat :public WingedAnimal, public Mammal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }  &lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
  int i = 0;&lt;br /&gt;
  Animal *instances[3];&lt;br /&gt;
  WingedAnimal w;&lt;br /&gt;
  Mammal m;&lt;br /&gt;
  Bat b;&lt;br /&gt;
  instances[0] = &amp;amp;w;&lt;br /&gt;
  instances[1] = &amp;amp;m;&lt;br /&gt;
  instances[2] = &amp;amp;b;&lt;br /&gt;
  while(i&amp;lt;3)&lt;br /&gt;
    instances[i++]-&amp;gt;canEat();&lt;br /&gt;
  return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Java===&lt;br /&gt;
Java doesn’t support multiple inheritance but provides a feature called interfaces to resemble inheritance from multiple classes. Interface is an abstract class which provides a generic template by just declaring variables and methods without implementing them. Concrete classes that inherit interfaces are supposed to implement its methods. The example written using Java interfaces is shown below.&lt;br /&gt;
&lt;br /&gt;
 interface Animal&lt;br /&gt;
 {&lt;br /&gt;
   void statistics();  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Batsman extends Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   void statistics();  &lt;br /&gt;
 }&lt;br /&gt;
 class ImplementBatsman implements Batsman  &lt;br /&gt;
 {&lt;br /&gt;
    public void statistics()&lt;br /&gt;
    {&lt;br /&gt;
      System.out.println(&amp;quot;Batsman's statistics include batting average, number of 100's &amp;amp; 50's etc.&amp;quot;);&lt;br /&gt;
    }   &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Bowler extends Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   void statistics();&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementBowler implements Bowler&lt;br /&gt;
 {&lt;br /&gt;
    public void statistics()&lt;br /&gt;
    {&lt;br /&gt;
      System.out.println(&amp;quot;Bowler's statistics include number of wickets, bowling average, economy etc.&amp;quot;);&lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Allrounder extends Batsman, Bowler &lt;br /&gt;
 {&lt;br /&gt;
    // other methods to operate on Cricketer, Batsman &amp;amp; Bowler specific attributes&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementAllrounder implements Allrounder&lt;br /&gt;
 {  &lt;br /&gt;
    ImplementBowler bowl = new ImplementBowler();&lt;br /&gt;
    ImplementBatsman bat = new ImplementBatsman(); &lt;br /&gt;
    public void statistics()&lt;br /&gt;
    {&lt;br /&gt;
      bowl.statistics();&lt;br /&gt;
      bat.statistics();   &lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class MultipleInheritance&lt;br /&gt;
 {&lt;br /&gt;
   public static void main(String args[])&lt;br /&gt;
   {&lt;br /&gt;
     ImplementAllrounder all = new ImplementAllrounder();&lt;br /&gt;
     all.statistics();       &lt;br /&gt;
   } &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Ruby===&lt;br /&gt;
Ruby doesn't support multiple inheritance but provides a similar feature using modules and mixins.  &lt;br /&gt;
====Modules====&lt;br /&gt;
A Module is an encapsulation of methods and variables, although similar in function to a Class, differs from a class in the sense that it cannot be instantiated. Modules are  more dynamic and allow us to organize the code in a more granular way.Each module can add something to the existing class. A module can be thought of an encapsulation of a  subset of the full capabilities of an object.&lt;br /&gt;
An example found in the book ,The Well-Grounded Rubyist &amp;lt;ref&amp;gt;http://www.manning.com/black2/SampleChapter4.pdf [Pg.92] &amp;lt;/ref&amp;gt;, is the stack-like module. The module encapsulates the characteristics of being like a stack,implements the operations that can be performed by a stack but is not the actual Stack class. This allows us to use this module to mix-in  stack-like behaviour into other classes that require them.&lt;br /&gt;
&lt;br /&gt;
====Mixins====&lt;br /&gt;
A Mixin is a concept that allows inclusion of special characteristics into existing classes, so that common features that need to be reused in several classes can be selectively included, thus avoiding implementation of unnecessary functions like in the case of interfaces.&lt;br /&gt;
Mixins allow us to dynamically bind special features defined in modules, into core classes without really making any changes to the objects. This improves flexibility when we want to add plugins and extensions to the existing framework.&amp;lt;ref&amp;gt;http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-frameworks/&amp;lt;/ref&amp;gt;.The source code for the same example implemented using ruby mixins is shown below.&lt;br /&gt;
&lt;br /&gt;
 module Cricketer&lt;br /&gt;
   def statistics&lt;br /&gt;
     puts &amp;quot;Cricketer's statistics include number of matches, average etc.&amp;quot;&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Batsman&lt;br /&gt;
   include Cricketer&lt;br /&gt;
   def statistics&lt;br /&gt;
     puts &amp;quot;Batsman's statistics include batting average, number of 100's &amp;amp; 50's etc.&amp;quot;&lt;br /&gt;
   end    &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Bowler&lt;br /&gt;
   include Cricketer&lt;br /&gt;
   def statistics&lt;br /&gt;
      puts &amp;quot;Bowler's statistics include number of wickets, bowling average, economy etc.&amp;quot;&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class AllRounder&lt;br /&gt;
   include Batsman&lt;br /&gt;
   include Bowler&lt;br /&gt;
   include Cricketer&lt;br /&gt;
   # Other implementation&lt;br /&gt;
 end&lt;br /&gt;
 AllRounder.new().statistics()&lt;br /&gt;
&lt;br /&gt;
===Multiple inheritance in other languages===&lt;br /&gt;
Scala offers traits that are similar to Java interfaces to support multiple inheritance. Traits define a template with methods and variables which any class can make use of. The difference between interfaces and traits is that traits can implement methods. A Scala class can inherit from any number of traits together with a class in way to support multiple inheritance. Traits can be mixed in to a class even without inheriting it but just by including it at the time of creating instances. &amp;lt;ref&amp;gt;http://joelabrahamsson.com/entry/learning-scala-traits&amp;lt;/ref&amp;gt;. Trait can also extend a class and methods to be overridden are usually prefixed by the keyword override. The language enforces method names to be different across inherited  traits to avoid the issues of multiple inheritance. Python supports a limited version of multiple inheritance by extending multiple classes in depth-first left-right order &amp;lt;ref&amp;gt;http://docs.python.org/tutorial/classes.html&amp;lt;/ref&amp;gt;. If a python class D inherits classes A, B, C and if an instance variable or method is not available in the current class, it checks in A and its base classes, then moves to B and its base classes and eventually to C thus moving from left to right. In this way, python avoids the diamond problem by making the search order linear by which the derived class accesses attributes of its parent classes. In addition python also defines mixins to access reusable features within a class.&lt;br /&gt;
&lt;br /&gt;
==Advantages of mixins==&lt;br /&gt;
1. Firstly, when a module gets mixed into different classes, each class acquires the same  reference  to the module and hence even dynamic changes to the module gets reflected in the behavior of the classes &amp;lt;ref&amp;gt;http://www.linuxtopia.org/online_books/programming_books/ruby_tutorial/Ruby_Modules_Mixins.html&amp;lt;/ref&amp;gt;. In this way, the module’s code is not just copied to individual classes as in static languages instead its reference is provided. &lt;br /&gt;
&lt;br /&gt;
2. Secondly, the mixed in state and behavior can be accessed by the instance of target class just like accessing its own features. &lt;br /&gt;
&lt;br /&gt;
3. Thirdly, a class can include any number of modules in a way it can simulate multiple inheritance without having the related issues.&lt;br /&gt;
&lt;br /&gt;
==Questions to Ponder==&lt;br /&gt;
===Do mixins solve all of the problems of multiple inheritance?===&lt;br /&gt;
Undoubtedly mixins solve some of the problems of multiple inheritance. There is no ambiguity in resolving the request to a common method implemented by multiple modules. In that case, the lastly included module is responsible for handling accesses to common methods. Though the order of precedence of included modules matters, it looks clearer than C++.  However languages like Scala and Eiffel implement multiple inheritance far more safely  by explicitly forcing the user to have different method names across inherited classes.&lt;br /&gt;
&lt;br /&gt;
===Are mixins a clear advantage over interfaces?===&lt;br /&gt;
Mixins are more advantageous than interfaces because of their features. Interfaces have some limitations. As seen from programs, the derived class Allrounder uses the instances of Batsman and Bowler classes (has-a relationship) to access respective attributes. Interface is nice in the sense it provides a generic template that could be implemented differently by different classes.  However the target class has to necessarily implement every method of the interface. This would pose problems to a Class which is interested in implementing specific features of the interface. On the contrary, Mixins eliminate this inconvenience by mixing in the state and behavior of the modules in the target class and allows a it to use them as it needs. Hence the target class can just use module’s features selectively or completely instead of implementing by itself. The size of multiple inheritance code using mixins is smaller than interfaces and C++ variants.&lt;br /&gt;
&lt;br /&gt;
===Do mixins have any disadvantages not shared by multiple inheritance or interfaces?===&lt;br /&gt;
Inspite of making the lives of programmers better with effective code reuse and avoiding unnecessary implementation of methods that cause clutter in the code, the concept of Mixins can easily be abused resulting in namespace pollution. This is caused when a class includes multiple modules each of which contains several hundred method implementations. Having hundreds of methods mixed into a single class, makes it difficult to identify the source modules of the methods, thereby causing confusion while implementing these methods, whereas in the case of multiple inheritance we can easily identify the class to which the method belongs.&lt;br /&gt;
Another disadvantage that follows from the namespace collision is the case where we have multiple methods with the same name in different modules.In such a case whenever a call to that method is made, the method which belongs to the module that was included last will be called. The problem with this is that, this takes place silently without any warning. This may result in accidental overriding of wrong methods.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
In this article, we have discussed about multiple inheritance and means by which it is implemented in different languages. Though multiple inheritance is a useful feature,&lt;br /&gt;
many languages do not support them due to its associated issues. Given its advantages, mixin in ruby seems to be a viable option to achieve multiple inheritance. However mixins too have limitations which should be taken care of during implementation.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
==See Also==&lt;br /&gt;
1. Lucas Carlson and Leonard Richardson. 2006. [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=1212915&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 Ruby Cookbook] (Cookbooks (O'Reilly)). O'Reilly Media, Inc..&lt;br /&gt;
&lt;br /&gt;
2. Herbert Schildt. 2000. Java 2: [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=557816&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 The Complete Reference] (4th ed.). McGraw-Hill Professional.&lt;/div&gt;</summary>
		<author><name>Snallan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65435</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65435"/>
		<updated>2012-09-21T13:19:12Z</updated>

		<summary type="html">&lt;p&gt;Snallan: /* Multiple Inheritance in C++ */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''1w23:  Multiple Inheritance and Mixins&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
The purpose of this article is to give a brief introduction about multiple inheritance, consider some of its issues and discuss techniques employed by different programming languages to handle such issues. In this relation, the article also compares solutions provided by different programming languages, especially against Ruby mixins, by listing their advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
Multiple Inheritance is a technique of establishing hierarchical relationships within a set of classes, such that a single class can inherit  behaviors from multiple parent classes. Although it might seem useful to include methods from multiple classes, the major issue with multiple inheritance is the ambiguity in referencing the parent class methods,  when more than one parent classes share a method with the same name.This ambiguity is predominant in cases where the inheritance hierarchy is more than one level deep, where the class in the lowermost level inherits from two classes, that have overridden the same method from their parent in different ways.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A Real World Example==&lt;br /&gt;
A known example of multiple inheritance to represent animals using classes and objects is cited for the sake of understanding &amp;lt;ref&amp;gt; http://stackoverflow.com/questions/9359948/multiple-inheritance-whats-a-good-example&amp;lt;/ref&amp;gt;. A Winged animal and a mammal inherit primitive attributes of an animal. While a winged animal can fly; a mammal can breathe and reproduce; they both eat, move and share features generic to animals. A bat being a winged mammal possesses features common to both winged animal and mammal. It follows that class Bat inherits from classes WingedAnimal and Mammal and in turn classes WingedAnimal and Mammal inherit from class Animal.   In reference to this context, let us give our own implementation and explore the issues of multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
[[File:Class_Diagram.png|thumb|right|x400px|alt=Class Diagram for the example.]]&lt;br /&gt;
&lt;br /&gt;
==Issues of Multiple Inheritance==&lt;br /&gt;
1. Due to inheritance, the parent class’s objects get duplicated to child class. As a result, Class Bat duplicates Animal objects through each of WingedAnimal and Mammal classes. When Bat accesses any of the Animal's attributes, the compiler would be ambiguous about determining the correct path to use since two paths are available from Animal to Bat. This problem of multiple inheritance is called diamond problem &amp;lt;ref&amp;gt;http://www.artima.com/weblogs/viewpost.jsp?thread=246488&amp;lt;/ref&amp;gt;. Compiler takes arbitrary decisions during such situations. &lt;br /&gt;
&lt;br /&gt;
2.Objects of parent classes can be separately managed by using [http://en.wikipedia.org/wiki/Virtual_method_table virtual tables]as in C++. By explicitly specifying the virtual table pointer, the Bat class can access WingedAnimal and Mammal classes unambiguously. However virtual tables occupy significant portion of memory thereby limiting their extensive usage.  &lt;br /&gt;
&lt;br /&gt;
3. The order by which a child class inherits parent classes also matters. If Bat inherits WingedAnimal prior to Mammal, then WingedAnimal precedes Mammal and common methods are accessed in this order of precedence.  Hence if the Programmer is not aware of such precedence, debugging would be difficult.&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
Programming languages provide different solutions to handle the issues of multiple inheritance. Here we consider the techniques offered by C++, Java, Ruby, Scala and Python.    &lt;br /&gt;
===Multiple Inheritance in C++===&lt;br /&gt;
C++ supports multiple inheritance with the notion of virtual classes and virtual functions. In the example, the Animal class defines virtual functions with WingedAnimal and Mammal classes virtually inheriting the features of Animal. The Bat class can now unambiguously access canEat method of Animal using virtual table pointers of WingedAnimal and Mammal. The example written using C++ virtual functions is shown below.&lt;br /&gt;
&lt;br /&gt;
 class Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void virtual canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class WingedAnimal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Mammal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Bat :public WingedAnimal, public Mammal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }  &lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
  int i = 0;&lt;br /&gt;
  Animal *instances[3];&lt;br /&gt;
  WingedAnimal w;&lt;br /&gt;
  Mammal m;&lt;br /&gt;
  Bat b;&lt;br /&gt;
  instances[0] = &amp;amp;w;&lt;br /&gt;
  instances[1] = &amp;amp;m;&lt;br /&gt;
  instances[2] = &amp;amp;b;&lt;br /&gt;
  while(i&amp;lt;3)&lt;br /&gt;
    instances[i++]-&amp;gt;canEat();&lt;br /&gt;
  return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Java===&lt;br /&gt;
Java doesn’t support multiple inheritance but provides a feature called interfaces to resemble inheritance from multiple classes. Interface is an abstract class which provides a generic template by just declaring variables and methods without implementing them. Concrete classes that inherit interfaces are supposed to implement its methods. The example written using Java interfaces is shown below.&lt;br /&gt;
&lt;br /&gt;
 interface Animal&lt;br /&gt;
 {&lt;br /&gt;
   void statistics();  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Batsman extends Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   void statistics();  &lt;br /&gt;
 }&lt;br /&gt;
 class ImplementBatsman implements Batsman  &lt;br /&gt;
 {&lt;br /&gt;
    public void statistics()&lt;br /&gt;
    {&lt;br /&gt;
      System.out.println(&amp;quot;Batsman's statistics include batting average, number of 100's &amp;amp; 50's etc.&amp;quot;);&lt;br /&gt;
    }   &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Bowler extends Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   void statistics();&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementBowler implements Bowler&lt;br /&gt;
 {&lt;br /&gt;
    public void statistics()&lt;br /&gt;
    {&lt;br /&gt;
      System.out.println(&amp;quot;Bowler's statistics include number of wickets, bowling average, economy etc.&amp;quot;);&lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Allrounder extends Batsman, Bowler &lt;br /&gt;
 {&lt;br /&gt;
    // other methods to operate on Cricketer, Batsman &amp;amp; Bowler specific attributes&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementAllrounder implements Allrounder&lt;br /&gt;
 {  &lt;br /&gt;
    ImplementBowler bowl = new ImplementBowler();&lt;br /&gt;
    ImplementBatsman bat = new ImplementBatsman(); &lt;br /&gt;
    public void statistics()&lt;br /&gt;
    {&lt;br /&gt;
      bowl.statistics();&lt;br /&gt;
      bat.statistics();   &lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class MultipleInheritance&lt;br /&gt;
 {&lt;br /&gt;
   public static void main(String args[])&lt;br /&gt;
   {&lt;br /&gt;
     ImplementAllrounder all = new ImplementAllrounder();&lt;br /&gt;
     all.statistics();       &lt;br /&gt;
   } &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Ruby===&lt;br /&gt;
Ruby doesn't support multiple inheritance but provides a similar feature using modules and mixins.  &lt;br /&gt;
====Modules====&lt;br /&gt;
A Module is an encapsulation of methods and variables, although similar in function to a Class, differs from a class in the sense that it cannot be instantiated. Modules are  more dynamic and allow us to organize the code in a more granular way.Each module can add something to the existing class. A module can be thought of an encapsulation of a  subset of the full capabilities of an object.&lt;br /&gt;
An example found in the book ,The Well-Grounded Rubyist &amp;lt;ref&amp;gt;http://www.manning.com/black2/SampleChapter4.pdf [Pg.92] &amp;lt;/ref&amp;gt;, is the stack-like module. The module encapsulates the characteristics of being like a stack,implements the operations that can be performed by a stack but is not the actual Stack class. This allows us to use this module to mix-in  stack-like behaviour into other classes that require them.&lt;br /&gt;
&lt;br /&gt;
====Mixins====&lt;br /&gt;
A Mixin is a concept that allows inclusion of special characteristics into existing classes, so that common features that need to be reused in several classes can be selectively included, thus avoiding implementation of unnecessary functions like in the case of interfaces.&lt;br /&gt;
Mixins allow us to dynamically bind special features defined in modules, into core classes without really making any changes to the objects. This improves flexibility when we want to add plugins and extensions to the existing framework.&amp;lt;ref&amp;gt;http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-frameworks/&amp;lt;/ref&amp;gt;.The source code for the same example implemented using ruby mixins is shown below.&lt;br /&gt;
&lt;br /&gt;
 module Cricketer&lt;br /&gt;
   def statistics&lt;br /&gt;
     puts &amp;quot;Cricketer's statistics include number of matches, average etc.&amp;quot;&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Batsman&lt;br /&gt;
   include Cricketer&lt;br /&gt;
   def statistics&lt;br /&gt;
     puts &amp;quot;Batsman's statistics include batting average, number of 100's &amp;amp; 50's etc.&amp;quot;&lt;br /&gt;
   end    &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Bowler&lt;br /&gt;
   include Cricketer&lt;br /&gt;
   def statistics&lt;br /&gt;
      puts &amp;quot;Bowler's statistics include number of wickets, bowling average, economy etc.&amp;quot;&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class AllRounder&lt;br /&gt;
   include Batsman&lt;br /&gt;
   include Bowler&lt;br /&gt;
   include Cricketer&lt;br /&gt;
   # Other implementation&lt;br /&gt;
 end&lt;br /&gt;
 AllRounder.new().statistics()&lt;br /&gt;
&lt;br /&gt;
===Multiple inheritance in other languages===&lt;br /&gt;
Scala offers traits that are similar to Java interfaces to support multiple inheritance. Traits define a template with methods and variables which any class can make use of. The difference between interfaces and traits is that traits can implement methods. A Scala class can inherit from any number of traits together with a class in way to support multiple inheritance. Traits can be mixed in to a class even without inheriting it but just by including it at the time of creating instances. &amp;lt;ref&amp;gt;http://joelabrahamsson.com/entry/learning-scala-traits&amp;lt;/ref&amp;gt;. Trait can also extend a class and methods to be overridden are usually prefixed by the keyword override. The language enforces method names to be different across inherited  traits to avoid the issues of multiple inheritance. Python supports a limited version of multiple inheritance by extending multiple classes in depth-first left-right order &amp;lt;ref&amp;gt;http://docs.python.org/tutorial/classes.html&amp;lt;/ref&amp;gt;. If a python class D inherits classes A, B, C and if an instance variable or method is not available in the current class, it checks in A and its base classes, then moves to B and its base classes and eventually to C thus moving from left to right. In this way, python avoids the diamond problem by making the search order linear by which the derived class accesses attributes of its parent classes. In addition python also defines mixins to access reusable features within a class.&lt;br /&gt;
&lt;br /&gt;
==Advantages of mixins==&lt;br /&gt;
1. Firstly, when a module gets mixed into different classes, each class acquires the same  reference  to the module and hence even dynamic changes to the module gets reflected in the behavior of the classes &amp;lt;ref&amp;gt;http://www.linuxtopia.org/online_books/programming_books/ruby_tutorial/Ruby_Modules_Mixins.html&amp;lt;/ref&amp;gt;. In this way, the module’s code is not just copied to individual classes as in static languages instead its reference is provided. &lt;br /&gt;
&lt;br /&gt;
2. Secondly, the mixed in state and behavior can be accessed by the instance of target class just like accessing its own features. &lt;br /&gt;
&lt;br /&gt;
3. Thirdly, a class can include any number of modules in a way it can simulate multiple inheritance without having the related issues.&lt;br /&gt;
&lt;br /&gt;
==Questions to Ponder==&lt;br /&gt;
===Do mixins solve all of the problems of multiple inheritance?===&lt;br /&gt;
Undoubtedly mixins solve some of the problems of multiple inheritance. There is no ambiguity in resolving the request to a common method implemented by multiple modules. In that case, the lastly included module is responsible for handling accesses to common methods. Though the order of precedence of included modules matters, it looks clearer than C++.  However languages like Scala and Eiffel implement multiple inheritance far more safely  by explicitly forcing the user to have different method names across inherited classes.&lt;br /&gt;
&lt;br /&gt;
===Are mixins a clear advantage over interfaces?===&lt;br /&gt;
Mixins are more advantageous than interfaces because of their features. Interfaces have some limitations. As seen from programs, the derived class Allrounder uses the instances of Batsman and Bowler classes (has-a relationship) to access respective attributes. Interface is nice in the sense it provides a generic template that could be implemented differently by different classes.  However the target class has to necessarily implement every method of the interface. This would pose problems to a Class which is interested in implementing specific features of the interface. On the contrary, Mixins eliminate this inconvenience by mixing in the state and behavior of the modules in the target class and allows a it to use them as it needs. Hence the target class can just use module’s features selectively or completely instead of implementing by itself. The size of multiple inheritance code using mixins is smaller than interfaces and C++ variants.&lt;br /&gt;
&lt;br /&gt;
===Do mixins have any disadvantages not shared by multiple inheritance or interfaces?===&lt;br /&gt;
Inspite of making the lives of programmers better with effective code reuse and avoiding unnecessary implementation of methods that cause clutter in the code, the concept of Mixins can easily be abused resulting in namespace pollution. This is caused when a class includes multiple modules each of which contains several hundred method implementations. Having hundreds of methods mixed into a single class, makes it difficult to identify the source modules of the methods, thereby causing confusion while implementing these methods, whereas in the case of multiple inheritance we can easily identify the class to which the method belongs.&lt;br /&gt;
Another disadvantage that follows from the namespace collision is the case where we have multiple methods with the same name in different modules.In such a case whenever a call to that method is made, the method which belongs to the module that was included last will be called. The problem with this is that, this takes place silently without any warning. This may result in accidental overriding of wrong methods.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
In this article, we have discussed about multiple inheritance and means by which it is implemented in different languages. Though multiple inheritance is a useful feature,&lt;br /&gt;
many languages do not support them due to its associated issues. Given its advantages, mixin in ruby seems to be a viable option to achieve multiple inheritance. However mixins too have limitations which should be taken care of during implementation.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
==See Also==&lt;br /&gt;
1. Lucas Carlson and Leonard Richardson. 2006. [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=1212915&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 Ruby Cookbook] (Cookbooks (O'Reilly)). O'Reilly Media, Inc..&lt;br /&gt;
&lt;br /&gt;
2. Herbert Schildt. 2000. Java 2: [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=557816&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 The Complete Reference] (4th ed.). McGraw-Hill Professional.&lt;/div&gt;</summary>
		<author><name>Snallan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65434</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65434"/>
		<updated>2012-09-21T13:17:36Z</updated>

		<summary type="html">&lt;p&gt;Snallan: /* Multiple Inheritance in C++ */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''1w23:  Multiple Inheritance and Mixins&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
The purpose of this article is to give a brief introduction about multiple inheritance, consider some of its issues and discuss techniques employed by different programming languages to handle such issues. In this relation, the article also compares solutions provided by different programming languages, especially against Ruby mixins, by listing their advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
Multiple Inheritance is a technique of establishing hierarchical relationships within a set of classes, such that a single class can inherit  behaviors from multiple parent classes. Although it might seem useful to include methods from multiple classes, the major issue with multiple inheritance is the ambiguity in referencing the parent class methods,  when more than one parent classes share a method with the same name.This ambiguity is predominant in cases where the inheritance hierarchy is more than one level deep, where the class in the lowermost level inherits from two classes, that have overridden the same method from their parent in different ways.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A Real World Example==&lt;br /&gt;
A known example of multiple inheritance to represent animals using classes and objects is cited for the sake of understanding &amp;lt;ref&amp;gt; http://stackoverflow.com/questions/9359948/multiple-inheritance-whats-a-good-example&amp;lt;/ref&amp;gt;. A Winged animal and a mammal inherit primitive attributes of an animal. While a winged animal can fly; a mammal can breathe and reproduce; they both eat, move and share features generic to animals. A bat being a winged mammal possesses features common to both winged animal and mammal. It follows that class Bat inherits from classes WingedAnimal and Mammal and in turn classes WingedAnimal and Mammal inherit from class Animal.   In reference to this context, let us give our own implementation and explore the issues of multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
[[File:Class_Diagram.png|thumb|right|x400px|alt=Class Diagram for the example.]]&lt;br /&gt;
&lt;br /&gt;
==Issues of Multiple Inheritance==&lt;br /&gt;
1. Due to inheritance, the parent class’s objects get duplicated to child class. As a result, Class Bat duplicates Animal objects through each of WingedAnimal and Mammal classes. When Bat accesses any of the Animal's attributes, the compiler would be ambiguous about determining the correct path to use since two paths are available from Animal to Bat. This problem of multiple inheritance is called diamond problem &amp;lt;ref&amp;gt;http://www.artima.com/weblogs/viewpost.jsp?thread=246488&amp;lt;/ref&amp;gt;. Compiler takes arbitrary decisions during such situations. &lt;br /&gt;
&lt;br /&gt;
2.Objects of parent classes can be separately managed by using [http://en.wikipedia.org/wiki/Virtual_method_table virtual tables]as in C++. By explicitly specifying the virtual table pointer, the Bat class can access WingedAnimal and Mammal classes unambiguously. However virtual tables occupy significant portion of memory thereby limiting their extensive usage.  &lt;br /&gt;
&lt;br /&gt;
3. The order by which a child class inherits parent classes also matters. If Bat inherits WingedAnimal prior to Mammal, then WingedAnimal precedes Mammal and common methods are accessed in this order of precedence.  Hence if the Programmer is not aware of such precedence, debugging would be difficult.&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
Programming languages provide different solutions to handle the issues of multiple inheritance. Here we consider the techniques offered by C++, Java, Ruby, Scala and Python.    &lt;br /&gt;
===Multiple Inheritance in C++===&lt;br /&gt;
C++ supports multiple inheritance with the notion of virtual classes and virtual functions. In the example, the Animal class defines virtual functions with WingedAnimal and Mammal classes virtually inheriting the features of Animal. The Bat class can now unambiguously access Cricketer class using virtual table pointers of WingedAnimal and Mammal. The example written using C++ virtual functions is shown below.&lt;br /&gt;
&lt;br /&gt;
 class Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void virtual canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class WingedAnimal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Mammal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Bat :public WingedAnimal, public Mammal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }  &lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
  int i = 0;&lt;br /&gt;
  Animal *instances[3];&lt;br /&gt;
  WingedAnimal w;&lt;br /&gt;
  Mammal m;&lt;br /&gt;
  Bat b;&lt;br /&gt;
  instances[0] = &amp;amp;w;&lt;br /&gt;
  instances[1] = &amp;amp;m;&lt;br /&gt;
  instances[2] = &amp;amp;b;&lt;br /&gt;
  while(i&amp;lt;3)&lt;br /&gt;
    instances[i++]-&amp;gt;canEat();&lt;br /&gt;
  return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Java===&lt;br /&gt;
Java doesn’t support multiple inheritance but provides a feature called interfaces to resemble inheritance from multiple classes. Interface is an abstract class which provides a generic template by just declaring variables and methods without implementing them. Concrete classes that inherit interfaces are supposed to implement its methods. The example written using Java interfaces is shown below.&lt;br /&gt;
&lt;br /&gt;
 interface Animal&lt;br /&gt;
 {&lt;br /&gt;
   void statistics();  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Batsman extends Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   void statistics();  &lt;br /&gt;
 }&lt;br /&gt;
 class ImplementBatsman implements Batsman  &lt;br /&gt;
 {&lt;br /&gt;
    public void statistics()&lt;br /&gt;
    {&lt;br /&gt;
      System.out.println(&amp;quot;Batsman's statistics include batting average, number of 100's &amp;amp; 50's etc.&amp;quot;);&lt;br /&gt;
    }   &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Bowler extends Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   void statistics();&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementBowler implements Bowler&lt;br /&gt;
 {&lt;br /&gt;
    public void statistics()&lt;br /&gt;
    {&lt;br /&gt;
      System.out.println(&amp;quot;Bowler's statistics include number of wickets, bowling average, economy etc.&amp;quot;);&lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Allrounder extends Batsman, Bowler &lt;br /&gt;
 {&lt;br /&gt;
    // other methods to operate on Cricketer, Batsman &amp;amp; Bowler specific attributes&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementAllrounder implements Allrounder&lt;br /&gt;
 {  &lt;br /&gt;
    ImplementBowler bowl = new ImplementBowler();&lt;br /&gt;
    ImplementBatsman bat = new ImplementBatsman(); &lt;br /&gt;
    public void statistics()&lt;br /&gt;
    {&lt;br /&gt;
      bowl.statistics();&lt;br /&gt;
      bat.statistics();   &lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class MultipleInheritance&lt;br /&gt;
 {&lt;br /&gt;
   public static void main(String args[])&lt;br /&gt;
   {&lt;br /&gt;
     ImplementAllrounder all = new ImplementAllrounder();&lt;br /&gt;
     all.statistics();       &lt;br /&gt;
   } &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Ruby===&lt;br /&gt;
Ruby doesn't support multiple inheritance but provides a similar feature using modules and mixins.  &lt;br /&gt;
====Modules====&lt;br /&gt;
A Module is an encapsulation of methods and variables, although similar in function to a Class, differs from a class in the sense that it cannot be instantiated. Modules are  more dynamic and allow us to organize the code in a more granular way.Each module can add something to the existing class. A module can be thought of an encapsulation of a  subset of the full capabilities of an object.&lt;br /&gt;
An example found in the book ,The Well-Grounded Rubyist &amp;lt;ref&amp;gt;http://www.manning.com/black2/SampleChapter4.pdf [Pg.92] &amp;lt;/ref&amp;gt;, is the stack-like module. The module encapsulates the characteristics of being like a stack,implements the operations that can be performed by a stack but is not the actual Stack class. This allows us to use this module to mix-in  stack-like behaviour into other classes that require them.&lt;br /&gt;
&lt;br /&gt;
====Mixins====&lt;br /&gt;
A Mixin is a concept that allows inclusion of special characteristics into existing classes, so that common features that need to be reused in several classes can be selectively included, thus avoiding implementation of unnecessary functions like in the case of interfaces.&lt;br /&gt;
Mixins allow us to dynamically bind special features defined in modules, into core classes without really making any changes to the objects. This improves flexibility when we want to add plugins and extensions to the existing framework.&amp;lt;ref&amp;gt;http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-frameworks/&amp;lt;/ref&amp;gt;.The source code for the same example implemented using ruby mixins is shown below.&lt;br /&gt;
&lt;br /&gt;
 module Cricketer&lt;br /&gt;
   def statistics&lt;br /&gt;
     puts &amp;quot;Cricketer's statistics include number of matches, average etc.&amp;quot;&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Batsman&lt;br /&gt;
   include Cricketer&lt;br /&gt;
   def statistics&lt;br /&gt;
     puts &amp;quot;Batsman's statistics include batting average, number of 100's &amp;amp; 50's etc.&amp;quot;&lt;br /&gt;
   end    &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Bowler&lt;br /&gt;
   include Cricketer&lt;br /&gt;
   def statistics&lt;br /&gt;
      puts &amp;quot;Bowler's statistics include number of wickets, bowling average, economy etc.&amp;quot;&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class AllRounder&lt;br /&gt;
   include Batsman&lt;br /&gt;
   include Bowler&lt;br /&gt;
   include Cricketer&lt;br /&gt;
   # Other implementation&lt;br /&gt;
 end&lt;br /&gt;
 AllRounder.new().statistics()&lt;br /&gt;
&lt;br /&gt;
===Multiple inheritance in other languages===&lt;br /&gt;
Scala offers traits that are similar to Java interfaces to support multiple inheritance. Traits define a template with methods and variables which any class can make use of. The difference between interfaces and traits is that traits can implement methods. A Scala class can inherit from any number of traits together with a class in way to support multiple inheritance. Traits can be mixed in to a class even without inheriting it but just by including it at the time of creating instances. &amp;lt;ref&amp;gt;http://joelabrahamsson.com/entry/learning-scala-traits&amp;lt;/ref&amp;gt;. Trait can also extend a class and methods to be overridden are usually prefixed by the keyword override. The language enforces method names to be different across inherited  traits to avoid the issues of multiple inheritance. Python supports a limited version of multiple inheritance by extending multiple classes in depth-first left-right order &amp;lt;ref&amp;gt;http://docs.python.org/tutorial/classes.html&amp;lt;/ref&amp;gt;. If a python class D inherits classes A, B, C and if an instance variable or method is not available in the current class, it checks in A and its base classes, then moves to B and its base classes and eventually to C thus moving from left to right. In this way, python avoids the diamond problem by making the search order linear by which the derived class accesses attributes of its parent classes. In addition python also defines mixins to access reusable features within a class.&lt;br /&gt;
&lt;br /&gt;
==Advantages of mixins==&lt;br /&gt;
1. Firstly, when a module gets mixed into different classes, each class acquires the same  reference  to the module and hence even dynamic changes to the module gets reflected in the behavior of the classes &amp;lt;ref&amp;gt;http://www.linuxtopia.org/online_books/programming_books/ruby_tutorial/Ruby_Modules_Mixins.html&amp;lt;/ref&amp;gt;. In this way, the module’s code is not just copied to individual classes as in static languages instead its reference is provided. &lt;br /&gt;
&lt;br /&gt;
2. Secondly, the mixed in state and behavior can be accessed by the instance of target class just like accessing its own features. &lt;br /&gt;
&lt;br /&gt;
3. Thirdly, a class can include any number of modules in a way it can simulate multiple inheritance without having the related issues.&lt;br /&gt;
&lt;br /&gt;
==Questions to Ponder==&lt;br /&gt;
===Do mixins solve all of the problems of multiple inheritance?===&lt;br /&gt;
Undoubtedly mixins solve some of the problems of multiple inheritance. There is no ambiguity in resolving the request to a common method implemented by multiple modules. In that case, the lastly included module is responsible for handling accesses to common methods. Though the order of precedence of included modules matters, it looks clearer than C++.  However languages like Scala and Eiffel implement multiple inheritance far more safely  by explicitly forcing the user to have different method names across inherited classes.&lt;br /&gt;
&lt;br /&gt;
===Are mixins a clear advantage over interfaces?===&lt;br /&gt;
Mixins are more advantageous than interfaces because of their features. Interfaces have some limitations. As seen from programs, the derived class Allrounder uses the instances of Batsman and Bowler classes (has-a relationship) to access respective attributes. Interface is nice in the sense it provides a generic template that could be implemented differently by different classes.  However the target class has to necessarily implement every method of the interface. This would pose problems to a Class which is interested in implementing specific features of the interface. On the contrary, Mixins eliminate this inconvenience by mixing in the state and behavior of the modules in the target class and allows a it to use them as it needs. Hence the target class can just use module’s features selectively or completely instead of implementing by itself. The size of multiple inheritance code using mixins is smaller than interfaces and C++ variants.&lt;br /&gt;
&lt;br /&gt;
===Do mixins have any disadvantages not shared by multiple inheritance or interfaces?===&lt;br /&gt;
Inspite of making the lives of programmers better with effective code reuse and avoiding unnecessary implementation of methods that cause clutter in the code, the concept of Mixins can easily be abused resulting in namespace pollution. This is caused when a class includes multiple modules each of which contains several hundred method implementations. Having hundreds of methods mixed into a single class, makes it difficult to identify the source modules of the methods, thereby causing confusion while implementing these methods, whereas in the case of multiple inheritance we can easily identify the class to which the method belongs.&lt;br /&gt;
Another disadvantage that follows from the namespace collision is the case where we have multiple methods with the same name in different modules.In such a case whenever a call to that method is made, the method which belongs to the module that was included last will be called. The problem with this is that, this takes place silently without any warning. This may result in accidental overriding of wrong methods.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
In this article, we have discussed about multiple inheritance and means by which it is implemented in different languages. Though multiple inheritance is a useful feature,&lt;br /&gt;
many languages do not support them due to its associated issues. Given its advantages, mixin in ruby seems to be a viable option to achieve multiple inheritance. However mixins too have limitations which should be taken care of during implementation.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
==See Also==&lt;br /&gt;
1. Lucas Carlson and Leonard Richardson. 2006. [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=1212915&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 Ruby Cookbook] (Cookbooks (O'Reilly)). O'Reilly Media, Inc..&lt;br /&gt;
&lt;br /&gt;
2. Herbert Schildt. 2000. Java 2: [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=557816&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 The Complete Reference] (4th ed.). McGraw-Hill Professional.&lt;/div&gt;</summary>
		<author><name>Snallan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65433</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65433"/>
		<updated>2012-09-21T13:16:52Z</updated>

		<summary type="html">&lt;p&gt;Snallan: /* Multiple Inheritance in C++ */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''1w23:  Multiple Inheritance and Mixins&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
The purpose of this article is to give a brief introduction about multiple inheritance, consider some of its issues and discuss techniques employed by different programming languages to handle such issues. In this relation, the article also compares solutions provided by different programming languages, especially against Ruby mixins, by listing their advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
Multiple Inheritance is a technique of establishing hierarchical relationships within a set of classes, such that a single class can inherit  behaviors from multiple parent classes. Although it might seem useful to include methods from multiple classes, the major issue with multiple inheritance is the ambiguity in referencing the parent class methods,  when more than one parent classes share a method with the same name.This ambiguity is predominant in cases where the inheritance hierarchy is more than one level deep, where the class in the lowermost level inherits from two classes, that have overridden the same method from their parent in different ways.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A Real World Example==&lt;br /&gt;
A known example of multiple inheritance to represent animals using classes and objects is cited for the sake of understanding &amp;lt;ref&amp;gt; http://stackoverflow.com/questions/9359948/multiple-inheritance-whats-a-good-example&amp;lt;/ref&amp;gt;. A Winged animal and a mammal inherit primitive attributes of an animal. While a winged animal can fly; a mammal can breathe and reproduce; they both eat, move and share features generic to animals. A bat being a winged mammal possesses features common to both winged animal and mammal. It follows that class Bat inherits from classes WingedAnimal and Mammal and in turn classes WingedAnimal and Mammal inherit from class Animal.   In reference to this context, let us give our own implementation and explore the issues of multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
[[File:Class_Diagram.png|thumb|right|x400px|alt=Class Diagram for the example.]]&lt;br /&gt;
&lt;br /&gt;
==Issues of Multiple Inheritance==&lt;br /&gt;
1. Due to inheritance, the parent class’s objects get duplicated to child class. As a result, Class Bat duplicates Animal objects through each of WingedAnimal and Mammal classes. When Bat accesses any of the Animal's attributes, the compiler would be ambiguous about determining the correct path to use since two paths are available from Animal to Bat. This problem of multiple inheritance is called diamond problem &amp;lt;ref&amp;gt;http://www.artima.com/weblogs/viewpost.jsp?thread=246488&amp;lt;/ref&amp;gt;. Compiler takes arbitrary decisions during such situations. &lt;br /&gt;
&lt;br /&gt;
2.Objects of parent classes can be separately managed by using [http://en.wikipedia.org/wiki/Virtual_method_table virtual tables]as in C++. By explicitly specifying the virtual table pointer, the Bat class can access WingedAnimal and Mammal classes unambiguously. However virtual tables occupy significant portion of memory thereby limiting their extensive usage.  &lt;br /&gt;
&lt;br /&gt;
3. The order by which a child class inherits parent classes also matters. If Bat inherits WingedAnimal prior to Mammal, then WingedAnimal precedes Mammal and common methods are accessed in this order of precedence.  Hence if the Programmer is not aware of such precedence, debugging would be difficult.&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
Programming languages provide different solutions to handle the issues of multiple inheritance. Here we consider the techniques offered by C++, Java, Ruby, Scala and Python.    &lt;br /&gt;
===Multiple Inheritance in C++===&lt;br /&gt;
C++ supports multiple inheritance with the notion of virtual classes and virtual functions. In the example, the Animal class defines virtual functions with WingedAnimal and Mammal classes virtually inheriting the features of Animal. The Bat class can now unambiguously access Cricketer class using virtual table pointers of WingedAnimal and Mammal. The example written using C++ virtual functions is shown below.&lt;br /&gt;
&lt;br /&gt;
 class Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void virtual canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class WingedAnimal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Mammal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Bat :public WingedAnimal, public Mammal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
  int i = 0;&lt;br /&gt;
  Animal *instances[3];&lt;br /&gt;
  WingedAnimal w;&lt;br /&gt;
  Mammal m;&lt;br /&gt;
  Bat b;&lt;br /&gt;
  instances[0] = &amp;amp;w;&lt;br /&gt;
  instances[1] = &amp;amp;m;&lt;br /&gt;
  instances[2] = &amp;amp;b;&lt;br /&gt;
  while(i&amp;lt;3)&lt;br /&gt;
    instances[i++]-&amp;gt;canEat();&lt;br /&gt;
  return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Java===&lt;br /&gt;
Java doesn’t support multiple inheritance but provides a feature called interfaces to resemble inheritance from multiple classes. Interface is an abstract class which provides a generic template by just declaring variables and methods without implementing them. Concrete classes that inherit interfaces are supposed to implement its methods. The example written using Java interfaces is shown below.&lt;br /&gt;
&lt;br /&gt;
 interface Animal&lt;br /&gt;
 {&lt;br /&gt;
   void statistics();  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Batsman extends Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   void statistics();  &lt;br /&gt;
 }&lt;br /&gt;
 class ImplementBatsman implements Batsman  &lt;br /&gt;
 {&lt;br /&gt;
    public void statistics()&lt;br /&gt;
    {&lt;br /&gt;
      System.out.println(&amp;quot;Batsman's statistics include batting average, number of 100's &amp;amp; 50's etc.&amp;quot;);&lt;br /&gt;
    }   &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Bowler extends Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   void statistics();&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementBowler implements Bowler&lt;br /&gt;
 {&lt;br /&gt;
    public void statistics()&lt;br /&gt;
    {&lt;br /&gt;
      System.out.println(&amp;quot;Bowler's statistics include number of wickets, bowling average, economy etc.&amp;quot;);&lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Allrounder extends Batsman, Bowler &lt;br /&gt;
 {&lt;br /&gt;
    // other methods to operate on Cricketer, Batsman &amp;amp; Bowler specific attributes&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementAllrounder implements Allrounder&lt;br /&gt;
 {  &lt;br /&gt;
    ImplementBowler bowl = new ImplementBowler();&lt;br /&gt;
    ImplementBatsman bat = new ImplementBatsman(); &lt;br /&gt;
    public void statistics()&lt;br /&gt;
    {&lt;br /&gt;
      bowl.statistics();&lt;br /&gt;
      bat.statistics();   &lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class MultipleInheritance&lt;br /&gt;
 {&lt;br /&gt;
   public static void main(String args[])&lt;br /&gt;
   {&lt;br /&gt;
     ImplementAllrounder all = new ImplementAllrounder();&lt;br /&gt;
     all.statistics();       &lt;br /&gt;
   } &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Ruby===&lt;br /&gt;
Ruby doesn't support multiple inheritance but provides a similar feature using modules and mixins.  &lt;br /&gt;
====Modules====&lt;br /&gt;
A Module is an encapsulation of methods and variables, although similar in function to a Class, differs from a class in the sense that it cannot be instantiated. Modules are  more dynamic and allow us to organize the code in a more granular way.Each module can add something to the existing class. A module can be thought of an encapsulation of a  subset of the full capabilities of an object.&lt;br /&gt;
An example found in the book ,The Well-Grounded Rubyist &amp;lt;ref&amp;gt;http://www.manning.com/black2/SampleChapter4.pdf [Pg.92] &amp;lt;/ref&amp;gt;, is the stack-like module. The module encapsulates the characteristics of being like a stack,implements the operations that can be performed by a stack but is not the actual Stack class. This allows us to use this module to mix-in  stack-like behaviour into other classes that require them.&lt;br /&gt;
&lt;br /&gt;
====Mixins====&lt;br /&gt;
A Mixin is a concept that allows inclusion of special characteristics into existing classes, so that common features that need to be reused in several classes can be selectively included, thus avoiding implementation of unnecessary functions like in the case of interfaces.&lt;br /&gt;
Mixins allow us to dynamically bind special features defined in modules, into core classes without really making any changes to the objects. This improves flexibility when we want to add plugins and extensions to the existing framework.&amp;lt;ref&amp;gt;http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-frameworks/&amp;lt;/ref&amp;gt;.The source code for the same example implemented using ruby mixins is shown below.&lt;br /&gt;
&lt;br /&gt;
 module Cricketer&lt;br /&gt;
   def statistics&lt;br /&gt;
     puts &amp;quot;Cricketer's statistics include number of matches, average etc.&amp;quot;&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Batsman&lt;br /&gt;
   include Cricketer&lt;br /&gt;
   def statistics&lt;br /&gt;
     puts &amp;quot;Batsman's statistics include batting average, number of 100's &amp;amp; 50's etc.&amp;quot;&lt;br /&gt;
   end    &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Bowler&lt;br /&gt;
   include Cricketer&lt;br /&gt;
   def statistics&lt;br /&gt;
      puts &amp;quot;Bowler's statistics include number of wickets, bowling average, economy etc.&amp;quot;&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class AllRounder&lt;br /&gt;
   include Batsman&lt;br /&gt;
   include Bowler&lt;br /&gt;
   include Cricketer&lt;br /&gt;
   # Other implementation&lt;br /&gt;
 end&lt;br /&gt;
 AllRounder.new().statistics()&lt;br /&gt;
&lt;br /&gt;
===Multiple inheritance in other languages===&lt;br /&gt;
Scala offers traits that are similar to Java interfaces to support multiple inheritance. Traits define a template with methods and variables which any class can make use of. The difference between interfaces and traits is that traits can implement methods. A Scala class can inherit from any number of traits together with a class in way to support multiple inheritance. Traits can be mixed in to a class even without inheriting it but just by including it at the time of creating instances. &amp;lt;ref&amp;gt;http://joelabrahamsson.com/entry/learning-scala-traits&amp;lt;/ref&amp;gt;. Trait can also extend a class and methods to be overridden are usually prefixed by the keyword override. The language enforces method names to be different across inherited  traits to avoid the issues of multiple inheritance. Python supports a limited version of multiple inheritance by extending multiple classes in depth-first left-right order &amp;lt;ref&amp;gt;http://docs.python.org/tutorial/classes.html&amp;lt;/ref&amp;gt;. If a python class D inherits classes A, B, C and if an instance variable or method is not available in the current class, it checks in A and its base classes, then moves to B and its base classes and eventually to C thus moving from left to right. In this way, python avoids the diamond problem by making the search order linear by which the derived class accesses attributes of its parent classes. In addition python also defines mixins to access reusable features within a class.&lt;br /&gt;
&lt;br /&gt;
==Advantages of mixins==&lt;br /&gt;
1. Firstly, when a module gets mixed into different classes, each class acquires the same  reference  to the module and hence even dynamic changes to the module gets reflected in the behavior of the classes &amp;lt;ref&amp;gt;http://www.linuxtopia.org/online_books/programming_books/ruby_tutorial/Ruby_Modules_Mixins.html&amp;lt;/ref&amp;gt;. In this way, the module’s code is not just copied to individual classes as in static languages instead its reference is provided. &lt;br /&gt;
&lt;br /&gt;
2. Secondly, the mixed in state and behavior can be accessed by the instance of target class just like accessing its own features. &lt;br /&gt;
&lt;br /&gt;
3. Thirdly, a class can include any number of modules in a way it can simulate multiple inheritance without having the related issues.&lt;br /&gt;
&lt;br /&gt;
==Questions to Ponder==&lt;br /&gt;
===Do mixins solve all of the problems of multiple inheritance?===&lt;br /&gt;
Undoubtedly mixins solve some of the problems of multiple inheritance. There is no ambiguity in resolving the request to a common method implemented by multiple modules. In that case, the lastly included module is responsible for handling accesses to common methods. Though the order of precedence of included modules matters, it looks clearer than C++.  However languages like Scala and Eiffel implement multiple inheritance far more safely  by explicitly forcing the user to have different method names across inherited classes.&lt;br /&gt;
&lt;br /&gt;
===Are mixins a clear advantage over interfaces?===&lt;br /&gt;
Mixins are more advantageous than interfaces because of their features. Interfaces have some limitations. As seen from programs, the derived class Allrounder uses the instances of Batsman and Bowler classes (has-a relationship) to access respective attributes. Interface is nice in the sense it provides a generic template that could be implemented differently by different classes.  However the target class has to necessarily implement every method of the interface. This would pose problems to a Class which is interested in implementing specific features of the interface. On the contrary, Mixins eliminate this inconvenience by mixing in the state and behavior of the modules in the target class and allows a it to use them as it needs. Hence the target class can just use module’s features selectively or completely instead of implementing by itself. The size of multiple inheritance code using mixins is smaller than interfaces and C++ variants.&lt;br /&gt;
&lt;br /&gt;
===Do mixins have any disadvantages not shared by multiple inheritance or interfaces?===&lt;br /&gt;
Inspite of making the lives of programmers better with effective code reuse and avoiding unnecessary implementation of methods that cause clutter in the code, the concept of Mixins can easily be abused resulting in namespace pollution. This is caused when a class includes multiple modules each of which contains several hundred method implementations. Having hundreds of methods mixed into a single class, makes it difficult to identify the source modules of the methods, thereby causing confusion while implementing these methods, whereas in the case of multiple inheritance we can easily identify the class to which the method belongs.&lt;br /&gt;
Another disadvantage that follows from the namespace collision is the case where we have multiple methods with the same name in different modules.In such a case whenever a call to that method is made, the method which belongs to the module that was included last will be called. The problem with this is that, this takes place silently without any warning. This may result in accidental overriding of wrong methods.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
In this article, we have discussed about multiple inheritance and means by which it is implemented in different languages. Though multiple inheritance is a useful feature,&lt;br /&gt;
many languages do not support them due to its associated issues. Given its advantages, mixin in ruby seems to be a viable option to achieve multiple inheritance. However mixins too have limitations which should be taken care of during implementation.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
==See Also==&lt;br /&gt;
1. Lucas Carlson and Leonard Richardson. 2006. [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=1212915&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 Ruby Cookbook] (Cookbooks (O'Reilly)). O'Reilly Media, Inc..&lt;br /&gt;
&lt;br /&gt;
2. Herbert Schildt. 2000. Java 2: [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=557816&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 The Complete Reference] (4th ed.). McGraw-Hill Professional.&lt;/div&gt;</summary>
		<author><name>Snallan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65432</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65432"/>
		<updated>2012-09-21T13:16:33Z</updated>

		<summary type="html">&lt;p&gt;Snallan: /* Multiple Inheritance in Java */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''1w23:  Multiple Inheritance and Mixins&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
The purpose of this article is to give a brief introduction about multiple inheritance, consider some of its issues and discuss techniques employed by different programming languages to handle such issues. In this relation, the article also compares solutions provided by different programming languages, especially against Ruby mixins, by listing their advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
Multiple Inheritance is a technique of establishing hierarchical relationships within a set of classes, such that a single class can inherit  behaviors from multiple parent classes. Although it might seem useful to include methods from multiple classes, the major issue with multiple inheritance is the ambiguity in referencing the parent class methods,  when more than one parent classes share a method with the same name.This ambiguity is predominant in cases where the inheritance hierarchy is more than one level deep, where the class in the lowermost level inherits from two classes, that have overridden the same method from their parent in different ways.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A Real World Example==&lt;br /&gt;
A known example of multiple inheritance to represent animals using classes and objects is cited for the sake of understanding &amp;lt;ref&amp;gt; http://stackoverflow.com/questions/9359948/multiple-inheritance-whats-a-good-example&amp;lt;/ref&amp;gt;. A Winged animal and a mammal inherit primitive attributes of an animal. While a winged animal can fly; a mammal can breathe and reproduce; they both eat, move and share features generic to animals. A bat being a winged mammal possesses features common to both winged animal and mammal. It follows that class Bat inherits from classes WingedAnimal and Mammal and in turn classes WingedAnimal and Mammal inherit from class Animal.   In reference to this context, let us give our own implementation and explore the issues of multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
[[File:Class_Diagram.png|thumb|right|x400px|alt=Class Diagram for the example.]]&lt;br /&gt;
&lt;br /&gt;
==Issues of Multiple Inheritance==&lt;br /&gt;
1. Due to inheritance, the parent class’s objects get duplicated to child class. As a result, Class Bat duplicates Animal objects through each of WingedAnimal and Mammal classes. When Bat accesses any of the Animal's attributes, the compiler would be ambiguous about determining the correct path to use since two paths are available from Animal to Bat. This problem of multiple inheritance is called diamond problem &amp;lt;ref&amp;gt;http://www.artima.com/weblogs/viewpost.jsp?thread=246488&amp;lt;/ref&amp;gt;. Compiler takes arbitrary decisions during such situations. &lt;br /&gt;
&lt;br /&gt;
2.Objects of parent classes can be separately managed by using [http://en.wikipedia.org/wiki/Virtual_method_table virtual tables]as in C++. By explicitly specifying the virtual table pointer, the Bat class can access WingedAnimal and Mammal classes unambiguously. However virtual tables occupy significant portion of memory thereby limiting their extensive usage.  &lt;br /&gt;
&lt;br /&gt;
3. The order by which a child class inherits parent classes also matters. If Bat inherits WingedAnimal prior to Mammal, then WingedAnimal precedes Mammal and common methods are accessed in this order of precedence.  Hence if the Programmer is not aware of such precedence, debugging would be difficult.&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
Programming languages provide different solutions to handle the issues of multiple inheritance. Here we consider the techniques offered by C++, Java, Ruby, Scala and Python.    &lt;br /&gt;
===Multiple Inheritance in C++===&lt;br /&gt;
C++ supports multiple inheritance with the notion of virtual classes and virtual functions. In the example, the Animal class defines virtual functions with WingedAnimal and Mammal classes virtually inheriting the features of Animal. The Bat class can now unambiguously access Cricketer class using virtual table pointers of WingedAnimal and Mammal. The example written using C++ virtual functions is shown below.&lt;br /&gt;
&lt;br /&gt;
 class Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void virtual canEat() {&lt;br /&gt;
  }&lt;br /&gt;
  void canMove() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class WingedAnimal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Mammal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Bat :public WingedAnimal, public Mammal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
  int i = 0;&lt;br /&gt;
  Animal *instances[3];&lt;br /&gt;
  WingedAnimal w;&lt;br /&gt;
  Mammal m;&lt;br /&gt;
  Bat b;&lt;br /&gt;
  instances[0] = &amp;amp;w;&lt;br /&gt;
  instances[1] = &amp;amp;m;&lt;br /&gt;
  instances[2] = &amp;amp;b;&lt;br /&gt;
  while(i&amp;lt;3)&lt;br /&gt;
    instances[i++]-&amp;gt;canEat();&lt;br /&gt;
  return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Java===&lt;br /&gt;
Java doesn’t support multiple inheritance but provides a feature called interfaces to resemble inheritance from multiple classes. Interface is an abstract class which provides a generic template by just declaring variables and methods without implementing them. Concrete classes that inherit interfaces are supposed to implement its methods. The example written using Java interfaces is shown below.&lt;br /&gt;
&lt;br /&gt;
 interface Animal&lt;br /&gt;
 {&lt;br /&gt;
   void statistics();  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Batsman extends Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   void statistics();  &lt;br /&gt;
 }&lt;br /&gt;
 class ImplementBatsman implements Batsman  &lt;br /&gt;
 {&lt;br /&gt;
    public void statistics()&lt;br /&gt;
    {&lt;br /&gt;
      System.out.println(&amp;quot;Batsman's statistics include batting average, number of 100's &amp;amp; 50's etc.&amp;quot;);&lt;br /&gt;
    }   &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Bowler extends Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   void statistics();&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementBowler implements Bowler&lt;br /&gt;
 {&lt;br /&gt;
    public void statistics()&lt;br /&gt;
    {&lt;br /&gt;
      System.out.println(&amp;quot;Bowler's statistics include number of wickets, bowling average, economy etc.&amp;quot;);&lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Allrounder extends Batsman, Bowler &lt;br /&gt;
 {&lt;br /&gt;
    // other methods to operate on Cricketer, Batsman &amp;amp; Bowler specific attributes&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementAllrounder implements Allrounder&lt;br /&gt;
 {  &lt;br /&gt;
    ImplementBowler bowl = new ImplementBowler();&lt;br /&gt;
    ImplementBatsman bat = new ImplementBatsman(); &lt;br /&gt;
    public void statistics()&lt;br /&gt;
    {&lt;br /&gt;
      bowl.statistics();&lt;br /&gt;
      bat.statistics();   &lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class MultipleInheritance&lt;br /&gt;
 {&lt;br /&gt;
   public static void main(String args[])&lt;br /&gt;
   {&lt;br /&gt;
     ImplementAllrounder all = new ImplementAllrounder();&lt;br /&gt;
     all.statistics();       &lt;br /&gt;
   } &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Ruby===&lt;br /&gt;
Ruby doesn't support multiple inheritance but provides a similar feature using modules and mixins.  &lt;br /&gt;
====Modules====&lt;br /&gt;
A Module is an encapsulation of methods and variables, although similar in function to a Class, differs from a class in the sense that it cannot be instantiated. Modules are  more dynamic and allow us to organize the code in a more granular way.Each module can add something to the existing class. A module can be thought of an encapsulation of a  subset of the full capabilities of an object.&lt;br /&gt;
An example found in the book ,The Well-Grounded Rubyist &amp;lt;ref&amp;gt;http://www.manning.com/black2/SampleChapter4.pdf [Pg.92] &amp;lt;/ref&amp;gt;, is the stack-like module. The module encapsulates the characteristics of being like a stack,implements the operations that can be performed by a stack but is not the actual Stack class. This allows us to use this module to mix-in  stack-like behaviour into other classes that require them.&lt;br /&gt;
&lt;br /&gt;
====Mixins====&lt;br /&gt;
A Mixin is a concept that allows inclusion of special characteristics into existing classes, so that common features that need to be reused in several classes can be selectively included, thus avoiding implementation of unnecessary functions like in the case of interfaces.&lt;br /&gt;
Mixins allow us to dynamically bind special features defined in modules, into core classes without really making any changes to the objects. This improves flexibility when we want to add plugins and extensions to the existing framework.&amp;lt;ref&amp;gt;http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-frameworks/&amp;lt;/ref&amp;gt;.The source code for the same example implemented using ruby mixins is shown below.&lt;br /&gt;
&lt;br /&gt;
 module Cricketer&lt;br /&gt;
   def statistics&lt;br /&gt;
     puts &amp;quot;Cricketer's statistics include number of matches, average etc.&amp;quot;&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Batsman&lt;br /&gt;
   include Cricketer&lt;br /&gt;
   def statistics&lt;br /&gt;
     puts &amp;quot;Batsman's statistics include batting average, number of 100's &amp;amp; 50's etc.&amp;quot;&lt;br /&gt;
   end    &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Bowler&lt;br /&gt;
   include Cricketer&lt;br /&gt;
   def statistics&lt;br /&gt;
      puts &amp;quot;Bowler's statistics include number of wickets, bowling average, economy etc.&amp;quot;&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class AllRounder&lt;br /&gt;
   include Batsman&lt;br /&gt;
   include Bowler&lt;br /&gt;
   include Cricketer&lt;br /&gt;
   # Other implementation&lt;br /&gt;
 end&lt;br /&gt;
 AllRounder.new().statistics()&lt;br /&gt;
&lt;br /&gt;
===Multiple inheritance in other languages===&lt;br /&gt;
Scala offers traits that are similar to Java interfaces to support multiple inheritance. Traits define a template with methods and variables which any class can make use of. The difference between interfaces and traits is that traits can implement methods. A Scala class can inherit from any number of traits together with a class in way to support multiple inheritance. Traits can be mixed in to a class even without inheriting it but just by including it at the time of creating instances. &amp;lt;ref&amp;gt;http://joelabrahamsson.com/entry/learning-scala-traits&amp;lt;/ref&amp;gt;. Trait can also extend a class and methods to be overridden are usually prefixed by the keyword override. The language enforces method names to be different across inherited  traits to avoid the issues of multiple inheritance. Python supports a limited version of multiple inheritance by extending multiple classes in depth-first left-right order &amp;lt;ref&amp;gt;http://docs.python.org/tutorial/classes.html&amp;lt;/ref&amp;gt;. If a python class D inherits classes A, B, C and if an instance variable or method is not available in the current class, it checks in A and its base classes, then moves to B and its base classes and eventually to C thus moving from left to right. In this way, python avoids the diamond problem by making the search order linear by which the derived class accesses attributes of its parent classes. In addition python also defines mixins to access reusable features within a class.&lt;br /&gt;
&lt;br /&gt;
==Advantages of mixins==&lt;br /&gt;
1. Firstly, when a module gets mixed into different classes, each class acquires the same  reference  to the module and hence even dynamic changes to the module gets reflected in the behavior of the classes &amp;lt;ref&amp;gt;http://www.linuxtopia.org/online_books/programming_books/ruby_tutorial/Ruby_Modules_Mixins.html&amp;lt;/ref&amp;gt;. In this way, the module’s code is not just copied to individual classes as in static languages instead its reference is provided. &lt;br /&gt;
&lt;br /&gt;
2. Secondly, the mixed in state and behavior can be accessed by the instance of target class just like accessing its own features. &lt;br /&gt;
&lt;br /&gt;
3. Thirdly, a class can include any number of modules in a way it can simulate multiple inheritance without having the related issues.&lt;br /&gt;
&lt;br /&gt;
==Questions to Ponder==&lt;br /&gt;
===Do mixins solve all of the problems of multiple inheritance?===&lt;br /&gt;
Undoubtedly mixins solve some of the problems of multiple inheritance. There is no ambiguity in resolving the request to a common method implemented by multiple modules. In that case, the lastly included module is responsible for handling accesses to common methods. Though the order of precedence of included modules matters, it looks clearer than C++.  However languages like Scala and Eiffel implement multiple inheritance far more safely  by explicitly forcing the user to have different method names across inherited classes.&lt;br /&gt;
&lt;br /&gt;
===Are mixins a clear advantage over interfaces?===&lt;br /&gt;
Mixins are more advantageous than interfaces because of their features. Interfaces have some limitations. As seen from programs, the derived class Allrounder uses the instances of Batsman and Bowler classes (has-a relationship) to access respective attributes. Interface is nice in the sense it provides a generic template that could be implemented differently by different classes.  However the target class has to necessarily implement every method of the interface. This would pose problems to a Class which is interested in implementing specific features of the interface. On the contrary, Mixins eliminate this inconvenience by mixing in the state and behavior of the modules in the target class and allows a it to use them as it needs. Hence the target class can just use module’s features selectively or completely instead of implementing by itself. The size of multiple inheritance code using mixins is smaller than interfaces and C++ variants.&lt;br /&gt;
&lt;br /&gt;
===Do mixins have any disadvantages not shared by multiple inheritance or interfaces?===&lt;br /&gt;
Inspite of making the lives of programmers better with effective code reuse and avoiding unnecessary implementation of methods that cause clutter in the code, the concept of Mixins can easily be abused resulting in namespace pollution. This is caused when a class includes multiple modules each of which contains several hundred method implementations. Having hundreds of methods mixed into a single class, makes it difficult to identify the source modules of the methods, thereby causing confusion while implementing these methods, whereas in the case of multiple inheritance we can easily identify the class to which the method belongs.&lt;br /&gt;
Another disadvantage that follows from the namespace collision is the case where we have multiple methods with the same name in different modules.In such a case whenever a call to that method is made, the method which belongs to the module that was included last will be called. The problem with this is that, this takes place silently without any warning. This may result in accidental overriding of wrong methods.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
In this article, we have discussed about multiple inheritance and means by which it is implemented in different languages. Though multiple inheritance is a useful feature,&lt;br /&gt;
many languages do not support them due to its associated issues. Given its advantages, mixin in ruby seems to be a viable option to achieve multiple inheritance. However mixins too have limitations which should be taken care of during implementation.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
==See Also==&lt;br /&gt;
1. Lucas Carlson and Leonard Richardson. 2006. [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=1212915&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 Ruby Cookbook] (Cookbooks (O'Reilly)). O'Reilly Media, Inc..&lt;br /&gt;
&lt;br /&gt;
2. Herbert Schildt. 2000. Java 2: [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=557816&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 The Complete Reference] (4th ed.). McGraw-Hill Professional.&lt;/div&gt;</summary>
		<author><name>Snallan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65431</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65431"/>
		<updated>2012-09-21T13:10:46Z</updated>

		<summary type="html">&lt;p&gt;Snallan: /* A Real World Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''1w23:  Multiple Inheritance and Mixins&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
The purpose of this article is to give a brief introduction about multiple inheritance, consider some of its issues and discuss techniques employed by different programming languages to handle such issues. In this relation, the article also compares solutions provided by different programming languages, especially against Ruby mixins, by listing their advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
Multiple Inheritance is a technique of establishing hierarchical relationships within a set of classes, such that a single class can inherit  behaviors from multiple parent classes. Although it might seem useful to include methods from multiple classes, the major issue with multiple inheritance is the ambiguity in referencing the parent class methods,  when more than one parent classes share a method with the same name.This ambiguity is predominant in cases where the inheritance hierarchy is more than one level deep, where the class in the lowermost level inherits from two classes, that have overridden the same method from their parent in different ways.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A Real World Example==&lt;br /&gt;
A known example of multiple inheritance to represent animals using classes and objects is cited for the sake of understanding &amp;lt;ref&amp;gt; http://stackoverflow.com/questions/9359948/multiple-inheritance-whats-a-good-example&amp;lt;/ref&amp;gt;. A Winged animal and a mammal inherit primitive attributes of an animal. While a winged animal can fly; a mammal can breathe and reproduce; they both eat, move and share features generic to animals. A bat being a winged mammal possesses features common to both winged animal and mammal. It follows that class Bat inherits from classes WingedAnimal and Mammal and in turn classes WingedAnimal and Mammal inherit from class Animal.   In reference to this context, let us give our own implementation and explore the issues of multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
[[File:Class_Diagram.png|thumb|right|x400px|alt=Class Diagram for the example.]]&lt;br /&gt;
&lt;br /&gt;
==Issues of Multiple Inheritance==&lt;br /&gt;
1. Due to inheritance, the parent class’s objects get duplicated to child class. As a result, Class Bat duplicates Animal objects through each of WingedAnimal and Mammal classes. When Bat accesses any of the Animal's attributes, the compiler would be ambiguous about determining the correct path to use since two paths are available from Animal to Bat. This problem of multiple inheritance is called diamond problem &amp;lt;ref&amp;gt;http://www.artima.com/weblogs/viewpost.jsp?thread=246488&amp;lt;/ref&amp;gt;. Compiler takes arbitrary decisions during such situations. &lt;br /&gt;
&lt;br /&gt;
2.Objects of parent classes can be separately managed by using [http://en.wikipedia.org/wiki/Virtual_method_table virtual tables]as in C++. By explicitly specifying the virtual table pointer, the Bat class can access WingedAnimal and Mammal classes unambiguously. However virtual tables occupy significant portion of memory thereby limiting their extensive usage.  &lt;br /&gt;
&lt;br /&gt;
3. The order by which a child class inherits parent classes also matters. If Bat inherits WingedAnimal prior to Mammal, then WingedAnimal precedes Mammal and common methods are accessed in this order of precedence.  Hence if the Programmer is not aware of such precedence, debugging would be difficult.&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
Programming languages provide different solutions to handle the issues of multiple inheritance. Here we consider the techniques offered by C++, Java, Ruby, Scala and Python.    &lt;br /&gt;
===Multiple Inheritance in C++===&lt;br /&gt;
C++ supports multiple inheritance with the notion of virtual classes and virtual functions. In the example, the Animal class defines virtual functions with WingedAnimal and Mammal classes virtually inheriting the features of Animal. The Bat class can now unambiguously access Cricketer class using virtual table pointers of WingedAnimal and Mammal. The example written using C++ virtual functions is shown below.&lt;br /&gt;
&lt;br /&gt;
 class Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void virtual canEat() {&lt;br /&gt;
  }&lt;br /&gt;
  void canMove() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class WingedAnimal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Mammal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Bat :public WingedAnimal, public Mammal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
  int i = 0;&lt;br /&gt;
  Animal *instances[3];&lt;br /&gt;
  WingedAnimal w;&lt;br /&gt;
  Mammal m;&lt;br /&gt;
  Bat b;&lt;br /&gt;
  instances[0] = &amp;amp;w;&lt;br /&gt;
  instances[1] = &amp;amp;m;&lt;br /&gt;
  instances[2] = &amp;amp;b;&lt;br /&gt;
  while(i&amp;lt;3)&lt;br /&gt;
    instances[i++]-&amp;gt;canEat();&lt;br /&gt;
  return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Java===&lt;br /&gt;
Java doesn’t support multiple inheritance but provides a feature called interfaces to resemble inheritance from multiple classes. Interface is an abstract class which provides a generic template by just declaring variables and methods without implementing them. Concrete classes that inherit interfaces are supposed to implement its methods. The example written using Java interfaces is shown below.&lt;br /&gt;
&lt;br /&gt;
 interface Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   void statistics();  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Batsman extends Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   void statistics();  &lt;br /&gt;
 }&lt;br /&gt;
 class ImplementBatsman implements Batsman  &lt;br /&gt;
 {&lt;br /&gt;
    public void statistics()&lt;br /&gt;
    {&lt;br /&gt;
      System.out.println(&amp;quot;Batsman's statistics include batting average, number of 100's &amp;amp; 50's etc.&amp;quot;);&lt;br /&gt;
    }   &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Bowler extends Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   void statistics();&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementBowler implements Bowler&lt;br /&gt;
 {&lt;br /&gt;
    public void statistics()&lt;br /&gt;
    {&lt;br /&gt;
      System.out.println(&amp;quot;Bowler's statistics include number of wickets, bowling average, economy etc.&amp;quot;);&lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Allrounder extends Batsman, Bowler &lt;br /&gt;
 {&lt;br /&gt;
    // other methods to operate on Cricketer, Batsman &amp;amp; Bowler specific attributes&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementAllrounder implements Allrounder&lt;br /&gt;
 {  &lt;br /&gt;
    ImplementBowler bowl = new ImplementBowler();&lt;br /&gt;
    ImplementBatsman bat = new ImplementBatsman(); &lt;br /&gt;
    public void statistics()&lt;br /&gt;
    {&lt;br /&gt;
      bowl.statistics();&lt;br /&gt;
      bat.statistics();   &lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class MultipleInheritance&lt;br /&gt;
 {&lt;br /&gt;
   public static void main(String args[])&lt;br /&gt;
   {&lt;br /&gt;
     ImplementAllrounder all = new ImplementAllrounder();&lt;br /&gt;
     all.statistics();       &lt;br /&gt;
   } &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Ruby===&lt;br /&gt;
Ruby doesn't support multiple inheritance but provides a similar feature using modules and mixins.  &lt;br /&gt;
====Modules====&lt;br /&gt;
A Module is an encapsulation of methods and variables, although similar in function to a Class, differs from a class in the sense that it cannot be instantiated. Modules are  more dynamic and allow us to organize the code in a more granular way.Each module can add something to the existing class. A module can be thought of an encapsulation of a  subset of the full capabilities of an object.&lt;br /&gt;
An example found in the book ,The Well-Grounded Rubyist &amp;lt;ref&amp;gt;http://www.manning.com/black2/SampleChapter4.pdf [Pg.92] &amp;lt;/ref&amp;gt;, is the stack-like module. The module encapsulates the characteristics of being like a stack,implements the operations that can be performed by a stack but is not the actual Stack class. This allows us to use this module to mix-in  stack-like behaviour into other classes that require them.&lt;br /&gt;
&lt;br /&gt;
====Mixins====&lt;br /&gt;
A Mixin is a concept that allows inclusion of special characteristics into existing classes, so that common features that need to be reused in several classes can be selectively included, thus avoiding implementation of unnecessary functions like in the case of interfaces.&lt;br /&gt;
Mixins allow us to dynamically bind special features defined in modules, into core classes without really making any changes to the objects. This improves flexibility when we want to add plugins and extensions to the existing framework.&amp;lt;ref&amp;gt;http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-frameworks/&amp;lt;/ref&amp;gt;.The source code for the same example implemented using ruby mixins is shown below.&lt;br /&gt;
&lt;br /&gt;
 module Cricketer&lt;br /&gt;
   def statistics&lt;br /&gt;
     puts &amp;quot;Cricketer's statistics include number of matches, average etc.&amp;quot;&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Batsman&lt;br /&gt;
   include Cricketer&lt;br /&gt;
   def statistics&lt;br /&gt;
     puts &amp;quot;Batsman's statistics include batting average, number of 100's &amp;amp; 50's etc.&amp;quot;&lt;br /&gt;
   end    &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Bowler&lt;br /&gt;
   include Cricketer&lt;br /&gt;
   def statistics&lt;br /&gt;
      puts &amp;quot;Bowler's statistics include number of wickets, bowling average, economy etc.&amp;quot;&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class AllRounder&lt;br /&gt;
   include Batsman&lt;br /&gt;
   include Bowler&lt;br /&gt;
   include Cricketer&lt;br /&gt;
   # Other implementation&lt;br /&gt;
 end&lt;br /&gt;
 AllRounder.new().statistics()&lt;br /&gt;
&lt;br /&gt;
===Multiple inheritance in other languages===&lt;br /&gt;
Scala offers traits that are similar to Java interfaces to support multiple inheritance. Traits define a template with methods and variables which any class can make use of. The difference between interfaces and traits is that traits can implement methods. A Scala class can inherit from any number of traits together with a class in way to support multiple inheritance. Traits can be mixed in to a class even without inheriting it but just by including it at the time of creating instances. &amp;lt;ref&amp;gt;http://joelabrahamsson.com/entry/learning-scala-traits&amp;lt;/ref&amp;gt;. Trait can also extend a class and methods to be overridden are usually prefixed by the keyword override. The language enforces method names to be different across inherited  traits to avoid the issues of multiple inheritance. Python supports a limited version of multiple inheritance by extending multiple classes in depth-first left-right order &amp;lt;ref&amp;gt;http://docs.python.org/tutorial/classes.html&amp;lt;/ref&amp;gt;. If a python class D inherits classes A, B, C and if an instance variable or method is not available in the current class, it checks in A and its base classes, then moves to B and its base classes and eventually to C thus moving from left to right. In this way, python avoids the diamond problem by making the search order linear by which the derived class accesses attributes of its parent classes. In addition python also defines mixins to access reusable features within a class.&lt;br /&gt;
&lt;br /&gt;
==Advantages of mixins==&lt;br /&gt;
1. Firstly, when a module gets mixed into different classes, each class acquires the same  reference  to the module and hence even dynamic changes to the module gets reflected in the behavior of the classes &amp;lt;ref&amp;gt;http://www.linuxtopia.org/online_books/programming_books/ruby_tutorial/Ruby_Modules_Mixins.html&amp;lt;/ref&amp;gt;. In this way, the module’s code is not just copied to individual classes as in static languages instead its reference is provided. &lt;br /&gt;
&lt;br /&gt;
2. Secondly, the mixed in state and behavior can be accessed by the instance of target class just like accessing its own features. &lt;br /&gt;
&lt;br /&gt;
3. Thirdly, a class can include any number of modules in a way it can simulate multiple inheritance without having the related issues.&lt;br /&gt;
&lt;br /&gt;
==Questions to Ponder==&lt;br /&gt;
===Do mixins solve all of the problems of multiple inheritance?===&lt;br /&gt;
Undoubtedly mixins solve some of the problems of multiple inheritance. There is no ambiguity in resolving the request to a common method implemented by multiple modules. In that case, the lastly included module is responsible for handling accesses to common methods. Though the order of precedence of included modules matters, it looks clearer than C++.  However languages like Scala and Eiffel implement multiple inheritance far more safely  by explicitly forcing the user to have different method names across inherited classes.&lt;br /&gt;
&lt;br /&gt;
===Are mixins a clear advantage over interfaces?===&lt;br /&gt;
Mixins are more advantageous than interfaces because of their features. Interfaces have some limitations. As seen from programs, the derived class Allrounder uses the instances of Batsman and Bowler classes (has-a relationship) to access respective attributes. Interface is nice in the sense it provides a generic template that could be implemented differently by different classes.  However the target class has to necessarily implement every method of the interface. This would pose problems to a Class which is interested in implementing specific features of the interface. On the contrary, Mixins eliminate this inconvenience by mixing in the state and behavior of the modules in the target class and allows a it to use them as it needs. Hence the target class can just use module’s features selectively or completely instead of implementing by itself. The size of multiple inheritance code using mixins is smaller than interfaces and C++ variants.&lt;br /&gt;
&lt;br /&gt;
===Do mixins have any disadvantages not shared by multiple inheritance or interfaces?===&lt;br /&gt;
Inspite of making the lives of programmers better with effective code reuse and avoiding unnecessary implementation of methods that cause clutter in the code, the concept of Mixins can easily be abused resulting in namespace pollution. This is caused when a class includes multiple modules each of which contains several hundred method implementations. Having hundreds of methods mixed into a single class, makes it difficult to identify the source modules of the methods, thereby causing confusion while implementing these methods, whereas in the case of multiple inheritance we can easily identify the class to which the method belongs.&lt;br /&gt;
Another disadvantage that follows from the namespace collision is the case where we have multiple methods with the same name in different modules.In such a case whenever a call to that method is made, the method which belongs to the module that was included last will be called. The problem with this is that, this takes place silently without any warning. This may result in accidental overriding of wrong methods.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
In this article, we have discussed about multiple inheritance and means by which it is implemented in different languages. Though multiple inheritance is a useful feature,&lt;br /&gt;
many languages do not support them due to its associated issues. Given its advantages, mixin in ruby seems to be a viable option to achieve multiple inheritance. However mixins too have limitations which should be taken care of during implementation.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
==See Also==&lt;br /&gt;
1. Lucas Carlson and Leonard Richardson. 2006. [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=1212915&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 Ruby Cookbook] (Cookbooks (O'Reilly)). O'Reilly Media, Inc..&lt;br /&gt;
&lt;br /&gt;
2. Herbert Schildt. 2000. Java 2: [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=557816&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 The Complete Reference] (4th ed.). McGraw-Hill Professional.&lt;/div&gt;</summary>
		<author><name>Snallan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65430</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65430"/>
		<updated>2012-09-21T13:08:31Z</updated>

		<summary type="html">&lt;p&gt;Snallan: /* Multiple Inheritance in C++ */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''1w23:  Multiple Inheritance and Mixins&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
The purpose of this article is to give a brief introduction about multiple inheritance, consider some of its issues and discuss techniques employed by different programming languages to handle such issues. In this relation, the article also compares solutions provided by different programming languages, especially against Ruby mixins, by listing their advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
Multiple Inheritance is a technique of establishing hierarchical relationships within a set of classes, such that a single class can inherit  behaviors from multiple parent classes. Although it might seem useful to include methods from multiple classes, the major issue with multiple inheritance is the ambiguity in referencing the parent class methods,  when more than one parent classes share a method with the same name.This ambiguity is predominant in cases where the inheritance hierarchy is more than one level deep, where the class in the lowermost level inherits from two classes, that have overridden the same method from their parent in different ways.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A Real World Example==&lt;br /&gt;
A known example of multiple inheritance to represent animals using classes and objects is cited here &amp;lt;ref&amp;gt; http://stackoverflow.com/questions/9359948/multiple-inheritance-whats-a-good-example&amp;lt;/ref&amp;gt; for the sake of understanding. A Winged animal and a mammal inherit primitive attributes of an animal. While a winged animal can fly; a mammal can breathe and reproduce; they both eat, move and share features generic to animals. A bat being a winged mammal possesses features common to both winged animal and mammal. It follows that class Bat inherits from classes WingedAnimal and Mammal and in turn classes WingedAnimal and Mammal inherit from class Animal.  Let us explore the issues of multiple inheritance in reference to this context.&lt;br /&gt;
&lt;br /&gt;
[[File:Class_Diagram.png|thumb|right|x400px|alt=Class Diagram for the example.]]&lt;br /&gt;
&lt;br /&gt;
==Issues of Multiple Inheritance==&lt;br /&gt;
1. Due to inheritance, the parent class’s objects get duplicated to child class. As a result, Class Bat duplicates Animal objects through each of WingedAnimal and Mammal classes. When Bat accesses any of the Animal's attributes, the compiler would be ambiguous about determining the correct path to use since two paths are available from Animal to Bat. This problem of multiple inheritance is called diamond problem &amp;lt;ref&amp;gt;http://www.artima.com/weblogs/viewpost.jsp?thread=246488&amp;lt;/ref&amp;gt;. Compiler takes arbitrary decisions during such situations. &lt;br /&gt;
&lt;br /&gt;
2.Objects of parent classes can be separately managed by using [http://en.wikipedia.org/wiki/Virtual_method_table virtual tables]as in C++. By explicitly specifying the virtual table pointer, the Bat class can access WingedAnimal and Mammal classes unambiguously. However virtual tables occupy significant portion of memory thereby limiting their extensive usage.  &lt;br /&gt;
&lt;br /&gt;
3. The order by which a child class inherits parent classes also matters. If Bat inherits WingedAnimal prior to Mammal, then WingedAnimal precedes Mammal and common methods are accessed in this order of precedence.  Hence if the Programmer is not aware of such precedence, debugging would be difficult.&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
Programming languages provide different solutions to handle the issues of multiple inheritance. Here we consider the techniques offered by C++, Java, Ruby, Scala and Python.    &lt;br /&gt;
===Multiple Inheritance in C++===&lt;br /&gt;
C++ supports multiple inheritance with the notion of virtual classes and virtual functions. In the example, the Animal class defines virtual functions with WingedAnimal and Mammal classes virtually inheriting the features of Animal. The Bat class can now unambiguously access Cricketer class using virtual table pointers of WingedAnimal and Mammal. The example written using C++ virtual functions is shown below.&lt;br /&gt;
&lt;br /&gt;
 class Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void virtual canEat() {&lt;br /&gt;
  }&lt;br /&gt;
  void canMove() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class WingedAnimal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Mammal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Bat :public WingedAnimal, public Mammal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
  int i = 0;&lt;br /&gt;
  Animal *instances[3];&lt;br /&gt;
  WingedAnimal w;&lt;br /&gt;
  Mammal m;&lt;br /&gt;
  Bat b;&lt;br /&gt;
  instances[0] = &amp;amp;w;&lt;br /&gt;
  instances[1] = &amp;amp;m;&lt;br /&gt;
  instances[2] = &amp;amp;b;&lt;br /&gt;
  while(i&amp;lt;3)&lt;br /&gt;
    instances[i++]-&amp;gt;canEat();&lt;br /&gt;
  return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Java===&lt;br /&gt;
Java doesn’t support multiple inheritance but provides a feature called interfaces to resemble inheritance from multiple classes. Interface is an abstract class which provides a generic template by just declaring variables and methods without implementing them. Concrete classes that inherit interfaces are supposed to implement its methods. The example written using Java interfaces is shown below.&lt;br /&gt;
&lt;br /&gt;
 interface Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   void statistics();  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Batsman extends Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   void statistics();  &lt;br /&gt;
 }&lt;br /&gt;
 class ImplementBatsman implements Batsman  &lt;br /&gt;
 {&lt;br /&gt;
    public void statistics()&lt;br /&gt;
    {&lt;br /&gt;
      System.out.println(&amp;quot;Batsman's statistics include batting average, number of 100's &amp;amp; 50's etc.&amp;quot;);&lt;br /&gt;
    }   &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Bowler extends Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   void statistics();&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementBowler implements Bowler&lt;br /&gt;
 {&lt;br /&gt;
    public void statistics()&lt;br /&gt;
    {&lt;br /&gt;
      System.out.println(&amp;quot;Bowler's statistics include number of wickets, bowling average, economy etc.&amp;quot;);&lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Allrounder extends Batsman, Bowler &lt;br /&gt;
 {&lt;br /&gt;
    // other methods to operate on Cricketer, Batsman &amp;amp; Bowler specific attributes&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementAllrounder implements Allrounder&lt;br /&gt;
 {  &lt;br /&gt;
    ImplementBowler bowl = new ImplementBowler();&lt;br /&gt;
    ImplementBatsman bat = new ImplementBatsman(); &lt;br /&gt;
    public void statistics()&lt;br /&gt;
    {&lt;br /&gt;
      bowl.statistics();&lt;br /&gt;
      bat.statistics();   &lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class MultipleInheritance&lt;br /&gt;
 {&lt;br /&gt;
   public static void main(String args[])&lt;br /&gt;
   {&lt;br /&gt;
     ImplementAllrounder all = new ImplementAllrounder();&lt;br /&gt;
     all.statistics();       &lt;br /&gt;
   } &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Ruby===&lt;br /&gt;
Ruby doesn't support multiple inheritance but provides a similar feature using modules and mixins.  &lt;br /&gt;
====Modules====&lt;br /&gt;
A Module is an encapsulation of methods and variables, although similar in function to a Class, differs from a class in the sense that it cannot be instantiated. Modules are  more dynamic and allow us to organize the code in a more granular way.Each module can add something to the existing class. A module can be thought of an encapsulation of a  subset of the full capabilities of an object.&lt;br /&gt;
An example found in the book ,The Well-Grounded Rubyist &amp;lt;ref&amp;gt;http://www.manning.com/black2/SampleChapter4.pdf [Pg.92] &amp;lt;/ref&amp;gt;, is the stack-like module. The module encapsulates the characteristics of being like a stack,implements the operations that can be performed by a stack but is not the actual Stack class. This allows us to use this module to mix-in  stack-like behaviour into other classes that require them.&lt;br /&gt;
&lt;br /&gt;
====Mixins====&lt;br /&gt;
A Mixin is a concept that allows inclusion of special characteristics into existing classes, so that common features that need to be reused in several classes can be selectively included, thus avoiding implementation of unnecessary functions like in the case of interfaces.&lt;br /&gt;
Mixins allow us to dynamically bind special features defined in modules, into core classes without really making any changes to the objects. This improves flexibility when we want to add plugins and extensions to the existing framework.&amp;lt;ref&amp;gt;http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-frameworks/&amp;lt;/ref&amp;gt;.The source code for the same example implemented using ruby mixins is shown below.&lt;br /&gt;
&lt;br /&gt;
 module Cricketer&lt;br /&gt;
   def statistics&lt;br /&gt;
     puts &amp;quot;Cricketer's statistics include number of matches, average etc.&amp;quot;&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Batsman&lt;br /&gt;
   include Cricketer&lt;br /&gt;
   def statistics&lt;br /&gt;
     puts &amp;quot;Batsman's statistics include batting average, number of 100's &amp;amp; 50's etc.&amp;quot;&lt;br /&gt;
   end    &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Bowler&lt;br /&gt;
   include Cricketer&lt;br /&gt;
   def statistics&lt;br /&gt;
      puts &amp;quot;Bowler's statistics include number of wickets, bowling average, economy etc.&amp;quot;&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class AllRounder&lt;br /&gt;
   include Batsman&lt;br /&gt;
   include Bowler&lt;br /&gt;
   include Cricketer&lt;br /&gt;
   # Other implementation&lt;br /&gt;
 end&lt;br /&gt;
 AllRounder.new().statistics()&lt;br /&gt;
&lt;br /&gt;
===Multiple inheritance in other languages===&lt;br /&gt;
Scala offers traits that are similar to Java interfaces to support multiple inheritance. Traits define a template with methods and variables which any class can make use of. The difference between interfaces and traits is that traits can implement methods. A Scala class can inherit from any number of traits together with a class in way to support multiple inheritance. Traits can be mixed in to a class even without inheriting it but just by including it at the time of creating instances. &amp;lt;ref&amp;gt;http://joelabrahamsson.com/entry/learning-scala-traits&amp;lt;/ref&amp;gt;. Trait can also extend a class and methods to be overridden are usually prefixed by the keyword override. The language enforces method names to be different across inherited  traits to avoid the issues of multiple inheritance. Python supports a limited version of multiple inheritance by extending multiple classes in depth-first left-right order &amp;lt;ref&amp;gt;http://docs.python.org/tutorial/classes.html&amp;lt;/ref&amp;gt;. If a python class D inherits classes A, B, C and if an instance variable or method is not available in the current class, it checks in A and its base classes, then moves to B and its base classes and eventually to C thus moving from left to right. In this way, python avoids the diamond problem by making the search order linear by which the derived class accesses attributes of its parent classes. In addition python also defines mixins to access reusable features within a class.&lt;br /&gt;
&lt;br /&gt;
==Advantages of mixins==&lt;br /&gt;
1. Firstly, when a module gets mixed into different classes, each class acquires the same  reference  to the module and hence even dynamic changes to the module gets reflected in the behavior of the classes &amp;lt;ref&amp;gt;http://www.linuxtopia.org/online_books/programming_books/ruby_tutorial/Ruby_Modules_Mixins.html&amp;lt;/ref&amp;gt;. In this way, the module’s code is not just copied to individual classes as in static languages instead its reference is provided. &lt;br /&gt;
&lt;br /&gt;
2. Secondly, the mixed in state and behavior can be accessed by the instance of target class just like accessing its own features. &lt;br /&gt;
&lt;br /&gt;
3. Thirdly, a class can include any number of modules in a way it can simulate multiple inheritance without having the related issues.&lt;br /&gt;
&lt;br /&gt;
==Questions to Ponder==&lt;br /&gt;
===Do mixins solve all of the problems of multiple inheritance?===&lt;br /&gt;
Undoubtedly mixins solve some of the problems of multiple inheritance. There is no ambiguity in resolving the request to a common method implemented by multiple modules. In that case, the lastly included module is responsible for handling accesses to common methods. Though the order of precedence of included modules matters, it looks clearer than C++.  However languages like Scala and Eiffel implement multiple inheritance far more safely  by explicitly forcing the user to have different method names across inherited classes.&lt;br /&gt;
&lt;br /&gt;
===Are mixins a clear advantage over interfaces?===&lt;br /&gt;
Mixins are more advantageous than interfaces because of their features. Interfaces have some limitations. As seen from programs, the derived class Allrounder uses the instances of Batsman and Bowler classes (has-a relationship) to access respective attributes. Interface is nice in the sense it provides a generic template that could be implemented differently by different classes.  However the target class has to necessarily implement every method of the interface. This would pose problems to a Class which is interested in implementing specific features of the interface. On the contrary, Mixins eliminate this inconvenience by mixing in the state and behavior of the modules in the target class and allows a it to use them as it needs. Hence the target class can just use module’s features selectively or completely instead of implementing by itself. The size of multiple inheritance code using mixins is smaller than interfaces and C++ variants.&lt;br /&gt;
&lt;br /&gt;
===Do mixins have any disadvantages not shared by multiple inheritance or interfaces?===&lt;br /&gt;
Inspite of making the lives of programmers better with effective code reuse and avoiding unnecessary implementation of methods that cause clutter in the code, the concept of Mixins can easily be abused resulting in namespace pollution. This is caused when a class includes multiple modules each of which contains several hundred method implementations. Having hundreds of methods mixed into a single class, makes it difficult to identify the source modules of the methods, thereby causing confusion while implementing these methods, whereas in the case of multiple inheritance we can easily identify the class to which the method belongs.&lt;br /&gt;
Another disadvantage that follows from the namespace collision is the case where we have multiple methods with the same name in different modules.In such a case whenever a call to that method is made, the method which belongs to the module that was included last will be called. The problem with this is that, this takes place silently without any warning. This may result in accidental overriding of wrong methods.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
In this article, we have discussed about multiple inheritance and means by which it is implemented in different languages. Though multiple inheritance is a useful feature,&lt;br /&gt;
many languages do not support them due to its associated issues. Given its advantages, mixin in ruby seems to be a viable option to achieve multiple inheritance. However mixins too have limitations which should be taken care of during implementation.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
==See Also==&lt;br /&gt;
1. Lucas Carlson and Leonard Richardson. 2006. [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=1212915&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 Ruby Cookbook] (Cookbooks (O'Reilly)). O'Reilly Media, Inc..&lt;br /&gt;
&lt;br /&gt;
2. Herbert Schildt. 2000. Java 2: [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=557816&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 The Complete Reference] (4th ed.). McGraw-Hill Professional.&lt;/div&gt;</summary>
		<author><name>Snallan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65429</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65429"/>
		<updated>2012-09-21T13:07:14Z</updated>

		<summary type="html">&lt;p&gt;Snallan: /* Multiple Inheritance in C++ */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''1w23:  Multiple Inheritance and Mixins&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
The purpose of this article is to give a brief introduction about multiple inheritance, consider some of its issues and discuss techniques employed by different programming languages to handle such issues. In this relation, the article also compares solutions provided by different programming languages, especially against Ruby mixins, by listing their advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
Multiple Inheritance is a technique of establishing hierarchical relationships within a set of classes, such that a single class can inherit  behaviors from multiple parent classes. Although it might seem useful to include methods from multiple classes, the major issue with multiple inheritance is the ambiguity in referencing the parent class methods,  when more than one parent classes share a method with the same name.This ambiguity is predominant in cases where the inheritance hierarchy is more than one level deep, where the class in the lowermost level inherits from two classes, that have overridden the same method from their parent in different ways.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A Real World Example==&lt;br /&gt;
A known example of multiple inheritance to represent animals using classes and objects is cited here &amp;lt;ref&amp;gt; http://stackoverflow.com/questions/9359948/multiple-inheritance-whats-a-good-example&amp;lt;/ref&amp;gt; for the sake of understanding. A Winged animal and a mammal inherit primitive attributes of an animal. While a winged animal can fly; a mammal can breathe and reproduce; they both eat, move and share features generic to animals. A bat being a winged mammal possesses features common to both winged animal and mammal. It follows that class Bat inherits from classes WingedAnimal and Mammal and in turn classes WingedAnimal and Mammal inherit from class Animal.  Let us explore the issues of multiple inheritance in reference to this context.&lt;br /&gt;
&lt;br /&gt;
[[File:Class_Diagram.png|thumb|right|x400px|alt=Class Diagram for the example.]]&lt;br /&gt;
&lt;br /&gt;
==Issues of Multiple Inheritance==&lt;br /&gt;
1. Due to inheritance, the parent class’s objects get duplicated to child class. As a result, Class Bat duplicates Animal objects through each of WingedAnimal and Mammal classes. When Bat accesses any of the Animal's attributes, the compiler would be ambiguous about determining the correct path to use since two paths are available from Animal to Bat. This problem of multiple inheritance is called diamond problem &amp;lt;ref&amp;gt;http://www.artima.com/weblogs/viewpost.jsp?thread=246488&amp;lt;/ref&amp;gt;. Compiler takes arbitrary decisions during such situations. &lt;br /&gt;
&lt;br /&gt;
2.Objects of parent classes can be separately managed by using [http://en.wikipedia.org/wiki/Virtual_method_table virtual tables]as in C++. By explicitly specifying the virtual table pointer, the Bat class can access WingedAnimal and Mammal classes unambiguously. However virtual tables occupy significant portion of memory thereby limiting their extensive usage.  &lt;br /&gt;
&lt;br /&gt;
3. The order by which a child class inherits parent classes also matters. If Bat inherits WingedAnimal prior to Mammal, then WingedAnimal precedes Mammal and common methods are accessed in this order of precedence.  Hence if the Programmer is not aware of such precedence, debugging would be difficult.&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
Programming languages provide different solutions to handle the issues of multiple inheritance. Here we consider the techniques offered by C++, Java, Ruby, Scala and Python.    &lt;br /&gt;
===Multiple Inheritance in C++===&lt;br /&gt;
C++ supports multiple inheritance with the notion of virtual classes and virtual functions. In the example, the Cricketer class defines virtual functions with Batsman and Bowler classes virtually inheriting the features of Cricketer. The Allrounder class can now unambiguously access Cricketer class using virtual table pointers of Batsman and Bowler. The example written using C++ virtual functions is shown below.&lt;br /&gt;
&lt;br /&gt;
 class Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void virtual canEat() {&lt;br /&gt;
  }&lt;br /&gt;
  void canMove() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class WingedAnimal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Mammal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Bat :public WingedAnimal, public Mammal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
  int i = 0;&lt;br /&gt;
  Animal *instances[3];&lt;br /&gt;
  WingedAnimal w;&lt;br /&gt;
  Mammal m;&lt;br /&gt;
  Bat b;&lt;br /&gt;
  instances[0] = &amp;amp;w;&lt;br /&gt;
  instances[1] = &amp;amp;m;&lt;br /&gt;
  instances[2] = &amp;amp;b;&lt;br /&gt;
  while(i&amp;lt;3)&lt;br /&gt;
    instances[i++]-&amp;gt;canEat();&lt;br /&gt;
  return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Java===&lt;br /&gt;
Java doesn’t support multiple inheritance but provides a feature called interfaces to resemble inheritance from multiple classes. Interface is an abstract class which provides a generic template by just declaring variables and methods without implementing them. Concrete classes that inherit interfaces are supposed to implement its methods. The example written using Java interfaces is shown below.&lt;br /&gt;
&lt;br /&gt;
 interface Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   void statistics();  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Batsman extends Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   void statistics();  &lt;br /&gt;
 }&lt;br /&gt;
 class ImplementBatsman implements Batsman  &lt;br /&gt;
 {&lt;br /&gt;
    public void statistics()&lt;br /&gt;
    {&lt;br /&gt;
      System.out.println(&amp;quot;Batsman's statistics include batting average, number of 100's &amp;amp; 50's etc.&amp;quot;);&lt;br /&gt;
    }   &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Bowler extends Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   void statistics();&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementBowler implements Bowler&lt;br /&gt;
 {&lt;br /&gt;
    public void statistics()&lt;br /&gt;
    {&lt;br /&gt;
      System.out.println(&amp;quot;Bowler's statistics include number of wickets, bowling average, economy etc.&amp;quot;);&lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Allrounder extends Batsman, Bowler &lt;br /&gt;
 {&lt;br /&gt;
    // other methods to operate on Cricketer, Batsman &amp;amp; Bowler specific attributes&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementAllrounder implements Allrounder&lt;br /&gt;
 {  &lt;br /&gt;
    ImplementBowler bowl = new ImplementBowler();&lt;br /&gt;
    ImplementBatsman bat = new ImplementBatsman(); &lt;br /&gt;
    public void statistics()&lt;br /&gt;
    {&lt;br /&gt;
      bowl.statistics();&lt;br /&gt;
      bat.statistics();   &lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class MultipleInheritance&lt;br /&gt;
 {&lt;br /&gt;
   public static void main(String args[])&lt;br /&gt;
   {&lt;br /&gt;
     ImplementAllrounder all = new ImplementAllrounder();&lt;br /&gt;
     all.statistics();       &lt;br /&gt;
   } &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Ruby===&lt;br /&gt;
Ruby doesn't support multiple inheritance but provides a similar feature using modules and mixins.  &lt;br /&gt;
====Modules====&lt;br /&gt;
A Module is an encapsulation of methods and variables, although similar in function to a Class, differs from a class in the sense that it cannot be instantiated. Modules are  more dynamic and allow us to organize the code in a more granular way.Each module can add something to the existing class. A module can be thought of an encapsulation of a  subset of the full capabilities of an object.&lt;br /&gt;
An example found in the book ,The Well-Grounded Rubyist &amp;lt;ref&amp;gt;http://www.manning.com/black2/SampleChapter4.pdf [Pg.92] &amp;lt;/ref&amp;gt;, is the stack-like module. The module encapsulates the characteristics of being like a stack,implements the operations that can be performed by a stack but is not the actual Stack class. This allows us to use this module to mix-in  stack-like behaviour into other classes that require them.&lt;br /&gt;
&lt;br /&gt;
====Mixins====&lt;br /&gt;
A Mixin is a concept that allows inclusion of special characteristics into existing classes, so that common features that need to be reused in several classes can be selectively included, thus avoiding implementation of unnecessary functions like in the case of interfaces.&lt;br /&gt;
Mixins allow us to dynamically bind special features defined in modules, into core classes without really making any changes to the objects. This improves flexibility when we want to add plugins and extensions to the existing framework.&amp;lt;ref&amp;gt;http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-frameworks/&amp;lt;/ref&amp;gt;.The source code for the same example implemented using ruby mixins is shown below.&lt;br /&gt;
&lt;br /&gt;
 module Cricketer&lt;br /&gt;
   def statistics&lt;br /&gt;
     puts &amp;quot;Cricketer's statistics include number of matches, average etc.&amp;quot;&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Batsman&lt;br /&gt;
   include Cricketer&lt;br /&gt;
   def statistics&lt;br /&gt;
     puts &amp;quot;Batsman's statistics include batting average, number of 100's &amp;amp; 50's etc.&amp;quot;&lt;br /&gt;
   end    &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Bowler&lt;br /&gt;
   include Cricketer&lt;br /&gt;
   def statistics&lt;br /&gt;
      puts &amp;quot;Bowler's statistics include number of wickets, bowling average, economy etc.&amp;quot;&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class AllRounder&lt;br /&gt;
   include Batsman&lt;br /&gt;
   include Bowler&lt;br /&gt;
   include Cricketer&lt;br /&gt;
   # Other implementation&lt;br /&gt;
 end&lt;br /&gt;
 AllRounder.new().statistics()&lt;br /&gt;
&lt;br /&gt;
===Multiple inheritance in other languages===&lt;br /&gt;
Scala offers traits that are similar to Java interfaces to support multiple inheritance. Traits define a template with methods and variables which any class can make use of. The difference between interfaces and traits is that traits can implement methods. A Scala class can inherit from any number of traits together with a class in way to support multiple inheritance. Traits can be mixed in to a class even without inheriting it but just by including it at the time of creating instances. &amp;lt;ref&amp;gt;http://joelabrahamsson.com/entry/learning-scala-traits&amp;lt;/ref&amp;gt;. Trait can also extend a class and methods to be overridden are usually prefixed by the keyword override. The language enforces method names to be different across inherited  traits to avoid the issues of multiple inheritance. Python supports a limited version of multiple inheritance by extending multiple classes in depth-first left-right order &amp;lt;ref&amp;gt;http://docs.python.org/tutorial/classes.html&amp;lt;/ref&amp;gt;. If a python class D inherits classes A, B, C and if an instance variable or method is not available in the current class, it checks in A and its base classes, then moves to B and its base classes and eventually to C thus moving from left to right. In this way, python avoids the diamond problem by making the search order linear by which the derived class accesses attributes of its parent classes. In addition python also defines mixins to access reusable features within a class.&lt;br /&gt;
&lt;br /&gt;
==Advantages of mixins==&lt;br /&gt;
1. Firstly, when a module gets mixed into different classes, each class acquires the same  reference  to the module and hence even dynamic changes to the module gets reflected in the behavior of the classes &amp;lt;ref&amp;gt;http://www.linuxtopia.org/online_books/programming_books/ruby_tutorial/Ruby_Modules_Mixins.html&amp;lt;/ref&amp;gt;. In this way, the module’s code is not just copied to individual classes as in static languages instead its reference is provided. &lt;br /&gt;
&lt;br /&gt;
2. Secondly, the mixed in state and behavior can be accessed by the instance of target class just like accessing its own features. &lt;br /&gt;
&lt;br /&gt;
3. Thirdly, a class can include any number of modules in a way it can simulate multiple inheritance without having the related issues.&lt;br /&gt;
&lt;br /&gt;
==Questions to Ponder==&lt;br /&gt;
===Do mixins solve all of the problems of multiple inheritance?===&lt;br /&gt;
Undoubtedly mixins solve some of the problems of multiple inheritance. There is no ambiguity in resolving the request to a common method implemented by multiple modules. In that case, the lastly included module is responsible for handling accesses to common methods. Though the order of precedence of included modules matters, it looks clearer than C++.  However languages like Scala and Eiffel implement multiple inheritance far more safely  by explicitly forcing the user to have different method names across inherited classes.&lt;br /&gt;
&lt;br /&gt;
===Are mixins a clear advantage over interfaces?===&lt;br /&gt;
Mixins are more advantageous than interfaces because of their features. Interfaces have some limitations. As seen from programs, the derived class Allrounder uses the instances of Batsman and Bowler classes (has-a relationship) to access respective attributes. Interface is nice in the sense it provides a generic template that could be implemented differently by different classes.  However the target class has to necessarily implement every method of the interface. This would pose problems to a Class which is interested in implementing specific features of the interface. On the contrary, Mixins eliminate this inconvenience by mixing in the state and behavior of the modules in the target class and allows a it to use them as it needs. Hence the target class can just use module’s features selectively or completely instead of implementing by itself. The size of multiple inheritance code using mixins is smaller than interfaces and C++ variants.&lt;br /&gt;
&lt;br /&gt;
===Do mixins have any disadvantages not shared by multiple inheritance or interfaces?===&lt;br /&gt;
Inspite of making the lives of programmers better with effective code reuse and avoiding unnecessary implementation of methods that cause clutter in the code, the concept of Mixins can easily be abused resulting in namespace pollution. This is caused when a class includes multiple modules each of which contains several hundred method implementations. Having hundreds of methods mixed into a single class, makes it difficult to identify the source modules of the methods, thereby causing confusion while implementing these methods, whereas in the case of multiple inheritance we can easily identify the class to which the method belongs.&lt;br /&gt;
Another disadvantage that follows from the namespace collision is the case where we have multiple methods with the same name in different modules.In such a case whenever a call to that method is made, the method which belongs to the module that was included last will be called. The problem with this is that, this takes place silently without any warning. This may result in accidental overriding of wrong methods.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
In this article, we have discussed about multiple inheritance and means by which it is implemented in different languages. Though multiple inheritance is a useful feature,&lt;br /&gt;
many languages do not support them due to its associated issues. Given its advantages, mixin in ruby seems to be a viable option to achieve multiple inheritance. However mixins too have limitations which should be taken care of during implementation.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
==See Also==&lt;br /&gt;
1. Lucas Carlson and Leonard Richardson. 2006. [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=1212915&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 Ruby Cookbook] (Cookbooks (O'Reilly)). O'Reilly Media, Inc..&lt;br /&gt;
&lt;br /&gt;
2. Herbert Schildt. 2000. Java 2: [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=557816&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 The Complete Reference] (4th ed.). McGraw-Hill Professional.&lt;/div&gt;</summary>
		<author><name>Snallan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65428</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65428"/>
		<updated>2012-09-21T13:06:22Z</updated>

		<summary type="html">&lt;p&gt;Snallan: /* Multiple Inheritance in C++ */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''1w23:  Multiple Inheritance and Mixins&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
The purpose of this article is to give a brief introduction about multiple inheritance, consider some of its issues and discuss techniques employed by different programming languages to handle such issues. In this relation, the article also compares solutions provided by different programming languages, especially against Ruby mixins, by listing their advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
Multiple Inheritance is a technique of establishing hierarchical relationships within a set of classes, such that a single class can inherit  behaviors from multiple parent classes. Although it might seem useful to include methods from multiple classes, the major issue with multiple inheritance is the ambiguity in referencing the parent class methods,  when more than one parent classes share a method with the same name.This ambiguity is predominant in cases where the inheritance hierarchy is more than one level deep, where the class in the lowermost level inherits from two classes, that have overridden the same method from their parent in different ways.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A Real World Example==&lt;br /&gt;
A known example of multiple inheritance to represent animals using classes and objects is cited here &amp;lt;ref&amp;gt; http://stackoverflow.com/questions/9359948/multiple-inheritance-whats-a-good-example&amp;lt;/ref&amp;gt; for the sake of understanding. A Winged animal and a mammal inherit primitive attributes of an animal. While a winged animal can fly; a mammal can breathe and reproduce; they both eat, move and share features generic to animals. A bat being a winged mammal possesses features common to both winged animal and mammal. It follows that class Bat inherits from classes WingedAnimal and Mammal and in turn classes WingedAnimal and Mammal inherit from class Animal.  Let us explore the issues of multiple inheritance in reference to this context.&lt;br /&gt;
&lt;br /&gt;
[[File:Class_Diagram.png|thumb|right|x400px|alt=Class Diagram for the example.]]&lt;br /&gt;
&lt;br /&gt;
==Issues of Multiple Inheritance==&lt;br /&gt;
1. Due to inheritance, the parent class’s objects get duplicated to child class. As a result, Class Bat duplicates Animal objects through each of WingedAnimal and Mammal classes. When Bat accesses any of the Animal's attributes, the compiler would be ambiguous about determining the correct path to use since two paths are available from Animal to Bat. This problem of multiple inheritance is called diamond problem &amp;lt;ref&amp;gt;http://www.artima.com/weblogs/viewpost.jsp?thread=246488&amp;lt;/ref&amp;gt;. Compiler takes arbitrary decisions during such situations. &lt;br /&gt;
&lt;br /&gt;
2.Objects of parent classes can be separately managed by using [http://en.wikipedia.org/wiki/Virtual_method_table virtual tables]as in C++. By explicitly specifying the virtual table pointer, the Bat class can access WingedAnimal and Mammal classes unambiguously. However virtual tables occupy significant portion of memory thereby limiting their extensive usage.  &lt;br /&gt;
&lt;br /&gt;
3. The order by which a child class inherits parent classes also matters. If Bat inherits WingedAnimal prior to Mammal, then WingedAnimal precedes Mammal and common methods are accessed in this order of precedence.  Hence if the Programmer is not aware of such precedence, debugging would be difficult.&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
Programming languages provide different solutions to handle the issues of multiple inheritance. Here we consider the techniques offered by C++, Java, Ruby, Scala and Python.    &lt;br /&gt;
===Multiple Inheritance in C++===&lt;br /&gt;
C++ supports multiple inheritance with the notion of virtual classes and virtual functions. In the example, the Cricketer class defines virtual functions with Batsman and Bowler classes virtually inheriting the features of Cricketer. The Allrounder class can now unambiguously access Cricketer class using virtual table pointers of Batsman and Bowler. The example written using C++ virtual functions is shown below.&lt;br /&gt;
&lt;br /&gt;
 class Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void virtual canEat() {&lt;br /&gt;
  }&lt;br /&gt;
  void canMove() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class WingedAnimal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Mammal :virtual public Animal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Bat :public WingedAnimal, public Mammal&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
  void canFly() {&lt;br /&gt;
  }&lt;br /&gt;
  void canGiveBirth() {&lt;br /&gt;
  }&lt;br /&gt;
  void canEat() {&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
   int i = 0;&lt;br /&gt;
   Cricketer *instances[3];&lt;br /&gt;
   Batsman bat;&lt;br /&gt;
   Bowler bowl;&lt;br /&gt;
   Allrounder all;&lt;br /&gt;
   instances[0] = &amp;amp;bat;&lt;br /&gt;
   instances[1] = &amp;amp;bowl;&lt;br /&gt;
   instances[2] = &amp;amp;all;&lt;br /&gt;
   while(i&amp;lt;3) &lt;br /&gt;
     instances[i++]-&amp;gt;statistics();      &lt;br /&gt;
   return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Java===&lt;br /&gt;
Java doesn’t support multiple inheritance but provides a feature called interfaces to resemble inheritance from multiple classes. Interface is an abstract class which provides a generic template by just declaring variables and methods without implementing them. Concrete classes that inherit interfaces are supposed to implement its methods. The example written using Java interfaces is shown below.&lt;br /&gt;
&lt;br /&gt;
 interface Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   void statistics();  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Batsman extends Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   void statistics();  &lt;br /&gt;
 }&lt;br /&gt;
 class ImplementBatsman implements Batsman  &lt;br /&gt;
 {&lt;br /&gt;
    public void statistics()&lt;br /&gt;
    {&lt;br /&gt;
      System.out.println(&amp;quot;Batsman's statistics include batting average, number of 100's &amp;amp; 50's etc.&amp;quot;);&lt;br /&gt;
    }   &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Bowler extends Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   void statistics();&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementBowler implements Bowler&lt;br /&gt;
 {&lt;br /&gt;
    public void statistics()&lt;br /&gt;
    {&lt;br /&gt;
      System.out.println(&amp;quot;Bowler's statistics include number of wickets, bowling average, economy etc.&amp;quot;);&lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Allrounder extends Batsman, Bowler &lt;br /&gt;
 {&lt;br /&gt;
    // other methods to operate on Cricketer, Batsman &amp;amp; Bowler specific attributes&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementAllrounder implements Allrounder&lt;br /&gt;
 {  &lt;br /&gt;
    ImplementBowler bowl = new ImplementBowler();&lt;br /&gt;
    ImplementBatsman bat = new ImplementBatsman(); &lt;br /&gt;
    public void statistics()&lt;br /&gt;
    {&lt;br /&gt;
      bowl.statistics();&lt;br /&gt;
      bat.statistics();   &lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class MultipleInheritance&lt;br /&gt;
 {&lt;br /&gt;
   public static void main(String args[])&lt;br /&gt;
   {&lt;br /&gt;
     ImplementAllrounder all = new ImplementAllrounder();&lt;br /&gt;
     all.statistics();       &lt;br /&gt;
   } &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Ruby===&lt;br /&gt;
Ruby doesn't support multiple inheritance but provides a similar feature using modules and mixins.  &lt;br /&gt;
====Modules====&lt;br /&gt;
A Module is an encapsulation of methods and variables, although similar in function to a Class, differs from a class in the sense that it cannot be instantiated. Modules are  more dynamic and allow us to organize the code in a more granular way.Each module can add something to the existing class. A module can be thought of an encapsulation of a  subset of the full capabilities of an object.&lt;br /&gt;
An example found in the book ,The Well-Grounded Rubyist &amp;lt;ref&amp;gt;http://www.manning.com/black2/SampleChapter4.pdf [Pg.92] &amp;lt;/ref&amp;gt;, is the stack-like module. The module encapsulates the characteristics of being like a stack,implements the operations that can be performed by a stack but is not the actual Stack class. This allows us to use this module to mix-in  stack-like behaviour into other classes that require them.&lt;br /&gt;
&lt;br /&gt;
====Mixins====&lt;br /&gt;
A Mixin is a concept that allows inclusion of special characteristics into existing classes, so that common features that need to be reused in several classes can be selectively included, thus avoiding implementation of unnecessary functions like in the case of interfaces.&lt;br /&gt;
Mixins allow us to dynamically bind special features defined in modules, into core classes without really making any changes to the objects. This improves flexibility when we want to add plugins and extensions to the existing framework.&amp;lt;ref&amp;gt;http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-frameworks/&amp;lt;/ref&amp;gt;.The source code for the same example implemented using ruby mixins is shown below.&lt;br /&gt;
&lt;br /&gt;
 module Cricketer&lt;br /&gt;
   def statistics&lt;br /&gt;
     puts &amp;quot;Cricketer's statistics include number of matches, average etc.&amp;quot;&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Batsman&lt;br /&gt;
   include Cricketer&lt;br /&gt;
   def statistics&lt;br /&gt;
     puts &amp;quot;Batsman's statistics include batting average, number of 100's &amp;amp; 50's etc.&amp;quot;&lt;br /&gt;
   end    &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Bowler&lt;br /&gt;
   include Cricketer&lt;br /&gt;
   def statistics&lt;br /&gt;
      puts &amp;quot;Bowler's statistics include number of wickets, bowling average, economy etc.&amp;quot;&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class AllRounder&lt;br /&gt;
   include Batsman&lt;br /&gt;
   include Bowler&lt;br /&gt;
   include Cricketer&lt;br /&gt;
   # Other implementation&lt;br /&gt;
 end&lt;br /&gt;
 AllRounder.new().statistics()&lt;br /&gt;
&lt;br /&gt;
===Multiple inheritance in other languages===&lt;br /&gt;
Scala offers traits that are similar to Java interfaces to support multiple inheritance. Traits define a template with methods and variables which any class can make use of. The difference between interfaces and traits is that traits can implement methods. A Scala class can inherit from any number of traits together with a class in way to support multiple inheritance. Traits can be mixed in to a class even without inheriting it but just by including it at the time of creating instances. &amp;lt;ref&amp;gt;http://joelabrahamsson.com/entry/learning-scala-traits&amp;lt;/ref&amp;gt;. Trait can also extend a class and methods to be overridden are usually prefixed by the keyword override. The language enforces method names to be different across inherited  traits to avoid the issues of multiple inheritance. Python supports a limited version of multiple inheritance by extending multiple classes in depth-first left-right order &amp;lt;ref&amp;gt;http://docs.python.org/tutorial/classes.html&amp;lt;/ref&amp;gt;. If a python class D inherits classes A, B, C and if an instance variable or method is not available in the current class, it checks in A and its base classes, then moves to B and its base classes and eventually to C thus moving from left to right. In this way, python avoids the diamond problem by making the search order linear by which the derived class accesses attributes of its parent classes. In addition python also defines mixins to access reusable features within a class.&lt;br /&gt;
&lt;br /&gt;
==Advantages of mixins==&lt;br /&gt;
1. Firstly, when a module gets mixed into different classes, each class acquires the same  reference  to the module and hence even dynamic changes to the module gets reflected in the behavior of the classes &amp;lt;ref&amp;gt;http://www.linuxtopia.org/online_books/programming_books/ruby_tutorial/Ruby_Modules_Mixins.html&amp;lt;/ref&amp;gt;. In this way, the module’s code is not just copied to individual classes as in static languages instead its reference is provided. &lt;br /&gt;
&lt;br /&gt;
2. Secondly, the mixed in state and behavior can be accessed by the instance of target class just like accessing its own features. &lt;br /&gt;
&lt;br /&gt;
3. Thirdly, a class can include any number of modules in a way it can simulate multiple inheritance without having the related issues.&lt;br /&gt;
&lt;br /&gt;
==Questions to Ponder==&lt;br /&gt;
===Do mixins solve all of the problems of multiple inheritance?===&lt;br /&gt;
Undoubtedly mixins solve some of the problems of multiple inheritance. There is no ambiguity in resolving the request to a common method implemented by multiple modules. In that case, the lastly included module is responsible for handling accesses to common methods. Though the order of precedence of included modules matters, it looks clearer than C++.  However languages like Scala and Eiffel implement multiple inheritance far more safely  by explicitly forcing the user to have different method names across inherited classes.&lt;br /&gt;
&lt;br /&gt;
===Are mixins a clear advantage over interfaces?===&lt;br /&gt;
Mixins are more advantageous than interfaces because of their features. Interfaces have some limitations. As seen from programs, the derived class Allrounder uses the instances of Batsman and Bowler classes (has-a relationship) to access respective attributes. Interface is nice in the sense it provides a generic template that could be implemented differently by different classes.  However the target class has to necessarily implement every method of the interface. This would pose problems to a Class which is interested in implementing specific features of the interface. On the contrary, Mixins eliminate this inconvenience by mixing in the state and behavior of the modules in the target class and allows a it to use them as it needs. Hence the target class can just use module’s features selectively or completely instead of implementing by itself. The size of multiple inheritance code using mixins is smaller than interfaces and C++ variants.&lt;br /&gt;
&lt;br /&gt;
===Do mixins have any disadvantages not shared by multiple inheritance or interfaces?===&lt;br /&gt;
Inspite of making the lives of programmers better with effective code reuse and avoiding unnecessary implementation of methods that cause clutter in the code, the concept of Mixins can easily be abused resulting in namespace pollution. This is caused when a class includes multiple modules each of which contains several hundred method implementations. Having hundreds of methods mixed into a single class, makes it difficult to identify the source modules of the methods, thereby causing confusion while implementing these methods, whereas in the case of multiple inheritance we can easily identify the class to which the method belongs.&lt;br /&gt;
Another disadvantage that follows from the namespace collision is the case where we have multiple methods with the same name in different modules.In such a case whenever a call to that method is made, the method which belongs to the module that was included last will be called. The problem with this is that, this takes place silently without any warning. This may result in accidental overriding of wrong methods.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
In this article, we have discussed about multiple inheritance and means by which it is implemented in different languages. Though multiple inheritance is a useful feature,&lt;br /&gt;
many languages do not support them due to its associated issues. Given its advantages, mixin in ruby seems to be a viable option to achieve multiple inheritance. However mixins too have limitations which should be taken care of during implementation.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
==See Also==&lt;br /&gt;
1. Lucas Carlson and Leonard Richardson. 2006. [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=1212915&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 Ruby Cookbook] (Cookbooks (O'Reilly)). O'Reilly Media, Inc..&lt;br /&gt;
&lt;br /&gt;
2. Herbert Schildt. 2000. Java 2: [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=557816&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 The Complete Reference] (4th ed.). McGraw-Hill Professional.&lt;/div&gt;</summary>
		<author><name>Snallan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65426</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65426"/>
		<updated>2012-09-21T12:42:17Z</updated>

		<summary type="html">&lt;p&gt;Snallan: /* Issues of Multiple Inheritance */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''1w23:  Multiple Inheritance and Mixins&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
The purpose of this article is to give a brief introduction about multiple inheritance, consider some of its issues and discuss techniques employed by different programming languages to handle such issues. In this relation, the article also compares solutions provided by different programming languages, especially against Ruby mixins, by listing their advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
Multiple Inheritance is a technique of establishing hierarchical relationships within a set of classes, such that a single class can inherit  behaviors from multiple parent classes. Although it might seem useful to include methods from multiple classes, the major issue with multiple inheritance is the ambiguity in referencing the parent class methods,  when more than one parent classes share a method with the same name.This ambiguity is predominant in cases where the inheritance hierarchy is more than one level deep, where the class in the lowermost level inherits from two classes, that have overridden the same method from their parent in different ways.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A Real World Example==&lt;br /&gt;
A known example of multiple inheritance to represent animals using classes and objects is cited here &amp;lt;ref&amp;gt; http://stackoverflow.com/questions/9359948/multiple-inheritance-whats-a-good-example&amp;lt;/ref&amp;gt; for the sake of understanding. A Winged animal and a mammal inherit primitive attributes of an animal. While a winged animal can fly; a mammal can breathe and reproduce; they both eat, move and share features generic to animals. A bat being a winged mammal possesses features common to both winged animal and mammal. It follows that class Bat inherits from classes WingedAnimal and Mammal and in turn classes WingedAnimal and Mammal inherit from class Animal.  Let us explore the issues of multiple inheritance in reference to this context.&lt;br /&gt;
&lt;br /&gt;
[[File:Class_Diagram.png|thumb|right|x400px|alt=Class Diagram for the example.]]&lt;br /&gt;
&lt;br /&gt;
==Issues of Multiple Inheritance==&lt;br /&gt;
1. Due to inheritance, the parent class’s objects get duplicated to child class. As a result, Class Bat duplicates Animal objects through each of WingedAnimal and Mammal classes. When Bat accesses any of the Animal's attributes, the compiler would be ambiguous about determining the correct path to use since two paths are available from Animal to Bat. This problem of multiple inheritance is called diamond problem &amp;lt;ref&amp;gt;http://www.artima.com/weblogs/viewpost.jsp?thread=246488&amp;lt;/ref&amp;gt;. Compiler takes arbitrary decisions during such situations. &lt;br /&gt;
&lt;br /&gt;
2.Objects of parent classes can be separately managed by using [http://en.wikipedia.org/wiki/Virtual_method_table virtual tables]as in C++. By explicitly specifying the virtual table pointer, the Bat class can access WingedAnimal and Mammal classes unambiguously. However virtual tables occupy significant portion of memory thereby limiting their extensive usage.  &lt;br /&gt;
&lt;br /&gt;
3. The order by which a child class inherits parent classes also matters. If Bat inherits WingedAnimal prior to Mammal, then WingedAnimal precedes Mammal and common methods are accessed in this order of precedence.  Hence if the Programmer is not aware of such precedence, debugging would be difficult.&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
Programming languages provide different solutions to handle the issues of multiple inheritance. Here we consider the techniques offered by C++, Java, Ruby, Scala and Python.    &lt;br /&gt;
===Multiple Inheritance in C++===&lt;br /&gt;
C++ supports multiple inheritance with the notion of virtual classes and virtual functions. In the example, the Cricketer class defines virtual functions with Batsman and Bowler classes virtually inheriting the features of Cricketer. The Allrounder class can now unambiguously access Cricketer class using virtual table pointers of Batsman and Bowler. The example written using C++ virtual functions is shown below.&lt;br /&gt;
&lt;br /&gt;
 class Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   protected:&lt;br /&gt;
     char* country;&lt;br /&gt;
     char* player_type;&lt;br /&gt;
   public:&lt;br /&gt;
     virtual void statistics()&lt;br /&gt;
     {&lt;br /&gt;
        cout&amp;lt;&amp;lt;&amp;quot;Cricketer's statistics include number of matches, average, best performances etc.&amp;quot;&amp;lt;&amp;lt;endl; }&lt;br /&gt;
     }    &lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Batsman : virtual public Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   public:&lt;br /&gt;
     void statistics()&lt;br /&gt;
     {&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;Batsman's statistics include batting average, number of 100's &amp;amp; 50's, top score etc.&amp;quot;&amp;lt;&amp;lt;endl;&lt;br /&gt;
     }&lt;br /&gt;
     // other methods to operate on Cricketer &amp;amp; Batsman specific attributes&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Bowler : virtual public Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   public:&lt;br /&gt;
     void statistics()&lt;br /&gt;
     {&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;Bowler's statistics include number of wickets, bowling average, economy etc.&amp;quot;&amp;lt;&amp;lt;endl;&lt;br /&gt;
     }&lt;br /&gt;
     // other methods to operate on Cricketer &amp;amp; Bowler specific attributes&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Allrounder: public Batsman, public Bowler&lt;br /&gt;
 {&lt;br /&gt;
   public:&lt;br /&gt;
     void statistics()&lt;br /&gt;
     {&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;Allrounder's statistics include batting average, bowling average etc.&amp;quot;&amp;lt;&amp;lt;endl;&lt;br /&gt;
     }&lt;br /&gt;
     // other methods to operate on Cricketer, Batsman &amp;amp; Bowler specific attributes&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
   int i = 0;&lt;br /&gt;
   Cricketer *instances[3];&lt;br /&gt;
   Batsman bat;&lt;br /&gt;
   Bowler bowl;&lt;br /&gt;
   Allrounder all;&lt;br /&gt;
   instances[0] = &amp;amp;bat;&lt;br /&gt;
   instances[1] = &amp;amp;bowl;&lt;br /&gt;
   instances[2] = &amp;amp;all;&lt;br /&gt;
   while(i&amp;lt;3) &lt;br /&gt;
     instances[i++]-&amp;gt;statistics();      &lt;br /&gt;
   return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Java===&lt;br /&gt;
Java doesn’t support multiple inheritance but provides a feature called interfaces to resemble inheritance from multiple classes. Interface is an abstract class which provides a generic template by just declaring variables and methods without implementing them. Concrete classes that inherit interfaces are supposed to implement its methods. The example written using Java interfaces is shown below.&lt;br /&gt;
&lt;br /&gt;
 interface Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   void statistics();  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Batsman extends Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   void statistics();  &lt;br /&gt;
 }&lt;br /&gt;
 class ImplementBatsman implements Batsman  &lt;br /&gt;
 {&lt;br /&gt;
    public void statistics()&lt;br /&gt;
    {&lt;br /&gt;
      System.out.println(&amp;quot;Batsman's statistics include batting average, number of 100's &amp;amp; 50's etc.&amp;quot;);&lt;br /&gt;
    }   &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Bowler extends Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   void statistics();&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementBowler implements Bowler&lt;br /&gt;
 {&lt;br /&gt;
    public void statistics()&lt;br /&gt;
    {&lt;br /&gt;
      System.out.println(&amp;quot;Bowler's statistics include number of wickets, bowling average, economy etc.&amp;quot;);&lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Allrounder extends Batsman, Bowler &lt;br /&gt;
 {&lt;br /&gt;
    // other methods to operate on Cricketer, Batsman &amp;amp; Bowler specific attributes&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementAllrounder implements Allrounder&lt;br /&gt;
 {  &lt;br /&gt;
    ImplementBowler bowl = new ImplementBowler();&lt;br /&gt;
    ImplementBatsman bat = new ImplementBatsman(); &lt;br /&gt;
    public void statistics()&lt;br /&gt;
    {&lt;br /&gt;
      bowl.statistics();&lt;br /&gt;
      bat.statistics();   &lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class MultipleInheritance&lt;br /&gt;
 {&lt;br /&gt;
   public static void main(String args[])&lt;br /&gt;
   {&lt;br /&gt;
     ImplementAllrounder all = new ImplementAllrounder();&lt;br /&gt;
     all.statistics();       &lt;br /&gt;
   } &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Ruby===&lt;br /&gt;
Ruby doesn't support multiple inheritance but provides a similar feature using modules and mixins.  &lt;br /&gt;
====Modules====&lt;br /&gt;
A Module is an encapsulation of methods and variables, although similar in function to a Class, differs from a class in the sense that it cannot be instantiated. Modules are  more dynamic and allow us to organize the code in a more granular way.Each module can add something to the existing class. A module can be thought of an encapsulation of a  subset of the full capabilities of an object.&lt;br /&gt;
An example found in the book ,The Well-Grounded Rubyist &amp;lt;ref&amp;gt;http://www.manning.com/black2/SampleChapter4.pdf [Pg.92] &amp;lt;/ref&amp;gt;, is the stack-like module. The module encapsulates the characteristics of being like a stack,implements the operations that can be performed by a stack but is not the actual Stack class. This allows us to use this module to mix-in  stack-like behaviour into other classes that require them.&lt;br /&gt;
&lt;br /&gt;
====Mixins====&lt;br /&gt;
A Mixin is a concept that allows inclusion of special characteristics into existing classes, so that common features that need to be reused in several classes can be selectively included, thus avoiding implementation of unnecessary functions like in the case of interfaces.&lt;br /&gt;
Mixins allow us to dynamically bind special features defined in modules, into core classes without really making any changes to the objects. This improves flexibility when we want to add plugins and extensions to the existing framework.&amp;lt;ref&amp;gt;http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-frameworks/&amp;lt;/ref&amp;gt;.The source code for the same example implemented using ruby mixins is shown below.&lt;br /&gt;
&lt;br /&gt;
 module Cricketer&lt;br /&gt;
   def statistics&lt;br /&gt;
     puts &amp;quot;Cricketer's statistics include number of matches, average etc.&amp;quot;&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Batsman&lt;br /&gt;
   include Cricketer&lt;br /&gt;
   def statistics&lt;br /&gt;
     puts &amp;quot;Batsman's statistics include batting average, number of 100's &amp;amp; 50's etc.&amp;quot;&lt;br /&gt;
   end    &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Bowler&lt;br /&gt;
   include Cricketer&lt;br /&gt;
   def statistics&lt;br /&gt;
      puts &amp;quot;Bowler's statistics include number of wickets, bowling average, economy etc.&amp;quot;&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class AllRounder&lt;br /&gt;
   include Batsman&lt;br /&gt;
   include Bowler&lt;br /&gt;
   include Cricketer&lt;br /&gt;
   # Other implementation&lt;br /&gt;
 end&lt;br /&gt;
 AllRounder.new().statistics()&lt;br /&gt;
&lt;br /&gt;
===Multiple inheritance in other languages===&lt;br /&gt;
Scala offers traits that are similar to Java interfaces to support multiple inheritance. Traits define a template with methods and variables which any class can make use of. The difference between interfaces and traits is that traits can implement methods. A Scala class can inherit from any number of traits together with a class in way to support multiple inheritance. Traits can be mixed in to a class even without inheriting it but just by including it at the time of creating instances. &amp;lt;ref&amp;gt;http://joelabrahamsson.com/entry/learning-scala-traits&amp;lt;/ref&amp;gt;. Trait can also extend a class and methods to be overridden are usually prefixed by the keyword override. The language enforces method names to be different across inherited  traits to avoid the issues of multiple inheritance. Python supports a limited version of multiple inheritance by extending multiple classes in depth-first left-right order &amp;lt;ref&amp;gt;http://docs.python.org/tutorial/classes.html&amp;lt;/ref&amp;gt;. If a python class D inherits classes A, B, C and if an instance variable or method is not available in the current class, it checks in A and its base classes, then moves to B and its base classes and eventually to C thus moving from left to right. In this way, python avoids the diamond problem by making the search order linear by which the derived class accesses attributes of its parent classes. In addition python also defines mixins to access reusable features within a class.&lt;br /&gt;
&lt;br /&gt;
==Advantages of mixins==&lt;br /&gt;
1. Firstly, when a module gets mixed into different classes, each class acquires the same  reference  to the module and hence even dynamic changes to the module gets reflected in the behavior of the classes &amp;lt;ref&amp;gt;http://www.linuxtopia.org/online_books/programming_books/ruby_tutorial/Ruby_Modules_Mixins.html&amp;lt;/ref&amp;gt;. In this way, the module’s code is not just copied to individual classes as in static languages instead its reference is provided. &lt;br /&gt;
&lt;br /&gt;
2. Secondly, the mixed in state and behavior can be accessed by the instance of target class just like accessing its own features. &lt;br /&gt;
&lt;br /&gt;
3. Thirdly, a class can include any number of modules in a way it can simulate multiple inheritance without having the related issues.&lt;br /&gt;
&lt;br /&gt;
==Questions to Ponder==&lt;br /&gt;
===Do mixins solve all of the problems of multiple inheritance?===&lt;br /&gt;
Undoubtedly mixins solve some of the problems of multiple inheritance. There is no ambiguity in resolving the request to a common method implemented by multiple modules. In that case, the lastly included module is responsible for handling accesses to common methods. Though the order of precedence of included modules matters, it looks clearer than C++.  However languages like Scala and Eiffel implement multiple inheritance far more safely  by explicitly forcing the user to have different method names across inherited classes.&lt;br /&gt;
&lt;br /&gt;
===Are mixins a clear advantage over interfaces?===&lt;br /&gt;
Mixins are more advantageous than interfaces because of their features. Interfaces have some limitations. As seen from programs, the derived class Allrounder uses the instances of Batsman and Bowler classes (has-a relationship) to access respective attributes. Interface is nice in the sense it provides a generic template that could be implemented differently by different classes.  However the target class has to necessarily implement every method of the interface. This would pose problems to a Class which is interested in implementing specific features of the interface. On the contrary, Mixins eliminate this inconvenience by mixing in the state and behavior of the modules in the target class and allows a it to use them as it needs. Hence the target class can just use module’s features selectively or completely instead of implementing by itself. The size of multiple inheritance code using mixins is smaller than interfaces and C++ variants.&lt;br /&gt;
&lt;br /&gt;
===Do mixins have any disadvantages not shared by multiple inheritance or interfaces?===&lt;br /&gt;
Inspite of making the lives of programmers better with effective code reuse and avoiding unnecessary implementation of methods that cause clutter in the code, the concept of Mixins can easily be abused resulting in namespace pollution. This is caused when a class includes multiple modules each of which contains several hundred method implementations. Having hundreds of methods mixed into a single class, makes it difficult to identify the source modules of the methods, thereby causing confusion while implementing these methods, whereas in the case of multiple inheritance we can easily identify the class to which the method belongs.&lt;br /&gt;
Another disadvantage that follows from the namespace collision is the case where we have multiple methods with the same name in different modules.In such a case whenever a call to that method is made, the method which belongs to the module that was included last will be called. The problem with this is that, this takes place silently without any warning. This may result in accidental overriding of wrong methods.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
In this article, we have discussed about multiple inheritance and means by which it is implemented in different languages. Though multiple inheritance is a useful feature,&lt;br /&gt;
many languages do not support them due to its associated issues. Given its advantages, mixin in ruby seems to be a viable option to achieve multiple inheritance. However mixins too have limitations which should be taken care of during implementation.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
==See Also==&lt;br /&gt;
1. Lucas Carlson and Leonard Richardson. 2006. [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=1212915&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 Ruby Cookbook] (Cookbooks (O'Reilly)). O'Reilly Media, Inc..&lt;br /&gt;
&lt;br /&gt;
2. Herbert Schildt. 2000. Java 2: [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=557816&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 The Complete Reference] (4th ed.). McGraw-Hill Professional.&lt;/div&gt;</summary>
		<author><name>Snallan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65425</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65425"/>
		<updated>2012-09-21T12:35:28Z</updated>

		<summary type="html">&lt;p&gt;Snallan: /* A Real World Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''1w23:  Multiple Inheritance and Mixins&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
The purpose of this article is to give a brief introduction about multiple inheritance, consider some of its issues and discuss techniques employed by different programming languages to handle such issues. In this relation, the article also compares solutions provided by different programming languages, especially against Ruby mixins, by listing their advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
Multiple Inheritance is a technique of establishing hierarchical relationships within a set of classes, such that a single class can inherit  behaviors from multiple parent classes. Although it might seem useful to include methods from multiple classes, the major issue with multiple inheritance is the ambiguity in referencing the parent class methods,  when more than one parent classes share a method with the same name.This ambiguity is predominant in cases where the inheritance hierarchy is more than one level deep, where the class in the lowermost level inherits from two classes, that have overridden the same method from their parent in different ways.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A Real World Example==&lt;br /&gt;
A known example of multiple inheritance to represent animals using classes and objects is cited here &amp;lt;ref&amp;gt; http://stackoverflow.com/questions/9359948/multiple-inheritance-whats-a-good-example&amp;lt;/ref&amp;gt; for the sake of understanding. A Winged animal and a mammal inherit primitive attributes of an animal. While a winged animal can fly; a mammal can breathe and reproduce; they both eat, move and share features generic to animals. A bat being a winged mammal possesses features common to both winged animal and mammal. It follows that class Bat inherits from classes WingedAnimal and Mammal and in turn classes WingedAnimal and Mammal inherit from class Animal.  Let us explore the issues of multiple inheritance in reference to this context.&lt;br /&gt;
&lt;br /&gt;
[[File:Class_Diagram.png|thumb|right|x400px|alt=Class Diagram for the example.]]&lt;br /&gt;
&lt;br /&gt;
==Issues of Multiple Inheritance==&lt;br /&gt;
1. Due to inheritance, the parent class’s objects get duplicated to child class. As a result, Allrounder Class duplicates Cricketer objects through each of Batsman and Bowler classes. When Allrounder accesses any of the Cricketer’s attributes, the compiler would be ambiguous about determining the correct path to use since two paths are available from Allrounder to Cricketer. This problem of multiple inheritance is called diamond problem. Compiler takes arbitrary decisions during such situations. &amp;lt;ref&amp;gt;http://www.artima.com/weblogs/viewpost.jsp?thread=246488&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
2.Objects of parent classes can be separately managed by using virtual tables as in C++. By explicitly specifying the virtual table pointer, the Allrounder class can access Batsman and Bowler classes unambiguously. However virtual tables occupy significant portion of memory thereby limiting their extensive usage.  &lt;br /&gt;
&lt;br /&gt;
3. The order by which a child class inherits parent classes also matters. If Allrounder inherits Batsman prior to Bowler, then Batsman precedes Bowler and common methods are accessed in this order of precedence.  Hence if the Programmer is not aware of such precedences, debugging would be difficult.&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
Programming languages provide different solutions to handle the issues of multiple inheritance. Here we consider the techniques offered by C++, Java, Ruby, Scala and Python.    &lt;br /&gt;
===Multiple Inheritance in C++===&lt;br /&gt;
C++ supports multiple inheritance with the notion of virtual classes and virtual functions. In the example, the Cricketer class defines virtual functions with Batsman and Bowler classes virtually inheriting the features of Cricketer. The Allrounder class can now unambiguously access Cricketer class using virtual table pointers of Batsman and Bowler. The example written using C++ virtual functions is shown below.&lt;br /&gt;
&lt;br /&gt;
 class Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   protected:&lt;br /&gt;
     char* country;&lt;br /&gt;
     char* player_type;&lt;br /&gt;
   public:&lt;br /&gt;
     virtual void statistics()&lt;br /&gt;
     {&lt;br /&gt;
        cout&amp;lt;&amp;lt;&amp;quot;Cricketer's statistics include number of matches, average, best performances etc.&amp;quot;&amp;lt;&amp;lt;endl; }&lt;br /&gt;
     }    &lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Batsman : virtual public Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   public:&lt;br /&gt;
     void statistics()&lt;br /&gt;
     {&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;Batsman's statistics include batting average, number of 100's &amp;amp; 50's, top score etc.&amp;quot;&amp;lt;&amp;lt;endl;&lt;br /&gt;
     }&lt;br /&gt;
     // other methods to operate on Cricketer &amp;amp; Batsman specific attributes&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Bowler : virtual public Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   public:&lt;br /&gt;
     void statistics()&lt;br /&gt;
     {&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;Bowler's statistics include number of wickets, bowling average, economy etc.&amp;quot;&amp;lt;&amp;lt;endl;&lt;br /&gt;
     }&lt;br /&gt;
     // other methods to operate on Cricketer &amp;amp; Bowler specific attributes&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Allrounder: public Batsman, public Bowler&lt;br /&gt;
 {&lt;br /&gt;
   public:&lt;br /&gt;
     void statistics()&lt;br /&gt;
     {&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;Allrounder's statistics include batting average, bowling average etc.&amp;quot;&amp;lt;&amp;lt;endl;&lt;br /&gt;
     }&lt;br /&gt;
     // other methods to operate on Cricketer, Batsman &amp;amp; Bowler specific attributes&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
   int i = 0;&lt;br /&gt;
   Cricketer *instances[3];&lt;br /&gt;
   Batsman bat;&lt;br /&gt;
   Bowler bowl;&lt;br /&gt;
   Allrounder all;&lt;br /&gt;
   instances[0] = &amp;amp;bat;&lt;br /&gt;
   instances[1] = &amp;amp;bowl;&lt;br /&gt;
   instances[2] = &amp;amp;all;&lt;br /&gt;
   while(i&amp;lt;3) &lt;br /&gt;
     instances[i++]-&amp;gt;statistics();      &lt;br /&gt;
   return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Java===&lt;br /&gt;
Java doesn’t support multiple inheritance but provides a feature called interfaces to resemble inheritance from multiple classes. Interface is an abstract class which provides a generic template by just declaring variables and methods without implementing them. Concrete classes that inherit interfaces are supposed to implement its methods. The example written using Java interfaces is shown below.&lt;br /&gt;
&lt;br /&gt;
 interface Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   void statistics();  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Batsman extends Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   void statistics();  &lt;br /&gt;
 }&lt;br /&gt;
 class ImplementBatsman implements Batsman  &lt;br /&gt;
 {&lt;br /&gt;
    public void statistics()&lt;br /&gt;
    {&lt;br /&gt;
      System.out.println(&amp;quot;Batsman's statistics include batting average, number of 100's &amp;amp; 50's etc.&amp;quot;);&lt;br /&gt;
    }   &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Bowler extends Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   void statistics();&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementBowler implements Bowler&lt;br /&gt;
 {&lt;br /&gt;
    public void statistics()&lt;br /&gt;
    {&lt;br /&gt;
      System.out.println(&amp;quot;Bowler's statistics include number of wickets, bowling average, economy etc.&amp;quot;);&lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Allrounder extends Batsman, Bowler &lt;br /&gt;
 {&lt;br /&gt;
    // other methods to operate on Cricketer, Batsman &amp;amp; Bowler specific attributes&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementAllrounder implements Allrounder&lt;br /&gt;
 {  &lt;br /&gt;
    ImplementBowler bowl = new ImplementBowler();&lt;br /&gt;
    ImplementBatsman bat = new ImplementBatsman(); &lt;br /&gt;
    public void statistics()&lt;br /&gt;
    {&lt;br /&gt;
      bowl.statistics();&lt;br /&gt;
      bat.statistics();   &lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class MultipleInheritance&lt;br /&gt;
 {&lt;br /&gt;
   public static void main(String args[])&lt;br /&gt;
   {&lt;br /&gt;
     ImplementAllrounder all = new ImplementAllrounder();&lt;br /&gt;
     all.statistics();       &lt;br /&gt;
   } &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Ruby===&lt;br /&gt;
Ruby doesn't support multiple inheritance but provides a similar feature using modules and mixins.  &lt;br /&gt;
====Modules====&lt;br /&gt;
A Module is an encapsulation of methods and variables, although similar in function to a Class, differs from a class in the sense that it cannot be instantiated. Modules are  more dynamic and allow us to organize the code in a more granular way.Each module can add something to the existing class. A module can be thought of an encapsulation of a  subset of the full capabilities of an object.&lt;br /&gt;
An example found in the book ,The Well-Grounded Rubyist &amp;lt;ref&amp;gt;http://www.manning.com/black2/SampleChapter4.pdf [Pg.92] &amp;lt;/ref&amp;gt;, is the stack-like module. The module encapsulates the characteristics of being like a stack,implements the operations that can be performed by a stack but is not the actual Stack class. This allows us to use this module to mix-in  stack-like behaviour into other classes that require them.&lt;br /&gt;
&lt;br /&gt;
====Mixins====&lt;br /&gt;
A Mixin is a concept that allows inclusion of special characteristics into existing classes, so that common features that need to be reused in several classes can be selectively included, thus avoiding implementation of unnecessary functions like in the case of interfaces.&lt;br /&gt;
Mixins allow us to dynamically bind special features defined in modules, into core classes without really making any changes to the objects. This improves flexibility when we want to add plugins and extensions to the existing framework.&amp;lt;ref&amp;gt;http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-frameworks/&amp;lt;/ref&amp;gt;.The source code for the same example implemented using ruby mixins is shown below.&lt;br /&gt;
&lt;br /&gt;
 module Cricketer&lt;br /&gt;
   def statistics&lt;br /&gt;
     puts &amp;quot;Cricketer's statistics include number of matches, average etc.&amp;quot;&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Batsman&lt;br /&gt;
   include Cricketer&lt;br /&gt;
   def statistics&lt;br /&gt;
     puts &amp;quot;Batsman's statistics include batting average, number of 100's &amp;amp; 50's etc.&amp;quot;&lt;br /&gt;
   end    &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Bowler&lt;br /&gt;
   include Cricketer&lt;br /&gt;
   def statistics&lt;br /&gt;
      puts &amp;quot;Bowler's statistics include number of wickets, bowling average, economy etc.&amp;quot;&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class AllRounder&lt;br /&gt;
   include Batsman&lt;br /&gt;
   include Bowler&lt;br /&gt;
   include Cricketer&lt;br /&gt;
   # Other implementation&lt;br /&gt;
 end&lt;br /&gt;
 AllRounder.new().statistics()&lt;br /&gt;
&lt;br /&gt;
===Multiple inheritance in other languages===&lt;br /&gt;
Scala offers traits that are similar to Java interfaces to support multiple inheritance. Traits define a template with methods and variables which any class can make use of. The difference between interfaces and traits is that traits can implement methods. A Scala class can inherit from any number of traits together with a class in way to support multiple inheritance. Traits can be mixed in to a class even without inheriting it but just by including it at the time of creating instances. &amp;lt;ref&amp;gt;http://joelabrahamsson.com/entry/learning-scala-traits&amp;lt;/ref&amp;gt;. Trait can also extend a class and methods to be overridden are usually prefixed by the keyword override. The language enforces method names to be different across inherited  traits to avoid the issues of multiple inheritance. Python supports a limited version of multiple inheritance by extending multiple classes in depth-first left-right order &amp;lt;ref&amp;gt;http://docs.python.org/tutorial/classes.html&amp;lt;/ref&amp;gt;. If a python class D inherits classes A, B, C and if an instance variable or method is not available in the current class, it checks in A and its base classes, then moves to B and its base classes and eventually to C thus moving from left to right. In this way, python avoids the diamond problem by making the search order linear by which the derived class accesses attributes of its parent classes. In addition python also defines mixins to access reusable features within a class.&lt;br /&gt;
&lt;br /&gt;
==Advantages of mixins==&lt;br /&gt;
1. Firstly, when a module gets mixed into different classes, each class acquires the same  reference  to the module and hence even dynamic changes to the module gets reflected in the behavior of the classes &amp;lt;ref&amp;gt;http://www.linuxtopia.org/online_books/programming_books/ruby_tutorial/Ruby_Modules_Mixins.html&amp;lt;/ref&amp;gt;. In this way, the module’s code is not just copied to individual classes as in static languages instead its reference is provided. &lt;br /&gt;
&lt;br /&gt;
2. Secondly, the mixed in state and behavior can be accessed by the instance of target class just like accessing its own features. &lt;br /&gt;
&lt;br /&gt;
3. Thirdly, a class can include any number of modules in a way it can simulate multiple inheritance without having the related issues.&lt;br /&gt;
&lt;br /&gt;
==Questions to Ponder==&lt;br /&gt;
===Do mixins solve all of the problems of multiple inheritance?===&lt;br /&gt;
Undoubtedly mixins solve some of the problems of multiple inheritance. There is no ambiguity in resolving the request to a common method implemented by multiple modules. In that case, the lastly included module is responsible for handling accesses to common methods. Though the order of precedence of included modules matters, it looks clearer than C++.  However languages like Scala and Eiffel implement multiple inheritance far more safely  by explicitly forcing the user to have different method names across inherited classes.&lt;br /&gt;
&lt;br /&gt;
===Are mixins a clear advantage over interfaces?===&lt;br /&gt;
Mixins are more advantageous than interfaces because of their features. Interfaces have some limitations. As seen from programs, the derived class Allrounder uses the instances of Batsman and Bowler classes (has-a relationship) to access respective attributes. Interface is nice in the sense it provides a generic template that could be implemented differently by different classes.  However the target class has to necessarily implement every method of the interface. This would pose problems to a Class which is interested in implementing specific features of the interface. On the contrary, Mixins eliminate this inconvenience by mixing in the state and behavior of the modules in the target class and allows a it to use them as it needs. Hence the target class can just use module’s features selectively or completely instead of implementing by itself. The size of multiple inheritance code using mixins is smaller than interfaces and C++ variants.&lt;br /&gt;
&lt;br /&gt;
===Do mixins have any disadvantages not shared by multiple inheritance or interfaces?===&lt;br /&gt;
Inspite of making the lives of programmers better with effective code reuse and avoiding unnecessary implementation of methods that cause clutter in the code, the concept of Mixins can easily be abused resulting in namespace pollution. This is caused when a class includes multiple modules each of which contains several hundred method implementations. Having hundreds of methods mixed into a single class, makes it difficult to identify the source modules of the methods, thereby causing confusion while implementing these methods, whereas in the case of multiple inheritance we can easily identify the class to which the method belongs.&lt;br /&gt;
Another disadvantage that follows from the namespace collision is the case where we have multiple methods with the same name in different modules.In such a case whenever a call to that method is made, the method which belongs to the module that was included last will be called. The problem with this is that, this takes place silently without any warning. This may result in accidental overriding of wrong methods.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
In this article, we have discussed about multiple inheritance and means by which it is implemented in different languages. Though multiple inheritance is a useful feature,&lt;br /&gt;
many languages do not support them due to its associated issues. Given its advantages, mixin in ruby seems to be a viable option to achieve multiple inheritance. However mixins too have limitations which should be taken care of during implementation.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
==See Also==&lt;br /&gt;
1. Lucas Carlson and Leonard Richardson. 2006. [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=1212915&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 Ruby Cookbook] (Cookbooks (O'Reilly)). O'Reilly Media, Inc..&lt;br /&gt;
&lt;br /&gt;
2. Herbert Schildt. 2000. Java 2: [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=557816&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 The Complete Reference] (4th ed.). McGraw-Hill Professional.&lt;/div&gt;</summary>
		<author><name>Snallan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65424</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65424"/>
		<updated>2012-09-21T12:34:14Z</updated>

		<summary type="html">&lt;p&gt;Snallan: /* A Real World Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''1w23:  Multiple Inheritance and Mixins&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
The purpose of this article is to give a brief introduction about multiple inheritance, consider some of its issues and discuss techniques employed by different programming languages to handle such issues. In this relation, the article also compares solutions provided by different programming languages, especially against Ruby mixins, by listing their advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
Multiple Inheritance is a technique of establishing hierarchical relationships within a set of classes, such that a single class can inherit  behaviors from multiple parent classes. Although it might seem useful to include methods from multiple classes, the major issue with multiple inheritance is the ambiguity in referencing the parent class methods,  when more than one parent classes share a method with the same name.This ambiguity is predominant in cases where the inheritance hierarchy is more than one level deep, where the class in the lowermost level inherits from two classes, that have overridden the same method from their parent in different ways.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A Real World Example==&lt;br /&gt;
A known example of multiple inheritance to represent animals using classes and objects is cited here &amp;lt;ref&amp;gt; http://stackoverflow.com/questions/9359948/multiple-inheritance-whats-a-good-example&amp;lt;/ref&amp;gt; for the sake of understanding. A Winged animal and a mammal inherit primitive attributes of an animal. While a winged animal can fly, a mammal can breathe and reproduce, they both eat, move and share features generic to animals. A bat being a winged mammal possesses features common to both winged animal and mammal. It follows that class Bat inherits from classes WingedAnimal and Mammal and in turn classes WingedAnimal and Mammal inherit from class Animal.  Let us explore the issues of multiple inheritance in reference to this context.&lt;br /&gt;
&lt;br /&gt;
[[File:Class_Diagram.png|thumb|right|x400px|alt=Class Diagram for the example.]]&lt;br /&gt;
&lt;br /&gt;
==Issues of Multiple Inheritance==&lt;br /&gt;
1. Due to inheritance, the parent class’s objects get duplicated to child class. As a result, Allrounder Class duplicates Cricketer objects through each of Batsman and Bowler classes. When Allrounder accesses any of the Cricketer’s attributes, the compiler would be ambiguous about determining the correct path to use since two paths are available from Allrounder to Cricketer. This problem of multiple inheritance is called diamond problem. Compiler takes arbitrary decisions during such situations. &amp;lt;ref&amp;gt;http://www.artima.com/weblogs/viewpost.jsp?thread=246488&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
2.Objects of parent classes can be separately managed by using virtual tables as in C++. By explicitly specifying the virtual table pointer, the Allrounder class can access Batsman and Bowler classes unambiguously. However virtual tables occupy significant portion of memory thereby limiting their extensive usage.  &lt;br /&gt;
&lt;br /&gt;
3. The order by which a child class inherits parent classes also matters. If Allrounder inherits Batsman prior to Bowler, then Batsman precedes Bowler and common methods are accessed in this order of precedence.  Hence if the Programmer is not aware of such precedences, debugging would be difficult.&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
Programming languages provide different solutions to handle the issues of multiple inheritance. Here we consider the techniques offered by C++, Java, Ruby, Scala and Python.    &lt;br /&gt;
===Multiple Inheritance in C++===&lt;br /&gt;
C++ supports multiple inheritance with the notion of virtual classes and virtual functions. In the example, the Cricketer class defines virtual functions with Batsman and Bowler classes virtually inheriting the features of Cricketer. The Allrounder class can now unambiguously access Cricketer class using virtual table pointers of Batsman and Bowler. The example written using C++ virtual functions is shown below.&lt;br /&gt;
&lt;br /&gt;
 class Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   protected:&lt;br /&gt;
     char* country;&lt;br /&gt;
     char* player_type;&lt;br /&gt;
   public:&lt;br /&gt;
     virtual void statistics()&lt;br /&gt;
     {&lt;br /&gt;
        cout&amp;lt;&amp;lt;&amp;quot;Cricketer's statistics include number of matches, average, best performances etc.&amp;quot;&amp;lt;&amp;lt;endl; }&lt;br /&gt;
     }    &lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Batsman : virtual public Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   public:&lt;br /&gt;
     void statistics()&lt;br /&gt;
     {&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;Batsman's statistics include batting average, number of 100's &amp;amp; 50's, top score etc.&amp;quot;&amp;lt;&amp;lt;endl;&lt;br /&gt;
     }&lt;br /&gt;
     // other methods to operate on Cricketer &amp;amp; Batsman specific attributes&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Bowler : virtual public Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   public:&lt;br /&gt;
     void statistics()&lt;br /&gt;
     {&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;Bowler's statistics include number of wickets, bowling average, economy etc.&amp;quot;&amp;lt;&amp;lt;endl;&lt;br /&gt;
     }&lt;br /&gt;
     // other methods to operate on Cricketer &amp;amp; Bowler specific attributes&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Allrounder: public Batsman, public Bowler&lt;br /&gt;
 {&lt;br /&gt;
   public:&lt;br /&gt;
     void statistics()&lt;br /&gt;
     {&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;Allrounder's statistics include batting average, bowling average etc.&amp;quot;&amp;lt;&amp;lt;endl;&lt;br /&gt;
     }&lt;br /&gt;
     // other methods to operate on Cricketer, Batsman &amp;amp; Bowler specific attributes&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
   int i = 0;&lt;br /&gt;
   Cricketer *instances[3];&lt;br /&gt;
   Batsman bat;&lt;br /&gt;
   Bowler bowl;&lt;br /&gt;
   Allrounder all;&lt;br /&gt;
   instances[0] = &amp;amp;bat;&lt;br /&gt;
   instances[1] = &amp;amp;bowl;&lt;br /&gt;
   instances[2] = &amp;amp;all;&lt;br /&gt;
   while(i&amp;lt;3) &lt;br /&gt;
     instances[i++]-&amp;gt;statistics();      &lt;br /&gt;
   return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Java===&lt;br /&gt;
Java doesn’t support multiple inheritance but provides a feature called interfaces to resemble inheritance from multiple classes. Interface is an abstract class which provides a generic template by just declaring variables and methods without implementing them. Concrete classes that inherit interfaces are supposed to implement its methods. The example written using Java interfaces is shown below.&lt;br /&gt;
&lt;br /&gt;
 interface Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   void statistics();  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Batsman extends Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   void statistics();  &lt;br /&gt;
 }&lt;br /&gt;
 class ImplementBatsman implements Batsman  &lt;br /&gt;
 {&lt;br /&gt;
    public void statistics()&lt;br /&gt;
    {&lt;br /&gt;
      System.out.println(&amp;quot;Batsman's statistics include batting average, number of 100's &amp;amp; 50's etc.&amp;quot;);&lt;br /&gt;
    }   &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Bowler extends Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   void statistics();&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementBowler implements Bowler&lt;br /&gt;
 {&lt;br /&gt;
    public void statistics()&lt;br /&gt;
    {&lt;br /&gt;
      System.out.println(&amp;quot;Bowler's statistics include number of wickets, bowling average, economy etc.&amp;quot;);&lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Allrounder extends Batsman, Bowler &lt;br /&gt;
 {&lt;br /&gt;
    // other methods to operate on Cricketer, Batsman &amp;amp; Bowler specific attributes&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementAllrounder implements Allrounder&lt;br /&gt;
 {  &lt;br /&gt;
    ImplementBowler bowl = new ImplementBowler();&lt;br /&gt;
    ImplementBatsman bat = new ImplementBatsman(); &lt;br /&gt;
    public void statistics()&lt;br /&gt;
    {&lt;br /&gt;
      bowl.statistics();&lt;br /&gt;
      bat.statistics();   &lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class MultipleInheritance&lt;br /&gt;
 {&lt;br /&gt;
   public static void main(String args[])&lt;br /&gt;
   {&lt;br /&gt;
     ImplementAllrounder all = new ImplementAllrounder();&lt;br /&gt;
     all.statistics();       &lt;br /&gt;
   } &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Ruby===&lt;br /&gt;
Ruby doesn't support multiple inheritance but provides a similar feature using modules and mixins.  &lt;br /&gt;
====Modules====&lt;br /&gt;
A Module is an encapsulation of methods and variables, although similar in function to a Class, differs from a class in the sense that it cannot be instantiated. Modules are  more dynamic and allow us to organize the code in a more granular way.Each module can add something to the existing class. A module can be thought of an encapsulation of a  subset of the full capabilities of an object.&lt;br /&gt;
An example found in the book ,The Well-Grounded Rubyist &amp;lt;ref&amp;gt;http://www.manning.com/black2/SampleChapter4.pdf [Pg.92] &amp;lt;/ref&amp;gt;, is the stack-like module. The module encapsulates the characteristics of being like a stack,implements the operations that can be performed by a stack but is not the actual Stack class. This allows us to use this module to mix-in  stack-like behaviour into other classes that require them.&lt;br /&gt;
&lt;br /&gt;
====Mixins====&lt;br /&gt;
A Mixin is a concept that allows inclusion of special characteristics into existing classes, so that common features that need to be reused in several classes can be selectively included, thus avoiding implementation of unnecessary functions like in the case of interfaces.&lt;br /&gt;
Mixins allow us to dynamically bind special features defined in modules, into core classes without really making any changes to the objects. This improves flexibility when we want to add plugins and extensions to the existing framework.&amp;lt;ref&amp;gt;http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-frameworks/&amp;lt;/ref&amp;gt;.The source code for the same example implemented using ruby mixins is shown below.&lt;br /&gt;
&lt;br /&gt;
 module Cricketer&lt;br /&gt;
   def statistics&lt;br /&gt;
     puts &amp;quot;Cricketer's statistics include number of matches, average etc.&amp;quot;&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Batsman&lt;br /&gt;
   include Cricketer&lt;br /&gt;
   def statistics&lt;br /&gt;
     puts &amp;quot;Batsman's statistics include batting average, number of 100's &amp;amp; 50's etc.&amp;quot;&lt;br /&gt;
   end    &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Bowler&lt;br /&gt;
   include Cricketer&lt;br /&gt;
   def statistics&lt;br /&gt;
      puts &amp;quot;Bowler's statistics include number of wickets, bowling average, economy etc.&amp;quot;&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class AllRounder&lt;br /&gt;
   include Batsman&lt;br /&gt;
   include Bowler&lt;br /&gt;
   include Cricketer&lt;br /&gt;
   # Other implementation&lt;br /&gt;
 end&lt;br /&gt;
 AllRounder.new().statistics()&lt;br /&gt;
&lt;br /&gt;
===Multiple inheritance in other languages===&lt;br /&gt;
Scala offers traits that are similar to Java interfaces to support multiple inheritance. Traits define a template with methods and variables which any class can make use of. The difference between interfaces and traits is that traits can implement methods. A Scala class can inherit from any number of traits together with a class in way to support multiple inheritance. Traits can be mixed in to a class even without inheriting it but just by including it at the time of creating instances. &amp;lt;ref&amp;gt;http://joelabrahamsson.com/entry/learning-scala-traits&amp;lt;/ref&amp;gt;. Trait can also extend a class and methods to be overridden are usually prefixed by the keyword override. The language enforces method names to be different across inherited  traits to avoid the issues of multiple inheritance. Python supports a limited version of multiple inheritance by extending multiple classes in depth-first left-right order &amp;lt;ref&amp;gt;http://docs.python.org/tutorial/classes.html&amp;lt;/ref&amp;gt;. If a python class D inherits classes A, B, C and if an instance variable or method is not available in the current class, it checks in A and its base classes, then moves to B and its base classes and eventually to C thus moving from left to right. In this way, python avoids the diamond problem by making the search order linear by which the derived class accesses attributes of its parent classes. In addition python also defines mixins to access reusable features within a class.&lt;br /&gt;
&lt;br /&gt;
==Advantages of mixins==&lt;br /&gt;
1. Firstly, when a module gets mixed into different classes, each class acquires the same  reference  to the module and hence even dynamic changes to the module gets reflected in the behavior of the classes &amp;lt;ref&amp;gt;http://www.linuxtopia.org/online_books/programming_books/ruby_tutorial/Ruby_Modules_Mixins.html&amp;lt;/ref&amp;gt;. In this way, the module’s code is not just copied to individual classes as in static languages instead its reference is provided. &lt;br /&gt;
&lt;br /&gt;
2. Secondly, the mixed in state and behavior can be accessed by the instance of target class just like accessing its own features. &lt;br /&gt;
&lt;br /&gt;
3. Thirdly, a class can include any number of modules in a way it can simulate multiple inheritance without having the related issues.&lt;br /&gt;
&lt;br /&gt;
==Questions to Ponder==&lt;br /&gt;
===Do mixins solve all of the problems of multiple inheritance?===&lt;br /&gt;
Undoubtedly mixins solve some of the problems of multiple inheritance. There is no ambiguity in resolving the request to a common method implemented by multiple modules. In that case, the lastly included module is responsible for handling accesses to common methods. Though the order of precedence of included modules matters, it looks clearer than C++.  However languages like Scala and Eiffel implement multiple inheritance far more safely  by explicitly forcing the user to have different method names across inherited classes.&lt;br /&gt;
&lt;br /&gt;
===Are mixins a clear advantage over interfaces?===&lt;br /&gt;
Mixins are more advantageous than interfaces because of their features. Interfaces have some limitations. As seen from programs, the derived class Allrounder uses the instances of Batsman and Bowler classes (has-a relationship) to access respective attributes. Interface is nice in the sense it provides a generic template that could be implemented differently by different classes.  However the target class has to necessarily implement every method of the interface. This would pose problems to a Class which is interested in implementing specific features of the interface. On the contrary, Mixins eliminate this inconvenience by mixing in the state and behavior of the modules in the target class and allows a it to use them as it needs. Hence the target class can just use module’s features selectively or completely instead of implementing by itself. The size of multiple inheritance code using mixins is smaller than interfaces and C++ variants.&lt;br /&gt;
&lt;br /&gt;
===Do mixins have any disadvantages not shared by multiple inheritance or interfaces?===&lt;br /&gt;
Inspite of making the lives of programmers better with effective code reuse and avoiding unnecessary implementation of methods that cause clutter in the code, the concept of Mixins can easily be abused resulting in namespace pollution. This is caused when a class includes multiple modules each of which contains several hundred method implementations. Having hundreds of methods mixed into a single class, makes it difficult to identify the source modules of the methods, thereby causing confusion while implementing these methods, whereas in the case of multiple inheritance we can easily identify the class to which the method belongs.&lt;br /&gt;
Another disadvantage that follows from the namespace collision is the case where we have multiple methods with the same name in different modules.In such a case whenever a call to that method is made, the method which belongs to the module that was included last will be called. The problem with this is that, this takes place silently without any warning. This may result in accidental overriding of wrong methods.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
In this article, we have discussed about multiple inheritance and means by which it is implemented in different languages. Though multiple inheritance is a useful feature,&lt;br /&gt;
many languages do not support them due to its associated issues. Given its advantages, mixin in ruby seems to be a viable option to achieve multiple inheritance. However mixins too have limitations which should be taken care of during implementation.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
==See Also==&lt;br /&gt;
1. Lucas Carlson and Leonard Richardson. 2006. [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=1212915&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 Ruby Cookbook] (Cookbooks (O'Reilly)). O'Reilly Media, Inc..&lt;br /&gt;
&lt;br /&gt;
2. Herbert Schildt. 2000. Java 2: [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=557816&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 The Complete Reference] (4th ed.). McGraw-Hill Professional.&lt;/div&gt;</summary>
		<author><name>Snallan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65423</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65423"/>
		<updated>2012-09-21T12:33:36Z</updated>

		<summary type="html">&lt;p&gt;Snallan: /* A Real World Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''1w23:  Multiple Inheritance and Mixins&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
The purpose of this article is to give a brief introduction about multiple inheritance, consider some of its issues and discuss techniques employed by different programming languages to handle such issues. In this relation, the article also compares solutions provided by different programming languages, especially against Ruby mixins, by listing their advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
Multiple Inheritance is a technique of establishing hierarchical relationships within a set of classes, such that a single class can inherit  behaviors from multiple parent classes. Although it might seem useful to include methods from multiple classes, the major issue with multiple inheritance is the ambiguity in referencing the parent class methods,  when more than one parent classes share a method with the same name.This ambiguity is predominant in cases where the inheritance hierarchy is more than one level deep, where the class in the lowermost level inherits from two classes, that have overridden the same method from their parent in different ways.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A Real World Example==&lt;br /&gt;
A known example of multiple inheritance to represent animals using classes and objects is cited for the sake of understanding &amp;lt;ref&amp;gt; http://stackoverflow.com/questions/9359948/multiple-inheritance-whats-a-good-example&amp;lt;/ref&amp;gt;. A Winged animal and a mammal inherit primitive attributes of an animal. While a winged animal can fly, a mammal can breathe and reproduce, they both eat, move and share features generic to animals. A bat being a winged mammal possesses features common to both winged animal and mammal. It follows that class Bat inherits from classes WingedAnimal and Mammal and in turn classes WingedAnimal and Mammal inherit from class Animal.  Let us explore the issues of multiple inheritance in reference to this context.&lt;br /&gt;
&lt;br /&gt;
[[File:Class_Diagram.png|thumb|right|x400px|alt=Class Diagram for the example.]]&lt;br /&gt;
&lt;br /&gt;
==Issues of Multiple Inheritance==&lt;br /&gt;
1. Due to inheritance, the parent class’s objects get duplicated to child class. As a result, Allrounder Class duplicates Cricketer objects through each of Batsman and Bowler classes. When Allrounder accesses any of the Cricketer’s attributes, the compiler would be ambiguous about determining the correct path to use since two paths are available from Allrounder to Cricketer. This problem of multiple inheritance is called diamond problem. Compiler takes arbitrary decisions during such situations. &amp;lt;ref&amp;gt;http://www.artima.com/weblogs/viewpost.jsp?thread=246488&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
2.Objects of parent classes can be separately managed by using virtual tables as in C++. By explicitly specifying the virtual table pointer, the Allrounder class can access Batsman and Bowler classes unambiguously. However virtual tables occupy significant portion of memory thereby limiting their extensive usage.  &lt;br /&gt;
&lt;br /&gt;
3. The order by which a child class inherits parent classes also matters. If Allrounder inherits Batsman prior to Bowler, then Batsman precedes Bowler and common methods are accessed in this order of precedence.  Hence if the Programmer is not aware of such precedences, debugging would be difficult.&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
Programming languages provide different solutions to handle the issues of multiple inheritance. Here we consider the techniques offered by C++, Java, Ruby, Scala and Python.    &lt;br /&gt;
===Multiple Inheritance in C++===&lt;br /&gt;
C++ supports multiple inheritance with the notion of virtual classes and virtual functions. In the example, the Cricketer class defines virtual functions with Batsman and Bowler classes virtually inheriting the features of Cricketer. The Allrounder class can now unambiguously access Cricketer class using virtual table pointers of Batsman and Bowler. The example written using C++ virtual functions is shown below.&lt;br /&gt;
&lt;br /&gt;
 class Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   protected:&lt;br /&gt;
     char* country;&lt;br /&gt;
     char* player_type;&lt;br /&gt;
   public:&lt;br /&gt;
     virtual void statistics()&lt;br /&gt;
     {&lt;br /&gt;
        cout&amp;lt;&amp;lt;&amp;quot;Cricketer's statistics include number of matches, average, best performances etc.&amp;quot;&amp;lt;&amp;lt;endl; }&lt;br /&gt;
     }    &lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Batsman : virtual public Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   public:&lt;br /&gt;
     void statistics()&lt;br /&gt;
     {&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;Batsman's statistics include batting average, number of 100's &amp;amp; 50's, top score etc.&amp;quot;&amp;lt;&amp;lt;endl;&lt;br /&gt;
     }&lt;br /&gt;
     // other methods to operate on Cricketer &amp;amp; Batsman specific attributes&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Bowler : virtual public Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   public:&lt;br /&gt;
     void statistics()&lt;br /&gt;
     {&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;Bowler's statistics include number of wickets, bowling average, economy etc.&amp;quot;&amp;lt;&amp;lt;endl;&lt;br /&gt;
     }&lt;br /&gt;
     // other methods to operate on Cricketer &amp;amp; Bowler specific attributes&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Allrounder: public Batsman, public Bowler&lt;br /&gt;
 {&lt;br /&gt;
   public:&lt;br /&gt;
     void statistics()&lt;br /&gt;
     {&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;Allrounder's statistics include batting average, bowling average etc.&amp;quot;&amp;lt;&amp;lt;endl;&lt;br /&gt;
     }&lt;br /&gt;
     // other methods to operate on Cricketer, Batsman &amp;amp; Bowler specific attributes&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
   int i = 0;&lt;br /&gt;
   Cricketer *instances[3];&lt;br /&gt;
   Batsman bat;&lt;br /&gt;
   Bowler bowl;&lt;br /&gt;
   Allrounder all;&lt;br /&gt;
   instances[0] = &amp;amp;bat;&lt;br /&gt;
   instances[1] = &amp;amp;bowl;&lt;br /&gt;
   instances[2] = &amp;amp;all;&lt;br /&gt;
   while(i&amp;lt;3) &lt;br /&gt;
     instances[i++]-&amp;gt;statistics();      &lt;br /&gt;
   return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Java===&lt;br /&gt;
Java doesn’t support multiple inheritance but provides a feature called interfaces to resemble inheritance from multiple classes. Interface is an abstract class which provides a generic template by just declaring variables and methods without implementing them. Concrete classes that inherit interfaces are supposed to implement its methods. The example written using Java interfaces is shown below.&lt;br /&gt;
&lt;br /&gt;
 interface Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   void statistics();  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Batsman extends Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   void statistics();  &lt;br /&gt;
 }&lt;br /&gt;
 class ImplementBatsman implements Batsman  &lt;br /&gt;
 {&lt;br /&gt;
    public void statistics()&lt;br /&gt;
    {&lt;br /&gt;
      System.out.println(&amp;quot;Batsman's statistics include batting average, number of 100's &amp;amp; 50's etc.&amp;quot;);&lt;br /&gt;
    }   &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Bowler extends Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   void statistics();&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementBowler implements Bowler&lt;br /&gt;
 {&lt;br /&gt;
    public void statistics()&lt;br /&gt;
    {&lt;br /&gt;
      System.out.println(&amp;quot;Bowler's statistics include number of wickets, bowling average, economy etc.&amp;quot;);&lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Allrounder extends Batsman, Bowler &lt;br /&gt;
 {&lt;br /&gt;
    // other methods to operate on Cricketer, Batsman &amp;amp; Bowler specific attributes&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementAllrounder implements Allrounder&lt;br /&gt;
 {  &lt;br /&gt;
    ImplementBowler bowl = new ImplementBowler();&lt;br /&gt;
    ImplementBatsman bat = new ImplementBatsman(); &lt;br /&gt;
    public void statistics()&lt;br /&gt;
    {&lt;br /&gt;
      bowl.statistics();&lt;br /&gt;
      bat.statistics();   &lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class MultipleInheritance&lt;br /&gt;
 {&lt;br /&gt;
   public static void main(String args[])&lt;br /&gt;
   {&lt;br /&gt;
     ImplementAllrounder all = new ImplementAllrounder();&lt;br /&gt;
     all.statistics();       &lt;br /&gt;
   } &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Ruby===&lt;br /&gt;
Ruby doesn't support multiple inheritance but provides a similar feature using modules and mixins.  &lt;br /&gt;
====Modules====&lt;br /&gt;
A Module is an encapsulation of methods and variables, although similar in function to a Class, differs from a class in the sense that it cannot be instantiated. Modules are  more dynamic and allow us to organize the code in a more granular way.Each module can add something to the existing class. A module can be thought of an encapsulation of a  subset of the full capabilities of an object.&lt;br /&gt;
An example found in the book ,The Well-Grounded Rubyist &amp;lt;ref&amp;gt;http://www.manning.com/black2/SampleChapter4.pdf [Pg.92] &amp;lt;/ref&amp;gt;, is the stack-like module. The module encapsulates the characteristics of being like a stack,implements the operations that can be performed by a stack but is not the actual Stack class. This allows us to use this module to mix-in  stack-like behaviour into other classes that require them.&lt;br /&gt;
&lt;br /&gt;
====Mixins====&lt;br /&gt;
A Mixin is a concept that allows inclusion of special characteristics into existing classes, so that common features that need to be reused in several classes can be selectively included, thus avoiding implementation of unnecessary functions like in the case of interfaces.&lt;br /&gt;
Mixins allow us to dynamically bind special features defined in modules, into core classes without really making any changes to the objects. This improves flexibility when we want to add plugins and extensions to the existing framework.&amp;lt;ref&amp;gt;http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-frameworks/&amp;lt;/ref&amp;gt;.The source code for the same example implemented using ruby mixins is shown below.&lt;br /&gt;
&lt;br /&gt;
 module Cricketer&lt;br /&gt;
   def statistics&lt;br /&gt;
     puts &amp;quot;Cricketer's statistics include number of matches, average etc.&amp;quot;&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Batsman&lt;br /&gt;
   include Cricketer&lt;br /&gt;
   def statistics&lt;br /&gt;
     puts &amp;quot;Batsman's statistics include batting average, number of 100's &amp;amp; 50's etc.&amp;quot;&lt;br /&gt;
   end    &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Bowler&lt;br /&gt;
   include Cricketer&lt;br /&gt;
   def statistics&lt;br /&gt;
      puts &amp;quot;Bowler's statistics include number of wickets, bowling average, economy etc.&amp;quot;&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class AllRounder&lt;br /&gt;
   include Batsman&lt;br /&gt;
   include Bowler&lt;br /&gt;
   include Cricketer&lt;br /&gt;
   # Other implementation&lt;br /&gt;
 end&lt;br /&gt;
 AllRounder.new().statistics()&lt;br /&gt;
&lt;br /&gt;
===Multiple inheritance in other languages===&lt;br /&gt;
Scala offers traits that are similar to Java interfaces to support multiple inheritance. Traits define a template with methods and variables which any class can make use of. The difference between interfaces and traits is that traits can implement methods. A Scala class can inherit from any number of traits together with a class in way to support multiple inheritance. Traits can be mixed in to a class even without inheriting it but just by including it at the time of creating instances. &amp;lt;ref&amp;gt;http://joelabrahamsson.com/entry/learning-scala-traits&amp;lt;/ref&amp;gt;. Trait can also extend a class and methods to be overridden are usually prefixed by the keyword override. The language enforces method names to be different across inherited  traits to avoid the issues of multiple inheritance. Python supports a limited version of multiple inheritance by extending multiple classes in depth-first left-right order &amp;lt;ref&amp;gt;http://docs.python.org/tutorial/classes.html&amp;lt;/ref&amp;gt;. If a python class D inherits classes A, B, C and if an instance variable or method is not available in the current class, it checks in A and its base classes, then moves to B and its base classes and eventually to C thus moving from left to right. In this way, python avoids the diamond problem by making the search order linear by which the derived class accesses attributes of its parent classes. In addition python also defines mixins to access reusable features within a class.&lt;br /&gt;
&lt;br /&gt;
==Advantages of mixins==&lt;br /&gt;
1. Firstly, when a module gets mixed into different classes, each class acquires the same  reference  to the module and hence even dynamic changes to the module gets reflected in the behavior of the classes &amp;lt;ref&amp;gt;http://www.linuxtopia.org/online_books/programming_books/ruby_tutorial/Ruby_Modules_Mixins.html&amp;lt;/ref&amp;gt;. In this way, the module’s code is not just copied to individual classes as in static languages instead its reference is provided. &lt;br /&gt;
&lt;br /&gt;
2. Secondly, the mixed in state and behavior can be accessed by the instance of target class just like accessing its own features. &lt;br /&gt;
&lt;br /&gt;
3. Thirdly, a class can include any number of modules in a way it can simulate multiple inheritance without having the related issues.&lt;br /&gt;
&lt;br /&gt;
==Questions to Ponder==&lt;br /&gt;
===Do mixins solve all of the problems of multiple inheritance?===&lt;br /&gt;
Undoubtedly mixins solve some of the problems of multiple inheritance. There is no ambiguity in resolving the request to a common method implemented by multiple modules. In that case, the lastly included module is responsible for handling accesses to common methods. Though the order of precedence of included modules matters, it looks clearer than C++.  However languages like Scala and Eiffel implement multiple inheritance far more safely  by explicitly forcing the user to have different method names across inherited classes.&lt;br /&gt;
&lt;br /&gt;
===Are mixins a clear advantage over interfaces?===&lt;br /&gt;
Mixins are more advantageous than interfaces because of their features. Interfaces have some limitations. As seen from programs, the derived class Allrounder uses the instances of Batsman and Bowler classes (has-a relationship) to access respective attributes. Interface is nice in the sense it provides a generic template that could be implemented differently by different classes.  However the target class has to necessarily implement every method of the interface. This would pose problems to a Class which is interested in implementing specific features of the interface. On the contrary, Mixins eliminate this inconvenience by mixing in the state and behavior of the modules in the target class and allows a it to use them as it needs. Hence the target class can just use module’s features selectively or completely instead of implementing by itself. The size of multiple inheritance code using mixins is smaller than interfaces and C++ variants.&lt;br /&gt;
&lt;br /&gt;
===Do mixins have any disadvantages not shared by multiple inheritance or interfaces?===&lt;br /&gt;
Inspite of making the lives of programmers better with effective code reuse and avoiding unnecessary implementation of methods that cause clutter in the code, the concept of Mixins can easily be abused resulting in namespace pollution. This is caused when a class includes multiple modules each of which contains several hundred method implementations. Having hundreds of methods mixed into a single class, makes it difficult to identify the source modules of the methods, thereby causing confusion while implementing these methods, whereas in the case of multiple inheritance we can easily identify the class to which the method belongs.&lt;br /&gt;
Another disadvantage that follows from the namespace collision is the case where we have multiple methods with the same name in different modules.In such a case whenever a call to that method is made, the method which belongs to the module that was included last will be called. The problem with this is that, this takes place silently without any warning. This may result in accidental overriding of wrong methods.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
In this article, we have discussed about multiple inheritance and means by which it is implemented in different languages. Though multiple inheritance is a useful feature,&lt;br /&gt;
many languages do not support them due to its associated issues. Given its advantages, mixin in ruby seems to be a viable option to achieve multiple inheritance. However mixins too have limitations which should be taken care of during implementation.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
==See Also==&lt;br /&gt;
1. Lucas Carlson and Leonard Richardson. 2006. [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=1212915&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 Ruby Cookbook] (Cookbooks (O'Reilly)). O'Reilly Media, Inc..&lt;br /&gt;
&lt;br /&gt;
2. Herbert Schildt. 2000. Java 2: [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=557816&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 The Complete Reference] (4th ed.). McGraw-Hill Professional.&lt;/div&gt;</summary>
		<author><name>Snallan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65422</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_as&amp;diff=65422"/>
		<updated>2012-09-21T12:25:08Z</updated>

		<summary type="html">&lt;p&gt;Snallan: /* A Real World Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''1w23:  Multiple Inheritance and Mixins&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
The purpose of this article is to give a brief introduction about multiple inheritance, consider some of its issues and discuss techniques employed by different programming languages to handle such issues. In this relation, the article also compares solutions provided by different programming languages, especially against Ruby mixins, by listing their advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
Multiple Inheritance is a technique of establishing hierarchical relationships within a set of classes, such that a single class can inherit  behaviors from multiple parent classes. Although it might seem useful to include methods from multiple classes, the major issue with multiple inheritance is the ambiguity in referencing the parent class methods,  when more than one parent classes share a method with the same name.This ambiguity is predominant in cases where the inheritance hierarchy is more than one level deep, where the class in the lowermost level inherits from two classes, that have overridden the same method from their parent in different ways.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A Real World Example==&lt;br /&gt;
A known example of multiple inheritance to represent animals using classes and objects is cited here&amp;lt;ref&amp;gt; http://stackoverflow.com/questions/9359948/multiple-inheritance-whats-a-good-example&amp;lt;/ref&amp;gt;. Winged animal and mammal inherit primitive attributes of an animal. While a winged animal can fly, a mammal can breathe and reproduce, they both eat, move and share feature generic to animals. Let us explore the issues of multiple inheritance in reference to this context.&lt;br /&gt;
&lt;br /&gt;
[[File:Class_Diagram.png|thumb|right|x400px|alt=Class Diagram for the example.]]&lt;br /&gt;
&lt;br /&gt;
==Issues of Multiple Inheritance==&lt;br /&gt;
1. Due to inheritance, the parent class’s objects get duplicated to child class. As a result, Allrounder Class duplicates Cricketer objects through each of Batsman and Bowler classes. When Allrounder accesses any of the Cricketer’s attributes, the compiler would be ambiguous about determining the correct path to use since two paths are available from Allrounder to Cricketer. This problem of multiple inheritance is called diamond problem. Compiler takes arbitrary decisions during such situations. &amp;lt;ref&amp;gt;http://www.artima.com/weblogs/viewpost.jsp?thread=246488&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
2.Objects of parent classes can be separately managed by using virtual tables as in C++. By explicitly specifying the virtual table pointer, the Allrounder class can access Batsman and Bowler classes unambiguously. However virtual tables occupy significant portion of memory thereby limiting their extensive usage.  &lt;br /&gt;
&lt;br /&gt;
3. The order by which a child class inherits parent classes also matters. If Allrounder inherits Batsman prior to Bowler, then Batsman precedes Bowler and common methods are accessed in this order of precedence.  Hence if the Programmer is not aware of such precedences, debugging would be difficult.&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
Programming languages provide different solutions to handle the issues of multiple inheritance. Here we consider the techniques offered by C++, Java, Ruby, Scala and Python.    &lt;br /&gt;
===Multiple Inheritance in C++===&lt;br /&gt;
C++ supports multiple inheritance with the notion of virtual classes and virtual functions. In the example, the Cricketer class defines virtual functions with Batsman and Bowler classes virtually inheriting the features of Cricketer. The Allrounder class can now unambiguously access Cricketer class using virtual table pointers of Batsman and Bowler. The example written using C++ virtual functions is shown below.&lt;br /&gt;
&lt;br /&gt;
 class Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   protected:&lt;br /&gt;
     char* country;&lt;br /&gt;
     char* player_type;&lt;br /&gt;
   public:&lt;br /&gt;
     virtual void statistics()&lt;br /&gt;
     {&lt;br /&gt;
        cout&amp;lt;&amp;lt;&amp;quot;Cricketer's statistics include number of matches, average, best performances etc.&amp;quot;&amp;lt;&amp;lt;endl; }&lt;br /&gt;
     }    &lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Batsman : virtual public Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   public:&lt;br /&gt;
     void statistics()&lt;br /&gt;
     {&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;Batsman's statistics include batting average, number of 100's &amp;amp; 50's, top score etc.&amp;quot;&amp;lt;&amp;lt;endl;&lt;br /&gt;
     }&lt;br /&gt;
     // other methods to operate on Cricketer &amp;amp; Batsman specific attributes&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Bowler : virtual public Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   public:&lt;br /&gt;
     void statistics()&lt;br /&gt;
     {&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;Bowler's statistics include number of wickets, bowling average, economy etc.&amp;quot;&amp;lt;&amp;lt;endl;&lt;br /&gt;
     }&lt;br /&gt;
     // other methods to operate on Cricketer &amp;amp; Bowler specific attributes&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Allrounder: public Batsman, public Bowler&lt;br /&gt;
 {&lt;br /&gt;
   public:&lt;br /&gt;
     void statistics()&lt;br /&gt;
     {&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;Allrounder's statistics include batting average, bowling average etc.&amp;quot;&amp;lt;&amp;lt;endl;&lt;br /&gt;
     }&lt;br /&gt;
     // other methods to operate on Cricketer, Batsman &amp;amp; Bowler specific attributes&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
   int i = 0;&lt;br /&gt;
   Cricketer *instances[3];&lt;br /&gt;
   Batsman bat;&lt;br /&gt;
   Bowler bowl;&lt;br /&gt;
   Allrounder all;&lt;br /&gt;
   instances[0] = &amp;amp;bat;&lt;br /&gt;
   instances[1] = &amp;amp;bowl;&lt;br /&gt;
   instances[2] = &amp;amp;all;&lt;br /&gt;
   while(i&amp;lt;3) &lt;br /&gt;
     instances[i++]-&amp;gt;statistics();      &lt;br /&gt;
   return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Java===&lt;br /&gt;
Java doesn’t support multiple inheritance but provides a feature called interfaces to resemble inheritance from multiple classes. Interface is an abstract class which provides a generic template by just declaring variables and methods without implementing them. Concrete classes that inherit interfaces are supposed to implement its methods. The example written using Java interfaces is shown below.&lt;br /&gt;
&lt;br /&gt;
 interface Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   void statistics();  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Batsman extends Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   void statistics();  &lt;br /&gt;
 }&lt;br /&gt;
 class ImplementBatsman implements Batsman  &lt;br /&gt;
 {&lt;br /&gt;
    public void statistics()&lt;br /&gt;
    {&lt;br /&gt;
      System.out.println(&amp;quot;Batsman's statistics include batting average, number of 100's &amp;amp; 50's etc.&amp;quot;);&lt;br /&gt;
    }   &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Bowler extends Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   void statistics();&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementBowler implements Bowler&lt;br /&gt;
 {&lt;br /&gt;
    public void statistics()&lt;br /&gt;
    {&lt;br /&gt;
      System.out.println(&amp;quot;Bowler's statistics include number of wickets, bowling average, economy etc.&amp;quot;);&lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Allrounder extends Batsman, Bowler &lt;br /&gt;
 {&lt;br /&gt;
    // other methods to operate on Cricketer, Batsman &amp;amp; Bowler specific attributes&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementAllrounder implements Allrounder&lt;br /&gt;
 {  &lt;br /&gt;
    ImplementBowler bowl = new ImplementBowler();&lt;br /&gt;
    ImplementBatsman bat = new ImplementBatsman(); &lt;br /&gt;
    public void statistics()&lt;br /&gt;
    {&lt;br /&gt;
      bowl.statistics();&lt;br /&gt;
      bat.statistics();   &lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class MultipleInheritance&lt;br /&gt;
 {&lt;br /&gt;
   public static void main(String args[])&lt;br /&gt;
   {&lt;br /&gt;
     ImplementAllrounder all = new ImplementAllrounder();&lt;br /&gt;
     all.statistics();       &lt;br /&gt;
   } &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Ruby===&lt;br /&gt;
Ruby doesn't support multiple inheritance but provides a similar feature using modules and mixins.  &lt;br /&gt;
====Modules====&lt;br /&gt;
A Module is an encapsulation of methods and variables, although similar in function to a Class, differs from a class in the sense that it cannot be instantiated. Modules are  more dynamic and allow us to organize the code in a more granular way.Each module can add something to the existing class. A module can be thought of an encapsulation of a  subset of the full capabilities of an object.&lt;br /&gt;
An example found in the book ,The Well-Grounded Rubyist &amp;lt;ref&amp;gt;http://www.manning.com/black2/SampleChapter4.pdf [Pg.92] &amp;lt;/ref&amp;gt;, is the stack-like module. The module encapsulates the characteristics of being like a stack,implements the operations that can be performed by a stack but is not the actual Stack class. This allows us to use this module to mix-in  stack-like behaviour into other classes that require them.&lt;br /&gt;
&lt;br /&gt;
====Mixins====&lt;br /&gt;
A Mixin is a concept that allows inclusion of special characteristics into existing classes, so that common features that need to be reused in several classes can be selectively included, thus avoiding implementation of unnecessary functions like in the case of interfaces.&lt;br /&gt;
Mixins allow us to dynamically bind special features defined in modules, into core classes without really making any changes to the objects. This improves flexibility when we want to add plugins and extensions to the existing framework.&amp;lt;ref&amp;gt;http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-frameworks/&amp;lt;/ref&amp;gt;.The source code for the same example implemented using ruby mixins is shown below.&lt;br /&gt;
&lt;br /&gt;
 module Cricketer&lt;br /&gt;
   def statistics&lt;br /&gt;
     puts &amp;quot;Cricketer's statistics include number of matches, average etc.&amp;quot;&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Batsman&lt;br /&gt;
   include Cricketer&lt;br /&gt;
   def statistics&lt;br /&gt;
     puts &amp;quot;Batsman's statistics include batting average, number of 100's &amp;amp; 50's etc.&amp;quot;&lt;br /&gt;
   end    &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Bowler&lt;br /&gt;
   include Cricketer&lt;br /&gt;
   def statistics&lt;br /&gt;
      puts &amp;quot;Bowler's statistics include number of wickets, bowling average, economy etc.&amp;quot;&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class AllRounder&lt;br /&gt;
   include Batsman&lt;br /&gt;
   include Bowler&lt;br /&gt;
   include Cricketer&lt;br /&gt;
   # Other implementation&lt;br /&gt;
 end&lt;br /&gt;
 AllRounder.new().statistics()&lt;br /&gt;
&lt;br /&gt;
===Multiple inheritance in other languages===&lt;br /&gt;
Scala offers traits that are similar to Java interfaces to support multiple inheritance. Traits define a template with methods and variables which any class can make use of. The difference between interfaces and traits is that traits can implement methods. A Scala class can inherit from any number of traits together with a class in way to support multiple inheritance. Traits can be mixed in to a class even without inheriting it but just by including it at the time of creating instances. &amp;lt;ref&amp;gt;http://joelabrahamsson.com/entry/learning-scala-traits&amp;lt;/ref&amp;gt;. Trait can also extend a class and methods to be overridden are usually prefixed by the keyword override. The language enforces method names to be different across inherited  traits to avoid the issues of multiple inheritance. Python supports a limited version of multiple inheritance by extending multiple classes in depth-first left-right order &amp;lt;ref&amp;gt;http://docs.python.org/tutorial/classes.html&amp;lt;/ref&amp;gt;. If a python class D inherits classes A, B, C and if an instance variable or method is not available in the current class, it checks in A and its base classes, then moves to B and its base classes and eventually to C thus moving from left to right. In this way, python avoids the diamond problem by making the search order linear by which the derived class accesses attributes of its parent classes. In addition python also defines mixins to access reusable features within a class.&lt;br /&gt;
&lt;br /&gt;
==Advantages of mixins==&lt;br /&gt;
1. Firstly, when a module gets mixed into different classes, each class acquires the same  reference  to the module and hence even dynamic changes to the module gets reflected in the behavior of the classes &amp;lt;ref&amp;gt;http://www.linuxtopia.org/online_books/programming_books/ruby_tutorial/Ruby_Modules_Mixins.html&amp;lt;/ref&amp;gt;. In this way, the module’s code is not just copied to individual classes as in static languages instead its reference is provided. &lt;br /&gt;
&lt;br /&gt;
2. Secondly, the mixed in state and behavior can be accessed by the instance of target class just like accessing its own features. &lt;br /&gt;
&lt;br /&gt;
3. Thirdly, a class can include any number of modules in a way it can simulate multiple inheritance without having the related issues.&lt;br /&gt;
&lt;br /&gt;
==Questions to Ponder==&lt;br /&gt;
===Do mixins solve all of the problems of multiple inheritance?===&lt;br /&gt;
Undoubtedly mixins solve some of the problems of multiple inheritance. There is no ambiguity in resolving the request to a common method implemented by multiple modules. In that case, the lastly included module is responsible for handling accesses to common methods. Though the order of precedence of included modules matters, it looks clearer than C++.  However languages like Scala and Eiffel implement multiple inheritance far more safely  by explicitly forcing the user to have different method names across inherited classes.&lt;br /&gt;
&lt;br /&gt;
===Are mixins a clear advantage over interfaces?===&lt;br /&gt;
Mixins are more advantageous than interfaces because of their features. Interfaces have some limitations. As seen from programs, the derived class Allrounder uses the instances of Batsman and Bowler classes (has-a relationship) to access respective attributes. Interface is nice in the sense it provides a generic template that could be implemented differently by different classes.  However the target class has to necessarily implement every method of the interface. This would pose problems to a Class which is interested in implementing specific features of the interface. On the contrary, Mixins eliminate this inconvenience by mixing in the state and behavior of the modules in the target class and allows a it to use them as it needs. Hence the target class can just use module’s features selectively or completely instead of implementing by itself. The size of multiple inheritance code using mixins is smaller than interfaces and C++ variants.&lt;br /&gt;
&lt;br /&gt;
===Do mixins have any disadvantages not shared by multiple inheritance or interfaces?===&lt;br /&gt;
Inspite of making the lives of programmers better with effective code reuse and avoiding unnecessary implementation of methods that cause clutter in the code, the concept of Mixins can easily be abused resulting in namespace pollution. This is caused when a class includes multiple modules each of which contains several hundred method implementations. Having hundreds of methods mixed into a single class, makes it difficult to identify the source modules of the methods, thereby causing confusion while implementing these methods, whereas in the case of multiple inheritance we can easily identify the class to which the method belongs.&lt;br /&gt;
Another disadvantage that follows from the namespace collision is the case where we have multiple methods with the same name in different modules.In such a case whenever a call to that method is made, the method which belongs to the module that was included last will be called. The problem with this is that, this takes place silently without any warning. This may result in accidental overriding of wrong methods.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
In this article, we have discussed about multiple inheritance and means by which it is implemented in different languages. Though multiple inheritance is a useful feature,&lt;br /&gt;
many languages do not support them due to its associated issues. Given its advantages, mixin in ruby seems to be a viable option to achieve multiple inheritance. However mixins too have limitations which should be taken care of during implementation.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
==See Also==&lt;br /&gt;
1. Lucas Carlson and Leonard Richardson. 2006. [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=1212915&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 Ruby Cookbook] (Cookbooks (O'Reilly)). O'Reilly Media, Inc..&lt;br /&gt;
&lt;br /&gt;
2. Herbert Schildt. 2000. Java 2: [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=557816&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 The Complete Reference] (4th ed.). McGraw-Hill Professional.&lt;/div&gt;</summary>
		<author><name>Snallan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1a_1w23_as&amp;diff=64635</id>
		<title>CSC/ECE 517 Fall 2012/ch1a 1w23 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1a_1w23_as&amp;diff=64635"/>
		<updated>2012-09-14T22:38:48Z</updated>

		<summary type="html">&lt;p&gt;Snallan: /* Are mixins a clear advantage over interfaces? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''1w23:  Multiple Inheritance and Mixins&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
The purpose of this article is to give a brief introduction about multiple inheritance, consider some of its issues and discuss techniques employed by different programming languages to handle such issues. In this relation, the article also compares solutions provided by different programming languages, especially against Ruby mixins, by listing their advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
Multiple Inheritance is a technique of establishing hierarchical relationships within a set of classes, such that a single class can inherit  behaviors from multiple parent classes. Although it might seem useful to include methods from multiple classes, the major issue with multiple inheritance is the ambiguity in referencing the parent class methods,  when more than one parent classes share a method with the same name.This ambiguity is predominant in cases where the inheritance hierarchy is more than one level deep, where the class in the lowermost level inherits from two classes, that have overridden the same method from their parent in different ways.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A Real World Example==&lt;br /&gt;
Let us consider an example of representing players of Cricket&amp;lt;ref&amp;gt; http://en.wikipedia.org/wiki/Cricket&amp;lt;/ref&amp;gt; using classes and objects. The players of Cricket are instances of class Cricketer. Classes Bowler and Batsman inherit  Cricketer’s state and behavior. All rounders inherit states and behaviors of Batsman, Bowler and subsequently the Cricketer through Batsman and Bowler classes. Let us explore the issues of multiple inheritance in reference to this context.&lt;br /&gt;
&lt;br /&gt;
[[File:Class_Diagram.png|thumb|right|x400px|alt=Class Diagram for the example.]]&lt;br /&gt;
&lt;br /&gt;
==Issues of Multiple Inheritance==&lt;br /&gt;
1. Due to inheritance, the parent class’s objects get duplicated to child class. As a result, Allrounder Class duplicates Cricketer objects through each of Batsman and Bowler classes. When Allrounder accesses any of the Cricketer’s attributes, the compiler would be ambiguous about determining the correct path to use since two paths are available from Allrounder to Cricketer. This problem of multiple inheritance is called diamond problem. Compiler takes arbitrary decisions during such situations. &amp;lt;ref&amp;gt;http://www.artima.com/weblogs/viewpost.jsp?thread=246488&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
2.Objects of parent classes can be separately managed by using virtual tables as in C++. By explicitly specifying the virtual table pointer, the Allrounder class can access Batsman and Bowler classes unambiguously. However virtual tables occupy significant portion of memory thereby limiting their extensive usage.  &lt;br /&gt;
&lt;br /&gt;
3. The order by which a child class inherits parent classes also matters. If Allrounder inherits Batsman prior to Bowler, then Batsman precedes Bowler and common methods are accessed in this order of precedence.  Hence if the Programmer is not aware of such precedences, debugging would be difficult.&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
Programming languages provide different solutions to handle the issues of multiple inheritance. Here we consider the techniques offered by C++, Java, Ruby, Scala and Python.    &lt;br /&gt;
===Multiple Inheritance in C++===&lt;br /&gt;
C++ supports multiple inheritance with the notion of virtual classes and virtual functions. In the example, the Cricketer class defines virtual functions with Batsman and Bowler classes virtually inheriting the features of Cricketer. The Allrounder class can now unambiguously access Cricketer class using virtual table pointers of Batsman and Bowler. The example written using C++ virtual functions is shown below.&lt;br /&gt;
&lt;br /&gt;
 class Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   protected:&lt;br /&gt;
     char* country;&lt;br /&gt;
     char* player_type;&lt;br /&gt;
   public:&lt;br /&gt;
     virtual void statistics()&lt;br /&gt;
     {&lt;br /&gt;
        cout&amp;lt;&amp;lt;&amp;quot;Cricketer's statistics include number of matches, average, best performances etc.&amp;quot;&amp;lt;&amp;lt;endl; }&lt;br /&gt;
     }    &lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Batsman : virtual public Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   public:&lt;br /&gt;
     void statistics()&lt;br /&gt;
     {&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;Batsman's statistics include batting average, number of 100's &amp;amp; 50's, top score etc.&amp;quot;&amp;lt;&amp;lt;endl;&lt;br /&gt;
     }&lt;br /&gt;
     // other methods to operate on Cricketer &amp;amp; Batsman specific attributes&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Bowler : virtual public Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   public:&lt;br /&gt;
     void statistics()&lt;br /&gt;
     {&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;Bowler's statistics include number of wickets, bowling average, economy etc.&amp;quot;&amp;lt;&amp;lt;endl;&lt;br /&gt;
     }&lt;br /&gt;
     // other methods to operate on Cricketer &amp;amp; Bowler specific attributes&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Allrounder: public Batsman, public Bowler&lt;br /&gt;
 {&lt;br /&gt;
   public:&lt;br /&gt;
     void statistics()&lt;br /&gt;
     {&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;Allrounder's statistics include batting average, bowling average etc.&amp;quot;&amp;lt;&amp;lt;endl;&lt;br /&gt;
     }&lt;br /&gt;
     // other methods to operate on Cricketer, Batsman &amp;amp; Bowler specific attributes&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
   int i = 0;&lt;br /&gt;
   Cricketer *instances[3];&lt;br /&gt;
   Batsman bat;&lt;br /&gt;
   Bowler bowl;&lt;br /&gt;
   Allrounder all;&lt;br /&gt;
   instances[0] = &amp;amp;bat;&lt;br /&gt;
   instances[1] = &amp;amp;bowl;&lt;br /&gt;
   instances[2] = &amp;amp;all;&lt;br /&gt;
   while(i&amp;lt;3) &lt;br /&gt;
     instances[i++]-&amp;gt;statistics();      &lt;br /&gt;
   return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Java===&lt;br /&gt;
Java doesn’t support multiple inheritance but provides a feature called interfaces to simulate inheritance from multiple classes. Interface is an abstract class which provides a generic template by just declaring variables and methods without implementing them. Concrete classes that inherit interfaces are supposed to implement its methods. The example written using Java interfaces is shown below.&lt;br /&gt;
&lt;br /&gt;
 interface Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   void statistics();  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Batsman extends Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   void statistics();  &lt;br /&gt;
 }&lt;br /&gt;
 class ImplementBatsman implements Batsman  &lt;br /&gt;
 {&lt;br /&gt;
    public void statistics()&lt;br /&gt;
    {&lt;br /&gt;
      System.out.println(&amp;quot;Batsman's statistics include batting average, number of 100's &amp;amp; 50's etc.&amp;quot;);&lt;br /&gt;
    }   &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Bowler extends Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   void statistics();&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementBowler implements Bowler&lt;br /&gt;
 {&lt;br /&gt;
    public void statistics()&lt;br /&gt;
    {&lt;br /&gt;
      System.out.println(&amp;quot;Bowler's statistics include number of wickets, bowling average, economy etc.&amp;quot;);&lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Allrounder extends Batsman, Bowler &lt;br /&gt;
 {&lt;br /&gt;
    // other methods to operate on Cricketer, Batsman &amp;amp; Bowler specific attributes&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementAllrounder implements Allrounder&lt;br /&gt;
 {  &lt;br /&gt;
    ImplementBowler bowl = new ImplementBowler();&lt;br /&gt;
    ImplementBatsman bat = new ImplementBatsman(); &lt;br /&gt;
    public void statistics()&lt;br /&gt;
    {&lt;br /&gt;
      bowl.statistics();&lt;br /&gt;
      bat.statistics();   &lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class MultipleInheritance&lt;br /&gt;
 {&lt;br /&gt;
   public static void main(String args[])&lt;br /&gt;
   {&lt;br /&gt;
     ImplementAllrounder all = new ImplementAllrounder();&lt;br /&gt;
     all.statistics();       &lt;br /&gt;
   } &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Ruby===&lt;br /&gt;
Ruby doesn't support multiple inheritance either. However it provides a similar feature using modules and mixins.  &lt;br /&gt;
====Modules====&lt;br /&gt;
A Module is an encapsulation of methods and variables, although similar in function to a Class, differs from a class in the sense that it cannot be instantiated. Modules are  more dynamic and allow us to organize the code in a more granular way.Each module can add something to the existing class. Conceptually a module is like namespaces in C++ which avoids name collisions. A module can be thought of an encapsulation of a  subset of the full capabilities of an object.&lt;br /&gt;
An example found in the book ,The Well-Grounded Rubyist &amp;lt;ref&amp;gt;http://www.manning.com/black2/SampleChapter4.pdf [Pg.92] &amp;lt;/ref&amp;gt;, is the stack-like module. The module encapsulates the characteristics of being like a stack,implements the operations that can be performed by a stack but is not the actual Stack class. This allows us to use this module to mix-in  stack-like behaviour into other classes that require them.&lt;br /&gt;
&lt;br /&gt;
====Mixins====&lt;br /&gt;
A Mixin is a concept that allows inclusion of special characteristics into existing classes, so that common features that need to be reused in several classes can be selectively included, thus avoiding implementation of unnecessary functions like in the case of interfaces.&lt;br /&gt;
Mixins allow us to dynamically bind special features defined in modules, into core classes without really making any changes to the objects. This improves flexibility when we want to add plugins and extensions to the existing framework.&amp;lt;ref&amp;gt;http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-frameworks/&amp;lt;/ref&amp;gt;.The source code for the same example implemented using ruby mixins is shown below.&lt;br /&gt;
&lt;br /&gt;
 module Cricketer&lt;br /&gt;
   def statistics&lt;br /&gt;
     puts &amp;quot;Cricketer's statistics include number of matches, average etc.&amp;quot;&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Batsman&lt;br /&gt;
   include Cricketer&lt;br /&gt;
   def statistics&lt;br /&gt;
     puts &amp;quot;Batsman's statistics include batting average, number of 100's &amp;amp; 50's etc.&amp;quot;&lt;br /&gt;
   end    &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Bowler&lt;br /&gt;
   include Cricketer&lt;br /&gt;
   def statistics&lt;br /&gt;
      puts &amp;quot;Bowler's statistics include number of wickets, bowling average, economy etc.&amp;quot;&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class AllRounder&lt;br /&gt;
   include Batsman&lt;br /&gt;
   include Bowler&lt;br /&gt;
   include Cricketer&lt;br /&gt;
   # Other implementation&lt;br /&gt;
 end&lt;br /&gt;
 AllRounder.new().statistics()&lt;br /&gt;
&lt;br /&gt;
===Multiple inheritance in other languages===&lt;br /&gt;
Scala offers traits that are similar to Java interfaces to support multiple inheritance. Traits define a template with methods and variables which any class can make use of. The difference between interfaces and traits is that traits can implement methods. A Scala class can inherit from any number of traits together with a class in a way to support multiple inheritance. Traits can be mixed in to a class even without inheriting it but just by including it at the time of creating instances. &amp;lt;ref&amp;gt;http://joelabrahamsson.com/entry/learning-scala-traits&amp;lt;/ref&amp;gt;. Trait can also extend a class and methods to be overridden are usually prefixed by the keyword override. The language enforces method names to be different across inherited  traits to avoid the issues of multiple inheritance. Python supports a limited version of multiple inheritance by extending multiple classes in depth-first left-right order &amp;lt;ref&amp;gt;http://docs.python.org/tutorial/classes.html&amp;lt;/ref&amp;gt;. If a python class D inherits classes A, B, C and if an instance variable or method is not available in the current class, it checks in A and its base classes, then moves to B and its base classes and eventually to C thus moving from left to right. In this way, python avoids the diamond problem by making the search order linear by which the derived class accesses attributes of its parent classes. In addition python also defines mixins to access reusable features within a class.&lt;br /&gt;
&lt;br /&gt;
==Advantages of mixins==&lt;br /&gt;
1. Firstly, when a module gets mixed into different classes, each class acquires the same  reference  to the module and hence even dynamic changes to the module gets reflected in the behavior of the classes &amp;lt;ref&amp;gt;http://www.linuxtopia.org/online_books/programming_books/ruby_tutorial/Ruby_Modules_Mixins.html&amp;lt;/ref&amp;gt;. In this way, the module’s code is not just copied to individual classes as in static languages instead its reference is provided. &lt;br /&gt;
&lt;br /&gt;
2. Secondly, the mixed in state and behavior can be accessed by the instance of target class just like accessing its own features. &lt;br /&gt;
&lt;br /&gt;
3. Thirdly, a class can include any number of modules in a way it can simulate multiple inheritance without having the related issues.&lt;br /&gt;
&lt;br /&gt;
==Questions to Ponder==&lt;br /&gt;
===Do mixins solve all of the problems of multiple inheritance?===&lt;br /&gt;
Undoubtedly mixins solve some of the problems of multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
1. There is no ambiguity in resolving the request to a common method implemented by multiple modules. In that case, the lastly included module is responsible for handling accesses to common methods. &lt;br /&gt;
&lt;br /&gt;
2. Though the order of precedence of included modules matters, it looks clearer than C++.  However languages like Scala and Eiffel implement multiple inheritance far more safely  by explicitly forcing the user to have different method names across inherited classes.&lt;br /&gt;
&lt;br /&gt;
===Are mixins a clear advantage over interfaces?===&lt;br /&gt;
Mixins are more advantageous than interfaces because of their features. They are as follows.&lt;br /&gt;
&lt;br /&gt;
1. As seen from the Java interface program, the derived class Allrounder uses the instances of Batsman and Bowler classes (has-a relationship) to access respective attributes. Interface is nice in the sense it provides a generic template that could be implemented differently by different classes.  However the target class has to necessarily implement every method of the interface. This would pose problems to a Class which is interested in implementing specific features of the interface. On the contrary, Mixins eliminate this inconvenience by mixing in the state and behavior of the modules in the target class and allows it to use them as it needs. &lt;br /&gt;
&lt;br /&gt;
2. The target class can use module’s features selectively or completely instead of implementing by itself. Thus mixins provide reuse of behvior rather than resuse of implementation. &lt;br /&gt;
&lt;br /&gt;
3. The size of the multiple inheritance code using mixins is smaller than interfaces and C++ variants.&lt;br /&gt;
&lt;br /&gt;
===Do mixins have any disadvantages not shared by multiple inheritance or interfaces?===&lt;br /&gt;
Inspite of making the lives of programmers better with effective code reuse and avoiding unnecessary implementation of methods that cause clutter in the code, the concept of Mixins can easily be abused resulting in namespace pollution. Mixins do have the following disadvantages.&lt;br /&gt;
&lt;br /&gt;
1. Namespace collision is caused when a class includes multiple modules each of which contains several hundred method implementations. Having hundreds of methods mixed into a single class, makes it difficult to identify the source modules of the methods, thereby causing confusion while implementing these methods, whereas in the case of multiple inheritance we can easily identify the class to which the method belongs.&lt;br /&gt;
&lt;br /&gt;
2. Another disadvantage that follows from the namespace collision is the case where we have multiple methods with the same name in different modules.In such a case whenever a call to that method is made, the method which belongs to the module that was included last will be called. The problem with this is that, this takes place silently without any warning. This may result in accidental overriding of wrong methods.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
In this article, we have discussed about multiple inheritance and means by which it is implemented in different languages. Though multiple inheritance is a useful feature,&lt;br /&gt;
many languages do not support them due to its associated issues. Given its advantages, mixin in ruby seems to be a viable option to achieve multiple inheritance. However mixins too have limitations which should be taken care of during implementation.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
==See Also==&lt;br /&gt;
1. Lucas Carlson and Leonard Richardson. 2006. [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=1212915&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 Ruby Cookbook] (Cookbooks (O'Reilly)). O'Reilly Media, Inc..&lt;br /&gt;
&lt;br /&gt;
2. Herbert Schildt. 2000. Java 2: [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=557816&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 The Complete Reference] (4th ed.). McGraw-Hill Professional.&lt;/div&gt;</summary>
		<author><name>Snallan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1a_1w23_as&amp;diff=64624</id>
		<title>CSC/ECE 517 Fall 2012/ch1a 1w23 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1a_1w23_as&amp;diff=64624"/>
		<updated>2012-09-14T22:34:38Z</updated>

		<summary type="html">&lt;p&gt;Snallan: /* Multiple inheritance in other languages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''1w23:  Multiple Inheritance and Mixins&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
The purpose of this article is to give a brief introduction about multiple inheritance, consider some of its issues and discuss techniques employed by different programming languages to handle such issues. In this relation, the article also compares solutions provided by different programming languages, especially against Ruby mixins, by listing their advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
Multiple Inheritance is a technique of establishing hierarchical relationships within a set of classes, such that a single class can inherit  behaviors from multiple parent classes. Although it might seem useful to include methods from multiple classes, the major issue with multiple inheritance is the ambiguity in referencing the parent class methods,  when more than one parent classes share a method with the same name.This ambiguity is predominant in cases where the inheritance hierarchy is more than one level deep, where the class in the lowermost level inherits from two classes, that have overridden the same method from their parent in different ways.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A Real World Example==&lt;br /&gt;
Let us consider an example of representing players of Cricket&amp;lt;ref&amp;gt; http://en.wikipedia.org/wiki/Cricket&amp;lt;/ref&amp;gt; using classes and objects. The players of Cricket are instances of class Cricketer. Classes Bowler and Batsman inherit  Cricketer’s state and behavior. All rounders inherit states and behaviors of Batsman, Bowler and subsequently the Cricketer through Batsman and Bowler classes. Let us explore the issues of multiple inheritance in reference to this context.&lt;br /&gt;
&lt;br /&gt;
[[File:Class_Diagram.png|thumb|right|x400px|alt=Class Diagram for the example.]]&lt;br /&gt;
&lt;br /&gt;
==Issues of Multiple Inheritance==&lt;br /&gt;
1. Due to inheritance, the parent class’s objects get duplicated to child class. As a result, Allrounder Class duplicates Cricketer objects through each of Batsman and Bowler classes. When Allrounder accesses any of the Cricketer’s attributes, the compiler would be ambiguous about determining the correct path to use since two paths are available from Allrounder to Cricketer. This problem of multiple inheritance is called diamond problem. Compiler takes arbitrary decisions during such situations. &amp;lt;ref&amp;gt;http://www.artima.com/weblogs/viewpost.jsp?thread=246488&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
2.Objects of parent classes can be separately managed by using virtual tables as in C++. By explicitly specifying the virtual table pointer, the Allrounder class can access Batsman and Bowler classes unambiguously. However virtual tables occupy significant portion of memory thereby limiting their extensive usage.  &lt;br /&gt;
&lt;br /&gt;
3. The order by which a child class inherits parent classes also matters. If Allrounder inherits Batsman prior to Bowler, then Batsman precedes Bowler and common methods are accessed in this order of precedence.  Hence if the Programmer is not aware of such precedences, debugging would be difficult.&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
Programming languages provide different solutions to handle the issues of multiple inheritance. Here we consider the techniques offered by C++, Java, Ruby, Scala and Python.    &lt;br /&gt;
===Multiple Inheritance in C++===&lt;br /&gt;
C++ supports multiple inheritance with the notion of virtual classes and virtual functions. In the example, the Cricketer class defines virtual functions with Batsman and Bowler classes virtually inheriting the features of Cricketer. The Allrounder class can now unambiguously access Cricketer class using virtual table pointers of Batsman and Bowler. The example written using C++ virtual functions is shown below.&lt;br /&gt;
&lt;br /&gt;
 class Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   protected:&lt;br /&gt;
     char* country;&lt;br /&gt;
     char* player_type;&lt;br /&gt;
   public:&lt;br /&gt;
     virtual void statistics()&lt;br /&gt;
     {&lt;br /&gt;
        cout&amp;lt;&amp;lt;&amp;quot;Cricketer's statistics include number of matches, average, best performances etc.&amp;quot;&amp;lt;&amp;lt;endl; }&lt;br /&gt;
     }    &lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Batsman : virtual public Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   public:&lt;br /&gt;
     void statistics()&lt;br /&gt;
     {&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;Batsman's statistics include batting average, number of 100's &amp;amp; 50's, top score etc.&amp;quot;&amp;lt;&amp;lt;endl;&lt;br /&gt;
     }&lt;br /&gt;
     // other methods to operate on Cricketer &amp;amp; Batsman specific attributes&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Bowler : virtual public Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   public:&lt;br /&gt;
     void statistics()&lt;br /&gt;
     {&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;Bowler's statistics include number of wickets, bowling average, economy etc.&amp;quot;&amp;lt;&amp;lt;endl;&lt;br /&gt;
     }&lt;br /&gt;
     // other methods to operate on Cricketer &amp;amp; Bowler specific attributes&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 class Allrounder: public Batsman, public Bowler&lt;br /&gt;
 {&lt;br /&gt;
   public:&lt;br /&gt;
     void statistics()&lt;br /&gt;
     {&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;Allrounder's statistics include batting average, bowling average etc.&amp;quot;&amp;lt;&amp;lt;endl;&lt;br /&gt;
     }&lt;br /&gt;
     // other methods to operate on Cricketer, Batsman &amp;amp; Bowler specific attributes&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
   int i = 0;&lt;br /&gt;
   Cricketer *instances[3];&lt;br /&gt;
   Batsman bat;&lt;br /&gt;
   Bowler bowl;&lt;br /&gt;
   Allrounder all;&lt;br /&gt;
   instances[0] = &amp;amp;bat;&lt;br /&gt;
   instances[1] = &amp;amp;bowl;&lt;br /&gt;
   instances[2] = &amp;amp;all;&lt;br /&gt;
   while(i&amp;lt;3) &lt;br /&gt;
     instances[i++]-&amp;gt;statistics();      &lt;br /&gt;
   return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Java===&lt;br /&gt;
Java doesn’t support multiple inheritance but provides a feature called interfaces to simulate inheritance from multiple classes. Interface is an abstract class which provides a generic template by just declaring variables and methods without implementing them. Concrete classes that inherit interfaces are supposed to implement its methods. The example written using Java interfaces is shown below.&lt;br /&gt;
&lt;br /&gt;
 interface Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   void statistics();  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Batsman extends Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   void statistics();  &lt;br /&gt;
 }&lt;br /&gt;
 class ImplementBatsman implements Batsman  &lt;br /&gt;
 {&lt;br /&gt;
    public void statistics()&lt;br /&gt;
    {&lt;br /&gt;
      System.out.println(&amp;quot;Batsman's statistics include batting average, number of 100's &amp;amp; 50's etc.&amp;quot;);&lt;br /&gt;
    }   &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Bowler extends Cricketer&lt;br /&gt;
 {&lt;br /&gt;
   void statistics();&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementBowler implements Bowler&lt;br /&gt;
 {&lt;br /&gt;
    public void statistics()&lt;br /&gt;
    {&lt;br /&gt;
      System.out.println(&amp;quot;Bowler's statistics include number of wickets, bowling average, economy etc.&amp;quot;);&lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 interface Allrounder extends Batsman, Bowler &lt;br /&gt;
 {&lt;br /&gt;
    // other methods to operate on Cricketer, Batsman &amp;amp; Bowler specific attributes&lt;br /&gt;
 }&lt;br /&gt;
 class ImplementAllrounder implements Allrounder&lt;br /&gt;
 {  &lt;br /&gt;
    ImplementBowler bowl = new ImplementBowler();&lt;br /&gt;
    ImplementBatsman bat = new ImplementBatsman(); &lt;br /&gt;
    public void statistics()&lt;br /&gt;
    {&lt;br /&gt;
      bowl.statistics();&lt;br /&gt;
      bat.statistics();   &lt;br /&gt;
    }    &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class MultipleInheritance&lt;br /&gt;
 {&lt;br /&gt;
   public static void main(String args[])&lt;br /&gt;
   {&lt;br /&gt;
     ImplementAllrounder all = new ImplementAllrounder();&lt;br /&gt;
     all.statistics();       &lt;br /&gt;
   } &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance in Ruby===&lt;br /&gt;
Ruby doesn't support multiple inheritance either. However it provides a similar feature using modules and mixins.  &lt;br /&gt;
====Modules====&lt;br /&gt;
A Module is an encapsulation of methods and variables, although similar in function to a Class, differs from a class in the sense that it cannot be instantiated. Modules are  more dynamic and allow us to organize the code in a more granular way.Each module can add something to the existing class. Conceptually a module is like namespaces in C++ which avoids name collisions. A module can be thought of an encapsulation of a  subset of the full capabilities of an object.&lt;br /&gt;
An example found in the book ,The Well-Grounded Rubyist &amp;lt;ref&amp;gt;http://www.manning.com/black2/SampleChapter4.pdf [Pg.92] &amp;lt;/ref&amp;gt;, is the stack-like module. The module encapsulates the characteristics of being like a stack,implements the operations that can be performed by a stack but is not the actual Stack class. This allows us to use this module to mix-in  stack-like behaviour into other classes that require them.&lt;br /&gt;
&lt;br /&gt;
====Mixins====&lt;br /&gt;
A Mixin is a concept that allows inclusion of special characteristics into existing classes, so that common features that need to be reused in several classes can be selectively included, thus avoiding implementation of unnecessary functions like in the case of interfaces.&lt;br /&gt;
Mixins allow us to dynamically bind special features defined in modules, into core classes without really making any changes to the objects. This improves flexibility when we want to add plugins and extensions to the existing framework.&amp;lt;ref&amp;gt;http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-frameworks/&amp;lt;/ref&amp;gt;.The source code for the same example implemented using ruby mixins is shown below.&lt;br /&gt;
&lt;br /&gt;
 module Cricketer&lt;br /&gt;
   def statistics&lt;br /&gt;
     puts &amp;quot;Cricketer's statistics include number of matches, average etc.&amp;quot;&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Batsman&lt;br /&gt;
   include Cricketer&lt;br /&gt;
   def statistics&lt;br /&gt;
     puts &amp;quot;Batsman's statistics include batting average, number of 100's &amp;amp; 50's etc.&amp;quot;&lt;br /&gt;
   end    &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Bowler&lt;br /&gt;
   include Cricketer&lt;br /&gt;
   def statistics&lt;br /&gt;
      puts &amp;quot;Bowler's statistics include number of wickets, bowling average, economy etc.&amp;quot;&lt;br /&gt;
   end  &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class AllRounder&lt;br /&gt;
   include Batsman&lt;br /&gt;
   include Bowler&lt;br /&gt;
   include Cricketer&lt;br /&gt;
   # Other implementation&lt;br /&gt;
 end&lt;br /&gt;
 AllRounder.new().statistics()&lt;br /&gt;
&lt;br /&gt;
===Multiple inheritance in other languages===&lt;br /&gt;
Scala offers traits that are similar to Java interfaces to support multiple inheritance. Traits define a template with methods and variables which any class can make use of. The difference between interfaces and traits is that traits can implement methods. A Scala class can inherit from any number of traits together with a class in a way to support multiple inheritance. Traits can be mixed in to a class even without inheriting it but just by including it at the time of creating instances. &amp;lt;ref&amp;gt;http://joelabrahamsson.com/entry/learning-scala-traits&amp;lt;/ref&amp;gt;. Trait can also extend a class and methods to be overridden are usually prefixed by the keyword override. The language enforces method names to be different across inherited  traits to avoid the issues of multiple inheritance. Python supports a limited version of multiple inheritance by extending multiple classes in depth-first left-right order &amp;lt;ref&amp;gt;http://docs.python.org/tutorial/classes.html&amp;lt;/ref&amp;gt;. If a python class D inherits classes A, B, C and if an instance variable or method is not available in the current class, it checks in A and its base classes, then moves to B and its base classes and eventually to C thus moving from left to right. In this way, python avoids the diamond problem by making the search order linear by which the derived class accesses attributes of its parent classes. In addition python also defines mixins to access reusable features within a class.&lt;br /&gt;
&lt;br /&gt;
==Advantages of mixins==&lt;br /&gt;
1. Firstly, when a module gets mixed into different classes, each class acquires the same  reference  to the module and hence even dynamic changes to the module gets reflected in the behavior of the classes &amp;lt;ref&amp;gt;http://www.linuxtopia.org/online_books/programming_books/ruby_tutorial/Ruby_Modules_Mixins.html&amp;lt;/ref&amp;gt;. In this way, the module’s code is not just copied to individual classes as in static languages instead its reference is provided. &lt;br /&gt;
&lt;br /&gt;
2. Secondly, the mixed in state and behavior can be accessed by the instance of target class just like accessing its own features. &lt;br /&gt;
&lt;br /&gt;
3. Thirdly, a class can include any number of modules in a way it can simulate multiple inheritance without having the related issues.&lt;br /&gt;
&lt;br /&gt;
==Questions to Ponder==&lt;br /&gt;
===Do mixins solve all of the problems of multiple inheritance?===&lt;br /&gt;
Undoubtedly mixins solve some of the problems of multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
1. There is no ambiguity in resolving the request to a common method implemented by multiple modules. In that case, the lastly included module is responsible for handling accesses to common methods. &lt;br /&gt;
&lt;br /&gt;
2. Though the order of precedence of included modules matters, it looks clearer than C++.  However languages like Scala and Eiffel implement multiple inheritance far more safely  by explicitly forcing the user to have different method names across inherited classes.&lt;br /&gt;
&lt;br /&gt;
===Are mixins a clear advantage over interfaces?===&lt;br /&gt;
Mixins are more advantageous than interfaces because of their features. They are as follows.&lt;br /&gt;
&lt;br /&gt;
1. As seen from programs, the derived class Allrounder uses the instances of Batsman and Bowler classes (has-a relationship) to access respective attributes. Interface is nice in the sense it provides a generic template that could be implemented differently by different classes.  However the target class has to necessarily implement every method of the interface. This would pose problems to a Class which is interested in implementing specific features of the interface. On the contrary, Mixins eliminate this inconvenience by mixing in the state and behavior of the modules in the target class and allows it to use them as it needs. &lt;br /&gt;
&lt;br /&gt;
2. The target class can use module’s features selectively or completely instead of implementing by itself. Thus mixins provide reuse of behvior rather than resuse of implementation. &lt;br /&gt;
&lt;br /&gt;
3. The size of the multiple inheritance code using mixins is smaller than interfaces and C++ variants.&lt;br /&gt;
&lt;br /&gt;
===Do mixins have any disadvantages not shared by multiple inheritance or interfaces?===&lt;br /&gt;
Inspite of making the lives of programmers better with effective code reuse and avoiding unnecessary implementation of methods that cause clutter in the code, the concept of Mixins can easily be abused resulting in namespace pollution. Mixins do have the following disadvantages.&lt;br /&gt;
&lt;br /&gt;
1. Namespace collision is caused when a class includes multiple modules each of which contains several hundred method implementations. Having hundreds of methods mixed into a single class, makes it difficult to identify the source modules of the methods, thereby causing confusion while implementing these methods, whereas in the case of multiple inheritance we can easily identify the class to which the method belongs.&lt;br /&gt;
&lt;br /&gt;
2. Another disadvantage that follows from the namespace collision is the case where we have multiple methods with the same name in different modules.In such a case whenever a call to that method is made, the method which belongs to the module that was included last will be called. The problem with this is that, this takes place silently without any warning. This may result in accidental overriding of wrong methods.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
In this article, we have discussed about multiple inheritance and means by which it is implemented in different languages. Though multiple inheritance is a useful feature,&lt;br /&gt;
many languages do not support them due to its associated issues. Given its advantages, mixin in ruby seems to be a viable option to achieve multiple inheritance. However mixins too have limitations which should be taken care of during implementation.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
==See Also==&lt;br /&gt;
1. Lucas Carlson and Leonard Richardson. 2006. [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=1212915&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 Ruby Cookbook] (Cookbooks (O'Reilly)). O'Reilly Media, Inc..&lt;br /&gt;
&lt;br /&gt;
2. Herbert Schildt. 2000. Java 2: [http://dl.acm.org.prox.lib.ncsu.edu/citation.cfm?id=557816&amp;amp;coll=DL&amp;amp;dl=GUIDE&amp;amp;CFID=157189429&amp;amp;CFTOKEN=65094375 The Complete Reference] (4th ed.). McGraw-Hill Professional.&lt;/div&gt;</summary>
		<author><name>Snallan</name></author>
	</entry>
</feed>