<?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=Cgoogc</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=Cgoogc"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Cgoogc"/>
	<updated>2026-05-07T18:14:33Z</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_2010/ch5_5c_gn&amp;diff=39841</id>
		<title>CSC/ECE 517 Fall 2010/ch5 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch5_5c_gn&amp;diff=39841"/>
		<updated>2010-11-03T01:38:04Z</updated>

		<summary type="html">&lt;p&gt;Cgoogc: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Dynamic Dispatch'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding the concept of Dynamic Dispatch in OOLS. Here we will make an effort to address the efficiency considerations about Dynamic Dispatch, advantages of Dynamic Dispatch using parameter types and dynamic binding in CLOS (Common Lisp Object System).&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
Dynamic Dispatch is the process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function. It is generally used in &amp;lt;b&amp;gt;Object-Oriented Language&amp;lt;/b&amp;gt; (OOL) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Object-oriented_programming]&amp;lt;/sup&amp;gt;. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called as &amp;lt;b&amp;gt;Method Overriding&amp;lt;/b&amp;gt;  &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overriding]&amp;lt;/sup&amp;gt;). When a reference to the super class is used to execute that function, then which version of the function gets executed depends on the type, the reference to which it points to at that time rather than the type of the reference. In literature it also referred to in different names like &amp;lt;b&amp;gt;Runtime Binding&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;Dynamic Polymorphism&amp;lt;/b&amp;gt;. &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_polymorphism]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
   &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
==Need for Dynamic Dispatch==&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object-Oriented Language &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_polymorphism]&amp;lt;/sup&amp;gt;. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
==How Dynamic Dispatch works?==&lt;br /&gt;
The implementation of Dynamic Dispatch varies significantly based on type language, in fact it changes even from Compiler to Compiler for a same language. We will consider how function are dispatch at runtime in both C and C++. We will try to understand if there is any overhead involved with virtual functions. &lt;br /&gt;
&lt;br /&gt;
In a machine compiled language like C, program logic is compiled into underlying processor instruction set by the compiler. Incase of x86 compiler, C code will be compiled into X86 instruction sets. The function will be compiled and placed in Code Segment &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Code_segment]&amp;lt;/sup&amp;gt; area of the process image. So a function in C will just map to a memory location in Code Segment of the process image. For example calling a function 'print()', might be converted to something like 'jmp 0xFFDE123' (jmp is a branching instruction). So a function call is in fact as simple as executing a jump statement (of course you push the arguments into stack before you call jump).&lt;br /&gt;
&lt;br /&gt;
Now lets consider a OOL like C++ which too compiles to hardware instruction set on compilation. Consider the below example of two classes which extend from a common class called Animal (not shown here) with one method talk. The below example just demonstrates how dynamic dispatch could be implemented by a compiler. &lt;br /&gt;
&lt;br /&gt;
A word of caution before you proceed; the implementation is hypothetical and far from any practical use. The objective of the example shown below is just to give an idea of how Dynamic Dispatch might be implemented. Practical implementations are lot more complicated, so we have taken this simple example for easy explanation and understanding. &lt;br /&gt;
&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
The above C++ program might infact be converted to an equivalent C program by the compiler during pre-compilation. Each newly defined structure gets an __id__ and this id will be used to lookup vtable (Virtual Method Table &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_method_table]&amp;lt;/sup&amp;gt;) which maps id to the actual function memory location. One we get the memory location we will execute a jump instruction like we did before.&lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
  &lt;br /&gt;
 &lt;br /&gt;
 // C++ function call like &lt;br /&gt;
 Animal *d = new Dog(); &lt;br /&gt;
 d-&amp;gt;talk();&lt;br /&gt;
 &lt;br /&gt;
 // might be changed like this by the compiler&lt;br /&gt;
 Dog *d = malloc(sizeof(Dog));&lt;br /&gt;
 d-&amp;gt;__id__ = 1;&lt;br /&gt;
 talk(d);&lt;br /&gt;
 &lt;br /&gt;
 // talk function can use the __id__ attribute to offset into the vtable to identify which version of function to call.&lt;br /&gt;
&lt;br /&gt;
[[Image:Vtable.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;text-align: center;&amp;quot;&amp;gt; Fig.1 - VTable Working &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Virtual Method table or vtable or dispatch table [5] is the key to virtual function working. It is evident from the above that vtable infact makes the function calling little more expensive compared to normal function calls.&lt;br /&gt;
&lt;br /&gt;
=Performance Evaluation=&lt;br /&gt;
From the above explanation it is evident that Virtual functions &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_function]     [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc14cplr139.htm]&amp;lt;/sup&amp;gt; are more complicated that dispatching normal function calls. For example Virtual functions in C++ will need extra memory for each class to store the Virtual method table [5] and needs additional CPU cycles to perform vtable lookup, before it can make the actual function call. &lt;br /&gt;
&lt;br /&gt;
This is one of the important reasons why not all functions are dynamically binded in C++. In C++, functions which are explicitly marked virtual are dynamically bound whereas remaining functions are statically bound during the compile time. When we say bound/bind we are actually talking about the mapping between the function name and memory location where it is stored for execution.&lt;br /&gt;
&lt;br /&gt;
In languages like Java, the creators thought dynamic binding being an essential feature of OOL, hence, all functions are dynamically bound. It is also reasonable to assume that virtual functions might need 20 to 40 extra instruction sets to execute a function call. Given today's processors speed this might be very insignificant.&lt;br /&gt;
&lt;br /&gt;
We performed a very small experiment to see if Dynamic dispatch is practically slower than static dispatch. We opted to choose C#, since this language provide both static binding and dynamic binding. We measured the time taken to execute 1000000000 dynamically dispatched functions and statically dispatched functions. Below are our observations.&lt;br /&gt;
&lt;br /&gt;
 Dynamic Dispatch:&lt;br /&gt;
 00:00:10.0995777&lt;br /&gt;
 00:00:10.4315966&lt;br /&gt;
 00:00:10.4675987&lt;br /&gt;
&lt;br /&gt;
 Static Dispatch:&lt;br /&gt;
 00:00:10.2565866&lt;br /&gt;
 00:00:10.3255906&lt;br /&gt;
 00:00:10.1575809&lt;br /&gt;
&lt;br /&gt;
It looks like the performance overhead of dynamic binding is negligible in today's high performing computer. Even if it Dynamic Binding needs few more instruction to perform function call, the overhead in terms of time and memory is not much of a concern, but effective software design is a matter of concern. So we can conclude that Dynamic Binding does not cause serious performance problem in present day Computers.&lt;br /&gt;
&lt;br /&gt;
==Overriding and Overloading==&lt;br /&gt;
Overriding &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overriding]&amp;lt;/sup&amp;gt; and Overloading &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overloading]&amp;lt;/sup&amp;gt; are two different features of OOL. In function overloading, which version of function to call is decided by the compiler during the compile time itself, where as when a function is overridden, which version of the function to call is deferred till runtime.&lt;br /&gt;
&lt;br /&gt;
Let us see an two examples which provides insight into Overriding and Overloading. &lt;br /&gt;
&lt;br /&gt;
Example - 1: Overloading&lt;br /&gt;
&lt;br /&gt;
  public class A{&lt;br /&gt;
    public boolean equals( A check){                  # Equals method of parameter type A&lt;br /&gt;
      System.out.println( &amp;quot;A.equals method called&amp;quot;);&lt;br /&gt;
      return true;&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static void main(String [] args){          # Main method&lt;br /&gt;
      Object a1 = new A();                            # Instantiate class to get objects&lt;br /&gt;
      A a2 = new A();&lt;br /&gt;
      Object o1 = new Object();&lt;br /&gt;
 &lt;br /&gt;
      a1.equals(a2);                                  # Object equals method called&lt;br /&gt;
      a2.equals(a1);                                  # Object equals method called&lt;br /&gt;
      a2.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(o1);                                  # Object equals method called&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Example - 2: Overriding&lt;br /&gt;
&lt;br /&gt;
  public class A{&lt;br /&gt;
    @Override&lt;br /&gt;
    public boolean equals( Object check){             # Equals method of parameter type Object&lt;br /&gt;
      System.out.println( &amp;quot;A.equals method called&amp;quot;);&lt;br /&gt;
      return true;&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static void main(String [] args){          # Main method&lt;br /&gt;
      Object a1 = new A();                            # Instantiate class to get objects&lt;br /&gt;
      A a2 = new A();&lt;br /&gt;
      Object o1 = new Object();&lt;br /&gt;
 &lt;br /&gt;
      a1.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(a1);                                  # A equals method called&lt;br /&gt;
      a2.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(o1);                                  # A equals method called&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you see the comments provided to the side of the method calls, you would note the difference in the equals method called in case of both Overloading and Overriding. The decision which method to use, basically has two phases: First overload resolution, then method dispatch. Overload resolution happens at compile-time, method dispatch at runtime.&lt;br /&gt;
&lt;br /&gt;
==Does Dynamic Dispatching hurts in today's power packed Computers?==&lt;br /&gt;
&lt;br /&gt;
Even though Dynamic Dispatching is done in run-time, and it takes good amount of memory, today's computers are power packed with good amount of memory. Hence efficiency concerns for implementing Dynamic Dispatching in OOL is of little concern.&lt;br /&gt;
&lt;br /&gt;
However, the method to be called cannot be chosen based on the actual arguments passed to the function, rather, the method is called based on the declared type of the parameters.&lt;br /&gt;
&lt;br /&gt;
=Advantages of Dynamic Binding=&lt;br /&gt;
&lt;br /&gt;
Dynamic binding has several advantages. It provides tremendous flexibilities. Also, it allows the software to be malleable when requirements change as the system evolves. Because of dynamic binding, caller objects are not concerned how the invoked objects carry out their methods. All they need to know is that the invoked objects know how to carry out their responsibilities, but they themselves need to know only what the invoked objects can do for them. As a consequence of this, type dependencies (which are the bane of procedural programming) cannot have a ripple effect through the system, when system requirements change. The beauty is that such dependencies remain encapsulated within the objects. The flexibility that developers can derive out of this is enormous. For instance, one can install newer types without having to change or stopping the functioning of existing systems. This is something along the lines of &amp;quot;hot pluggable components&amp;quot; of the hardware cousins.&lt;br /&gt;
&lt;br /&gt;
One other advantage of Dynamic binding based on parameter types is that more than one parameter can be used in the selection of a method. Methods that use dynamic binding in this way are called multi-methods and the concept is called &amp;lt;b&amp;gt;Multiple Dispatch&amp;lt;/b&amp;gt; &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Multiple_dispatch]&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Multiple Dispatch==&lt;br /&gt;
&lt;br /&gt;
The Multiple Dispatch is used in Common Lisp Object System (CLOS) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Common_Lisp_Object_System]&amp;lt;/sup&amp;gt;. Multiple-polymorphism allows specialized functions or methods to be defined to handle various cases:&lt;br /&gt;
&lt;br /&gt;
  +(int, int)&lt;br /&gt;
  +(int, float)&lt;br /&gt;
  +(int, complex) .. etc&lt;br /&gt;
&lt;br /&gt;
The above functions are specialized to each of the cases required allowing single, highly cohesive and loosely coupled functions to be defined. This is also the true essence of object-oriented polymorphism, which allows objects to define methods for each specific case desired. In addition to better coupling and cohesion, multiple-polymorphism reduces program complexity by avoiding coding logic (switch statements) and because small methods further reduce complexity, as code complexity doesn't grow linearly with lines of code per method, but perhaps exponentially.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
Hence from the above discussion, we can see that Dynamic Dispatch, though slower when compared to static dispatch is no longer slower due to the new generation of power packed computers. Also, dynamic dispatch can help in preserving the object-oriented concepts in highly complex software products. We have also looked up the differences between method Overloading and Overriding. We also had a sneak-view to Multiple Dispatch where more than one parameter can be used for Dynamic method lookup.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overriding Method Overriding]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Code_segment Code Segment]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Virtual_method_table Virtual Method Table]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Virtual_function Virtual Functions]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc14cplr139.htm Virtual Functions in C++]. publib.boulder.ibm.com. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overloading Method Overloading]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Multiple_dispatch Multiple Dispatch]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;/div&gt;</summary>
		<author><name>Cgoogc</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39840</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39840"/>
		<updated>2010-11-03T01:35:09Z</updated>

		<summary type="html">&lt;p&gt;Cgoogc: /* Need for Dynamic Dispatch */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Dynamic Dispatch'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding the concept of Dynamic Dispatch in OOLS. Here we will make an effort to address the efficiency considerations about Dynamic Dispatch, advantages of Dynamic Dispatch using parameter types and dynamic binding in CLOS (Common Lisp Object System).&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
Dynamic Dispatch is the process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function. It is generally used in &amp;lt;b&amp;gt;Object-Oriented Language&amp;lt;/b&amp;gt; (OOL) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Object-oriented_programming]&amp;lt;/sup&amp;gt;. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called as &amp;lt;b&amp;gt;Method Overriding&amp;lt;/b&amp;gt;  &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overriding]&amp;lt;/sup&amp;gt;). When a reference to the super class is used to execute that function, then which version of the function gets executed depends on the type, the reference to which it points to at that time rather than the type of the reference. In literature it also referred to in different names like &amp;lt;b&amp;gt;Runtime Binding&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;Dynamic Polymorphism&amp;lt;/b&amp;gt;. &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_polymorphism]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
   &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
==Need for Dynamic Dispatch==&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object-Oriented Language &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_polymorphism]&amp;lt;/sup&amp;gt;. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
==How Dynamic Dispatch works?==&lt;br /&gt;
The implementation of Dynamic Dispatch varies significantly based on type language, in fact it changes even from Compiler to Compiler for a same language. We will consider how function are dispatch at runtime in both C and C++. We will try to understand if there is any overhead involved with virtual functions. &lt;br /&gt;
&lt;br /&gt;
In a machine compiled language like C, program logic is compiled into underlying processor instruction set by the compiler. Incase of x86 compiler, C code will be compiled into X86 instruction sets. The function will be compiled and placed in Code Segment &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Code_segment]&amp;lt;/sup&amp;gt; area of the process image. So a function in C will just map to a memory location in Code Segment of the process image. For example calling a function 'print()', might be converted to something like 'jmp 0xFFDE123' (jmp is a branching instruction). So a function call is in fact as simple as executing a jump statement (of course you push the arguments into stack before you call jump).&lt;br /&gt;
&lt;br /&gt;
Now lets consider a OOL like C++ which too compiles to hardware instruction set on compilation. Consider the below example of two classes which extend from a common class called Animal (not shown here) with one method talk. The below example just demonstrates how dynamic dispatch could be implemented by a compiler. &lt;br /&gt;
&lt;br /&gt;
A word of caution before you proceed; the implementation is hypothetical and far from any practical use. The objective of the example shown below is just to give an idea of how Dynamic Dispatch might be implemented. Practical implementations are lot more complicated, so we have taken this simple example for easy explanation and understanding. &lt;br /&gt;
&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
The above C++ program might infact be converted to an equivalent C program by the compiler during pre-compilation. Each newly defined structure gets an __id__ and this id will be used to lookup vtable (Virtual Method Table &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_method_table]&amp;lt;/sup&amp;gt;) which maps id to the actual function memory location. One we get the memory location we will execute a jump instruction like we did before.&lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
  &lt;br /&gt;
 &lt;br /&gt;
 // C++ function call like &lt;br /&gt;
 Animal *d = new Dog(); &lt;br /&gt;
 d-&amp;gt;talk();&lt;br /&gt;
 &lt;br /&gt;
 // might be changed like this by the compiler&lt;br /&gt;
 Dog *d = malloc(sizeof(Dog));&lt;br /&gt;
 d-&amp;gt;__id__ = 1;&lt;br /&gt;
 talk(d);&lt;br /&gt;
 &lt;br /&gt;
 // talk function can use the __id__ attribute to offset into the vtable to identify which version of function to call.&lt;br /&gt;
&lt;br /&gt;
[[Image:Vtable.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;text-align: center;&amp;quot;&amp;gt; Fig.1 - VTable Working &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Virtual Method table or vtable or dispatch table [5] is the key to virtual function working. It is evident from the above that vtable infact makes the function calling little more expensive compared to normal function calls.&lt;br /&gt;
&lt;br /&gt;
=Performance Evaluation=&lt;br /&gt;
From the above explanation it is evident that Virtual functions &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_function]     [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc14cplr139.htm]&amp;lt;/sup&amp;gt; are more complicated that dispatching normal function calls. For example Virtual functions in C++ will need extra memory for each class to store the Virtual method table [5] and needs additional CPU cycles to perform vtable lookup, before it can make the actual function call. &lt;br /&gt;
&lt;br /&gt;
This is one of the important reasons why not all functions are dynamically binded in C++. In C++, functions which are explicitly marked virtual are dynamically bound whereas remaining functions are statically bound during the compile time. When we say bound/bind we are actually talking about the mapping between the function name and memory location where it is stored for execution.&lt;br /&gt;
&lt;br /&gt;
In languages like Java, the creators thought dynamic binding being an essential feature of OOL, hence, all functions are dynamically bound. It is also reasonable to assume that virtual functions might need 20 to 40 extra instruction sets to execute a function call. Given today's processors speed this might be very insignificant.&lt;br /&gt;
&lt;br /&gt;
We performed a very small experiment to see if Dynamic dispatch is practically slower than static dispatch. We opted to choose C#, since this language provide both static binding and dynamic binding. We measured the time taken to execute 1000000000 dynamically dispatched functions and statically dispatched functions. Below are our observations.&lt;br /&gt;
&lt;br /&gt;
 Dynamic Dispatch:&lt;br /&gt;
 00:00:10.0995777&lt;br /&gt;
 00:00:10.4315966&lt;br /&gt;
 00:00:10.4675987&lt;br /&gt;
&lt;br /&gt;
 Static Dispatch:&lt;br /&gt;
 00:00:10.2565866&lt;br /&gt;
 00:00:10.3255906&lt;br /&gt;
 00:00:10.1575809&lt;br /&gt;
&lt;br /&gt;
It looks like the performance overhead of dynamic binding is negligible in today's high performing computer. Even if it Dynamic Binding needs few more instruction to perform function call, the overhead in terms of time and memory is not much of a concern, but effective software design is a matter of concern. So we can conclude that Dynamic Binding does not cause serious performance problem in present day Computers.&lt;br /&gt;
&lt;br /&gt;
==Overriding and Overloading==&lt;br /&gt;
Overriding &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overriding]&amp;lt;/sup&amp;gt; and Overloading &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overloading]&amp;lt;/sup&amp;gt; are two different features of OOL. In function overloading, which version of function to call is decided by the compiler during the compile time itself, where as when a function is overridden, which version of the function to call is deferred till runtime.&lt;br /&gt;
&lt;br /&gt;
Let us see an two examples which provides insight into Overriding and Overloading. &lt;br /&gt;
&lt;br /&gt;
Example - 1: Overloading&lt;br /&gt;
&lt;br /&gt;
  public class A{&lt;br /&gt;
    public boolean equals( A check){                  # Equals method of parameter type A&lt;br /&gt;
      System.out.println( &amp;quot;A.equals method called&amp;quot;);&lt;br /&gt;
      return true;&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static void main(String [] args){          # Main method&lt;br /&gt;
      Object a1 = new A();                            # Instantiate class to get objects&lt;br /&gt;
      A a2 = new A();&lt;br /&gt;
      Object o1 = new Object();&lt;br /&gt;
 &lt;br /&gt;
      a1.equals(a2);                                  # Object equals method called&lt;br /&gt;
      a2.equals(a1);                                  # Object equals method called&lt;br /&gt;
      a2.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(o1);                                  # Object equals method called&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Example - 2: Overriding&lt;br /&gt;
&lt;br /&gt;
  public class A{&lt;br /&gt;
    @Override&lt;br /&gt;
    public boolean equals( Object check){             # Equals method of parameter type Object&lt;br /&gt;
      System.out.println( &amp;quot;A.equals method called&amp;quot;);&lt;br /&gt;
      return true;&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static void main(String [] args){          # Main method&lt;br /&gt;
      Object a1 = new A();                            # Instantiate class to get objects&lt;br /&gt;
      A a2 = new A();&lt;br /&gt;
      Object o1 = new Object();&lt;br /&gt;
 &lt;br /&gt;
      a1.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(a1);                                  # A equals method called&lt;br /&gt;
      a2.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(o1);                                  # A equals method called&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you see the comments provided to the side of the method calls, you would note the difference in the equals method called in case of both Overloading and Overriding. The decision which method to use, basically has two phases: First overload resolution, then method dispatch. Overload resolution happens at compile-time, method dispatch at runtime.&lt;br /&gt;
&lt;br /&gt;
==Does Dynamic Dispatching hurts in today's power packed Computers?==&lt;br /&gt;
&lt;br /&gt;
Even though Dynamic Dispatching is done in run-time, and it takes good amount of memory, today's computers are power packed with good amount of memory. Hence efficiency concerns for implementing Dynamic Dispatching in OOL is of little concern.&lt;br /&gt;
&lt;br /&gt;
However, the method to be called cannot be chosen based on the actual arguments passed to the function, rather, the method is called based on the declared type of the parameters.&lt;br /&gt;
&lt;br /&gt;
=Advantages of Dynamic Binding=&lt;br /&gt;
&lt;br /&gt;
Dynamic binding has several advantages. It provides tremendous flexibilities. Also, it allows the software to be malleable when requirements change as the system evolves. Because of dynamic binding, caller objects are not concerned how the invoked objects carry out their methods. All they need to know is that the invoked objects know how to carry out their responsibilities, but they themselves need to know only what the invoked objects can do for them. As a consequence of this, type dependencies (which are the bane of procedural programming) cannot have a ripple effect through the system, when system requirements change. The beauty is that such dependencies remain encapsulated within the objects. The flexibility that developers can derive out of this is enormous. For instance, one can install newer types without having to change or stopping the functioning of existing systems. This is something along the lines of &amp;quot;hot pluggable components&amp;quot; of the hardware cousins.&lt;br /&gt;
&lt;br /&gt;
One other advantage of Dynamic binding based on parameter types is that more than one parameter can be used in the selection of a method. Methods that use dynamic binding in this way are called multi-methods and the concept is called &amp;lt;b&amp;gt;Multiple Dispatch&amp;lt;/b&amp;gt; &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Multiple_dispatch]&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Multiple Dispatch==&lt;br /&gt;
&lt;br /&gt;
The Multiple Dispatch is used in Common Lisp Object System (CLOS) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Common_Lisp_Object_System]&amp;lt;/sup&amp;gt;. Multiple-polymorphism allows specialized functions or methods to be defined to handle various cases:&lt;br /&gt;
&lt;br /&gt;
  +(int, int)&lt;br /&gt;
  +(int, float)&lt;br /&gt;
  +(int, complex) .. etc&lt;br /&gt;
&lt;br /&gt;
The above functions are specialized to each of the cases required allowing single, highly cohesive and loosely coupled functions to be defined. This is also the true essence of object-oriented polymorphism, which allows objects to define methods for each specific case desired. In addition to better coupling and cohesion, multiple-polymorphism reduces program complexity by avoiding coding logic (switch statements) and because small methods further reduce complexity, as code complexity doesn't grow linearly with lines of code per method, but perhaps exponentially.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
Hence from the above discussion, we can see that Dynamic Dispatch, though slower when compared to static dispatch is no longer slower due to the new generation of power packed computers. Also, dynamic dispatch can help in preserving the object-oriented concepts in highly complex software products. We have also looked up the differences between method Overloading and Overriding. We also had a sneak-view to Multiple Dispatch where more than one parameter can be used for Dynamic method lookup.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overriding Method Overriding]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Code_segment Code Segment]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Virtual_method_table Virtual Method Table]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Virtual_function Virtual Functions]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc14cplr139.htm Virtual Functions in C++]. publib.boulder.ibm.com. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overloading Method Overloading]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Multiple_dispatch Multiple Dispatch]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;/div&gt;</summary>
		<author><name>Cgoogc</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39839</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39839"/>
		<updated>2010-11-03T01:33:57Z</updated>

		<summary type="html">&lt;p&gt;Cgoogc: /* '''Dynamic Dispatch''' */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Dynamic Dispatch'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding the concept of Dynamic Dispatch in OOLS. Here we will make an effort to address the efficiency considerations about Dynamic Dispatch, advantages of Dynamic Dispatch using parameter types and dynamic binding in CLOS (Common Lisp Object System).&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
Dynamic Dispatch is the process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function. It is generally used in &amp;lt;b&amp;gt;Object-Oriented Language&amp;lt;/b&amp;gt; (OOL) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Object-oriented_programming]&amp;lt;/sup&amp;gt;. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called as &amp;lt;b&amp;gt;Method Overriding&amp;lt;/b&amp;gt;  &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overriding]&amp;lt;/sup&amp;gt;). When a reference to the super class is used to execute that function, then which version of the function gets executed depends on the type, the reference to which it points to at that time rather than the type of the reference. In literature it also referred to in different names like &amp;lt;b&amp;gt;Runtime Binding&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;Dynamic Polymorphism&amp;lt;/b&amp;gt;. &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_polymorphism]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
   &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
==Need for Dynamic Dispatch==&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object-Oriented Language [1]. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
==How Dynamic Dispatch works?==&lt;br /&gt;
The implementation of Dynamic Dispatch varies significantly based on type language, in fact it changes even from Compiler to Compiler for a same language. We will consider how function are dispatch at runtime in both C and C++. We will try to understand if there is any overhead involved with virtual functions. &lt;br /&gt;
&lt;br /&gt;
In a machine compiled language like C, program logic is compiled into underlying processor instruction set by the compiler. Incase of x86 compiler, C code will be compiled into X86 instruction sets. The function will be compiled and placed in Code Segment &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Code_segment]&amp;lt;/sup&amp;gt; area of the process image. So a function in C will just map to a memory location in Code Segment of the process image. For example calling a function 'print()', might be converted to something like 'jmp 0xFFDE123' (jmp is a branching instruction). So a function call is in fact as simple as executing a jump statement (of course you push the arguments into stack before you call jump).&lt;br /&gt;
&lt;br /&gt;
Now lets consider a OOL like C++ which too compiles to hardware instruction set on compilation. Consider the below example of two classes which extend from a common class called Animal (not shown here) with one method talk. The below example just demonstrates how dynamic dispatch could be implemented by a compiler. &lt;br /&gt;
&lt;br /&gt;
A word of caution before you proceed; the implementation is hypothetical and far from any practical use. The objective of the example shown below is just to give an idea of how Dynamic Dispatch might be implemented. Practical implementations are lot more complicated, so we have taken this simple example for easy explanation and understanding. &lt;br /&gt;
&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
The above C++ program might infact be converted to an equivalent C program by the compiler during pre-compilation. Each newly defined structure gets an __id__ and this id will be used to lookup vtable (Virtual Method Table &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_method_table]&amp;lt;/sup&amp;gt;) which maps id to the actual function memory location. One we get the memory location we will execute a jump instruction like we did before.&lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
  &lt;br /&gt;
 &lt;br /&gt;
 // C++ function call like &lt;br /&gt;
 Animal *d = new Dog(); &lt;br /&gt;
 d-&amp;gt;talk();&lt;br /&gt;
 &lt;br /&gt;
 // might be changed like this by the compiler&lt;br /&gt;
 Dog *d = malloc(sizeof(Dog));&lt;br /&gt;
 d-&amp;gt;__id__ = 1;&lt;br /&gt;
 talk(d);&lt;br /&gt;
 &lt;br /&gt;
 // talk function can use the __id__ attribute to offset into the vtable to identify which version of function to call.&lt;br /&gt;
&lt;br /&gt;
[[Image:Vtable.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;text-align: center;&amp;quot;&amp;gt; Fig.1 - VTable Working &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Virtual Method table or vtable or dispatch table [5] is the key to virtual function working. It is evident from the above that vtable infact makes the function calling little more expensive compared to normal function calls.&lt;br /&gt;
&lt;br /&gt;
=Performance Evaluation=&lt;br /&gt;
From the above explanation it is evident that Virtual functions &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_function]     [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc14cplr139.htm]&amp;lt;/sup&amp;gt; are more complicated that dispatching normal function calls. For example Virtual functions in C++ will need extra memory for each class to store the Virtual method table [5] and needs additional CPU cycles to perform vtable lookup, before it can make the actual function call. &lt;br /&gt;
&lt;br /&gt;
This is one of the important reasons why not all functions are dynamically binded in C++. In C++, functions which are explicitly marked virtual are dynamically bound whereas remaining functions are statically bound during the compile time. When we say bound/bind we are actually talking about the mapping between the function name and memory location where it is stored for execution.&lt;br /&gt;
&lt;br /&gt;
In languages like Java, the creators thought dynamic binding being an essential feature of OOL, hence, all functions are dynamically bound. It is also reasonable to assume that virtual functions might need 20 to 40 extra instruction sets to execute a function call. Given today's processors speed this might be very insignificant.&lt;br /&gt;
&lt;br /&gt;
We performed a very small experiment to see if Dynamic dispatch is practically slower than static dispatch. We opted to choose C#, since this language provide both static binding and dynamic binding. We measured the time taken to execute 1000000000 dynamically dispatched functions and statically dispatched functions. Below are our observations.&lt;br /&gt;
&lt;br /&gt;
 Dynamic Dispatch:&lt;br /&gt;
 00:00:10.0995777&lt;br /&gt;
 00:00:10.4315966&lt;br /&gt;
 00:00:10.4675987&lt;br /&gt;
&lt;br /&gt;
 Static Dispatch:&lt;br /&gt;
 00:00:10.2565866&lt;br /&gt;
 00:00:10.3255906&lt;br /&gt;
 00:00:10.1575809&lt;br /&gt;
&lt;br /&gt;
It looks like the performance overhead of dynamic binding is negligible in today's high performing computer. Even if it Dynamic Binding needs few more instruction to perform function call, the overhead in terms of time and memory is not much of a concern, but effective software design is a matter of concern. So we can conclude that Dynamic Binding does not cause serious performance problem in present day Computers.&lt;br /&gt;
&lt;br /&gt;
==Overriding and Overloading==&lt;br /&gt;
Overriding &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overriding]&amp;lt;/sup&amp;gt; and Overloading &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overloading]&amp;lt;/sup&amp;gt; are two different features of OOL. In function overloading, which version of function to call is decided by the compiler during the compile time itself, where as when a function is overridden, which version of the function to call is deferred till runtime.&lt;br /&gt;
&lt;br /&gt;
Let us see an two examples which provides insight into Overriding and Overloading. &lt;br /&gt;
&lt;br /&gt;
Example - 1: Overloading&lt;br /&gt;
&lt;br /&gt;
  public class A{&lt;br /&gt;
    public boolean equals( A check){                  # Equals method of parameter type A&lt;br /&gt;
      System.out.println( &amp;quot;A.equals method called&amp;quot;);&lt;br /&gt;
      return true;&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static void main(String [] args){          # Main method&lt;br /&gt;
      Object a1 = new A();                            # Instantiate class to get objects&lt;br /&gt;
      A a2 = new A();&lt;br /&gt;
      Object o1 = new Object();&lt;br /&gt;
 &lt;br /&gt;
      a1.equals(a2);                                  # Object equals method called&lt;br /&gt;
      a2.equals(a1);                                  # Object equals method called&lt;br /&gt;
      a2.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(o1);                                  # Object equals method called&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Example - 2: Overriding&lt;br /&gt;
&lt;br /&gt;
  public class A{&lt;br /&gt;
    @Override&lt;br /&gt;
    public boolean equals( Object check){             # Equals method of parameter type Object&lt;br /&gt;
      System.out.println( &amp;quot;A.equals method called&amp;quot;);&lt;br /&gt;
      return true;&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static void main(String [] args){          # Main method&lt;br /&gt;
      Object a1 = new A();                            # Instantiate class to get objects&lt;br /&gt;
      A a2 = new A();&lt;br /&gt;
      Object o1 = new Object();&lt;br /&gt;
 &lt;br /&gt;
      a1.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(a1);                                  # A equals method called&lt;br /&gt;
      a2.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(o1);                                  # A equals method called&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you see the comments provided to the side of the method calls, you would note the difference in the equals method called in case of both Overloading and Overriding. The decision which method to use, basically has two phases: First overload resolution, then method dispatch. Overload resolution happens at compile-time, method dispatch at runtime.&lt;br /&gt;
&lt;br /&gt;
==Does Dynamic Dispatching hurts in today's power packed Computers?==&lt;br /&gt;
&lt;br /&gt;
Even though Dynamic Dispatching is done in run-time, and it takes good amount of memory, today's computers are power packed with good amount of memory. Hence efficiency concerns for implementing Dynamic Dispatching in OOL is of little concern.&lt;br /&gt;
&lt;br /&gt;
However, the method to be called cannot be chosen based on the actual arguments passed to the function, rather, the method is called based on the declared type of the parameters.&lt;br /&gt;
&lt;br /&gt;
=Advantages of Dynamic Binding=&lt;br /&gt;
&lt;br /&gt;
Dynamic binding has several advantages. It provides tremendous flexibilities. Also, it allows the software to be malleable when requirements change as the system evolves. Because of dynamic binding, caller objects are not concerned how the invoked objects carry out their methods. All they need to know is that the invoked objects know how to carry out their responsibilities, but they themselves need to know only what the invoked objects can do for them. As a consequence of this, type dependencies (which are the bane of procedural programming) cannot have a ripple effect through the system, when system requirements change. The beauty is that such dependencies remain encapsulated within the objects. The flexibility that developers can derive out of this is enormous. For instance, one can install newer types without having to change or stopping the functioning of existing systems. This is something along the lines of &amp;quot;hot pluggable components&amp;quot; of the hardware cousins.&lt;br /&gt;
&lt;br /&gt;
One other advantage of Dynamic binding based on parameter types is that more than one parameter can be used in the selection of a method. Methods that use dynamic binding in this way are called multi-methods and the concept is called &amp;lt;b&amp;gt;Multiple Dispatch&amp;lt;/b&amp;gt; &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Multiple_dispatch]&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Multiple Dispatch==&lt;br /&gt;
&lt;br /&gt;
The Multiple Dispatch is used in Common Lisp Object System (CLOS) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Common_Lisp_Object_System]&amp;lt;/sup&amp;gt;. Multiple-polymorphism allows specialized functions or methods to be defined to handle various cases:&lt;br /&gt;
&lt;br /&gt;
  +(int, int)&lt;br /&gt;
  +(int, float)&lt;br /&gt;
  +(int, complex) .. etc&lt;br /&gt;
&lt;br /&gt;
The above functions are specialized to each of the cases required allowing single, highly cohesive and loosely coupled functions to be defined. This is also the true essence of object-oriented polymorphism, which allows objects to define methods for each specific case desired. In addition to better coupling and cohesion, multiple-polymorphism reduces program complexity by avoiding coding logic (switch statements) and because small methods further reduce complexity, as code complexity doesn't grow linearly with lines of code per method, but perhaps exponentially.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
Hence from the above discussion, we can see that Dynamic Dispatch, though slower when compared to static dispatch is no longer slower due to the new generation of power packed computers. Also, dynamic dispatch can help in preserving the object-oriented concepts in highly complex software products. We have also looked up the differences between method Overloading and Overriding. We also had a sneak-view to Multiple Dispatch where more than one parameter can be used for Dynamic method lookup.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overriding Method Overriding]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Code_segment Code Segment]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Virtual_method_table Virtual Method Table]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Virtual_function Virtual Functions]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc14cplr139.htm Virtual Functions in C++]. publib.boulder.ibm.com. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overloading Method Overloading]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Multiple_dispatch Multiple Dispatch]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;/div&gt;</summary>
		<author><name>Cgoogc</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39838</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39838"/>
		<updated>2010-11-03T01:33:05Z</updated>

		<summary type="html">&lt;p&gt;Cgoogc: /* Further Reading */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Dynamic Dispatch'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding the concept of Dynamic Dispatch in Computer Science. Here we will make an effort to address the efficiency considerations about Dynamic Dispatch, advantages of Dynamic Dispatch using parameter types and dynamic binding in CLOS (Common Lisp Object System).&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
Dynamic Dispatch is the process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function. It is generally used in &amp;lt;b&amp;gt;Object-Oriented Language&amp;lt;/b&amp;gt; (OOL) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Object-oriented_programming]&amp;lt;/sup&amp;gt;. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called as &amp;lt;b&amp;gt;Method Overriding&amp;lt;/b&amp;gt;  &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overriding]&amp;lt;/sup&amp;gt;). When a reference to the super class is used to execute that function, then which version of the function gets executed depends on the type, the reference to which it points to at that time rather than the type of the reference. In literature it also referred to in different names like &amp;lt;b&amp;gt;Runtime Binding&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;Dynamic Polymorphism&amp;lt;/b&amp;gt;. &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_polymorphism]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
   &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
==Need for Dynamic Dispatch==&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object-Oriented Language [1]. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
==How Dynamic Dispatch works?==&lt;br /&gt;
The implementation of Dynamic Dispatch varies significantly based on type language, in fact it changes even from Compiler to Compiler for a same language. We will consider how function are dispatch at runtime in both C and C++. We will try to understand if there is any overhead involved with virtual functions. &lt;br /&gt;
&lt;br /&gt;
In a machine compiled language like C, program logic is compiled into underlying processor instruction set by the compiler. Incase of x86 compiler, C code will be compiled into X86 instruction sets. The function will be compiled and placed in Code Segment &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Code_segment]&amp;lt;/sup&amp;gt; area of the process image. So a function in C will just map to a memory location in Code Segment of the process image. For example calling a function 'print()', might be converted to something like 'jmp 0xFFDE123' (jmp is a branching instruction). So a function call is in fact as simple as executing a jump statement (of course you push the arguments into stack before you call jump).&lt;br /&gt;
&lt;br /&gt;
Now lets consider a OOL like C++ which too compiles to hardware instruction set on compilation. Consider the below example of two classes which extend from a common class called Animal (not shown here) with one method talk. The below example just demonstrates how dynamic dispatch could be implemented by a compiler. &lt;br /&gt;
&lt;br /&gt;
A word of caution before you proceed; the implementation is hypothetical and far from any practical use. The objective of the example shown below is just to give an idea of how Dynamic Dispatch might be implemented. Practical implementations are lot more complicated, so we have taken this simple example for easy explanation and understanding. &lt;br /&gt;
&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
The above C++ program might infact be converted to an equivalent C program by the compiler during pre-compilation. Each newly defined structure gets an __id__ and this id will be used to lookup vtable (Virtual Method Table &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_method_table]&amp;lt;/sup&amp;gt;) which maps id to the actual function memory location. One we get the memory location we will execute a jump instruction like we did before.&lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
  &lt;br /&gt;
 &lt;br /&gt;
 // C++ function call like &lt;br /&gt;
 Animal *d = new Dog(); &lt;br /&gt;
 d-&amp;gt;talk();&lt;br /&gt;
 &lt;br /&gt;
 // might be changed like this by the compiler&lt;br /&gt;
 Dog *d = malloc(sizeof(Dog));&lt;br /&gt;
 d-&amp;gt;__id__ = 1;&lt;br /&gt;
 talk(d);&lt;br /&gt;
 &lt;br /&gt;
 // talk function can use the __id__ attribute to offset into the vtable to identify which version of function to call.&lt;br /&gt;
&lt;br /&gt;
[[Image:Vtable.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;text-align: center;&amp;quot;&amp;gt; Fig.1 - VTable Working &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Virtual Method table or vtable or dispatch table [5] is the key to virtual function working. It is evident from the above that vtable infact makes the function calling little more expensive compared to normal function calls.&lt;br /&gt;
&lt;br /&gt;
=Performance Evaluation=&lt;br /&gt;
From the above explanation it is evident that Virtual functions &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_function]     [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc14cplr139.htm]&amp;lt;/sup&amp;gt; are more complicated that dispatching normal function calls. For example Virtual functions in C++ will need extra memory for each class to store the Virtual method table [5] and needs additional CPU cycles to perform vtable lookup, before it can make the actual function call. &lt;br /&gt;
&lt;br /&gt;
This is one of the important reasons why not all functions are dynamically binded in C++. In C++, functions which are explicitly marked virtual are dynamically bound whereas remaining functions are statically bound during the compile time. When we say bound/bind we are actually talking about the mapping between the function name and memory location where it is stored for execution.&lt;br /&gt;
&lt;br /&gt;
In languages like Java, the creators thought dynamic binding being an essential feature of OOL, hence, all functions are dynamically bound. It is also reasonable to assume that virtual functions might need 20 to 40 extra instruction sets to execute a function call. Given today's processors speed this might be very insignificant.&lt;br /&gt;
&lt;br /&gt;
We performed a very small experiment to see if Dynamic dispatch is practically slower than static dispatch. We opted to choose C#, since this language provide both static binding and dynamic binding. We measured the time taken to execute 1000000000 dynamically dispatched functions and statically dispatched functions. Below are our observations.&lt;br /&gt;
&lt;br /&gt;
 Dynamic Dispatch:&lt;br /&gt;
 00:00:10.0995777&lt;br /&gt;
 00:00:10.4315966&lt;br /&gt;
 00:00:10.4675987&lt;br /&gt;
&lt;br /&gt;
 Static Dispatch:&lt;br /&gt;
 00:00:10.2565866&lt;br /&gt;
 00:00:10.3255906&lt;br /&gt;
 00:00:10.1575809&lt;br /&gt;
&lt;br /&gt;
It looks like the performance overhead of dynamic binding is negligible in today's high performing computer. Even if it Dynamic Binding needs few more instruction to perform function call, the overhead in terms of time and memory is not much of a concern, but effective software design is a matter of concern. So we can conclude that Dynamic Binding does not cause serious performance problem in present day Computers.&lt;br /&gt;
&lt;br /&gt;
==Overriding and Overloading==&lt;br /&gt;
Overriding &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overriding]&amp;lt;/sup&amp;gt; and Overloading &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overloading]&amp;lt;/sup&amp;gt; are two different features of OOL. In function overloading, which version of function to call is decided by the compiler during the compile time itself, where as when a function is overridden, which version of the function to call is deferred till runtime.&lt;br /&gt;
&lt;br /&gt;
Let us see an two examples which provides insight into Overriding and Overloading. &lt;br /&gt;
&lt;br /&gt;
Example - 1: Overloading&lt;br /&gt;
&lt;br /&gt;
  public class A{&lt;br /&gt;
    public boolean equals( A check){                  # Equals method of parameter type A&lt;br /&gt;
      System.out.println( &amp;quot;A.equals method called&amp;quot;);&lt;br /&gt;
      return true;&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static void main(String [] args){          # Main method&lt;br /&gt;
      Object a1 = new A();                            # Instantiate class to get objects&lt;br /&gt;
      A a2 = new A();&lt;br /&gt;
      Object o1 = new Object();&lt;br /&gt;
 &lt;br /&gt;
      a1.equals(a2);                                  # Object equals method called&lt;br /&gt;
      a2.equals(a1);                                  # Object equals method called&lt;br /&gt;
      a2.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(o1);                                  # Object equals method called&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Example - 2: Overriding&lt;br /&gt;
&lt;br /&gt;
  public class A{&lt;br /&gt;
    @Override&lt;br /&gt;
    public boolean equals( Object check){             # Equals method of parameter type Object&lt;br /&gt;
      System.out.println( &amp;quot;A.equals method called&amp;quot;);&lt;br /&gt;
      return true;&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static void main(String [] args){          # Main method&lt;br /&gt;
      Object a1 = new A();                            # Instantiate class to get objects&lt;br /&gt;
      A a2 = new A();&lt;br /&gt;
      Object o1 = new Object();&lt;br /&gt;
 &lt;br /&gt;
      a1.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(a1);                                  # A equals method called&lt;br /&gt;
      a2.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(o1);                                  # A equals method called&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you see the comments provided to the side of the method calls, you would note the difference in the equals method called in case of both Overloading and Overriding. The decision which method to use, basically has two phases: First overload resolution, then method dispatch. Overload resolution happens at compile-time, method dispatch at runtime.&lt;br /&gt;
&lt;br /&gt;
==Does Dynamic Dispatching hurts in today's power packed Computers?==&lt;br /&gt;
&lt;br /&gt;
Even though Dynamic Dispatching is done in run-time, and it takes good amount of memory, today's computers are power packed with good amount of memory. Hence efficiency concerns for implementing Dynamic Dispatching in OOL is of little concern.&lt;br /&gt;
&lt;br /&gt;
However, the method to be called cannot be chosen based on the actual arguments passed to the function, rather, the method is called based on the declared type of the parameters.&lt;br /&gt;
&lt;br /&gt;
=Advantages of Dynamic Binding=&lt;br /&gt;
&lt;br /&gt;
Dynamic binding has several advantages. It provides tremendous flexibilities. Also, it allows the software to be malleable when requirements change as the system evolves. Because of dynamic binding, caller objects are not concerned how the invoked objects carry out their methods. All they need to know is that the invoked objects know how to carry out their responsibilities, but they themselves need to know only what the invoked objects can do for them. As a consequence of this, type dependencies (which are the bane of procedural programming) cannot have a ripple effect through the system, when system requirements change. The beauty is that such dependencies remain encapsulated within the objects. The flexibility that developers can derive out of this is enormous. For instance, one can install newer types without having to change or stopping the functioning of existing systems. This is something along the lines of &amp;quot;hot pluggable components&amp;quot; of the hardware cousins.&lt;br /&gt;
&lt;br /&gt;
One other advantage of Dynamic binding based on parameter types is that more than one parameter can be used in the selection of a method. Methods that use dynamic binding in this way are called multi-methods and the concept is called &amp;lt;b&amp;gt;Multiple Dispatch&amp;lt;/b&amp;gt; &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Multiple_dispatch]&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Multiple Dispatch==&lt;br /&gt;
&lt;br /&gt;
The Multiple Dispatch is used in Common Lisp Object System (CLOS) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Common_Lisp_Object_System]&amp;lt;/sup&amp;gt;. Multiple-polymorphism allows specialized functions or methods to be defined to handle various cases:&lt;br /&gt;
&lt;br /&gt;
  +(int, int)&lt;br /&gt;
  +(int, float)&lt;br /&gt;
  +(int, complex) .. etc&lt;br /&gt;
&lt;br /&gt;
The above functions are specialized to each of the cases required allowing single, highly cohesive and loosely coupled functions to be defined. This is also the true essence of object-oriented polymorphism, which allows objects to define methods for each specific case desired. In addition to better coupling and cohesion, multiple-polymorphism reduces program complexity by avoiding coding logic (switch statements) and because small methods further reduce complexity, as code complexity doesn't grow linearly with lines of code per method, but perhaps exponentially.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
Hence from the above discussion, we can see that Dynamic Dispatch, though slower when compared to static dispatch is no longer slower due to the new generation of power packed computers. Also, dynamic dispatch can help in preserving the object-oriented concepts in highly complex software products. We have also looked up the differences between method Overloading and Overriding. We also had a sneak-view to Multiple Dispatch where more than one parameter can be used for Dynamic method lookup.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overriding Method Overriding]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Code_segment Code Segment]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Virtual_method_table Virtual Method Table]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Virtual_function Virtual Functions]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc14cplr139.htm Virtual Functions in C++]. publib.boulder.ibm.com. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overloading Method Overloading]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Multiple_dispatch Multiple Dispatch]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;/div&gt;</summary>
		<author><name>Cgoogc</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39837</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39837"/>
		<updated>2010-11-03T01:32:14Z</updated>

		<summary type="html">&lt;p&gt;Cgoogc: /* Overriding and Overloading */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Dynamic Dispatch'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding the concept of Dynamic Dispatch in Computer Science. Here we will make an effort to address the efficiency considerations about Dynamic Dispatch, advantages of Dynamic Dispatch using parameter types and dynamic binding in CLOS (Common Lisp Object System).&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
Dynamic Dispatch is the process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function. It is generally used in &amp;lt;b&amp;gt;Object-Oriented Language&amp;lt;/b&amp;gt; (OOL) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Object-oriented_programming]&amp;lt;/sup&amp;gt;. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called as &amp;lt;b&amp;gt;Method Overriding&amp;lt;/b&amp;gt;  &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overriding]&amp;lt;/sup&amp;gt;). When a reference to the super class is used to execute that function, then which version of the function gets executed depends on the type, the reference to which it points to at that time rather than the type of the reference. In literature it also referred to in different names like &amp;lt;b&amp;gt;Runtime Binding&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;Dynamic Polymorphism&amp;lt;/b&amp;gt;. &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_polymorphism]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
   &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
==Need for Dynamic Dispatch==&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object-Oriented Language [1]. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
==How Dynamic Dispatch works?==&lt;br /&gt;
The implementation of Dynamic Dispatch varies significantly based on type language, in fact it changes even from Compiler to Compiler for a same language. We will consider how function are dispatch at runtime in both C and C++. We will try to understand if there is any overhead involved with virtual functions. &lt;br /&gt;
&lt;br /&gt;
In a machine compiled language like C, program logic is compiled into underlying processor instruction set by the compiler. Incase of x86 compiler, C code will be compiled into X86 instruction sets. The function will be compiled and placed in Code Segment &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Code_segment]&amp;lt;/sup&amp;gt; area of the process image. So a function in C will just map to a memory location in Code Segment of the process image. For example calling a function 'print()', might be converted to something like 'jmp 0xFFDE123' (jmp is a branching instruction). So a function call is in fact as simple as executing a jump statement (of course you push the arguments into stack before you call jump).&lt;br /&gt;
&lt;br /&gt;
Now lets consider a OOL like C++ which too compiles to hardware instruction set on compilation. Consider the below example of two classes which extend from a common class called Animal (not shown here) with one method talk. The below example just demonstrates how dynamic dispatch could be implemented by a compiler. &lt;br /&gt;
&lt;br /&gt;
A word of caution before you proceed; the implementation is hypothetical and far from any practical use. The objective of the example shown below is just to give an idea of how Dynamic Dispatch might be implemented. Practical implementations are lot more complicated, so we have taken this simple example for easy explanation and understanding. &lt;br /&gt;
&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
The above C++ program might infact be converted to an equivalent C program by the compiler during pre-compilation. Each newly defined structure gets an __id__ and this id will be used to lookup vtable (Virtual Method Table &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_method_table]&amp;lt;/sup&amp;gt;) which maps id to the actual function memory location. One we get the memory location we will execute a jump instruction like we did before.&lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
  &lt;br /&gt;
 &lt;br /&gt;
 // C++ function call like &lt;br /&gt;
 Animal *d = new Dog(); &lt;br /&gt;
 d-&amp;gt;talk();&lt;br /&gt;
 &lt;br /&gt;
 // might be changed like this by the compiler&lt;br /&gt;
 Dog *d = malloc(sizeof(Dog));&lt;br /&gt;
 d-&amp;gt;__id__ = 1;&lt;br /&gt;
 talk(d);&lt;br /&gt;
 &lt;br /&gt;
 // talk function can use the __id__ attribute to offset into the vtable to identify which version of function to call.&lt;br /&gt;
&lt;br /&gt;
[[Image:Vtable.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;text-align: center;&amp;quot;&amp;gt; Fig.1 - VTable Working &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Virtual Method table or vtable or dispatch table [5] is the key to virtual function working. It is evident from the above that vtable infact makes the function calling little more expensive compared to normal function calls.&lt;br /&gt;
&lt;br /&gt;
=Performance Evaluation=&lt;br /&gt;
From the above explanation it is evident that Virtual functions &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_function]     [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc14cplr139.htm]&amp;lt;/sup&amp;gt; are more complicated that dispatching normal function calls. For example Virtual functions in C++ will need extra memory for each class to store the Virtual method table [5] and needs additional CPU cycles to perform vtable lookup, before it can make the actual function call. &lt;br /&gt;
&lt;br /&gt;
This is one of the important reasons why not all functions are dynamically binded in C++. In C++, functions which are explicitly marked virtual are dynamically bound whereas remaining functions are statically bound during the compile time. When we say bound/bind we are actually talking about the mapping between the function name and memory location where it is stored for execution.&lt;br /&gt;
&lt;br /&gt;
In languages like Java, the creators thought dynamic binding being an essential feature of OOL, hence, all functions are dynamically bound. It is also reasonable to assume that virtual functions might need 20 to 40 extra instruction sets to execute a function call. Given today's processors speed this might be very insignificant.&lt;br /&gt;
&lt;br /&gt;
We performed a very small experiment to see if Dynamic dispatch is practically slower than static dispatch. We opted to choose C#, since this language provide both static binding and dynamic binding. We measured the time taken to execute 1000000000 dynamically dispatched functions and statically dispatched functions. Below are our observations.&lt;br /&gt;
&lt;br /&gt;
 Dynamic Dispatch:&lt;br /&gt;
 00:00:10.0995777&lt;br /&gt;
 00:00:10.4315966&lt;br /&gt;
 00:00:10.4675987&lt;br /&gt;
&lt;br /&gt;
 Static Dispatch:&lt;br /&gt;
 00:00:10.2565866&lt;br /&gt;
 00:00:10.3255906&lt;br /&gt;
 00:00:10.1575809&lt;br /&gt;
&lt;br /&gt;
It looks like the performance overhead of dynamic binding is negligible in today's high performing computer. Even if it Dynamic Binding needs few more instruction to perform function call, the overhead in terms of time and memory is not much of a concern, but effective software design is a matter of concern. So we can conclude that Dynamic Binding does not cause serious performance problem in present day Computers.&lt;br /&gt;
&lt;br /&gt;
==Overriding and Overloading==&lt;br /&gt;
Overriding &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overriding]&amp;lt;/sup&amp;gt; and Overloading &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overloading]&amp;lt;/sup&amp;gt; are two different features of OOL. In function overloading, which version of function to call is decided by the compiler during the compile time itself, where as when a function is overridden, which version of the function to call is deferred till runtime.&lt;br /&gt;
&lt;br /&gt;
Let us see an two examples which provides insight into Overriding and Overloading. &lt;br /&gt;
&lt;br /&gt;
Example - 1: Overloading&lt;br /&gt;
&lt;br /&gt;
  public class A{&lt;br /&gt;
    public boolean equals( A check){                  # Equals method of parameter type A&lt;br /&gt;
      System.out.println( &amp;quot;A.equals method called&amp;quot;);&lt;br /&gt;
      return true;&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static void main(String [] args){          # Main method&lt;br /&gt;
      Object a1 = new A();                            # Instantiate class to get objects&lt;br /&gt;
      A a2 = new A();&lt;br /&gt;
      Object o1 = new Object();&lt;br /&gt;
 &lt;br /&gt;
      a1.equals(a2);                                  # Object equals method called&lt;br /&gt;
      a2.equals(a1);                                  # Object equals method called&lt;br /&gt;
      a2.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(o1);                                  # Object equals method called&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Example - 2: Overriding&lt;br /&gt;
&lt;br /&gt;
  public class A{&lt;br /&gt;
    @Override&lt;br /&gt;
    public boolean equals( Object check){             # Equals method of parameter type Object&lt;br /&gt;
      System.out.println( &amp;quot;A.equals method called&amp;quot;);&lt;br /&gt;
      return true;&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static void main(String [] args){          # Main method&lt;br /&gt;
      Object a1 = new A();                            # Instantiate class to get objects&lt;br /&gt;
      A a2 = new A();&lt;br /&gt;
      Object o1 = new Object();&lt;br /&gt;
 &lt;br /&gt;
      a1.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(a1);                                  # A equals method called&lt;br /&gt;
      a2.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(o1);                                  # A equals method called&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you see the comments provided to the side of the method calls, you would note the difference in the equals method called in case of both Overloading and Overriding. The decision which method to use, basically has two phases: First overload resolution, then method dispatch. Overload resolution happens at compile-time, method dispatch at runtime.&lt;br /&gt;
&lt;br /&gt;
==Does Dynamic Dispatching hurts in today's power packed Computers?==&lt;br /&gt;
&lt;br /&gt;
Even though Dynamic Dispatching is done in run-time, and it takes good amount of memory, today's computers are power packed with good amount of memory. Hence efficiency concerns for implementing Dynamic Dispatching in OOL is of little concern.&lt;br /&gt;
&lt;br /&gt;
However, the method to be called cannot be chosen based on the actual arguments passed to the function, rather, the method is called based on the declared type of the parameters.&lt;br /&gt;
&lt;br /&gt;
=Advantages of Dynamic Binding=&lt;br /&gt;
&lt;br /&gt;
Dynamic binding has several advantages. It provides tremendous flexibilities. Also, it allows the software to be malleable when requirements change as the system evolves. Because of dynamic binding, caller objects are not concerned how the invoked objects carry out their methods. All they need to know is that the invoked objects know how to carry out their responsibilities, but they themselves need to know only what the invoked objects can do for them. As a consequence of this, type dependencies (which are the bane of procedural programming) cannot have a ripple effect through the system, when system requirements change. The beauty is that such dependencies remain encapsulated within the objects. The flexibility that developers can derive out of this is enormous. For instance, one can install newer types without having to change or stopping the functioning of existing systems. This is something along the lines of &amp;quot;hot pluggable components&amp;quot; of the hardware cousins.&lt;br /&gt;
&lt;br /&gt;
One other advantage of Dynamic binding based on parameter types is that more than one parameter can be used in the selection of a method. Methods that use dynamic binding in this way are called multi-methods and the concept is called &amp;lt;b&amp;gt;Multiple Dispatch&amp;lt;/b&amp;gt; &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Multiple_dispatch]&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Multiple Dispatch==&lt;br /&gt;
&lt;br /&gt;
The Multiple Dispatch is used in Common Lisp Object System (CLOS) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Common_Lisp_Object_System]&amp;lt;/sup&amp;gt;. Multiple-polymorphism allows specialized functions or methods to be defined to handle various cases:&lt;br /&gt;
&lt;br /&gt;
  +(int, int)&lt;br /&gt;
  +(int, float)&lt;br /&gt;
  +(int, complex) .. etc&lt;br /&gt;
&lt;br /&gt;
The above functions are specialized to each of the cases required allowing single, highly cohesive and loosely coupled functions to be defined. This is also the true essence of object-oriented polymorphism, which allows objects to define methods for each specific case desired. In addition to better coupling and cohesion, multiple-polymorphism reduces program complexity by avoiding coding logic (switch statements) and because small methods further reduce complexity, as code complexity doesn't grow linearly with lines of code per method, but perhaps exponentially.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
Hence from the above discussion, we can see that Dynamic Dispatch, though slower when compared to static dispatch is no longer slower due to the new generation of power packed computers. Also, dynamic dispatch can help in preserving the object-oriented concepts in highly complex software products. We have also looked up the differences between method Overloading and Overriding. We also had a sneak-view to Multiple Dispatch where more than one parameter can be used for Dynamic method lookup.&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overriding Method Overriding]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Code_segment Code Segment]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Virtual_method_table Virtual Method Table]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Virtual_function Virtual Functions]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc14cplr139.htm Virtual Functions in C++]. publib.boulder.ibm.com. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overloading Method Overloading]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Multiple_dispatch Multiple Dispatch]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;/div&gt;</summary>
		<author><name>Cgoogc</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39836</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39836"/>
		<updated>2010-11-03T01:31:27Z</updated>

		<summary type="html">&lt;p&gt;Cgoogc: /* Performance Evaluation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Dynamic Dispatch'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding the concept of Dynamic Dispatch in Computer Science. Here we will make an effort to address the efficiency considerations about Dynamic Dispatch, advantages of Dynamic Dispatch using parameter types and dynamic binding in CLOS (Common Lisp Object System).&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
Dynamic Dispatch is the process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function. It is generally used in &amp;lt;b&amp;gt;Object-Oriented Language&amp;lt;/b&amp;gt; (OOL) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Object-oriented_programming]&amp;lt;/sup&amp;gt;. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called as &amp;lt;b&amp;gt;Method Overriding&amp;lt;/b&amp;gt;  &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overriding]&amp;lt;/sup&amp;gt;). When a reference to the super class is used to execute that function, then which version of the function gets executed depends on the type, the reference to which it points to at that time rather than the type of the reference. In literature it also referred to in different names like &amp;lt;b&amp;gt;Runtime Binding&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;Dynamic Polymorphism&amp;lt;/b&amp;gt;. &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_polymorphism]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
   &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
==Need for Dynamic Dispatch==&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object-Oriented Language [1]. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
==How Dynamic Dispatch works?==&lt;br /&gt;
The implementation of Dynamic Dispatch varies significantly based on type language, in fact it changes even from Compiler to Compiler for a same language. We will consider how function are dispatch at runtime in both C and C++. We will try to understand if there is any overhead involved with virtual functions. &lt;br /&gt;
&lt;br /&gt;
In a machine compiled language like C, program logic is compiled into underlying processor instruction set by the compiler. Incase of x86 compiler, C code will be compiled into X86 instruction sets. The function will be compiled and placed in Code Segment &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Code_segment]&amp;lt;/sup&amp;gt; area of the process image. So a function in C will just map to a memory location in Code Segment of the process image. For example calling a function 'print()', might be converted to something like 'jmp 0xFFDE123' (jmp is a branching instruction). So a function call is in fact as simple as executing a jump statement (of course you push the arguments into stack before you call jump).&lt;br /&gt;
&lt;br /&gt;
Now lets consider a OOL like C++ which too compiles to hardware instruction set on compilation. Consider the below example of two classes which extend from a common class called Animal (not shown here) with one method talk. The below example just demonstrates how dynamic dispatch could be implemented by a compiler. &lt;br /&gt;
&lt;br /&gt;
A word of caution before you proceed; the implementation is hypothetical and far from any practical use. The objective of the example shown below is just to give an idea of how Dynamic Dispatch might be implemented. Practical implementations are lot more complicated, so we have taken this simple example for easy explanation and understanding. &lt;br /&gt;
&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
The above C++ program might infact be converted to an equivalent C program by the compiler during pre-compilation. Each newly defined structure gets an __id__ and this id will be used to lookup vtable (Virtual Method Table &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_method_table]&amp;lt;/sup&amp;gt;) which maps id to the actual function memory location. One we get the memory location we will execute a jump instruction like we did before.&lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
  &lt;br /&gt;
 &lt;br /&gt;
 // C++ function call like &lt;br /&gt;
 Animal *d = new Dog(); &lt;br /&gt;
 d-&amp;gt;talk();&lt;br /&gt;
 &lt;br /&gt;
 // might be changed like this by the compiler&lt;br /&gt;
 Dog *d = malloc(sizeof(Dog));&lt;br /&gt;
 d-&amp;gt;__id__ = 1;&lt;br /&gt;
 talk(d);&lt;br /&gt;
 &lt;br /&gt;
 // talk function can use the __id__ attribute to offset into the vtable to identify which version of function to call.&lt;br /&gt;
&lt;br /&gt;
[[Image:Vtable.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;text-align: center;&amp;quot;&amp;gt; Fig.1 - VTable Working &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Virtual Method table or vtable or dispatch table [5] is the key to virtual function working. It is evident from the above that vtable infact makes the function calling little more expensive compared to normal function calls.&lt;br /&gt;
&lt;br /&gt;
=Performance Evaluation=&lt;br /&gt;
From the above explanation it is evident that Virtual functions &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_function]     [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc14cplr139.htm]&amp;lt;/sup&amp;gt; are more complicated that dispatching normal function calls. For example Virtual functions in C++ will need extra memory for each class to store the Virtual method table [5] and needs additional CPU cycles to perform vtable lookup, before it can make the actual function call. &lt;br /&gt;
&lt;br /&gt;
This is one of the important reasons why not all functions are dynamically binded in C++. In C++, functions which are explicitly marked virtual are dynamically bound whereas remaining functions are statically bound during the compile time. When we say bound/bind we are actually talking about the mapping between the function name and memory location where it is stored for execution.&lt;br /&gt;
&lt;br /&gt;
In languages like Java, the creators thought dynamic binding being an essential feature of OOL, hence, all functions are dynamically bound. It is also reasonable to assume that virtual functions might need 20 to 40 extra instruction sets to execute a function call. Given today's processors speed this might be very insignificant.&lt;br /&gt;
&lt;br /&gt;
We performed a very small experiment to see if Dynamic dispatch is practically slower than static dispatch. We opted to choose C#, since this language provide both static binding and dynamic binding. We measured the time taken to execute 1000000000 dynamically dispatched functions and statically dispatched functions. Below are our observations.&lt;br /&gt;
&lt;br /&gt;
 Dynamic Dispatch:&lt;br /&gt;
 00:00:10.0995777&lt;br /&gt;
 00:00:10.4315966&lt;br /&gt;
 00:00:10.4675987&lt;br /&gt;
&lt;br /&gt;
 Static Dispatch:&lt;br /&gt;
 00:00:10.2565866&lt;br /&gt;
 00:00:10.3255906&lt;br /&gt;
 00:00:10.1575809&lt;br /&gt;
&lt;br /&gt;
It looks like the performance overhead of dynamic binding is negligible in today's high performing computer. Even if it Dynamic Binding needs few more instruction to perform function call, the overhead in terms of time and memory is not much of a concern, but effective software design is a matter of concern. So we can conclude that Dynamic Binding does not cause serious performance problem in present day Computers.&lt;br /&gt;
&lt;br /&gt;
==Overriding and Overloading==&lt;br /&gt;
Overriding [2] and Overloading &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overloading]&amp;lt;/sup&amp;gt; are two different features of OOL. In function overloading, which version of function to call is decided by the compiler during the compile time itself, where as when a function is overridden, which version of the function to call is deferred till runtime.&lt;br /&gt;
&lt;br /&gt;
Let us see an two examples which provides insight into Overriding and Overloading. &lt;br /&gt;
&lt;br /&gt;
Example - 1: Overloading&lt;br /&gt;
&lt;br /&gt;
  public class A{&lt;br /&gt;
    public boolean equals( A check){                  # Equals method of parameter type A&lt;br /&gt;
      System.out.println( &amp;quot;A.equals method called&amp;quot;);&lt;br /&gt;
      return true;&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static void main(String [] args){          # Main method&lt;br /&gt;
      Object a1 = new A();                            # Instantiate class to get objects&lt;br /&gt;
      A a2 = new A();&lt;br /&gt;
      Object o1 = new Object();&lt;br /&gt;
 &lt;br /&gt;
      a1.equals(a2);                                  # Object equals method called&lt;br /&gt;
      a2.equals(a1);                                  # Object equals method called&lt;br /&gt;
      a2.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(o1);                                  # Object equals method called&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Example - 2: Overriding&lt;br /&gt;
&lt;br /&gt;
  public class A{&lt;br /&gt;
    @Override&lt;br /&gt;
    public boolean equals( Object check){             # Equals method of parameter type Object&lt;br /&gt;
      System.out.println( &amp;quot;A.equals method called&amp;quot;);&lt;br /&gt;
      return true;&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static void main(String [] args){          # Main method&lt;br /&gt;
      Object a1 = new A();                            # Instantiate class to get objects&lt;br /&gt;
      A a2 = new A();&lt;br /&gt;
      Object o1 = new Object();&lt;br /&gt;
 &lt;br /&gt;
      a1.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(a1);                                  # A equals method called&lt;br /&gt;
      a2.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(o1);                                  # A equals method called&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you see the comments provided to the side of the method calls, you would note the difference in the equals method called in case of both Overloading and Overriding. The decision which method to use, basically has two phases: First overload resolution, then method dispatch. Overload resolution happens at compile-time, method dispatch at runtime.&lt;br /&gt;
&lt;br /&gt;
==Does Dynamic Dispatching hurts in today's power packed Computers?==&lt;br /&gt;
&lt;br /&gt;
Even though Dynamic Dispatching is done in run-time, and it takes good amount of memory, today's computers are power packed with good amount of memory. Hence efficiency concerns for implementing Dynamic Dispatching in OOL is of little concern.&lt;br /&gt;
&lt;br /&gt;
However, the method to be called cannot be chosen based on the actual arguments passed to the function, rather, the method is called based on the declared type of the parameters.&lt;br /&gt;
&lt;br /&gt;
=Advantages of Dynamic Binding=&lt;br /&gt;
&lt;br /&gt;
Dynamic binding has several advantages. It provides tremendous flexibilities. Also, it allows the software to be malleable when requirements change as the system evolves. Because of dynamic binding, caller objects are not concerned how the invoked objects carry out their methods. All they need to know is that the invoked objects know how to carry out their responsibilities, but they themselves need to know only what the invoked objects can do for them. As a consequence of this, type dependencies (which are the bane of procedural programming) cannot have a ripple effect through the system, when system requirements change. The beauty is that such dependencies remain encapsulated within the objects. The flexibility that developers can derive out of this is enormous. For instance, one can install newer types without having to change or stopping the functioning of existing systems. This is something along the lines of &amp;quot;hot pluggable components&amp;quot; of the hardware cousins.&lt;br /&gt;
&lt;br /&gt;
One other advantage of Dynamic binding based on parameter types is that more than one parameter can be used in the selection of a method. Methods that use dynamic binding in this way are called multi-methods and the concept is called &amp;lt;b&amp;gt;Multiple Dispatch&amp;lt;/b&amp;gt; &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Multiple_dispatch]&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Multiple Dispatch==&lt;br /&gt;
&lt;br /&gt;
The Multiple Dispatch is used in Common Lisp Object System (CLOS) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Common_Lisp_Object_System]&amp;lt;/sup&amp;gt;. Multiple-polymorphism allows specialized functions or methods to be defined to handle various cases:&lt;br /&gt;
&lt;br /&gt;
  +(int, int)&lt;br /&gt;
  +(int, float)&lt;br /&gt;
  +(int, complex) .. etc&lt;br /&gt;
&lt;br /&gt;
The above functions are specialized to each of the cases required allowing single, highly cohesive and loosely coupled functions to be defined. This is also the true essence of object-oriented polymorphism, which allows objects to define methods for each specific case desired. In addition to better coupling and cohesion, multiple-polymorphism reduces program complexity by avoiding coding logic (switch statements) and because small methods further reduce complexity, as code complexity doesn't grow linearly with lines of code per method, but perhaps exponentially.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
Hence from the above discussion, we can see that Dynamic Dispatch, though slower when compared to static dispatch is no longer slower due to the new generation of power packed computers. Also, dynamic dispatch can help in preserving the object-oriented concepts in highly complex software products. We have also looked up the differences between method Overloading and Overriding. We also had a sneak-view to Multiple Dispatch where more than one parameter can be used for Dynamic method lookup.&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overriding Method Overriding]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Code_segment Code Segment]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Virtual_method_table Virtual Method Table]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Virtual_function Virtual Functions]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc14cplr139.htm Virtual Functions in C++]. publib.boulder.ibm.com. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overloading Method Overloading]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Multiple_dispatch Multiple Dispatch]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;/div&gt;</summary>
		<author><name>Cgoogc</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39834</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39834"/>
		<updated>2010-11-03T01:24:54Z</updated>

		<summary type="html">&lt;p&gt;Cgoogc: /* Overriding and Overloading */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Dynamic Dispatch'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding the concept of Dynamic Dispatch in Computer Science. Here we will make an effort to address the efficiency considerations about Dynamic Dispatch, advantages of Dynamic Dispatch using parameter types and dynamic binding in CLOS (Common Lisp Object System).&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
Dynamic Dispatch is the process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function. It is generally used in &amp;lt;b&amp;gt;Object-Oriented Language&amp;lt;/b&amp;gt; (OOL) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Object-oriented_programming]&amp;lt;/sup&amp;gt;. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called as &amp;lt;b&amp;gt;Method Overriding&amp;lt;/b&amp;gt;  &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overriding]&amp;lt;/sup&amp;gt;). When a reference to the super class is used to execute that function, then which version of the function gets executed depends on the type, the reference to which it points to at that time rather than the type of the reference. In literature it also referred to in different names like &amp;lt;b&amp;gt;Runtime Binding&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;Dynamic Polymorphism&amp;lt;/b&amp;gt;. &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_polymorphism]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
   &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
==Need for Dynamic Dispatch==&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object-Oriented Language [1]. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
==How Dynamic Dispatch works?==&lt;br /&gt;
The implementation of Dynamic Dispatch varies significantly based on type language, in fact it changes even from Compiler to Compiler for a same language. We will consider how function are dispatch at runtime in both C and C++. We will try to understand if there is any overhead involved with virtual functions. &lt;br /&gt;
&lt;br /&gt;
In a machine compiled language like C, program logic is compiled into underlying processor instruction set by the compiler. Incase of x86 compiler, C code will be compiled into X86 instruction sets. The function will be compiled and placed in Code Segment &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Code_segment]&amp;lt;/sup&amp;gt; area of the process image. So a function in C will just map to a memory location in Code Segment of the process image. For example calling a function 'print()', might be converted to something like 'jmp 0xFFDE123' (jmp is a branching instruction). So a function call is in fact as simple as executing a jump statement (of course you push the arguments into stack before you call jump).&lt;br /&gt;
&lt;br /&gt;
Now lets consider a OOL like C++ which too compiles to hardware instruction set on compilation. Consider the below example of two classes which extend from a common class called Animal (not shown here) with one method talk. The below example just demonstrates how dynamic dispatch could be implemented by a compiler. &lt;br /&gt;
&lt;br /&gt;
A word of caution before you proceed; the implementation is hypothetical and far from any practical use. The objective of the example shown below is just to give an idea of how Dynamic Dispatch might be implemented. Practical implementations are lot more complicated, so we have taken this simple example for easy explanation and understanding. &lt;br /&gt;
&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
The above C++ program might infact be converted to an equivalent C program by the compiler during pre-compilation. Each newly defined structure gets an __id__ and this id will be used to lookup vtable (Virtual Method Table &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_method_table]&amp;lt;/sup&amp;gt;) which maps id to the actual function memory location. One we get the memory location we will execute a jump instruction like we did before.&lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
  &lt;br /&gt;
 &lt;br /&gt;
 // C++ function call like &lt;br /&gt;
 Animal *d = new Dog(); &lt;br /&gt;
 d-&amp;gt;talk();&lt;br /&gt;
 &lt;br /&gt;
 // might be changed like this by the compiler&lt;br /&gt;
 Dog *d = malloc(sizeof(Dog));&lt;br /&gt;
 d-&amp;gt;__id__ = 1;&lt;br /&gt;
 talk(d);&lt;br /&gt;
 &lt;br /&gt;
 // talk function can use the __id__ attribute to offset into the vtable to identify which version of function to call.&lt;br /&gt;
&lt;br /&gt;
[[Image:Vtable.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;text-align: center;&amp;quot;&amp;gt; Fig.1 - VTable Working &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Virtual Method table or vtable or dispatch table [5] is the key to virtual function working. It is evident from the above that vtable infact makes the function calling little more expensive compared to normal function calls.&lt;br /&gt;
&lt;br /&gt;
=Performance Evaluation=&lt;br /&gt;
From the above explanation it is evident that Virtual functions &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_function]     [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc14cplr139.htm]&amp;lt;/sup&amp;gt; are more complicated that dispatching normal function calls. For example Virtual functions in C++ will need extra memory for each class to store the Virtual method table [5] and needs additional CPU cycles to perform vtable lookup, before it can make the actual function call. &lt;br /&gt;
&lt;br /&gt;
This is one of the important reasons why not all functions are dynamically binded in C++. In C++, functions which are explicitly marked virtual are dynamically bound whereas remaining functions are statically bound during the compile time. When we say bound/bind we are actually talking about the mapping between the function name and memory location where it is stored for execution.&lt;br /&gt;
&lt;br /&gt;
In languages like Java, the creators thought dynamic binding being an essential feature of OOL, hence, all functions are dynamically bound. It is also reasonable to assume that virtual functions might need 20 to 40 extra instruction sets to execute a function call. Given today's processors speed this might be very insignificant.&lt;br /&gt;
&lt;br /&gt;
--------try to find a performance comparison and discuss about that---------&lt;br /&gt;
&lt;br /&gt;
==Overriding and Overloading==&lt;br /&gt;
Overriding [2] and Overloading &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overloading]&amp;lt;/sup&amp;gt; are two different features of OOL. In function overloading, which version of function to call is decided by the compiler during the compile time itself, where as when a function is overridden, which version of the function to call is deferred till runtime.&lt;br /&gt;
&lt;br /&gt;
Let us see an two examples which provides insight into Overriding and Overloading. &lt;br /&gt;
&lt;br /&gt;
Example - 1: Overloading&lt;br /&gt;
&lt;br /&gt;
  public class A{&lt;br /&gt;
    public boolean equals( A check){                  # Equals method of parameter type A&lt;br /&gt;
      System.out.println( &amp;quot;A.equals method called&amp;quot;);&lt;br /&gt;
      return true;&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static void main(String [] args){          # Main method&lt;br /&gt;
      Object a1 = new A();                            # Instantiate class to get objects&lt;br /&gt;
      A a2 = new A();&lt;br /&gt;
      Object o1 = new Object();&lt;br /&gt;
 &lt;br /&gt;
      a1.equals(a2);                                  # Object equals method called&lt;br /&gt;
      a2.equals(a1);                                  # Object equals method called&lt;br /&gt;
      a2.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(o1);                                  # Object equals method called&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Example - 2: Overriding&lt;br /&gt;
&lt;br /&gt;
  public class A{&lt;br /&gt;
    @Override&lt;br /&gt;
    public boolean equals( Object check){             # Equals method of parameter type Object&lt;br /&gt;
      System.out.println( &amp;quot;A.equals method called&amp;quot;);&lt;br /&gt;
      return true;&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static void main(String [] args){          # Main method&lt;br /&gt;
      Object a1 = new A();                            # Instantiate class to get objects&lt;br /&gt;
      A a2 = new A();&lt;br /&gt;
      Object o1 = new Object();&lt;br /&gt;
 &lt;br /&gt;
      a1.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(a1);                                  # A equals method called&lt;br /&gt;
      a2.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(o1);                                  # A equals method called&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you see the comments provided to the side of the method calls, you would note the difference in the equals method called in case of both Overloading and Overriding. The decision which method to use, basically has two phases: First overload resolution, then method dispatch. Overload resolution happens at compile-time, method dispatch at runtime.&lt;br /&gt;
&lt;br /&gt;
==Does Dynamic Dispatching hurts in today's power packed Computers?==&lt;br /&gt;
&lt;br /&gt;
Even though Dynamic Dispatching is done in run-time, and it takes good amount of memory, today's computers are power packed with good amount of memory. Hence efficiency concerns for implementing Dynamic Dispatching in OOL is of little concern.&lt;br /&gt;
&lt;br /&gt;
However, the method to be called cannot be chosen based on the actual arguments passed to the function, rather, the method is called based on the declared type of the parameters.&lt;br /&gt;
&lt;br /&gt;
=Advantages of Dynamic Binding=&lt;br /&gt;
&lt;br /&gt;
Dynamic binding has several advantages. It provides tremendous flexibilities. Also, it allows the software to be malleable when requirements change as the system evolves. Because of dynamic binding, caller objects are not concerned how the invoked objects carry out their methods. All they need to know is that the invoked objects know how to carry out their responsibilities, but they themselves need to know only what the invoked objects can do for them. As a consequence of this, type dependencies (which are the bane of procedural programming) cannot have a ripple effect through the system, when system requirements change. The beauty is that such dependencies remain encapsulated within the objects. The flexibility that developers can derive out of this is enormous. For instance, one can install newer types without having to change or stopping the functioning of existing systems. This is something along the lines of &amp;quot;hot pluggable components&amp;quot; of the hardware cousins.&lt;br /&gt;
&lt;br /&gt;
One other advantage of Dynamic binding based on parameter types is that more than one parameter can be used in the selection of a method. Methods that use dynamic binding in this way are called multi-methods and the concept is called &amp;lt;b&amp;gt;Multiple Dispatch&amp;lt;/b&amp;gt; &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Multiple_dispatch]&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Multiple Dispatch==&lt;br /&gt;
&lt;br /&gt;
The Multiple Dispatch is used in Common Lisp Object System (CLOS) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Common_Lisp_Object_System]&amp;lt;/sup&amp;gt;. Multiple-polymorphism allows specialized functions or methods to be defined to handle various cases:&lt;br /&gt;
&lt;br /&gt;
  +(int, int)&lt;br /&gt;
  +(int, float)&lt;br /&gt;
  +(int, complex) .. etc&lt;br /&gt;
&lt;br /&gt;
The above functions are specialized to each of the cases required allowing single, highly cohesive and loosely coupled functions to be defined. This is also the true essence of object-oriented polymorphism, which allows objects to define methods for each specific case desired. In addition to better coupling and cohesion, multiple-polymorphism reduces program complexity by avoiding coding logic (switch statements) and because small methods further reduce complexity, as code complexity doesn't grow linearly with lines of code per method, but perhaps exponentially.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
Hence from the above discussion, we can see that Dynamic Dispatch, though slower when compared to static dispatch is no longer slower due to the new generation of power packed computers. Also, dynamic dispatch can help in preserving the object-oriented concepts in highly complex software products. We have also looked up the differences between method Overloading and Overriding. We also had a sneak-view to Multiple Dispatch where more than one parameter can be used for Dynamic method lookup.&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overriding Method Overriding]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Code_segment Code Segment]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Virtual_method_table Virtual Method Table]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Virtual_function Virtual Functions]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc14cplr139.htm Virtual Functions in C++]. publib.boulder.ibm.com. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overloading Method Overloading]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Multiple_dispatch Multiple Dispatch]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;/div&gt;</summary>
		<author><name>Cgoogc</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39413</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39413"/>
		<updated>2010-10-27T03:31:05Z</updated>

		<summary type="html">&lt;p&gt;Cgoogc: /* Performance Evaluation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Dynamic Dispatch ==&lt;br /&gt;
&lt;br /&gt;
=== Introduction ===&lt;br /&gt;
The process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function.  Dynamic Dispatch generally happens in OOL. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called overriding). When a reference to the super class is used to execute that function, then which version of the function gets execute depends on the type, the reference points to at that time, instead of type of the reference. In literature it also given different names like Runtime Binding or [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism ].&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
=== Why we need Dynamic Dispatch ===&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object Oriented Languauge [http://en.wikipedia.org/wiki/Object_oriented_language 1]. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== How Dynamic Dispatch works ===&lt;br /&gt;
The implementation of Dynamic Dispatch varies significantly based on type language, in fact it changes even from Compiler to Compiler for a same language. We will consider how function are dispatch at runtime in both C and C++. We will try to understand if there is any overhead involved with virtual functions. &lt;br /&gt;
&lt;br /&gt;
In a machine compiled language like C, program logic is compiled into underlying processor instruction set by the compiler. Incase of x86 compiler, C code will be compiled into X86 instruction sets. The function will be compiled and placed in Code Segment area of the process image. So a function in C will just map to a memory location in Code Segment of the process image.&lt;br /&gt;
&lt;br /&gt;
For example calling a function print(), might be converted to something like jmp 0xFFDE123 (jmp is a branching instruction). So a function call is in fact as simple as executing a jump statement (of course you push the arguments into stack before you call jump).&lt;br /&gt;
&lt;br /&gt;
Now lets consider a OOL like C++ which too compiles to hardware instruction set on compilation. Consider the below example of two classes which extend from a common class called Animal(not shown) with one method talk. The below example just demonstrates how dynamic dispatch could be implemented by a compiler. &lt;br /&gt;
A word of caution before you proceed; the implementation is hypothetical and far from any practical use. The objective of the example shown below is just to give an idea of how Dynamic Dispatch might be implemented. Practical implementation are lot more complicated, so we have taken this simple example for easy explanation and understanding. &lt;br /&gt;
&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
The above C++ program might infact be converted to a equivalent C program by the compiler during pre-compilation. Each newly defined structure gets an __id__ and this id will be used to lookup vtable which maps id to the actual function memory location. One we get the memory location we will execute a jump instruction like we did before.&lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // C++ function call like &lt;br /&gt;
 Animal *d = new Dog(); &lt;br /&gt;
 d-&amp;gt;talk();&lt;br /&gt;
&lt;br /&gt;
 // might be change like this by the compiler&lt;br /&gt;
&lt;br /&gt;
 Dog *d = malloc(sizeof(Dog));&lt;br /&gt;
 d-&amp;gt;__id__ = 1;&lt;br /&gt;
 talk(d);&lt;br /&gt;
&lt;br /&gt;
 // talk function can use the __id__ attribute to offset into the vtable to identify which version of function to call.&lt;br /&gt;
&lt;br /&gt;
[[Image:Vtable.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
virtual method table or [http://en.wikipedia.org/wiki/Virtual_method_table vtable] is the key to virtual function working. It is evident from the above that vtable infact makes the function calling little more expensive compared to normal function calls.&lt;br /&gt;
&lt;br /&gt;
=== Performance Evaluation ===&lt;br /&gt;
From the above explanation it is evident that virtual functions are more complicated that dispatching normal function calls. For example Virtual functions in C++ will need extra memory for each class to store the virtual method table and needs additional CPU cycles to perform vtable lookup, before it can make the actual function call. &lt;br /&gt;
&lt;br /&gt;
This is one of the important reason why not all functions are dynamic bind in C++. In C++, functions which are explicitly marked virtual are dynamically bound, remaining functions are statically bound during the compile time. When we say bound/bind we are actually talking about the mapping between the function name and memory location where it is stored for execution.&lt;br /&gt;
&lt;br /&gt;
In languages like Java, the creators thought dynamic binding being an essential feature of OOL, all functions are dynamically bound. It also reasonable to assume that virtual function might need 20 to 40 extra instruction sets to execute a function call. Given today's processors speed this might be very insignificant.&lt;br /&gt;
&lt;br /&gt;
--------try to find a performance comparison and discuss about that---------&lt;br /&gt;
&lt;br /&gt;
=== Overriding and Overloading ===&lt;br /&gt;
Overriding and Overloading are two different feature of OOL. In function overloading, which version of function to call is decided by the compiler during the compile time itself, where as when a function is overridden, which version to call is deferred till runtime. [http://stackoverflow.com/questions/321864/java-dynamic-binding-and-method-overriding This]provides an interesting discussion about this fact.&lt;br /&gt;
&lt;br /&gt;
=== Does Dynamic Dispatching hurts in today's power packed Computers? ===&lt;br /&gt;
=== Conclusion ===&lt;br /&gt;
=== Further Reading ===&lt;br /&gt;
=== References ===&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_oriented_language 1. Wikipedia - Object Oriented Language]&lt;/div&gt;</summary>
		<author><name>Cgoogc</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39412</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39412"/>
		<updated>2010-10-27T03:30:56Z</updated>

		<summary type="html">&lt;p&gt;Cgoogc: /* Performance Evaluation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Dynamic Dispatch ==&lt;br /&gt;
&lt;br /&gt;
=== Introduction ===&lt;br /&gt;
The process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function.  Dynamic Dispatch generally happens in OOL. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called overriding). When a reference to the super class is used to execute that function, then which version of the function gets execute depends on the type, the reference points to at that time, instead of type of the reference. In literature it also given different names like Runtime Binding or [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism ].&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
=== Why we need Dynamic Dispatch ===&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object Oriented Languauge [http://en.wikipedia.org/wiki/Object_oriented_language 1]. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== How Dynamic Dispatch works ===&lt;br /&gt;
The implementation of Dynamic Dispatch varies significantly based on type language, in fact it changes even from Compiler to Compiler for a same language. We will consider how function are dispatch at runtime in both C and C++. We will try to understand if there is any overhead involved with virtual functions. &lt;br /&gt;
&lt;br /&gt;
In a machine compiled language like C, program logic is compiled into underlying processor instruction set by the compiler. Incase of x86 compiler, C code will be compiled into X86 instruction sets. The function will be compiled and placed in Code Segment area of the process image. So a function in C will just map to a memory location in Code Segment of the process image.&lt;br /&gt;
&lt;br /&gt;
For example calling a function print(), might be converted to something like jmp 0xFFDE123 (jmp is a branching instruction). So a function call is in fact as simple as executing a jump statement (of course you push the arguments into stack before you call jump).&lt;br /&gt;
&lt;br /&gt;
Now lets consider a OOL like C++ which too compiles to hardware instruction set on compilation. Consider the below example of two classes which extend from a common class called Animal(not shown) with one method talk. The below example just demonstrates how dynamic dispatch could be implemented by a compiler. &lt;br /&gt;
A word of caution before you proceed; the implementation is hypothetical and far from any practical use. The objective of the example shown below is just to give an idea of how Dynamic Dispatch might be implemented. Practical implementation are lot more complicated, so we have taken this simple example for easy explanation and understanding. &lt;br /&gt;
&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
The above C++ program might infact be converted to a equivalent C program by the compiler during pre-compilation. Each newly defined structure gets an __id__ and this id will be used to lookup vtable which maps id to the actual function memory location. One we get the memory location we will execute a jump instruction like we did before.&lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // C++ function call like &lt;br /&gt;
 Animal *d = new Dog(); &lt;br /&gt;
 d-&amp;gt;talk();&lt;br /&gt;
&lt;br /&gt;
 // might be change like this by the compiler&lt;br /&gt;
&lt;br /&gt;
 Dog *d = malloc(sizeof(Dog));&lt;br /&gt;
 d-&amp;gt;__id__ = 1;&lt;br /&gt;
 talk(d);&lt;br /&gt;
&lt;br /&gt;
 // talk function can use the __id__ attribute to offset into the vtable to identify which version of function to call.&lt;br /&gt;
&lt;br /&gt;
[[Image:Vtable.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
virtual method table or [http://en.wikipedia.org/wiki/Virtual_method_table vtable] is the key to virtual function working. It is evident from the above that vtable infact makes the function calling little more expensive compared to normal function calls.&lt;br /&gt;
&lt;br /&gt;
=== Performance Evaluation ===&lt;br /&gt;
From the above explanation it is evident that virtual functions are more complicated that dispatching normal function calls. For example Virtual functions in C++ will need extra memory for each class to store the virtual method table and needs additional CPU cycles to perform vtable lookup, before it can make the actual function call. &lt;br /&gt;
&lt;br /&gt;
This is one of the important reason why not all functions are dynamic bind in C++. In C++, functions which are explicitly marked virtual are dynamically bound, remaining functions are statically bound during the compile time. When we say bound/bind we are actually talking about the mapping between the function name and memory location where it is stored for execution.&lt;br /&gt;
&lt;br /&gt;
In languages like Java, the creators thought dynamic binding being an essential feature of OOL, all functions are dynamically bound. It also reasonable to assume that virtual function might need 20 to 40 extra instruction sets to execute a function call. Given today's processors speed this might be very insignificant.&lt;br /&gt;
&lt;br /&gt;
try to find a performance comparison and discuss about that---------&lt;br /&gt;
&lt;br /&gt;
=== Overriding and Overloading ===&lt;br /&gt;
Overriding and Overloading are two different feature of OOL. In function overloading, which version of function to call is decided by the compiler during the compile time itself, where as when a function is overridden, which version to call is deferred till runtime. [http://stackoverflow.com/questions/321864/java-dynamic-binding-and-method-overriding This]provides an interesting discussion about this fact.&lt;br /&gt;
&lt;br /&gt;
=== Does Dynamic Dispatching hurts in today's power packed Computers? ===&lt;br /&gt;
=== Conclusion ===&lt;br /&gt;
=== Further Reading ===&lt;br /&gt;
=== References ===&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_oriented_language 1. Wikipedia - Object Oriented Language]&lt;/div&gt;</summary>
		<author><name>Cgoogc</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39411</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39411"/>
		<updated>2010-10-27T03:30:32Z</updated>

		<summary type="html">&lt;p&gt;Cgoogc: /* Performance Evaluation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Dynamic Dispatch ==&lt;br /&gt;
&lt;br /&gt;
=== Introduction ===&lt;br /&gt;
The process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function.  Dynamic Dispatch generally happens in OOL. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called overriding). When a reference to the super class is used to execute that function, then which version of the function gets execute depends on the type, the reference points to at that time, instead of type of the reference. In literature it also given different names like Runtime Binding or [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism ].&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
=== Why we need Dynamic Dispatch ===&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object Oriented Languauge [http://en.wikipedia.org/wiki/Object_oriented_language 1]. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== How Dynamic Dispatch works ===&lt;br /&gt;
The implementation of Dynamic Dispatch varies significantly based on type language, in fact it changes even from Compiler to Compiler for a same language. We will consider how function are dispatch at runtime in both C and C++. We will try to understand if there is any overhead involved with virtual functions. &lt;br /&gt;
&lt;br /&gt;
In a machine compiled language like C, program logic is compiled into underlying processor instruction set by the compiler. Incase of x86 compiler, C code will be compiled into X86 instruction sets. The function will be compiled and placed in Code Segment area of the process image. So a function in C will just map to a memory location in Code Segment of the process image.&lt;br /&gt;
&lt;br /&gt;
For example calling a function print(), might be converted to something like jmp 0xFFDE123 (jmp is a branching instruction). So a function call is in fact as simple as executing a jump statement (of course you push the arguments into stack before you call jump).&lt;br /&gt;
&lt;br /&gt;
Now lets consider a OOL like C++ which too compiles to hardware instruction set on compilation. Consider the below example of two classes which extend from a common class called Animal(not shown) with one method talk. The below example just demonstrates how dynamic dispatch could be implemented by a compiler. &lt;br /&gt;
A word of caution before you proceed; the implementation is hypothetical and far from any practical use. The objective of the example shown below is just to give an idea of how Dynamic Dispatch might be implemented. Practical implementation are lot more complicated, so we have taken this simple example for easy explanation and understanding. &lt;br /&gt;
&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
The above C++ program might infact be converted to a equivalent C program by the compiler during pre-compilation. Each newly defined structure gets an __id__ and this id will be used to lookup vtable which maps id to the actual function memory location. One we get the memory location we will execute a jump instruction like we did before.&lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // C++ function call like &lt;br /&gt;
 Animal *d = new Dog(); &lt;br /&gt;
 d-&amp;gt;talk();&lt;br /&gt;
&lt;br /&gt;
 // might be change like this by the compiler&lt;br /&gt;
&lt;br /&gt;
 Dog *d = malloc(sizeof(Dog));&lt;br /&gt;
 d-&amp;gt;__id__ = 1;&lt;br /&gt;
 talk(d);&lt;br /&gt;
&lt;br /&gt;
 // talk function can use the __id__ attribute to offset into the vtable to identify which version of function to call.&lt;br /&gt;
&lt;br /&gt;
[[Image:Vtable.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
virtual method table or [http://en.wikipedia.org/wiki/Virtual_method_table vtable] is the key to virtual function working. It is evident from the above that vtable infact makes the function calling little more expensive compared to normal function calls.&lt;br /&gt;
&lt;br /&gt;
=== Performance Evaluation ===&lt;br /&gt;
From the above explanation it is evident that virtual functions are more complicated that dispatching normal function calls. For example Virtual functions in C++ will need extra memory for each class to store the virtual method table and needs additional CPU cycles to perform vtable lookup, before it can make the actual function call. &lt;br /&gt;
&lt;br /&gt;
This is one of the important reason why not all functions are dynamic bind in C++. In C++, functions which are explicitly marked virtual are dynamically bound, remaining functions are statically bound during the compile time. When we say bound/bind we are actually talking about the mapping between the function name and memory location where it is stored for execution.&lt;br /&gt;
&lt;br /&gt;
In languages like Java, the creators thought dynamic binding being an essential feature of OOL, all functions are dynamically bound. It also reasonable to assume that virtual function might need 20 to 40 extra instruction sets to execute a function call. Given today's processors speed this might be very insignificant.&lt;br /&gt;
&lt;br /&gt;
****** try to find a performance comparison and discuss about that ******&lt;br /&gt;
&lt;br /&gt;
=== Overriding and Overloading ===&lt;br /&gt;
Overriding and Overloading are two different feature of OOL. In function overloading, which version of function to call is decided by the compiler during the compile time itself, where as when a function is overridden, which version to call is deferred till runtime. [http://stackoverflow.com/questions/321864/java-dynamic-binding-and-method-overriding This]provides an interesting discussion about this fact.&lt;br /&gt;
&lt;br /&gt;
=== Does Dynamic Dispatching hurts in today's power packed Computers? ===&lt;br /&gt;
=== Conclusion ===&lt;br /&gt;
=== Further Reading ===&lt;br /&gt;
=== References ===&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_oriented_language 1. Wikipedia - Object Oriented Language]&lt;/div&gt;</summary>
		<author><name>Cgoogc</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39410</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39410"/>
		<updated>2010-10-27T03:28:49Z</updated>

		<summary type="html">&lt;p&gt;Cgoogc: /* How Dynamic Dispatch works */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Dynamic Dispatch ==&lt;br /&gt;
&lt;br /&gt;
=== Introduction ===&lt;br /&gt;
The process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function.  Dynamic Dispatch generally happens in OOL. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called overriding). When a reference to the super class is used to execute that function, then which version of the function gets execute depends on the type, the reference points to at that time, instead of type of the reference. In literature it also given different names like Runtime Binding or [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism ].&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
=== Why we need Dynamic Dispatch ===&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object Oriented Languauge [http://en.wikipedia.org/wiki/Object_oriented_language 1]. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== How Dynamic Dispatch works ===&lt;br /&gt;
The implementation of Dynamic Dispatch varies significantly based on type language, in fact it changes even from Compiler to Compiler for a same language. We will consider how function are dispatch at runtime in both C and C++. We will try to understand if there is any overhead involved with virtual functions. &lt;br /&gt;
&lt;br /&gt;
In a machine compiled language like C, program logic is compiled into underlying processor instruction set by the compiler. Incase of x86 compiler, C code will be compiled into X86 instruction sets. The function will be compiled and placed in Code Segment area of the process image. So a function in C will just map to a memory location in Code Segment of the process image.&lt;br /&gt;
&lt;br /&gt;
For example calling a function print(), might be converted to something like jmp 0xFFDE123 (jmp is a branching instruction). So a function call is in fact as simple as executing a jump statement (of course you push the arguments into stack before you call jump).&lt;br /&gt;
&lt;br /&gt;
Now lets consider a OOL like C++ which too compiles to hardware instruction set on compilation. Consider the below example of two classes which extend from a common class called Animal(not shown) with one method talk. The below example just demonstrates how dynamic dispatch could be implemented by a compiler. &lt;br /&gt;
A word of caution before you proceed; the implementation is hypothetical and far from any practical use. The objective of the example shown below is just to give an idea of how Dynamic Dispatch might be implemented. Practical implementation are lot more complicated, so we have taken this simple example for easy explanation and understanding. &lt;br /&gt;
&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
The above C++ program might infact be converted to a equivalent C program by the compiler during pre-compilation. Each newly defined structure gets an __id__ and this id will be used to lookup vtable which maps id to the actual function memory location. One we get the memory location we will execute a jump instruction like we did before.&lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // C++ function call like &lt;br /&gt;
 Animal *d = new Dog(); &lt;br /&gt;
 d-&amp;gt;talk();&lt;br /&gt;
&lt;br /&gt;
 // might be change like this by the compiler&lt;br /&gt;
&lt;br /&gt;
 Dog *d = malloc(sizeof(Dog));&lt;br /&gt;
 d-&amp;gt;__id__ = 1;&lt;br /&gt;
 talk(d);&lt;br /&gt;
&lt;br /&gt;
 // talk function can use the __id__ attribute to offset into the vtable to identify which version of function to call.&lt;br /&gt;
&lt;br /&gt;
[[Image:Vtable.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
virtual method table or [http://en.wikipedia.org/wiki/Virtual_method_table vtable] is the key to virtual function working. It is evident from the above that vtable infact makes the function calling little more expensive compared to normal function calls.&lt;br /&gt;
&lt;br /&gt;
=== Performance Evaluation ===&lt;br /&gt;
From the above explanation it is evident that virtual functions are more complicated that dispatching normal function calls. For example Virtual functions in C++ will need extra memory for each class to store the virtual method table and needs additional CPU cycles to perform vtable lookup, before it can make the actual function call. &lt;br /&gt;
&lt;br /&gt;
This is one of the important reason why not all functions are dynamic bind in C++. In C++, functions which are explicitly marked virtual are dynamically bound, remaining functions are statically bound during the compile time. When we say bound/bind we are actually talking about the mapping between the function name and memory location where it is stored for execution.&lt;br /&gt;
&lt;br /&gt;
In languages like Java, the creators thought dynamic binding being an essential feature of OOL, all functions are dynamically bound. The &lt;br /&gt;
&lt;br /&gt;
=== Overriding and Overloading ===&lt;br /&gt;
Overriding and Overloading are two different feature of OOL. In function overloading, which version of function to call is decided by the compiler during the compile time itself, where as when a function is overridden, which version to call is deferred till runtime. [http://stackoverflow.com/questions/321864/java-dynamic-binding-and-method-overriding This]provides an interesting discussion about this fact.&lt;br /&gt;
&lt;br /&gt;
=== Does Dynamic Dispatching hurts in today's power packed Computers? ===&lt;br /&gt;
=== Conclusion ===&lt;br /&gt;
=== Further Reading ===&lt;br /&gt;
=== References ===&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_oriented_language 1. Wikipedia - Object Oriented Language]&lt;/div&gt;</summary>
		<author><name>Cgoogc</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39409</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39409"/>
		<updated>2010-10-27T03:25:56Z</updated>

		<summary type="html">&lt;p&gt;Cgoogc: /* Overriding and Overloading */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Dynamic Dispatch ==&lt;br /&gt;
&lt;br /&gt;
=== Introduction ===&lt;br /&gt;
The process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function.  Dynamic Dispatch generally happens in OOL. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called overriding). When a reference to the super class is used to execute that function, then which version of the function gets execute depends on the type, the reference points to at that time, instead of type of the reference. In literature it also given different names like Runtime Binding or [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism ].&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
=== Why we need Dynamic Dispatch ===&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object Oriented Languauge [http://en.wikipedia.org/wiki/Object_oriented_language 1]. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== How Dynamic Dispatch works ===&lt;br /&gt;
The implementation of Dynamic Dispatch varies significantly based on type language, in fact it changes even from Compiler to Compiler for a same language. We will consider how function are dispatch at runtime in both C and C++. We will try to understand if there is any overhead involved with virtual functions. &lt;br /&gt;
&lt;br /&gt;
In a machine compiled language like C, program logic is compiled into underlying processor instruction set by the compiler. Incase of x86 compiler, C code will be compiled into X86 instruction sets. The function will be compiled and placed in Code Segment area of the process image. So a function in C will just map to a memory location in Code Segment of the process image.&lt;br /&gt;
&lt;br /&gt;
For example calling a function print(), might be converted to something like jmp 0xFFDE123 (jmp is a branching instruction). So a function call is in fact as simple as executing a jump statement (of course you push the arguments into stack before you call jump).&lt;br /&gt;
&lt;br /&gt;
Now lets consider a OOL like C++ which too compiles to hardware instruction set on compilation. Consider the below example of two classes which extend from a common class called Animal(not shown) with one method talk. The below example just demonstrates how dynamic dispatch could be implemented by a compiler. &lt;br /&gt;
A word of caution before you proceed; the implementation is hypothetical and far from any practical use. The objective of the example shown below is just to give an idea of how Dynamic Dispatch might be implemented. Practical implementation are lot more complicated, so we have taken this simple example for easy explanation and understanding. &lt;br /&gt;
&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
The above C++ program might infact be converted to a equivalent C program by the compiler during pre-compilation. Each newly defined structure gets an __id__ and this id will be used to lookup vtable which maps id to the actual function memory location. One we get the memory location we will execute a jump instruction like we did before.&lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
 &lt;br /&gt;
 Cat *c = new Cat(); &lt;br /&gt;
 &lt;br /&gt;
 Cat *c = malloc(sizeof(cat));&lt;br /&gt;
 c-&amp;gt;__id__ = 0;&lt;br /&gt;
&lt;br /&gt;
[[Image:Vtable.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
virtual method table or [http://en.wikipedia.org/wiki/Virtual_method_table vtable] is the key to virtual function working. It is evident from the above that vtable infact makes the function calling little more expensive compared to normal function calls.&lt;br /&gt;
&lt;br /&gt;
=== Performance Evaluation ===&lt;br /&gt;
From the above explanation it is evident that virtual functions are more complicated that dispatching normal function calls. For example Virtual functions in C++ will need extra memory for each class to store the virtual method table and needs additional CPU cycles to perform vtable lookup, before it can make the actual function call. &lt;br /&gt;
&lt;br /&gt;
This is one of the important reason why not all functions are dynamic bind in C++. In C++, functions which are explicitly marked virtual are dynamically bound, remaining functions are statically bound during the compile time. When we say bound/bind we are actually talking about the mapping between the function name and memory location where it is stored for execution.&lt;br /&gt;
&lt;br /&gt;
In languages like Java, the creators thought dynamic binding being an essential feature of OOL, all functions are dynamically bound. The &lt;br /&gt;
&lt;br /&gt;
=== Overriding and Overloading ===&lt;br /&gt;
Overriding and Overloading are two different feature of OOL. In function overloading, which version of function to call is decided by the compiler during the compile time itself, where as when a function is overridden, which version to call is deferred till runtime. [http://stackoverflow.com/questions/321864/java-dynamic-binding-and-method-overriding This]provides an interesting discussion about this fact.&lt;br /&gt;
&lt;br /&gt;
=== Does Dynamic Dispatching hurts in today's power packed Computers? ===&lt;br /&gt;
=== Conclusion ===&lt;br /&gt;
=== Further Reading ===&lt;br /&gt;
=== References ===&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_oriented_language 1. Wikipedia - Object Oriented Language]&lt;/div&gt;</summary>
		<author><name>Cgoogc</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39408</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39408"/>
		<updated>2010-10-27T03:25:46Z</updated>

		<summary type="html">&lt;p&gt;Cgoogc: /* Performance Evaluation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Dynamic Dispatch ==&lt;br /&gt;
&lt;br /&gt;
=== Introduction ===&lt;br /&gt;
The process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function.  Dynamic Dispatch generally happens in OOL. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called overriding). When a reference to the super class is used to execute that function, then which version of the function gets execute depends on the type, the reference points to at that time, instead of type of the reference. In literature it also given different names like Runtime Binding or [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism ].&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
=== Why we need Dynamic Dispatch ===&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object Oriented Languauge [http://en.wikipedia.org/wiki/Object_oriented_language 1]. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== How Dynamic Dispatch works ===&lt;br /&gt;
The implementation of Dynamic Dispatch varies significantly based on type language, in fact it changes even from Compiler to Compiler for a same language. We will consider how function are dispatch at runtime in both C and C++. We will try to understand if there is any overhead involved with virtual functions. &lt;br /&gt;
&lt;br /&gt;
In a machine compiled language like C, program logic is compiled into underlying processor instruction set by the compiler. Incase of x86 compiler, C code will be compiled into X86 instruction sets. The function will be compiled and placed in Code Segment area of the process image. So a function in C will just map to a memory location in Code Segment of the process image.&lt;br /&gt;
&lt;br /&gt;
For example calling a function print(), might be converted to something like jmp 0xFFDE123 (jmp is a branching instruction). So a function call is in fact as simple as executing a jump statement (of course you push the arguments into stack before you call jump).&lt;br /&gt;
&lt;br /&gt;
Now lets consider a OOL like C++ which too compiles to hardware instruction set on compilation. Consider the below example of two classes which extend from a common class called Animal(not shown) with one method talk. The below example just demonstrates how dynamic dispatch could be implemented by a compiler. &lt;br /&gt;
A word of caution before you proceed; the implementation is hypothetical and far from any practical use. The objective of the example shown below is just to give an idea of how Dynamic Dispatch might be implemented. Practical implementation are lot more complicated, so we have taken this simple example for easy explanation and understanding. &lt;br /&gt;
&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
The above C++ program might infact be converted to a equivalent C program by the compiler during pre-compilation. Each newly defined structure gets an __id__ and this id will be used to lookup vtable which maps id to the actual function memory location. One we get the memory location we will execute a jump instruction like we did before.&lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
 &lt;br /&gt;
 Cat *c = new Cat(); &lt;br /&gt;
 &lt;br /&gt;
 Cat *c = malloc(sizeof(cat));&lt;br /&gt;
 c-&amp;gt;__id__ = 0;&lt;br /&gt;
&lt;br /&gt;
[[Image:Vtable.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
virtual method table or [http://en.wikipedia.org/wiki/Virtual_method_table vtable] is the key to virtual function working. It is evident from the above that vtable infact makes the function calling little more expensive compared to normal function calls.&lt;br /&gt;
&lt;br /&gt;
=== Performance Evaluation ===&lt;br /&gt;
From the above explanation it is evident that virtual functions are more complicated that dispatching normal function calls. For example Virtual functions in C++ will need extra memory for each class to store the virtual method table and needs additional CPU cycles to perform vtable lookup, before it can make the actual function call. &lt;br /&gt;
&lt;br /&gt;
This is one of the important reason why not all functions are dynamic bind in C++. In C++, functions which are explicitly marked virtual are dynamically bound, remaining functions are statically bound during the compile time. When we say bound/bind we are actually talking about the mapping between the function name and memory location where it is stored for execution.&lt;br /&gt;
&lt;br /&gt;
In languages like Java, the creators thought dynamic binding being an essential feature of OOL, all functions are dynamically bound. The &lt;br /&gt;
&lt;br /&gt;
=== Overriding and Overloading ===&lt;br /&gt;
Overriding and Overloading are two different feature of OOL. In function overloading, which version of function to call is decided by the compiler during the compile time itself, where as when a function is overridden, which version to call is deferred till runtime. [http://stackoverflow.com/questions/321864/java-dynamic-binding-and-method-overriding This] provides an interesting discussion about this fact.&lt;br /&gt;
&lt;br /&gt;
=== Does Dynamic Dispatching hurts in today's power packed Computers? ===&lt;br /&gt;
=== Conclusion ===&lt;br /&gt;
=== Further Reading ===&lt;br /&gt;
=== References ===&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_oriented_language 1. Wikipedia - Object Oriented Language]&lt;/div&gt;</summary>
		<author><name>Cgoogc</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39407</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39407"/>
		<updated>2010-10-27T03:18:22Z</updated>

		<summary type="html">&lt;p&gt;Cgoogc: /* How Dynamic Dispatch works */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Dynamic Dispatch ==&lt;br /&gt;
&lt;br /&gt;
=== Introduction ===&lt;br /&gt;
The process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function.  Dynamic Dispatch generally happens in OOL. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called overriding). When a reference to the super class is used to execute that function, then which version of the function gets execute depends on the type, the reference points to at that time, instead of type of the reference. In literature it also given different names like Runtime Binding or [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism ].&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
=== Why we need Dynamic Dispatch ===&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object Oriented Languauge [http://en.wikipedia.org/wiki/Object_oriented_language 1]. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== How Dynamic Dispatch works ===&lt;br /&gt;
The implementation of Dynamic Dispatch varies significantly based on type language, in fact it changes even from Compiler to Compiler for a same language. We will consider how function are dispatch at runtime in both C and C++. We will try to understand if there is any overhead involved with virtual functions. &lt;br /&gt;
&lt;br /&gt;
In a machine compiled language like C, program logic is compiled into underlying processor instruction set by the compiler. Incase of x86 compiler, C code will be compiled into X86 instruction sets. The function will be compiled and placed in Code Segment area of the process image. So a function in C will just map to a memory location in Code Segment of the process image.&lt;br /&gt;
&lt;br /&gt;
For example calling a function print(), might be converted to something like jmp 0xFFDE123 (jmp is a branching instruction). So a function call is in fact as simple as executing a jump statement (of course you push the arguments into stack before you call jump).&lt;br /&gt;
&lt;br /&gt;
Now lets consider a OOL like C++ which too compiles to hardware instruction set on compilation. Consider the below example of two classes which extend from a common class called Animal(not shown) with one method talk. The below example just demonstrates how dynamic dispatch could be implemented by a compiler. &lt;br /&gt;
A word of caution before you proceed; the implementation is hypothetical and far from any practical use. The objective of the example shown below is just to give an idea of how Dynamic Dispatch might be implemented. Practical implementation are lot more complicated, so we have taken this simple example for easy explanation and understanding. &lt;br /&gt;
&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
The above C++ program might infact be converted to a equivalent C program by the compiler during pre-compilation. Each newly defined structure gets an __id__ and this id will be used to lookup vtable which maps id to the actual function memory location. One we get the memory location we will execute a jump instruction like we did before.&lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
 &lt;br /&gt;
 Cat *c = new Cat(); &lt;br /&gt;
 &lt;br /&gt;
 Cat *c = malloc(sizeof(cat));&lt;br /&gt;
 c-&amp;gt;__id__ = 0;&lt;br /&gt;
&lt;br /&gt;
[[Image:Vtable.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
virtual method table or [http://en.wikipedia.org/wiki/Virtual_method_table vtable] is the key to virtual function working. It is evident from the above that vtable infact makes the function calling little more expensive compared to normal function calls.&lt;br /&gt;
&lt;br /&gt;
=== Performance Evaluation ===&lt;br /&gt;
=== Does Dynamic Dispatching hurts in today's power packed Computers? ===&lt;br /&gt;
=== Conclusion ===&lt;br /&gt;
=== Further Reading ===&lt;br /&gt;
=== References ===&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_oriented_language 1. Wikipedia - Object Oriented Language]&lt;/div&gt;</summary>
		<author><name>Cgoogc</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39406</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39406"/>
		<updated>2010-10-27T03:02:06Z</updated>

		<summary type="html">&lt;p&gt;Cgoogc: /* How Dynamic Dispatch works */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Dynamic Dispatch ==&lt;br /&gt;
&lt;br /&gt;
=== Introduction ===&lt;br /&gt;
The process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function.  Dynamic Dispatch generally happens in OOL. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called overriding). When a reference to the super class is used to execute that function, then which version of the function gets execute depends on the type, the reference points to at that time, instead of type of the reference. In literature it also given different names like Runtime Binding or [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism ].&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
=== Why we need Dynamic Dispatch ===&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object Oriented Languauge [http://en.wikipedia.org/wiki/Object_oriented_language 1]. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== How Dynamic Dispatch works ===&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
 &lt;br /&gt;
 Cat *c = new Cat(); &lt;br /&gt;
 &lt;br /&gt;
 Cat *c = malloc(sizeof(cat));&lt;br /&gt;
 c-&amp;gt;__id__ = 0;&lt;br /&gt;
&lt;br /&gt;
[[Image:Vtable.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
=== Performance Evaluation ===&lt;br /&gt;
=== Does Dynamic Dispatching hurts in today's power packed Computers? ===&lt;br /&gt;
=== Conclusion ===&lt;br /&gt;
=== Further Reading ===&lt;br /&gt;
=== References ===&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_oriented_language 1. Wikipedia - Object Oriented Language]&lt;/div&gt;</summary>
		<author><name>Cgoogc</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39405</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39405"/>
		<updated>2010-10-27T02:58:55Z</updated>

		<summary type="html">&lt;p&gt;Cgoogc: /* How Dynamic Dispatch works */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Dynamic Dispatch ==&lt;br /&gt;
&lt;br /&gt;
=== Introduction ===&lt;br /&gt;
The process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function.  Dynamic Dispatch generally happens in OOL. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called overriding). When a reference to the super class is used to execute that function, then which version of the function gets execute depends on the type, the reference points to at that time, instead of type of the reference. In literature it also given different names like Runtime Binding or [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism ].&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
=== Why we need Dynamic Dispatch ===&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object Oriented Languauge [http://en.wikipedia.org/wiki/Object_oriented_language 1]. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== How Dynamic Dispatch works ===&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
 &lt;br /&gt;
 Cat *c = new Cat(); &lt;br /&gt;
 &lt;br /&gt;
 Cat *c = malloc(sizeof(cat));&lt;br /&gt;
 c-&amp;gt;__id__ = 0;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[img: Vtable.png]]&lt;br /&gt;
&lt;br /&gt;
=== Performance Evaluation ===&lt;br /&gt;
=== Does Dynamic Dispatching hurts in today's power packed Computers? ===&lt;br /&gt;
=== Conclusion ===&lt;br /&gt;
=== Further Reading ===&lt;br /&gt;
=== References ===&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_oriented_language 1. Wikipedia - Object Oriented Language]&lt;/div&gt;</summary>
		<author><name>Cgoogc</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39404</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39404"/>
		<updated>2010-10-27T02:58:42Z</updated>

		<summary type="html">&lt;p&gt;Cgoogc: /* How Dynamic Dispatch works */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Dynamic Dispatch ==&lt;br /&gt;
&lt;br /&gt;
=== Introduction ===&lt;br /&gt;
The process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function.  Dynamic Dispatch generally happens in OOL. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called overriding). When a reference to the super class is used to execute that function, then which version of the function gets execute depends on the type, the reference points to at that time, instead of type of the reference. In literature it also given different names like Runtime Binding or [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism ].&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
=== Why we need Dynamic Dispatch ===&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object Oriented Languauge [http://en.wikipedia.org/wiki/Object_oriented_language 1]. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== How Dynamic Dispatch works ===&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
 &lt;br /&gt;
 Cat *c = new Cat(); &lt;br /&gt;
 &lt;br /&gt;
 Cat *c = malloc(sizeof(cat));&lt;br /&gt;
 c-&amp;gt;__id__ = 0;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[img: Vtable.png]&lt;br /&gt;
&lt;br /&gt;
=== Performance Evaluation ===&lt;br /&gt;
=== Does Dynamic Dispatching hurts in today's power packed Computers? ===&lt;br /&gt;
=== Conclusion ===&lt;br /&gt;
=== Further Reading ===&lt;br /&gt;
=== References ===&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_oriented_language 1. Wikipedia - Object Oriented Language]&lt;/div&gt;</summary>
		<author><name>Cgoogc</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39403</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39403"/>
		<updated>2010-10-27T02:58:25Z</updated>

		<summary type="html">&lt;p&gt;Cgoogc: /* How Dynamic Dispatch works */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Dynamic Dispatch ==&lt;br /&gt;
&lt;br /&gt;
=== Introduction ===&lt;br /&gt;
The process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function.  Dynamic Dispatch generally happens in OOL. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called overriding). When a reference to the super class is used to execute that function, then which version of the function gets execute depends on the type, the reference points to at that time, instead of type of the reference. In literature it also given different names like Runtime Binding or [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism ].&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
=== Why we need Dynamic Dispatch ===&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object Oriented Languauge [http://en.wikipedia.org/wiki/Object_oriented_language 1]. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== How Dynamic Dispatch works ===&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
 &lt;br /&gt;
 Cat *c = new Cat(); &lt;br /&gt;
 &lt;br /&gt;
 Cat *c = malloc(sizeof(cat));&lt;br /&gt;
 c-&amp;gt;__id__ = 0;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[media: Vtable.png]]&lt;br /&gt;
&lt;br /&gt;
=== Performance Evaluation ===&lt;br /&gt;
=== Does Dynamic Dispatching hurts in today's power packed Computers? ===&lt;br /&gt;
=== Conclusion ===&lt;br /&gt;
=== Further Reading ===&lt;br /&gt;
=== References ===&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_oriented_language 1. Wikipedia - Object Oriented Language]&lt;/div&gt;</summary>
		<author><name>Cgoogc</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:Vtable.png&amp;diff=39402</id>
		<title>File:Vtable.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:Vtable.png&amp;diff=39402"/>
		<updated>2010-10-27T02:57:59Z</updated>

		<summary type="html">&lt;p&gt;Cgoogc: hypothetical vtable structure&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;hypothetical vtable structure&lt;/div&gt;</summary>
		<author><name>Cgoogc</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39401</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39401"/>
		<updated>2010-10-27T02:50:20Z</updated>

		<summary type="html">&lt;p&gt;Cgoogc: /* Dynamic Dispatch */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Dynamic Dispatch ==&lt;br /&gt;
&lt;br /&gt;
=== Introduction ===&lt;br /&gt;
The process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function.  Dynamic Dispatch generally happens in OOL. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called overriding). When a reference to the super class is used to execute that function, then which version of the function gets execute depends on the type, the reference points to at that time, instead of type of the reference. In literature it also given different names like Runtime Binding or [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism ].&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
=== Why we need Dynamic Dispatch ===&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object Oriented Languauge [http://en.wikipedia.org/wiki/Object_oriented_language 1]. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== How Dynamic Dispatch works ===&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
 &lt;br /&gt;
 Cat *c = new Cat(); &lt;br /&gt;
 &lt;br /&gt;
 Cat *c = malloc(sizeof(cat));&lt;br /&gt;
 c-&amp;gt;__id__ = 0;&lt;br /&gt;
&lt;br /&gt;
=== Performance Evaluation ===&lt;br /&gt;
=== Does Dynamic Dispatching hurts in today's power packed Computers? ===&lt;br /&gt;
=== Conclusion ===&lt;br /&gt;
=== Further Reading ===&lt;br /&gt;
=== References ===&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_oriented_language 1. Wikipedia - Object Oriented Language]&lt;/div&gt;</summary>
		<author><name>Cgoogc</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39400</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39400"/>
		<updated>2010-10-27T02:49:54Z</updated>

		<summary type="html">&lt;p&gt;Cgoogc: /* How Dynamic Dispatch works */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Dynamic Dispatch ==&lt;br /&gt;
&lt;br /&gt;
=== Introduction ===&lt;br /&gt;
The process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function.  Dynamic Dispatch generally happens in OOL. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called overriding). When a reference to the super class is used to execute that function, then which version of the function gets execute depends on the type, the reference points to at that time, instead of type of the reference. In literature it also given different names like Runtime Binding or [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism ].&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
=== Why we need Dynamic Dispatch ===&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object Oriented Languauge [http://en.wikipedia.org/wiki/Object_oriented_language 1]. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
=== Dynamic Dispatch working in Detail ===&lt;br /&gt;
&lt;br /&gt;
=== Dynamic Dispatch working in Detail ===&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
 &lt;br /&gt;
 Cat *c = new Cat(); &lt;br /&gt;
 &lt;br /&gt;
 Cat *c = malloc(sizeof(cat));&lt;br /&gt;
 c-&amp;gt;__id__ = 0;&lt;br /&gt;
&lt;br /&gt;
=== Performance Evaluation ===&lt;br /&gt;
=== Does Dynamic Dispatching hurts in today's power packed Computers? ===&lt;br /&gt;
=== Conclusion ===&lt;br /&gt;
=== Further Reading ===&lt;br /&gt;
=== References ===&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_oriented_language 1. Wikipedia - Object Oriented Language]&lt;/div&gt;</summary>
		<author><name>Cgoogc</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39399</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39399"/>
		<updated>2010-10-27T02:49:30Z</updated>

		<summary type="html">&lt;p&gt;Cgoogc: /* Why we need Dynamic Dispatch */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Dynamic Dispatch ==&lt;br /&gt;
&lt;br /&gt;
=== Introduction ===&lt;br /&gt;
The process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function.  Dynamic Dispatch generally happens in OOL. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called overriding). When a reference to the super class is used to execute that function, then which version of the function gets execute depends on the type, the reference points to at that time, instead of type of the reference. In literature it also given different names like Runtime Binding or [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism ].&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
=== Why we need Dynamic Dispatch ===&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object Oriented Languauge [http://en.wikipedia.org/wiki/Object_oriented_language 1]. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
=== How Dynamic Dispatch works ===&lt;br /&gt;
=== Dynamic Dispatch working in Detail ===&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
 &lt;br /&gt;
 Cat *c = new Cat(); &lt;br /&gt;
 &lt;br /&gt;
 Cat *c = malloc(sizeof(cat));&lt;br /&gt;
 c-&amp;gt;__id__ = 0;&lt;br /&gt;
&lt;br /&gt;
=== Performance Evaluation ===&lt;br /&gt;
=== Does Dynamic Dispatching hurts in today's power packed Computers? ===&lt;br /&gt;
=== Conclusion ===&lt;br /&gt;
=== Further Reading ===&lt;br /&gt;
=== References ===&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_oriented_language 1. Wikipedia - Object Oriented Language]&lt;/div&gt;</summary>
		<author><name>Cgoogc</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39397</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39397"/>
		<updated>2010-10-27T02:39:54Z</updated>

		<summary type="html">&lt;p&gt;Cgoogc: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Dynamic Dispatch ==&lt;br /&gt;
&lt;br /&gt;
=== Introduction ===&lt;br /&gt;
The process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function.  Dynamic Dispatch generally happens in OOL. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called overriding). When a reference to the super class is used to execute that function, then which version of the function gets execute depends on the type, the reference points to at that time, instead of type of the reference. In literature it also given different names like Runtime Binding or [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism ].&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
=== Why we need Dynamic Dispatch ===&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object Oriented Languauge [http://en.wikipedia.org/wiki/Object_oriented_language 1].&lt;br /&gt;
&lt;br /&gt;
=== How Dynamic Dispatch works ===&lt;br /&gt;
=== Dynamic Dispatch working in Detail ===&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
 &lt;br /&gt;
 Cat *c = new Cat(); &lt;br /&gt;
 &lt;br /&gt;
 Cat *c = malloc(sizeof(cat));&lt;br /&gt;
 c-&amp;gt;__id__ = 0;&lt;br /&gt;
&lt;br /&gt;
=== Performance Evaluation ===&lt;br /&gt;
=== Does Dynamic Dispatching hurts in today's power packed Computers? ===&lt;br /&gt;
=== Conclusion ===&lt;br /&gt;
=== Further Reading ===&lt;br /&gt;
=== References ===&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_oriented_language 1. Wikipedia - Object Oriented Language]&lt;/div&gt;</summary>
		<author><name>Cgoogc</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39396</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39396"/>
		<updated>2010-10-27T02:39:29Z</updated>

		<summary type="html">&lt;p&gt;Cgoogc: /* Why we need Dynamic Dispatch */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Dynamic Dispatch ==&lt;br /&gt;
&lt;br /&gt;
=== Introduction ===&lt;br /&gt;
The process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function.  Dynamic Dispatch generally happens in OOL. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called overriding). When a reference to the super class is used to execute that function, then which version of the function gets execute depends on the type, the reference points to at that time, instead of type of the reference. In literature it also given different names like Runtime Binding or [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism ].&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
=== Why we need Dynamic Dispatch ===&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object Oriented Languauge [http://en.wikipedia.org/wiki/Object_oriented_language 1].&lt;br /&gt;
&lt;br /&gt;
=== How Dynamic Dispatch works ===&lt;br /&gt;
=== Dynamic Dispatch working in Detail ===&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
 &lt;br /&gt;
 Cat *c = new Cat(); &lt;br /&gt;
 &lt;br /&gt;
 Cat *c = malloc(sizeof(cat));&lt;br /&gt;
 c-&amp;gt;__id__ = 0;&lt;br /&gt;
&lt;br /&gt;
=== Performance Evaluation ===&lt;br /&gt;
=== Does Dynamic Dispatching hurts in today's power packed Computers? ===&lt;br /&gt;
=== Conclusion ===&lt;br /&gt;
=== Further Reading ===&lt;br /&gt;
=== References ===&lt;br /&gt;
*[1 http://en.wikipedia.org/wiki/Object_oriented_language]&lt;/div&gt;</summary>
		<author><name>Cgoogc</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39395</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39395"/>
		<updated>2010-10-27T02:39:13Z</updated>

		<summary type="html">&lt;p&gt;Cgoogc: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Dynamic Dispatch ==&lt;br /&gt;
&lt;br /&gt;
=== Introduction ===&lt;br /&gt;
The process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function.  Dynamic Dispatch generally happens in OOL. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called overriding). When a reference to the super class is used to execute that function, then which version of the function gets execute depends on the type, the reference points to at that time, instead of type of the reference. In literature it also given different names like Runtime Binding or [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism ].&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
=== Why we need Dynamic Dispatch ===&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object Oriented Languauge [1 http://en.wikipedia.org/wiki/Object_oriented_language]. &lt;br /&gt;
&lt;br /&gt;
=== How Dynamic Dispatch works ===&lt;br /&gt;
=== Dynamic Dispatch working in Detail ===&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
 &lt;br /&gt;
 Cat *c = new Cat(); &lt;br /&gt;
 &lt;br /&gt;
 Cat *c = malloc(sizeof(cat));&lt;br /&gt;
 c-&amp;gt;__id__ = 0;&lt;br /&gt;
&lt;br /&gt;
=== Performance Evaluation ===&lt;br /&gt;
=== Does Dynamic Dispatching hurts in today's power packed Computers? ===&lt;br /&gt;
=== Conclusion ===&lt;br /&gt;
=== Further Reading ===&lt;br /&gt;
=== References ===&lt;br /&gt;
*[1 http://en.wikipedia.org/wiki/Object_oriented_language]&lt;/div&gt;</summary>
		<author><name>Cgoogc</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39393</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39393"/>
		<updated>2010-10-27T02:38:36Z</updated>

		<summary type="html">&lt;p&gt;Cgoogc: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Dynamic Dispatch ==&lt;br /&gt;
&lt;br /&gt;
=== Introduction ===&lt;br /&gt;
The process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function.  Dynamic Dispatch generally happens in OOL. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called overriding). When a reference to the super class is used to execute that function, then which version of the function gets execute depends on the type, the reference points to at that time, instead of type of the reference. In literature it also given different names like Runtime Binding or [Dynamic Polymorphism http://en.wikipedia.org/wiki/Dynamic_polymorphism].&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
=== Why we need Dynamic Dispatch ===&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object Oriented Languauge [1 http://en.wikipedia.org/wiki/Object_oriented_language]. &lt;br /&gt;
&lt;br /&gt;
=== How Dynamic Dispatch works ===&lt;br /&gt;
=== Dynamic Dispatch working in Detail ===&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
 &lt;br /&gt;
 Cat *c = new Cat(); &lt;br /&gt;
 &lt;br /&gt;
 Cat *c = malloc(sizeof(cat));&lt;br /&gt;
 c-&amp;gt;__id__ = 0;&lt;br /&gt;
&lt;br /&gt;
=== Performance Evaluation ===&lt;br /&gt;
=== Does Dynamic Dispatching hurts in today's power packed Computers? ===&lt;br /&gt;
=== Conclusion ===&lt;br /&gt;
=== Further Reading ===&lt;br /&gt;
=== References ===&lt;br /&gt;
*[1 http://en.wikipedia.org/wiki/Object_oriented_language]&lt;/div&gt;</summary>
		<author><name>Cgoogc</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39391</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39391"/>
		<updated>2010-10-27T02:37:24Z</updated>

		<summary type="html">&lt;p&gt;Cgoogc: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Dynamic Dispatch ==&lt;br /&gt;
&lt;br /&gt;
=== Introduction ===&lt;br /&gt;
The process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function.  Dynamic Dispatch generally happens in OOL. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called overriding). When a reference to the super class is used to execute that function, then which version of the function gets execute depends on the type, the reference points to at that time, instead of type of the reference. In literature it also given different names like Runtime Binding or [[Dynamic Polymorphism http://en.wikipedia.org/wiki/Dynamic_polymorphism]].&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
=== Why we need Dynamic Dispatch ===&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object Oriented Languauge [1 http://en.wikipedia.org/wiki/Object_oriented_language]. &lt;br /&gt;
&lt;br /&gt;
=== How Dynamic Dispatch works ===&lt;br /&gt;
=== Dynamic Dispatch working in Detail ===&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
 &lt;br /&gt;
 Cat *c = new Cat(); &lt;br /&gt;
 &lt;br /&gt;
 Cat *c = malloc(sizeof(cat));&lt;br /&gt;
 c-&amp;gt;__id__ = 0;&lt;br /&gt;
&lt;br /&gt;
=== Performance Evaluation ===&lt;br /&gt;
=== Does Dynamic Dispatching hurts in today's power packed Computers? ===&lt;br /&gt;
=== Conclusion ===&lt;br /&gt;
=== Further Reading ===&lt;br /&gt;
=== References ===&lt;br /&gt;
*[1 http://en.wikipedia.org/wiki/Object_oriented_language]&lt;/div&gt;</summary>
		<author><name>Cgoogc</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39389</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39389"/>
		<updated>2010-10-27T02:36:57Z</updated>

		<summary type="html">&lt;p&gt;Cgoogc: /* Dynamic Dispath */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Dynamic Dispatch ==&lt;br /&gt;
&lt;br /&gt;
=== Introduction ===&lt;br /&gt;
The process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function.  Dynamic Dispatch generally happens in OOL. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called overriding). When a reference to the super class is used to execute that function, then which version of the function gets execute depends on the type, the reference points to at that time, instead of type of the reference. In literature it also given different names like Runtime Binding or [Dynamic Polymorphism http://en.wikipedia.org/wiki/Dynamic_polymorphism].&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
=== Why we need Dynamic Dispatch ===&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object Oriented Languauge [1 http://en.wikipedia.org/wiki/Object_oriented_language]. &lt;br /&gt;
&lt;br /&gt;
=== How Dynamic Dispatch works ===&lt;br /&gt;
=== Dynamic Dispatch working in Detail ===&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
 &lt;br /&gt;
 Cat *c = new Cat(); &lt;br /&gt;
 &lt;br /&gt;
 Cat *c = malloc(sizeof(cat));&lt;br /&gt;
 c-&amp;gt;__id__ = 0;&lt;br /&gt;
&lt;br /&gt;
=== Performance Evaluation ===&lt;br /&gt;
=== Does Dynamic Dispatching hurts in today's power packed Computers? ===&lt;br /&gt;
=== Conclusion ===&lt;br /&gt;
=== Further Reading ===&lt;br /&gt;
=== References ===&lt;br /&gt;
*[1 http://en.wikipedia.org/wiki/Object_oriented_language]&lt;/div&gt;</summary>
		<author><name>Cgoogc</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39386</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39386"/>
		<updated>2010-10-27T02:09:07Z</updated>

		<summary type="html">&lt;p&gt;Cgoogc: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Dynamic Dispath ==&lt;br /&gt;
&lt;br /&gt;
=== Introduction ===&lt;br /&gt;
The process of identifying which version of the function with identifier will be executed during the runtime. Dynamic Dispatch generally happens in OOLS. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called overriding). When a reference to the super class is used to execute that function, then which version of the function gets execute depends on the type the reference points to at that time, instead of type of reference. In literature it also given different names like Runtime Binding or [Dynamic Polymorphism http://en.wikipedia.org/wiki/Dynamic_polymorphism].&lt;br /&gt;
&lt;br /&gt;
=== Why we need Dynamic Dispatch ===&lt;br /&gt;
=== How Dynamic Dispatch works ===&lt;br /&gt;
=== Dynamic Dispatch working in Detail ===&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
 &lt;br /&gt;
 Cat *c = new Cat(); &lt;br /&gt;
 &lt;br /&gt;
 Cat *c = malloc(sizeof(cat));&lt;br /&gt;
 c-&amp;gt;__id__ = 0;&lt;br /&gt;
&lt;br /&gt;
=== Performance Evaluation ===&lt;br /&gt;
=== Does Dynamic Dispatching hurts in today's power packed Computers? ===&lt;br /&gt;
=== Conclusion ===&lt;br /&gt;
=== Further Reading ===&lt;br /&gt;
=== References ===&lt;/div&gt;</summary>
		<author><name>Cgoogc</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39361</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39361"/>
		<updated>2010-10-25T20:47:56Z</updated>

		<summary type="html">&lt;p&gt;Cgoogc: /* Dynamic Dispatch working in Detail */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Dynamic Dispath ==&lt;br /&gt;
&lt;br /&gt;
=== Introduction ===&lt;br /&gt;
=== Why we need Dynamic Dispatch ===&lt;br /&gt;
=== How Dynamic Dispatch works ===&lt;br /&gt;
=== Dynamic Dispatch working in Detail ===&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
 &lt;br /&gt;
 Cat *c = new Cat(); &lt;br /&gt;
 &lt;br /&gt;
 Cat *c = malloc(sizeof(cat));&lt;br /&gt;
 c-&amp;gt;__id__ = 0;&lt;br /&gt;
&lt;br /&gt;
=== Performance Evaluation ===&lt;br /&gt;
=== Does Dynamic Dispatching hurts in today's power packed Computers? ===&lt;br /&gt;
=== Conclusion ===&lt;br /&gt;
=== Further Reading ===&lt;br /&gt;
=== References ===&lt;/div&gt;</summary>
		<author><name>Cgoogc</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39360</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39360"/>
		<updated>2010-10-25T20:18:54Z</updated>

		<summary type="html">&lt;p&gt;Cgoogc: Details of dynamic dispatch&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Dynamic Dispath ==&lt;br /&gt;
&lt;br /&gt;
=== Introduction ===&lt;br /&gt;
=== Why we need Dynamic Dispatch ===&lt;br /&gt;
=== How Dynamic Dispatch works ===&lt;br /&gt;
=== Dynamic Dispatch working in Detail ===&lt;br /&gt;
=== Performance Evaluation ===&lt;br /&gt;
=== Does Dynamic Dispatching hurts in today's power packed Computers? ===&lt;br /&gt;
=== Conclusion ===&lt;br /&gt;
=== Further Reading ===&lt;br /&gt;
=== References ===&lt;/div&gt;</summary>
		<author><name>Cgoogc</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_25_ag&amp;diff=36215</id>
		<title>CSC/ECE 517 Fall 2010/ch1 25 ag</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_25_ag&amp;diff=36215"/>
		<updated>2010-09-22T23:06:12Z</updated>

		<summary type="html">&lt;p&gt;Cgoogc: /* Implementation issues */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
= What is Inheritance? =&lt;br /&gt;
Inheritance, along with polymorphism and encapsulation, is one of the pillar features of [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch1_1e_bb#Object_Oriented_Programming Object Oriented Programming] (OOP). Inheritance allows a class to reuse, extend or modify the behavior of another class. The term inheritance generally refers to single inheritance, where a sub-class or a derived class inherits methods and attributes from only one parent class. &lt;br /&gt;
&lt;br /&gt;
For example, consider a general class of electrical appliances. Switching an appliance on and off can be considered a standard feature of every appliance. Now, appliances that control the temperature can be construed as electric appliances with some added functionality (increase/decrease temperature). Here is how they are implemented.&lt;br /&gt;
&lt;br /&gt;
 class ElectricAppliance&lt;br /&gt;
 {&lt;br /&gt;
     public void on() { ... }&lt;br /&gt;
     public void off() { ... }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class TempControlAppliance : public ElectricAppliance&lt;br /&gt;
 {&lt;br /&gt;
     public void increaseTemp() { .. }&lt;br /&gt;
     public void decreaseTemp() { .. }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Consider a task where two specific temperature control devices has to be modeled. Apart from being temperature control devices, they also implement specific tasks related to their domains, eg heating, cooling.&lt;br /&gt;
&lt;br /&gt;
 class Heater : public TempControlAppliance &lt;br /&gt;
 {&lt;br /&gt;
     public void heat() { .. }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Cooler : public TempControlAppliance &lt;br /&gt;
 {&lt;br /&gt;
     public void cool() { .. }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
If we don't have inheritance, we might have to duplicate all the functions in both Heater and Cooler class. Inheritance is very important to achieve DRY (Don't Repeat Yourself). In the above class hierarchy, TempControlAppliance is super (parent) class for both Cooler and Heater subclass (child).&lt;br /&gt;
&lt;br /&gt;
= What is Multiple Inheritance =&lt;br /&gt;
When a class inherits from more than one class i.e., a child class having more than one parent class, is called multiple inheritance. Though this feature offers more flexibility, it seem to overwhelm most of the developer, so some of the object oriented language designer decided not to support Multiple Inheritance. Java and C# are some of the most famous language which don't support multiple inheritance. We need careful design to take proper advantage of Multiple inheritance, otherwise you might end up in spaghetti code, with classes inheritancing from multiple classes.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Building upon the above example, let us say we have a new device called Air Conditioner which can both heat and cool air. We might have a new class called AirConditioner as follows:&lt;br /&gt;
&lt;br /&gt;
 class AirConditioner : public Heater, public Cooler&lt;br /&gt;
 {&lt;br /&gt;
      public void setThresholdTemperature(int temp) { ... }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
= Good and bad of Multiple Inheritance =&lt;br /&gt;
Good:&lt;br /&gt;
* Multiple Inheritance strongly supports DRY principle, which leads to better software which is extensible, cohesive, less coupled and modular. &lt;br /&gt;
* Multiple Inheritance is very common in real world, in which object takes attributes from different objects.&lt;br /&gt;
* Multiple Inheritance is more expressive than Single Inheritane, so it gives flexibility in designing software with complex object relationships.&lt;br /&gt;
&lt;br /&gt;
Bad:&lt;br /&gt;
* When a class extends two functions which has same function, it will have two version of the function with same name. There is an ambiguity in resolving which function to call. This is called [http://en.wikipedia.org/wiki/Diamond_problem Diamond Problem(3)]&lt;br /&gt;
* The class might have more than one path to the top-most parent class because of multiple Inheritance&lt;br /&gt;
* Typecasting will become ambiguous in Multiple Inheritance. Refer to [http://en.wikipedia.org/wiki/Virtual_inheritance Virtual Inheritance] for an example of this problem.&lt;br /&gt;
* To take complete advantage of Multiple Inheritance needs a thoughtful design. Without a good design, Multiple Inheritance infact might lead to bad code.&lt;br /&gt;
* Multiple Inheritance is overwhelming to developers. Requires more experience to properly use Multiple Inheritance.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Diamond Problem in Detail ==&lt;br /&gt;
One of the most common problem with Multiple Inheritance is called the [http://en.wikipedia.org/wiki/Diamond_problem Diamond Problem(3)]. This is encountered when a derived class inherits from multiple parent classes, which in turn inherit from one super class. Let us try to understand this problem using an example.&lt;br /&gt;
&lt;br /&gt;
Consider a task of modelling a car. Now a car can broadly be divided into two subclasses, mechanical cars and electric cars. &lt;br /&gt;
&lt;br /&gt;
 class Car&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
        void start() { cout &amp;lt;&amp;lt; &amp;quot;Start with loud noise&amp;quot;;&lt;br /&gt;
        void steer();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class MechCar : public Car&lt;br /&gt;
 {&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class ElectricCar : public Car&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
       void start() { cout &amp;lt;&amp;lt; &amp;quot;start without noise&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Now considering modern advancements in vehicle design, a new subclass of hybrid cars have emerged in the market. These cars have traits of both mechanical and electric cars. Here is how this is achieved in C++.&lt;br /&gt;
&lt;br /&gt;
 class HybridCar : public MechCar, ElectricCar&lt;br /&gt;
 {&lt;br /&gt;
      // This has two copies of start()&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Here is an illustration of the inheritance hierarchy.&lt;br /&gt;
&lt;br /&gt;
[[Image:Car-hybrid.jpg|frame|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now if an object of the HybridCar class calls a method of class Car, an ambiguous situation arises. Assuming the method has been [http://en.wikipedia.org/wiki/Method_overriding overridden] by class MechCar and ElectricCar, then it is not clear which version of the method the object is referring to. This is what is known as the Diamond Problem. This problem is addressed in C++ solves this problem through the use of  [http://en.wikipedia.org/wiki/Virtual_inheritance Virtual Inheritance]. &lt;br /&gt;
&lt;br /&gt;
 HybridCar h;&lt;br /&gt;
 h.ElectricCar::start();&lt;br /&gt;
&lt;br /&gt;
== Multiple Inheritance - Do we really need this? ==&lt;br /&gt;
Many languages which don't support multiple inheritance completely, has some ways of emulating multiple inheritance. We will consider Java as an example which does not support Multiple Inheritance completely and see how we can emulate multiple inheritance in Java. It is not completely true to say that Java does not support multiple inheritance, in fact with interfaces we can have multiple inheritance. Since, Interface just defines the behaviour, it does not have any attributes to describe it, Interfaces are not considered as a Class.&lt;br /&gt;
&lt;br /&gt;
Let us consider the previous example of an AirConditioner:&lt;br /&gt;
&lt;br /&gt;
 class AirConditioner extends [______________]&lt;br /&gt;
&lt;br /&gt;
We cannot fit both the classes there since a class in Java can extend from a single class, unlike what we did in C++. But Java classes can implement multiple interfaces.&lt;br /&gt;
&lt;br /&gt;
 public interface IHeater {&lt;br /&gt;
    void heat();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public interface ICooler {&lt;br /&gt;
    void cool();&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public Heater extends TempControllerAppliance implements IHeater {&lt;br /&gt;
    public void heat() { .. }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public Cooler extends TempControllerAppliance implements ICooler{&lt;br /&gt;
    public void cool() { .. }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public IAirConditioner {&lt;br /&gt;
      public void setThresholdTemp(int t);&lt;br /&gt;
 } &lt;br /&gt;
&lt;br /&gt;
 public class AirConditioner extends TempControllerAppliance implements IHeater, ICooler, IAirConditioner {&lt;br /&gt;
    private ICooler cooler = new Cooler();&lt;br /&gt;
    private IHeater heater = new Heater();&lt;br /&gt;
 &lt;br /&gt;
    public void cool() {&lt;br /&gt;
        cooler.cool();&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void heat() {&lt;br /&gt;
        heater.heat();&lt;br /&gt;
    }  &lt;br /&gt;
    &lt;br /&gt;
    public void setThreshold(int t) { ... }   &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
To implement multiple inheritance in Java, we need to the following:&lt;br /&gt;
1. Make every class implement an Interface&lt;br /&gt;
2. Classes extends these interfaces to add logic&lt;br /&gt;
3. When a class needs to extend from Multiple interfaces, you can implement those interfaces and delegate the call to the actual implementation. This is achieved through Composition.&lt;br /&gt;
&lt;br /&gt;
We can see that it is possible to emulate multiple inheritance through [http://en.wikipedia.org/wiki/Object_composition composition]. &lt;br /&gt;
&lt;br /&gt;
Let us take another example and see how we can emulate Multiple inheritance in Java. We are developing a paint application. We have separate tools to draw each primitive shapes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
 class Shape&lt;br /&gt;
 {&lt;br /&gt;
    //...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Square : Shape&lt;br /&gt;
 {&lt;br /&gt;
   // ..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Circle : Shape&lt;br /&gt;
 {&lt;br /&gt;
    //..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Triangle : Shape&lt;br /&gt;
 {&lt;br /&gt;
    //..&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
 class Painter&lt;br /&gt;
 {&lt;br /&gt;
     //..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class SquarePainter : Painter&lt;br /&gt;
 {&lt;br /&gt;
     void draw(Square s) { }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class CirclePainter : Painter&lt;br /&gt;
 {&lt;br /&gt;
    void draw(Circle c) { }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class TrianglePainter : Painter&lt;br /&gt;
 {&lt;br /&gt;
    void draw(Triangle t) { }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we need a canvas painter, which should have the capability to draw any of the above shape. In C++, this is pretty straight forward, you define a new class which extends all painters.&lt;br /&gt;
&lt;br /&gt;
 class CanvasPainter : SquarePainter, CirclePainter, TrianglePainter&lt;br /&gt;
 {&lt;br /&gt;
     // ..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
How can do the same in Java? Ofcourse, we will use composition, but how will you know which version of draw function to call? May be we can write some thing like this:&lt;br /&gt;
&lt;br /&gt;
 if (shape instanceof Triangle)&lt;br /&gt;
 {&lt;br /&gt;
       trianglePainter.draw((Triangle) shape);&lt;br /&gt;
 }&lt;br /&gt;
 else if(shape instanceof Circle)&lt;br /&gt;
 {&lt;br /&gt;
       circlePainter.draw((Circle) shape);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Unfortunately this is not a scalable solution and for every new tool we add to our paint program, we will have to change this code. This is where design patterns like [http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern Chain of Responsibility] proves helpful.&lt;br /&gt;
&lt;br /&gt;
Implementing the above code in Java using Chain of Responsibility design pattern.&lt;br /&gt;
&lt;br /&gt;
 interface IPainter&lt;br /&gt;
 {&lt;br /&gt;
     boolean canDraw(Shape s);&lt;br /&gt;
     void draw(Shape s);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
All painters will implement the above function:&lt;br /&gt;
&lt;br /&gt;
 class TrianglePainter implements IPainter&lt;br /&gt;
 {&lt;br /&gt;
    public boolean canDraw(Shape s) { return s instanceof Triangle; }&lt;br /&gt;
    public void draw(Shape s) { // ... }  &lt;br /&gt;
 } &lt;br /&gt;
&lt;br /&gt;
 class CanvasPainter implements IPainter&lt;br /&gt;
 {&lt;br /&gt;
    // List&amp;lt;IPainter&amp;gt; = ....&lt;br /&gt;
    // function to add and remove painters&lt;br /&gt;
    public void draw(Shape s)&lt;br /&gt;
    {&lt;br /&gt;
       for(Paint p : painters)&lt;br /&gt;
       { &lt;br /&gt;
            if(p.canDraw(s)) { p.draw(s); return; }&lt;br /&gt;
       }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
We can see that, chain of responsibility design pattern provides a very neat way to handle multiple inheritance in languages which don't support them completely.&lt;br /&gt;
&lt;br /&gt;
= Alternates to Multiple Inheritance =&lt;br /&gt;
* Composition &amp;amp; Interfaces&lt;br /&gt;
Compositions provides a neat way of emulating Multiple Inheritance. With Multiple Inheritance, we bring functionality from different classes to the class which extends them. We can achieve the same using Composition in which one class contains instance of other class, there by importing the capability of all those classes. Since a class is allowed to implement Multiple Interfaces, the class behaves as though it extended from Multiple classes.&lt;br /&gt;
&lt;br /&gt;
* Mixins&lt;br /&gt;
Some languages provide [http://en.wikipedia.org/wiki/Mixin mixins] as a language feature. Mixins provide another elegant mechanism which mimics Multiple Inheritance. The common problems with Mixin is that it pollutes the class namespace as it imports functions from different modules. This [http://www.artima.com/weblogs/viewpost.jsp?thread=246341 article] talks about the limitations of mixins in greater detail.&lt;br /&gt;
&lt;br /&gt;
* Extended Dynamic Proxies&lt;br /&gt;
Dynamic Proxy is a technique in which we can dynamically synthesis a new object from existing objects using reflection.  With Dynamic Proxies, we can dynamically make a class implement different interfaces. This is achieved through reflection mechanism. This [http://java.sys-con.com/node/37748 article] talks about using Extended Dynamic Proxies to achieve Multiple Inheritance in Java. But this rather a verbose and a complicated solution. This [http://www.java2s.com/Code/Java/Reflection/Aninvocationhandlerthatcountsthenumberofcallsforallmethodsinthetargetclass.htm article] discusses using Dynamic proxies in greater detail.&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
We discussed briefly about Inheritance, Multiple Inheritance, its advantages and disadvantages. Multiple Inheritance surely offers more flexibility in designing, but languages which don't support Multiple Inheritance has different ways of simulating Multiple Inheritance if required to do so.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table border = &amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;Single Inheritance&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;Multiple Inheritance&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Flexibility&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Single Inheritance does have some restriction in terms of modeling real world objects, but mitigate this problem with careful design&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Multiple Inheritance provides lots more flexibility for designing software&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Easy of Use&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Single Inheritance is much simpler to use&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Multiple Inheritance sometimes leaves the programmer confused. One of the common problem with Multiple inheritance in Diamond Problem&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Availability&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Single Inheritance is supported by all the Object Oriented Programming Languages&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Multiple Inheritance is not supported by few of the OOP Languages, because the designer of the language thought it makes the languauge complicated to use and not necessary for most scenarios.&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Efficiency&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;This actually language specific. But generally it takes less time to figure which version of the function to call in a single inheritance&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Virtual Function dispatch in C++ is more time consuming in Multiple Inheritance. For example Multiple Inheritance in C++, needs multiple vtable for dispatching virtual functions. [http://www.cs.ucsb.edu/~urs/oocsb/papers/oopsla96.pdf (1)] discusses about this problem in great detail.&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Further Reading =&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Virtual_inheritance Virtual Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_composition Composition]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Mixin Mixins]&lt;br /&gt;
&lt;br /&gt;
= Reference =&lt;br /&gt;
*[http://www.cs.ucsb.edu/~urs/oocsb/papers/oopsla96.pdf (1) Cost of Virtual Functions C++]&lt;br /&gt;
*[http://www.artima.com/weblogs/viewpost.jsp?thread=246341 (2) Mixins considered Harmful]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Diamond_problem (3) Diamond Problem with Multiple Inheritance]&lt;br /&gt;
*[http://java.sun.com/docs/white/langenv/ (4) The Java Language]&lt;/div&gt;</summary>
		<author><name>Cgoogc</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_25_ag&amp;diff=36213</id>
		<title>CSC/ECE 517 Fall 2010/ch1 25 ag</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_25_ag&amp;diff=36213"/>
		<updated>2010-09-22T23:05:22Z</updated>

		<summary type="html">&lt;p&gt;Cgoogc: /* Good and bad of Multiple Inheritance */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
= What is Inheritance? =&lt;br /&gt;
Inheritance, along with polymorphism and encapsulation, is one of the pillar features of [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch1_1e_bb#Object_Oriented_Programming Object Oriented Programming] (OOP). Inheritance allows a class to reuse, extend or modify the behavior of another class. The term inheritance generally refers to single inheritance, where a sub-class or a derived class inherits methods and attributes from only one parent class. &lt;br /&gt;
&lt;br /&gt;
For example, consider a general class of electrical appliances. Switching an appliance on and off can be considered a standard feature of every appliance. Now, appliances that control the temperature can be construed as electric appliances with some added functionality (increase/decrease temperature). Here is how they are implemented.&lt;br /&gt;
&lt;br /&gt;
 class ElectricAppliance&lt;br /&gt;
 {&lt;br /&gt;
     public void on() { ... }&lt;br /&gt;
     public void off() { ... }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class TempControlAppliance : public ElectricAppliance&lt;br /&gt;
 {&lt;br /&gt;
     public void increaseTemp() { .. }&lt;br /&gt;
     public void decreaseTemp() { .. }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Consider a task where two specific temperature control devices has to be modeled. Apart from being temperature control devices, they also implement specific tasks related to their domains, eg heating, cooling.&lt;br /&gt;
&lt;br /&gt;
 class Heater : public TempControlAppliance &lt;br /&gt;
 {&lt;br /&gt;
     public void heat() { .. }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Cooler : public TempControlAppliance &lt;br /&gt;
 {&lt;br /&gt;
     public void cool() { .. }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
If we don't have inheritance, we might have to duplicate all the functions in both Heater and Cooler class. Inheritance is very important to achieve DRY (Don't Repeat Yourself). In the above class hierarchy, TempControlAppliance is super (parent) class for both Cooler and Heater subclass (child).&lt;br /&gt;
&lt;br /&gt;
= What is Multiple Inheritance =&lt;br /&gt;
When a class inherits from more than one class i.e., a child class having more than one parent class, is called multiple inheritance. Though this feature offers more flexibility, it seem to overwhelm most of the developer, so some of the object oriented language designer decided not to support Multiple Inheritance. Java and C# are some of the most famous language which don't support multiple inheritance. We need careful design to take proper advantage of Multiple inheritance, otherwise you might end up in spaghetti code, with classes inheritancing from multiple classes.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Building upon the above example, let us say we have a new device called Air Conditioner which can both heat and cool air. We might have a new class called AirConditioner as follows:&lt;br /&gt;
&lt;br /&gt;
 class AirConditioner : public Heater, public Cooler&lt;br /&gt;
 {&lt;br /&gt;
      public void setThresholdTemperature(int temp) { ... }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
= Good and bad of Multiple Inheritance =&lt;br /&gt;
Good:&lt;br /&gt;
* Multiple Inheritance strongly supports DRY principle, which leads to better software which is extensible, cohesive, less coupled and modular. &lt;br /&gt;
* Multiple Inheritance is very common in real world, in which object takes attributes from different objects.&lt;br /&gt;
* Multiple Inheritance is more expressive than Single Inheritane, so it gives flexibility in designing software with complex object relationships.&lt;br /&gt;
&lt;br /&gt;
Bad:&lt;br /&gt;
* When a class extends two functions which has same function, it will have two version of the function with same name. There is an ambiguity in resolving which function to call. This is called [http://en.wikipedia.org/wiki/Diamond_problem Diamond Problem(3)]&lt;br /&gt;
* The class might have more than one path to the top-most parent class because of multiple Inheritance&lt;br /&gt;
* Typecasting will become ambiguous in Multiple Inheritance. Refer to [http://en.wikipedia.org/wiki/Virtual_inheritance Virtual Inheritance] for an example of this problem.&lt;br /&gt;
* To take complete advantage of Multiple Inheritance needs a thoughtful design. Without a good design, Multiple Inheritance infact might lead to bad code.&lt;br /&gt;
* Multiple Inheritance is overwhelming to developers. Requires more experience to properly use Multiple Inheritance.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Implementation issues ==&lt;br /&gt;
One of the most common problem with Multiple Inheritance is called the [http://en.wikipedia.org/wiki/Diamond_problem Diamond Problem(3)]. This is encountered when a derived class inherits from multiple parent classes, which in turn inherit from one super class. Let us try to understand this problem using an example.&lt;br /&gt;
&lt;br /&gt;
Consider a task of modelling a car. Now a car can broadly be divided into two subclasses, mechanical cars and electric cars. &lt;br /&gt;
&lt;br /&gt;
 class Car&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
        void start() { cout &amp;lt;&amp;lt; &amp;quot;Start with loud noise&amp;quot;;&lt;br /&gt;
        void steer();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class MechCar : public Car&lt;br /&gt;
 {&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class ElectricCar : public Car&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
       void start() { cout &amp;lt;&amp;lt; &amp;quot;start without noise&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Now considering modern advancements in vehicle design, a new subclass of hybrid cars have emerged in the market. These cars have traits of both mechanical and electric cars. Here is how this is achieved in C++.&lt;br /&gt;
&lt;br /&gt;
 class HybridCar : public MechCar, ElectricCar&lt;br /&gt;
 {&lt;br /&gt;
      // This has two copies of start()&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Here is an illustration of the inheritance hierarchy.&lt;br /&gt;
&lt;br /&gt;
[[Image:Car-hybrid.jpg|frame|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now if an object of the HybridCar class calls a method of class Car, an ambiguous situation arises. Assuming the method has been [http://en.wikipedia.org/wiki/Method_overriding overridden] by class MechCar and ElectricCar, then it is not clear which version of the method the object is referring to. This is what is known as the Diamond Problem. This problem is addressed in C++ solves this problem through the use of  [http://en.wikipedia.org/wiki/Virtual_inheritance Virtual Inheritance]. &lt;br /&gt;
&lt;br /&gt;
 HybridCar h;&lt;br /&gt;
 h.ElectricCar::start();&lt;br /&gt;
&lt;br /&gt;
There are also ambiguities during the type casting. For more details about Virtual Inheritance refer [http://en.wikipedia.org/wiki/Virtual_inheritance here]&lt;br /&gt;
&lt;br /&gt;
== Multiple Inheritance - Do we really need this? ==&lt;br /&gt;
Many languages which don't support multiple inheritance completely, has some ways of emulating multiple inheritance. We will consider Java as an example which does not support Multiple Inheritance completely and see how we can emulate multiple inheritance in Java. It is not completely true to say that Java does not support multiple inheritance, in fact with interfaces we can have multiple inheritance. Since, Interface just defines the behaviour, it does not have any attributes to describe it, Interfaces are not considered as a Class.&lt;br /&gt;
&lt;br /&gt;
Let us consider the previous example of an AirConditioner:&lt;br /&gt;
&lt;br /&gt;
 class AirConditioner extends [______________]&lt;br /&gt;
&lt;br /&gt;
We cannot fit both the classes there since a class in Java can extend from a single class, unlike what we did in C++. But Java classes can implement multiple interfaces.&lt;br /&gt;
&lt;br /&gt;
 public interface IHeater {&lt;br /&gt;
    void heat();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public interface ICooler {&lt;br /&gt;
    void cool();&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public Heater extends TempControllerAppliance implements IHeater {&lt;br /&gt;
    public void heat() { .. }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public Cooler extends TempControllerAppliance implements ICooler{&lt;br /&gt;
    public void cool() { .. }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public IAirConditioner {&lt;br /&gt;
      public void setThresholdTemp(int t);&lt;br /&gt;
 } &lt;br /&gt;
&lt;br /&gt;
 public class AirConditioner extends TempControllerAppliance implements IHeater, ICooler, IAirConditioner {&lt;br /&gt;
    private ICooler cooler = new Cooler();&lt;br /&gt;
    private IHeater heater = new Heater();&lt;br /&gt;
 &lt;br /&gt;
    public void cool() {&lt;br /&gt;
        cooler.cool();&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void heat() {&lt;br /&gt;
        heater.heat();&lt;br /&gt;
    }  &lt;br /&gt;
    &lt;br /&gt;
    public void setThreshold(int t) { ... }   &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
To implement multiple inheritance in Java, we need to the following:&lt;br /&gt;
1. Make every class implement an Interface&lt;br /&gt;
2. Classes extends these interfaces to add logic&lt;br /&gt;
3. When a class needs to extend from Multiple interfaces, you can implement those interfaces and delegate the call to the actual implementation. This is achieved through Composition.&lt;br /&gt;
&lt;br /&gt;
We can see that it is possible to emulate multiple inheritance through [http://en.wikipedia.org/wiki/Object_composition composition]. &lt;br /&gt;
&lt;br /&gt;
Let us take another example and see how we can emulate Multiple inheritance in Java. We are developing a paint application. We have separate tools to draw each primitive shapes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
 class Shape&lt;br /&gt;
 {&lt;br /&gt;
    //...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Square : Shape&lt;br /&gt;
 {&lt;br /&gt;
   // ..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Circle : Shape&lt;br /&gt;
 {&lt;br /&gt;
    //..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Triangle : Shape&lt;br /&gt;
 {&lt;br /&gt;
    //..&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
 class Painter&lt;br /&gt;
 {&lt;br /&gt;
     //..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class SquarePainter : Painter&lt;br /&gt;
 {&lt;br /&gt;
     void draw(Square s) { }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class CirclePainter : Painter&lt;br /&gt;
 {&lt;br /&gt;
    void draw(Circle c) { }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class TrianglePainter : Painter&lt;br /&gt;
 {&lt;br /&gt;
    void draw(Triangle t) { }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we need a canvas painter, which should have the capability to draw any of the above shape. In C++, this is pretty straight forward, you define a new class which extends all painters.&lt;br /&gt;
&lt;br /&gt;
 class CanvasPainter : SquarePainter, CirclePainter, TrianglePainter&lt;br /&gt;
 {&lt;br /&gt;
     // ..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
How can do the same in Java? Ofcourse, we will use composition, but how will you know which version of draw function to call? May be we can write some thing like this:&lt;br /&gt;
&lt;br /&gt;
 if (shape instanceof Triangle)&lt;br /&gt;
 {&lt;br /&gt;
       trianglePainter.draw((Triangle) shape);&lt;br /&gt;
 }&lt;br /&gt;
 else if(shape instanceof Circle)&lt;br /&gt;
 {&lt;br /&gt;
       circlePainter.draw((Circle) shape);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Unfortunately this is not a scalable solution and for every new tool we add to our paint program, we will have to change this code. This is where design patterns like [http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern Chain of Responsibility] proves helpful.&lt;br /&gt;
&lt;br /&gt;
Implementing the above code in Java using Chain of Responsibility design pattern.&lt;br /&gt;
&lt;br /&gt;
 interface IPainter&lt;br /&gt;
 {&lt;br /&gt;
     boolean canDraw(Shape s);&lt;br /&gt;
     void draw(Shape s);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
All painters will implement the above function:&lt;br /&gt;
&lt;br /&gt;
 class TrianglePainter implements IPainter&lt;br /&gt;
 {&lt;br /&gt;
    public boolean canDraw(Shape s) { return s instanceof Triangle; }&lt;br /&gt;
    public void draw(Shape s) { // ... }  &lt;br /&gt;
 } &lt;br /&gt;
&lt;br /&gt;
 class CanvasPainter implements IPainter&lt;br /&gt;
 {&lt;br /&gt;
    // List&amp;lt;IPainter&amp;gt; = ....&lt;br /&gt;
    // function to add and remove painters&lt;br /&gt;
    public void draw(Shape s)&lt;br /&gt;
    {&lt;br /&gt;
       for(Paint p : painters)&lt;br /&gt;
       { &lt;br /&gt;
            if(p.canDraw(s)) { p.draw(s); return; }&lt;br /&gt;
       }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
We can see that, chain of responsibility design pattern provides a very neat way to handle multiple inheritance in languages which don't support them completely.&lt;br /&gt;
&lt;br /&gt;
= Alternates to Multiple Inheritance =&lt;br /&gt;
* Composition &amp;amp; Interfaces&lt;br /&gt;
Compositions provides a neat way of emulating Multiple Inheritance. With Multiple Inheritance, we bring functionality from different classes to the class which extends them. We can achieve the same using Composition in which one class contains instance of other class, there by importing the capability of all those classes. Since a class is allowed to implement Multiple Interfaces, the class behaves as though it extended from Multiple classes.&lt;br /&gt;
&lt;br /&gt;
* Mixins&lt;br /&gt;
Some languages provide [http://en.wikipedia.org/wiki/Mixin mixins] as a language feature. Mixins provide another elegant mechanism which mimics Multiple Inheritance. The common problems with Mixin is that it pollutes the class namespace as it imports functions from different modules. This [http://www.artima.com/weblogs/viewpost.jsp?thread=246341 article] talks about the limitations of mixins in greater detail.&lt;br /&gt;
&lt;br /&gt;
* Extended Dynamic Proxies&lt;br /&gt;
Dynamic Proxy is a technique in which we can dynamically synthesis a new object from existing objects using reflection.  With Dynamic Proxies, we can dynamically make a class implement different interfaces. This is achieved through reflection mechanism. This [http://java.sys-con.com/node/37748 article] talks about using Extended Dynamic Proxies to achieve Multiple Inheritance in Java. But this rather a verbose and a complicated solution. This [http://www.java2s.com/Code/Java/Reflection/Aninvocationhandlerthatcountsthenumberofcallsforallmethodsinthetargetclass.htm article] discusses using Dynamic proxies in greater detail.&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
We discussed briefly about Inheritance, Multiple Inheritance, its advantages and disadvantages. Multiple Inheritance surely offers more flexibility in designing, but languages which don't support Multiple Inheritance has different ways of simulating Multiple Inheritance if required to do so.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table border = &amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;Single Inheritance&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;Multiple Inheritance&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Flexibility&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Single Inheritance does have some restriction in terms of modeling real world objects, but mitigate this problem with careful design&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Multiple Inheritance provides lots more flexibility for designing software&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Easy of Use&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Single Inheritance is much simpler to use&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Multiple Inheritance sometimes leaves the programmer confused. One of the common problem with Multiple inheritance in Diamond Problem&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Availability&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Single Inheritance is supported by all the Object Oriented Programming Languages&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Multiple Inheritance is not supported by few of the OOP Languages, because the designer of the language thought it makes the languauge complicated to use and not necessary for most scenarios.&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Efficiency&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;This actually language specific. But generally it takes less time to figure which version of the function to call in a single inheritance&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Virtual Function dispatch in C++ is more time consuming in Multiple Inheritance. For example Multiple Inheritance in C++, needs multiple vtable for dispatching virtual functions. [http://www.cs.ucsb.edu/~urs/oocsb/papers/oopsla96.pdf (1)] discusses about this problem in great detail.&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Further Reading =&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Virtual_inheritance Virtual Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_composition Composition]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Mixin Mixins]&lt;br /&gt;
&lt;br /&gt;
= Reference =&lt;br /&gt;
*[http://www.cs.ucsb.edu/~urs/oocsb/papers/oopsla96.pdf (1) Cost of Virtual Functions C++]&lt;br /&gt;
*[http://www.artima.com/weblogs/viewpost.jsp?thread=246341 (2) Mixins considered Harmful]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Diamond_problem (3) Diamond Problem with Multiple Inheritance]&lt;br /&gt;
*[http://java.sun.com/docs/white/langenv/ (4) The Java Language]&lt;/div&gt;</summary>
		<author><name>Cgoogc</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_25_ag&amp;diff=36212</id>
		<title>CSC/ECE 517 Fall 2010/ch1 25 ag</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_25_ag&amp;diff=36212"/>
		<updated>2010-09-22T23:04:48Z</updated>

		<summary type="html">&lt;p&gt;Cgoogc: /* Good and bad of Multiple Inheritance */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
= What is Inheritance? =&lt;br /&gt;
Inheritance, along with polymorphism and encapsulation, is one of the pillar features of [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch1_1e_bb#Object_Oriented_Programming Object Oriented Programming] (OOP). Inheritance allows a class to reuse, extend or modify the behavior of another class. The term inheritance generally refers to single inheritance, where a sub-class or a derived class inherits methods and attributes from only one parent class. &lt;br /&gt;
&lt;br /&gt;
For example, consider a general class of electrical appliances. Switching an appliance on and off can be considered a standard feature of every appliance. Now, appliances that control the temperature can be construed as electric appliances with some added functionality (increase/decrease temperature). Here is how they are implemented.&lt;br /&gt;
&lt;br /&gt;
 class ElectricAppliance&lt;br /&gt;
 {&lt;br /&gt;
     public void on() { ... }&lt;br /&gt;
     public void off() { ... }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class TempControlAppliance : public ElectricAppliance&lt;br /&gt;
 {&lt;br /&gt;
     public void increaseTemp() { .. }&lt;br /&gt;
     public void decreaseTemp() { .. }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Consider a task where two specific temperature control devices has to be modeled. Apart from being temperature control devices, they also implement specific tasks related to their domains, eg heating, cooling.&lt;br /&gt;
&lt;br /&gt;
 class Heater : public TempControlAppliance &lt;br /&gt;
 {&lt;br /&gt;
     public void heat() { .. }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Cooler : public TempControlAppliance &lt;br /&gt;
 {&lt;br /&gt;
     public void cool() { .. }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
If we don't have inheritance, we might have to duplicate all the functions in both Heater and Cooler class. Inheritance is very important to achieve DRY (Don't Repeat Yourself). In the above class hierarchy, TempControlAppliance is super (parent) class for both Cooler and Heater subclass (child).&lt;br /&gt;
&lt;br /&gt;
= What is Multiple Inheritance =&lt;br /&gt;
When a class inherits from more than one class i.e., a child class having more than one parent class, is called multiple inheritance. Though this feature offers more flexibility, it seem to overwhelm most of the developer, so some of the object oriented language designer decided not to support Multiple Inheritance. Java and C# are some of the most famous language which don't support multiple inheritance. We need careful design to take proper advantage of Multiple inheritance, otherwise you might end up in spaghetti code, with classes inheritancing from multiple classes.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Building upon the above example, let us say we have a new device called Air Conditioner which can both heat and cool air. We might have a new class called AirConditioner as follows:&lt;br /&gt;
&lt;br /&gt;
 class AirConditioner : public Heater, public Cooler&lt;br /&gt;
 {&lt;br /&gt;
      public void setThresholdTemperature(int temp) { ... }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
= Good and bad of Multiple Inheritance =&lt;br /&gt;
Good:&lt;br /&gt;
* Multiple Inheritance strongly supports DRY principle, which leads to better software which is extensible, cohesive, less coupled and modular. &lt;br /&gt;
* Multiple Inheritance is very common in real world, in which object takes attributes from different objects.&lt;br /&gt;
* Multiple Inheritance is more expressive than Single Inheritane, so it gives flexibility in designing software with complex object relationships.&lt;br /&gt;
&lt;br /&gt;
Bad:&lt;br /&gt;
* When a class extends two functions which has same function, it will have two version of the function with same. There is an ambiguity in resolving which function to call. This is called [http://en.wikipedia.org/wiki/Diamond_problem Diamond Problem(3)]&lt;br /&gt;
* The class might have more than one path to the top-most parent class because of multiple Inheritance&lt;br /&gt;
* Typecasting will become ambiguous in Multiple Inheritance. Refer to [http://en.wikipedia.org/wiki/Virtual_inheritance Virtual Inheritance] for an example of this problem.&lt;br /&gt;
* To take complete advantage of Multiple Inheritance needs a thoughtful design. Without a good design, Multiple Inheritance infact might lead to bad code.&lt;br /&gt;
* Multiple Inheritance is overwhelming to developers. Requires more experience to properly use Multiple Inheritance.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Implementation issues ==&lt;br /&gt;
One of the most common problem with Multiple Inheritance is called the [http://en.wikipedia.org/wiki/Diamond_problem Diamond Problem(3)]. This is encountered when a derived class inherits from multiple parent classes, which in turn inherit from one super class. Let us try to understand this problem using an example.&lt;br /&gt;
&lt;br /&gt;
Consider a task of modelling a car. Now a car can broadly be divided into two subclasses, mechanical cars and electric cars. &lt;br /&gt;
&lt;br /&gt;
 class Car&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
        void start() { cout &amp;lt;&amp;lt; &amp;quot;Start with loud noise&amp;quot;;&lt;br /&gt;
        void steer();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class MechCar : public Car&lt;br /&gt;
 {&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class ElectricCar : public Car&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
       void start() { cout &amp;lt;&amp;lt; &amp;quot;start without noise&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Now considering modern advancements in vehicle design, a new subclass of hybrid cars have emerged in the market. These cars have traits of both mechanical and electric cars. Here is how this is achieved in C++.&lt;br /&gt;
&lt;br /&gt;
 class HybridCar : public MechCar, ElectricCar&lt;br /&gt;
 {&lt;br /&gt;
      // This has two copies of start()&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Here is an illustration of the inheritance hierarchy.&lt;br /&gt;
&lt;br /&gt;
[[Image:Car-hybrid.jpg|frame|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now if an object of the HybridCar class calls a method of class Car, an ambiguous situation arises. Assuming the method has been [http://en.wikipedia.org/wiki/Method_overriding overridden] by class MechCar and ElectricCar, then it is not clear which version of the method the object is referring to. This is what is known as the Diamond Problem. This problem is addressed in C++ solves this problem through the use of  [http://en.wikipedia.org/wiki/Virtual_inheritance Virtual Inheritance]. &lt;br /&gt;
&lt;br /&gt;
 HybridCar h;&lt;br /&gt;
 h.ElectricCar::start();&lt;br /&gt;
&lt;br /&gt;
There are also ambiguities during the type casting. For more details about Virtual Inheritance refer [http://en.wikipedia.org/wiki/Virtual_inheritance here]&lt;br /&gt;
&lt;br /&gt;
== Multiple Inheritance - Do we really need this? ==&lt;br /&gt;
Many languages which don't support multiple inheritance completely, has some ways of emulating multiple inheritance. We will consider Java as an example which does not support Multiple Inheritance completely and see how we can emulate multiple inheritance in Java. It is not completely true to say that Java does not support multiple inheritance, in fact with interfaces we can have multiple inheritance. Since, Interface just defines the behaviour, it does not have any attributes to describe it, Interfaces are not considered as a Class.&lt;br /&gt;
&lt;br /&gt;
Let us consider the previous example of an AirConditioner:&lt;br /&gt;
&lt;br /&gt;
 class AirConditioner extends [______________]&lt;br /&gt;
&lt;br /&gt;
We cannot fit both the classes there since a class in Java can extend from a single class, unlike what we did in C++. But Java classes can implement multiple interfaces.&lt;br /&gt;
&lt;br /&gt;
 public interface IHeater {&lt;br /&gt;
    void heat();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public interface ICooler {&lt;br /&gt;
    void cool();&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public Heater extends TempControllerAppliance implements IHeater {&lt;br /&gt;
    public void heat() { .. }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public Cooler extends TempControllerAppliance implements ICooler{&lt;br /&gt;
    public void cool() { .. }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public IAirConditioner {&lt;br /&gt;
      public void setThresholdTemp(int t);&lt;br /&gt;
 } &lt;br /&gt;
&lt;br /&gt;
 public class AirConditioner extends TempControllerAppliance implements IHeater, ICooler, IAirConditioner {&lt;br /&gt;
    private ICooler cooler = new Cooler();&lt;br /&gt;
    private IHeater heater = new Heater();&lt;br /&gt;
 &lt;br /&gt;
    public void cool() {&lt;br /&gt;
        cooler.cool();&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void heat() {&lt;br /&gt;
        heater.heat();&lt;br /&gt;
    }  &lt;br /&gt;
    &lt;br /&gt;
    public void setThreshold(int t) { ... }   &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
To implement multiple inheritance in Java, we need to the following:&lt;br /&gt;
1. Make every class implement an Interface&lt;br /&gt;
2. Classes extends these interfaces to add logic&lt;br /&gt;
3. When a class needs to extend from Multiple interfaces, you can implement those interfaces and delegate the call to the actual implementation. This is achieved through Composition.&lt;br /&gt;
&lt;br /&gt;
We can see that it is possible to emulate multiple inheritance through [http://en.wikipedia.org/wiki/Object_composition composition]. &lt;br /&gt;
&lt;br /&gt;
Let us take another example and see how we can emulate Multiple inheritance in Java. We are developing a paint application. We have separate tools to draw each primitive shapes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
 class Shape&lt;br /&gt;
 {&lt;br /&gt;
    //...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Square : Shape&lt;br /&gt;
 {&lt;br /&gt;
   // ..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Circle : Shape&lt;br /&gt;
 {&lt;br /&gt;
    //..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Triangle : Shape&lt;br /&gt;
 {&lt;br /&gt;
    //..&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
 class Painter&lt;br /&gt;
 {&lt;br /&gt;
     //..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class SquarePainter : Painter&lt;br /&gt;
 {&lt;br /&gt;
     void draw(Square s) { }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class CirclePainter : Painter&lt;br /&gt;
 {&lt;br /&gt;
    void draw(Circle c) { }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class TrianglePainter : Painter&lt;br /&gt;
 {&lt;br /&gt;
    void draw(Triangle t) { }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we need a canvas painter, which should have the capability to draw any of the above shape. In C++, this is pretty straight forward, you define a new class which extends all painters.&lt;br /&gt;
&lt;br /&gt;
 class CanvasPainter : SquarePainter, CirclePainter, TrianglePainter&lt;br /&gt;
 {&lt;br /&gt;
     // ..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
How can do the same in Java? Ofcourse, we will use composition, but how will you know which version of draw function to call? May be we can write some thing like this:&lt;br /&gt;
&lt;br /&gt;
 if (shape instanceof Triangle)&lt;br /&gt;
 {&lt;br /&gt;
       trianglePainter.draw((Triangle) shape);&lt;br /&gt;
 }&lt;br /&gt;
 else if(shape instanceof Circle)&lt;br /&gt;
 {&lt;br /&gt;
       circlePainter.draw((Circle) shape);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Unfortunately this is not a scalable solution and for every new tool we add to our paint program, we will have to change this code. This is where design patterns like [http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern Chain of Responsibility] proves helpful.&lt;br /&gt;
&lt;br /&gt;
Implementing the above code in Java using Chain of Responsibility design pattern.&lt;br /&gt;
&lt;br /&gt;
 interface IPainter&lt;br /&gt;
 {&lt;br /&gt;
     boolean canDraw(Shape s);&lt;br /&gt;
     void draw(Shape s);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
All painters will implement the above function:&lt;br /&gt;
&lt;br /&gt;
 class TrianglePainter implements IPainter&lt;br /&gt;
 {&lt;br /&gt;
    public boolean canDraw(Shape s) { return s instanceof Triangle; }&lt;br /&gt;
    public void draw(Shape s) { // ... }  &lt;br /&gt;
 } &lt;br /&gt;
&lt;br /&gt;
 class CanvasPainter implements IPainter&lt;br /&gt;
 {&lt;br /&gt;
    // List&amp;lt;IPainter&amp;gt; = ....&lt;br /&gt;
    // function to add and remove painters&lt;br /&gt;
    public void draw(Shape s)&lt;br /&gt;
    {&lt;br /&gt;
       for(Paint p : painters)&lt;br /&gt;
       { &lt;br /&gt;
            if(p.canDraw(s)) { p.draw(s); return; }&lt;br /&gt;
       }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
We can see that, chain of responsibility design pattern provides a very neat way to handle multiple inheritance in languages which don't support them completely.&lt;br /&gt;
&lt;br /&gt;
= Alternates to Multiple Inheritance =&lt;br /&gt;
* Composition &amp;amp; Interfaces&lt;br /&gt;
Compositions provides a neat way of emulating Multiple Inheritance. With Multiple Inheritance, we bring functionality from different classes to the class which extends them. We can achieve the same using Composition in which one class contains instance of other class, there by importing the capability of all those classes. Since a class is allowed to implement Multiple Interfaces, the class behaves as though it extended from Multiple classes.&lt;br /&gt;
&lt;br /&gt;
* Mixins&lt;br /&gt;
Some languages provide [http://en.wikipedia.org/wiki/Mixin mixins] as a language feature. Mixins provide another elegant mechanism which mimics Multiple Inheritance. The common problems with Mixin is that it pollutes the class namespace as it imports functions from different modules. This [http://www.artima.com/weblogs/viewpost.jsp?thread=246341 article] talks about the limitations of mixins in greater detail.&lt;br /&gt;
&lt;br /&gt;
* Extended Dynamic Proxies&lt;br /&gt;
Dynamic Proxy is a technique in which we can dynamically synthesis a new object from existing objects using reflection.  With Dynamic Proxies, we can dynamically make a class implement different interfaces. This is achieved through reflection mechanism. This [http://java.sys-con.com/node/37748 article] talks about using Extended Dynamic Proxies to achieve Multiple Inheritance in Java. But this rather a verbose and a complicated solution. This [http://www.java2s.com/Code/Java/Reflection/Aninvocationhandlerthatcountsthenumberofcallsforallmethodsinthetargetclass.htm article] discusses using Dynamic proxies in greater detail.&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
We discussed briefly about Inheritance, Multiple Inheritance, its advantages and disadvantages. Multiple Inheritance surely offers more flexibility in designing, but languages which don't support Multiple Inheritance has different ways of simulating Multiple Inheritance if required to do so.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table border = &amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;Single Inheritance&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;Multiple Inheritance&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Flexibility&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Single Inheritance does have some restriction in terms of modeling real world objects, but mitigate this problem with careful design&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Multiple Inheritance provides lots more flexibility for designing software&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Easy of Use&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Single Inheritance is much simpler to use&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Multiple Inheritance sometimes leaves the programmer confused. One of the common problem with Multiple inheritance in Diamond Problem&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Availability&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Single Inheritance is supported by all the Object Oriented Programming Languages&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Multiple Inheritance is not supported by few of the OOP Languages, because the designer of the language thought it makes the languauge complicated to use and not necessary for most scenarios.&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Efficiency&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;This actually language specific. But generally it takes less time to figure which version of the function to call in a single inheritance&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Virtual Function dispatch in C++ is more time consuming in Multiple Inheritance. For example Multiple Inheritance in C++, needs multiple vtable for dispatching virtual functions. [http://www.cs.ucsb.edu/~urs/oocsb/papers/oopsla96.pdf (1)] discusses about this problem in great detail.&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Further Reading =&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Virtual_inheritance Virtual Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_composition Composition]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Mixin Mixins]&lt;br /&gt;
&lt;br /&gt;
= Reference =&lt;br /&gt;
*[http://www.cs.ucsb.edu/~urs/oocsb/papers/oopsla96.pdf (1) Cost of Virtual Functions C++]&lt;br /&gt;
*[http://www.artima.com/weblogs/viewpost.jsp?thread=246341 (2) Mixins considered Harmful]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Diamond_problem (3) Diamond Problem with Multiple Inheritance]&lt;br /&gt;
*[http://java.sun.com/docs/white/langenv/ (4) The Java Language]&lt;/div&gt;</summary>
		<author><name>Cgoogc</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_25_ag&amp;diff=35996</id>
		<title>CSC/ECE 517 Fall 2010/ch1 25 ag</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_25_ag&amp;diff=35996"/>
		<updated>2010-09-22T14:33:20Z</updated>

		<summary type="html">&lt;p&gt;Cgoogc: /* Conclusion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
= What is Inheritance? =&lt;br /&gt;
Inheritance, along with polymorphism and encapsulation, is one of the pillar features of [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch1_1e_bb#Object_Oriented_Programming Object Oriented Programming] (OOP). Inheritance allows a class to reuse, extend or modify the behavior of another class. The term inheritance generally refers to single inheritance, where a sub-class or a derived class inherits methods and attributes from only one parent class. &lt;br /&gt;
&lt;br /&gt;
For example, consider a general class of electrical appliances. Switching an appliance on and off can be considered a standard feature of every appliance.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 class ElectricAppliance&lt;br /&gt;
 {&lt;br /&gt;
     public void on() { ... }&lt;br /&gt;
     public void off() { ... }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class TempControlAppliance : public ElectricAppliance&lt;br /&gt;
 {&lt;br /&gt;
     public void increaseTemp() { .. }&lt;br /&gt;
     public void decreaseTemp() { .. }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Heater : public TempControlAppliance &lt;br /&gt;
 {&lt;br /&gt;
     public void heat() { .. }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Cooler : public TempControlAppliance &lt;br /&gt;
 {&lt;br /&gt;
     public void cool() { .. }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
If we don't have inheritance, we might have to duplicate all the functions in both Heater and Cooler class. Inheritance is very important to achieve DRY (Don't Repeat Yourself). In the above class hierarchy, TempControlAppliance is super (parent) class for both Cooler and Heater subclass (child).&lt;br /&gt;
&lt;br /&gt;
= What is Multiple Inheritance =&lt;br /&gt;
When a class inherits from more than one class i.e., a child class having more than one parent class, is called multiple inheritance. Though this feature offers more flexibility, it seem to overwhelm most of the developer, so some of the object oriented language designer decided not to support Multiple Inheritance. Java and C# are some of the most famous language which don't support multiple inheritance. We need careful design to take proper advantage of Multiple inheritance, otherwise you might end up in spaghetti code, with classes inheritancing from multiple classes.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Building upon the above example, let us say we have a new device called Air Conditioner which can both heat and cool air. We might have a new class called AirConditioner as follows:&lt;br /&gt;
&lt;br /&gt;
 class AirConditioner : public Heater, public Cooler&lt;br /&gt;
 {&lt;br /&gt;
      public void setThresholdTemperature(int temp) { ... }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
= Good and bad of Multiple Inheritance =&lt;br /&gt;
Good:&lt;br /&gt;
* Multiple Inheritance strongly supports DRY principle, which leads to better software which is extensible, cohesive, less coupled and modular. &lt;br /&gt;
* Multiple Inheritance is very common in real world, in which object takes attributes from different objects.&lt;br /&gt;
* Multiple Inheritance is more expressive than Single Inheritane, so it gives flexibility in designing software with complex object relationships.&lt;br /&gt;
&lt;br /&gt;
Bad:&lt;br /&gt;
* To take complete advantage of Multiple Inheritance needs a thoughtful design. Without a good design, Multiple Inheritance infact might lead to bad code.&lt;br /&gt;
* Multiple Inheritance is overwhelming to developers. Requires more experience to properly use Multiple Inheritance.&lt;br /&gt;
* Multiple Inheritance is hard to use and make the language complicated. Some example like what happens when you Inherit from multiple classes which has same method?&lt;br /&gt;
&lt;br /&gt;
== Implementation issues ==&lt;br /&gt;
One of the most common problem with Multiple Inheritance is called the [http://en.wikipedia.org/wiki/Diamond_problem Diamond Problem(3)]. This is encountered when a derived class inherits from multiple parent classes, which in turn inherit from one super class. Let us try to understand this problem using an example.&lt;br /&gt;
&lt;br /&gt;
Consider a task of modelling a car. Now a car can broadly be divided into two subclasses, mechanical cars and electric cars. &lt;br /&gt;
&lt;br /&gt;
 class Car&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
        void start() { cout &amp;lt;&amp;lt; &amp;quot;Start with loud noise&amp;quot;;&lt;br /&gt;
        void steer();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class MechCar : public Car&lt;br /&gt;
 {&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class ElectricCar : public Car&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
       void start() { cout &amp;lt;&amp;lt; &amp;quot;start without noise&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Now considering modern advancements in vehicle design, a new subclass of hybrid cars have emerged in the market. These cars have traits of both mechanical and electric cars. Here is how this is achieved in C++.&lt;br /&gt;
&lt;br /&gt;
 class HybridCar : public MechCar, ElectricCar&lt;br /&gt;
 {&lt;br /&gt;
      // This has two copies of start()&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Here is an illustration of the inheritance hierarchy.&lt;br /&gt;
&lt;br /&gt;
[[Image:Car-hybrid.jpg|frame|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now if an object of the HybridCar class calls a method of class Car, an ambiguous situation arises. Assuming the method has been [http://en.wikipedia.org/wiki/Method_overriding overridden] by class MechCar and ElectricCar, then it is not clear which version of the method the object is referring to. This is what is known as the Diamond Problem. This problem is addressed in C++ solves this problem through the use of  [http://en.wikipedia.org/wiki/Virtual_inheritance Virtual Inheritance]. &lt;br /&gt;
&lt;br /&gt;
 HybridCar h;&lt;br /&gt;
 h.ElectricCar::start();&lt;br /&gt;
&lt;br /&gt;
There are also ambiguities during the type casting. For more details about Virtual Inheritance refer [http://en.wikipedia.org/wiki/Virtual_inheritance here]&lt;br /&gt;
&lt;br /&gt;
== Multiple Inheritance - Do we really need this? ==&lt;br /&gt;
Many languages which don't support multiple inheritance completely, has some ways of emulating multiple inheritance. We will consider Java as an example which does not support Multiple Inheritance completely and see how we can emulate multiple inheritance in Java. It is not completely true to say that Java does not support multiple inheritance, in fact with interfaces we can have multiple inheritance. Since, Interface just defines the behaviour, it does not have any attributes to describe it, Interfaces are not considered as a Class.&lt;br /&gt;
&lt;br /&gt;
Let us consider the previous example of an AirConditioner:&lt;br /&gt;
&lt;br /&gt;
 class AirConditioner extends [______________]&lt;br /&gt;
&lt;br /&gt;
We cannot fit both the classes there since a class in Java can extend from a single class, unlike what we did in C++. But Java classes can implement multiple interfaces.&lt;br /&gt;
&lt;br /&gt;
 public interface IHeater {&lt;br /&gt;
    void heat();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public interface ICooler {&lt;br /&gt;
    void cool();&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public Heater extends TempControllerAppliance implements IHeater {&lt;br /&gt;
    public void heat() { .. }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public Cooler extends TempControllerAppliance implements ICooler{&lt;br /&gt;
    public void cool() { .. }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public IAirConditioner {&lt;br /&gt;
      public void setThresholdTemp(int t);&lt;br /&gt;
 } &lt;br /&gt;
&lt;br /&gt;
 public class AirConditioner extends TempControllerAppliance implements IHeater, ICooler, IAirConditioner {&lt;br /&gt;
    private ICooler cooler = new Cooler();&lt;br /&gt;
    private IHeater heater = new Heater();&lt;br /&gt;
 &lt;br /&gt;
    public void cool() {&lt;br /&gt;
        cooler.cool();&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void heat() {&lt;br /&gt;
        heater.heat();&lt;br /&gt;
    }  &lt;br /&gt;
    &lt;br /&gt;
    public void setThreshold(int t) { ... }   &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
To implement multiple inheritance in Java, we need to the following:&lt;br /&gt;
1. Make every class implement an Interface&lt;br /&gt;
2. Classes extends these interfaces to add logic&lt;br /&gt;
3. When a class needs to extend from Multiple interfaces, you can implement those interfaces and delegate the call to the actual implementation. This is achieved through Composition.&lt;br /&gt;
&lt;br /&gt;
We can see that it is possible to emulate multiple inheritance through [http://en.wikipedia.org/wiki/Object_composition composition]. &lt;br /&gt;
&lt;br /&gt;
Let us take another example and see how we can emulate Multiple inheritance in Java. We are developing a paint application. We have separate tools to draw each primitive shapes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
 class Shape&lt;br /&gt;
 {&lt;br /&gt;
    //...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Square : Shape&lt;br /&gt;
 {&lt;br /&gt;
   // ..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Circle : Shape&lt;br /&gt;
 {&lt;br /&gt;
    //..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Triangle : Shape&lt;br /&gt;
 {&lt;br /&gt;
    //..&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
 class Painter&lt;br /&gt;
 {&lt;br /&gt;
     //..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class SquarePainter : Painter&lt;br /&gt;
 {&lt;br /&gt;
     void draw(Square s) { }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class CirclePainter : Painter&lt;br /&gt;
 {&lt;br /&gt;
    void draw(Circle c) { }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class TrianglePainter : Painter&lt;br /&gt;
 {&lt;br /&gt;
    void draw(Triangle t) { }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we need a canvas painter, which should have the capability to draw any of the above shape. In C++, this is pretty straight forward, you define a new class which extends all painters.&lt;br /&gt;
&lt;br /&gt;
 class CanvasPainter : SquarePainter, CirclePainter, TrianglePainter&lt;br /&gt;
 {&lt;br /&gt;
     // ..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
How can do the same in Java? Ofcourse, we will use composition, but how will you know which version of draw function to call? May be we can write some thing like this:&lt;br /&gt;
&lt;br /&gt;
 if (shape instanceof Triangle)&lt;br /&gt;
 {&lt;br /&gt;
       trianglePainter.draw((Triangle) shape);&lt;br /&gt;
 }&lt;br /&gt;
 else if(shape instanceof Circle)&lt;br /&gt;
 {&lt;br /&gt;
       circlePainter.draw((Circle) shape);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Unfortunately this is not a scalable solution and for every new tool we add to our paint program, we will have to change this code. This is where design patterns like [http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern Chain of Responsibility] proves helpful.&lt;br /&gt;
&lt;br /&gt;
Implementing the above code in Java using Chain of Responsibility design pattern.&lt;br /&gt;
&lt;br /&gt;
 interface IPainter&lt;br /&gt;
 {&lt;br /&gt;
     boolean canDraw(Shape s);&lt;br /&gt;
     void draw(Shape s);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
All painters will implement the above function:&lt;br /&gt;
&lt;br /&gt;
 class TrianglePainter implements IPainter&lt;br /&gt;
 {&lt;br /&gt;
    public boolean canDraw(Shape s) { return s instanceof Triangle; }&lt;br /&gt;
    public void draw(Shape s) { // ... }  &lt;br /&gt;
 } &lt;br /&gt;
&lt;br /&gt;
 class CanvasPainter implements IPainter&lt;br /&gt;
 {&lt;br /&gt;
    // List&amp;lt;IPainter&amp;gt; = ....&lt;br /&gt;
    // function to add and remove painters&lt;br /&gt;
    public void draw(Shape s)&lt;br /&gt;
    {&lt;br /&gt;
       for(Paint p : painters)&lt;br /&gt;
       { &lt;br /&gt;
            if(p.canDraw(s)) { p.draw(s); return; }&lt;br /&gt;
       }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
We can see that, chain of responsibility design pattern provides a very neat way to handle multiple inheritance in languages which don't support them completely.&lt;br /&gt;
&lt;br /&gt;
= Alternates to Multiple Inheritance =&lt;br /&gt;
* Composition &amp;amp; Interfaces&lt;br /&gt;
Compositions provides a neat way of emulating Multiple Inheritance. With Multiple Inheritance, we bring functionality from different classes to the class which extends them. We can achieve the same using Composition in which one class contains instance of other class, there by importing the capability of all those classes. Since a class is allowed to implement Multiple Interfaces, the class behaves as though it extended from Multiple classes.&lt;br /&gt;
&lt;br /&gt;
* Mixins&lt;br /&gt;
Some languages provide [http://en.wikipedia.org/wiki/Mixin mixins] as a language feature. Mixins provide another elegant mechanism which mimics Multiple Inheritance. The common problems with Mixin is that it pollutes the class namespace as it imports functions from different modules. This [http://www.artima.com/weblogs/viewpost.jsp?thread=246341 article] talks about the limitations of mixins in greater detail.&lt;br /&gt;
&lt;br /&gt;
* Extended Dynamic Proxies&lt;br /&gt;
Dynamic Proxy is a technique in which we can dynamically synthesis a new object from existing objects using reflection.  With Dynamic Proxies, we can dynamically make a class implement different interfaces. This is achieved through reflection mechanism. This [http://java.sys-con.com/node/37748 article] talks about using Extended Dynamic Proxies to achieve Multiple Inheritance in Java. But this rather a verbose and a complicated solution. This [http://www.java2s.com/Code/Java/Reflection/Aninvocationhandlerthatcountsthenumberofcallsforallmethodsinthetargetclass.htm article] discusses using Dynamic proxies in greater detail.&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
We discussed briefly about Inheritance, Multiple Inheritance, its advantages and disadvantages. Multiple Inheritance surely offers more flexibility in designing, but languages which don't support Multiple Inheritance has different ways of simulating Multiple Inheritance if required to do so.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table border = &amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;Single Inheritance&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;Multiple Inheritance&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Flexibility&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Single Inheritance does have some restriction in terms of modeling real world objects, but mitigate this problem with careful design&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Multiple Inheritance provides lots more flexibility for designing software&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Easy of Use&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Single Inheritance is much simpler to use&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Multiple Inheritance sometimes leaves the programmer confused. One of the common problem with Multiple inheritance in Diamond Problem&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Availability&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Single Inheritance is supported by all the Object Oriented Programming Languages&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Multiple Inheritance is not supported by few of the OOP Languages, because the designer of the language thought it makes the languauge complicated to use and not necessary for most scenarios.&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Efficiency&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;This actually language specific. But generally it takes less time to figure which version of the function to call in a single inheritance&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Virtual Function dispatch in C++ is more time consuming in Multiple Inheritance. For example Multiple Inheritance in C++, needs multiple vtable for dispatching virtual functions. [http://www.cs.ucsb.edu/~urs/oocsb/papers/oopsla96.pdf (1)] discusses about this problem in great detail.&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Further Reading =&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Virtual_inheritance Virtual Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_composition Composition]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Mixin Mixins]&lt;br /&gt;
&lt;br /&gt;
= Reference =&lt;br /&gt;
*[http://www.cs.ucsb.edu/~urs/oocsb/papers/oopsla96.pdf (1) Cost of Virtual Functions C++]&lt;br /&gt;
*[http://www.artima.com/weblogs/viewpost.jsp?thread=246341 (2) Mixins considered Harmful]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Diamond_problem (3) Diamond Problem with Multiple Inheritance]&lt;br /&gt;
*[http://java.sun.com/docs/white/langenv/ (4) The Java Language]&lt;/div&gt;</summary>
		<author><name>Cgoogc</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_25_ag&amp;diff=35995</id>
		<title>CSC/ECE 517 Fall 2010/ch1 25 ag</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_25_ag&amp;diff=35995"/>
		<updated>2010-09-22T14:33:13Z</updated>

		<summary type="html">&lt;p&gt;Cgoogc: /* Conclusion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
= What is Inheritance? =&lt;br /&gt;
Inheritance, along with polymorphism and encapsulation, is one of the pillar features of [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch1_1e_bb#Object_Oriented_Programming Object Oriented Programming] (OOP). Inheritance allows a class to reuse, extend or modify the behavior of another class. The term inheritance generally refers to single inheritance, where a sub-class or a derived class inherits methods and attributes from only one parent class. &lt;br /&gt;
&lt;br /&gt;
For example, consider a general class of electrical appliances. Switching an appliance on and off can be considered a standard feature of every appliance.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 class ElectricAppliance&lt;br /&gt;
 {&lt;br /&gt;
     public void on() { ... }&lt;br /&gt;
     public void off() { ... }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class TempControlAppliance : public ElectricAppliance&lt;br /&gt;
 {&lt;br /&gt;
     public void increaseTemp() { .. }&lt;br /&gt;
     public void decreaseTemp() { .. }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Heater : public TempControlAppliance &lt;br /&gt;
 {&lt;br /&gt;
     public void heat() { .. }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Cooler : public TempControlAppliance &lt;br /&gt;
 {&lt;br /&gt;
     public void cool() { .. }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
If we don't have inheritance, we might have to duplicate all the functions in both Heater and Cooler class. Inheritance is very important to achieve DRY (Don't Repeat Yourself). In the above class hierarchy, TempControlAppliance is super (parent) class for both Cooler and Heater subclass (child).&lt;br /&gt;
&lt;br /&gt;
= What is Multiple Inheritance =&lt;br /&gt;
When a class inherits from more than one class i.e., a child class having more than one parent class, is called multiple inheritance. Though this feature offers more flexibility, it seem to overwhelm most of the developer, so some of the object oriented language designer decided not to support Multiple Inheritance. Java and C# are some of the most famous language which don't support multiple inheritance. We need careful design to take proper advantage of Multiple inheritance, otherwise you might end up in spaghetti code, with classes inheritancing from multiple classes.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Building upon the above example, let us say we have a new device called Air Conditioner which can both heat and cool air. We might have a new class called AirConditioner as follows:&lt;br /&gt;
&lt;br /&gt;
 class AirConditioner : public Heater, public Cooler&lt;br /&gt;
 {&lt;br /&gt;
      public void setThresholdTemperature(int temp) { ... }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
= Good and bad of Multiple Inheritance =&lt;br /&gt;
Good:&lt;br /&gt;
* Multiple Inheritance strongly supports DRY principle, which leads to better software which is extensible, cohesive, less coupled and modular. &lt;br /&gt;
* Multiple Inheritance is very common in real world, in which object takes attributes from different objects.&lt;br /&gt;
* Multiple Inheritance is more expressive than Single Inheritane, so it gives flexibility in designing software with complex object relationships.&lt;br /&gt;
&lt;br /&gt;
Bad:&lt;br /&gt;
* To take complete advantage of Multiple Inheritance needs a thoughtful design. Without a good design, Multiple Inheritance infact might lead to bad code.&lt;br /&gt;
* Multiple Inheritance is overwhelming to developers. Requires more experience to properly use Multiple Inheritance.&lt;br /&gt;
* Multiple Inheritance is hard to use and make the language complicated. Some example like what happens when you Inherit from multiple classes which has same method?&lt;br /&gt;
&lt;br /&gt;
== Implementation issues ==&lt;br /&gt;
One of the most common problem with Multiple Inheritance is called the [http://en.wikipedia.org/wiki/Diamond_problem Diamond Problem(3)]. This is encountered when a derived class inherits from multiple parent classes, which in turn inherit from one super class. Let us try to understand this problem using an example.&lt;br /&gt;
&lt;br /&gt;
Consider a task of modelling a car. Now a car can broadly be divided into two subclasses, mechanical cars and electric cars. &lt;br /&gt;
&lt;br /&gt;
 class Car&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
        void start() { cout &amp;lt;&amp;lt; &amp;quot;Start with loud noise&amp;quot;;&lt;br /&gt;
        void steer();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class MechCar : public Car&lt;br /&gt;
 {&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class ElectricCar : public Car&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
       void start() { cout &amp;lt;&amp;lt; &amp;quot;start without noise&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Now considering modern advancements in vehicle design, a new subclass of hybrid cars have emerged in the market. These cars have traits of both mechanical and electric cars. Here is how this is achieved in C++.&lt;br /&gt;
&lt;br /&gt;
 class HybridCar : public MechCar, ElectricCar&lt;br /&gt;
 {&lt;br /&gt;
      // This has two copies of start()&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Here is an illustration of the inheritance hierarchy.&lt;br /&gt;
&lt;br /&gt;
[[Image:Car-hybrid.jpg|frame|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now if an object of the HybridCar class calls a method of class Car, an ambiguous situation arises. Assuming the method has been [http://en.wikipedia.org/wiki/Method_overriding overridden] by class MechCar and ElectricCar, then it is not clear which version of the method the object is referring to. This is what is known as the Diamond Problem. This problem is addressed in C++ solves this problem through the use of  [http://en.wikipedia.org/wiki/Virtual_inheritance Virtual Inheritance]. &lt;br /&gt;
&lt;br /&gt;
 HybridCar h;&lt;br /&gt;
 h.ElectricCar::start();&lt;br /&gt;
&lt;br /&gt;
There are also ambiguities during the type casting. For more details about Virtual Inheritance refer [http://en.wikipedia.org/wiki/Virtual_inheritance here]&lt;br /&gt;
&lt;br /&gt;
== Multiple Inheritance - Do we really need this? ==&lt;br /&gt;
Many languages which don't support multiple inheritance completely, has some ways of emulating multiple inheritance. We will consider Java as an example which does not support Multiple Inheritance completely and see how we can emulate multiple inheritance in Java. It is not completely true to say that Java does not support multiple inheritance, in fact with interfaces we can have multiple inheritance. Since, Interface just defines the behaviour, it does not have any attributes to describe it, Interfaces are not considered as a Class.&lt;br /&gt;
&lt;br /&gt;
Let us consider the previous example of an AirConditioner:&lt;br /&gt;
&lt;br /&gt;
 class AirConditioner extends [______________]&lt;br /&gt;
&lt;br /&gt;
We cannot fit both the classes there since a class in Java can extend from a single class, unlike what we did in C++. But Java classes can implement multiple interfaces.&lt;br /&gt;
&lt;br /&gt;
 public interface IHeater {&lt;br /&gt;
    void heat();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public interface ICooler {&lt;br /&gt;
    void cool();&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public Heater extends TempControllerAppliance implements IHeater {&lt;br /&gt;
    public void heat() { .. }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public Cooler extends TempControllerAppliance implements ICooler{&lt;br /&gt;
    public void cool() { .. }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public IAirConditioner {&lt;br /&gt;
      public void setThresholdTemp(int t);&lt;br /&gt;
 } &lt;br /&gt;
&lt;br /&gt;
 public class AirConditioner extends TempControllerAppliance implements IHeater, ICooler, IAirConditioner {&lt;br /&gt;
    private ICooler cooler = new Cooler();&lt;br /&gt;
    private IHeater heater = new Heater();&lt;br /&gt;
 &lt;br /&gt;
    public void cool() {&lt;br /&gt;
        cooler.cool();&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void heat() {&lt;br /&gt;
        heater.heat();&lt;br /&gt;
    }  &lt;br /&gt;
    &lt;br /&gt;
    public void setThreshold(int t) { ... }   &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
To implement multiple inheritance in Java, we need to the following:&lt;br /&gt;
1. Make every class implement an Interface&lt;br /&gt;
2. Classes extends these interfaces to add logic&lt;br /&gt;
3. When a class needs to extend from Multiple interfaces, you can implement those interfaces and delegate the call to the actual implementation. This is achieved through Composition.&lt;br /&gt;
&lt;br /&gt;
We can see that it is possible to emulate multiple inheritance through [http://en.wikipedia.org/wiki/Object_composition composition]. &lt;br /&gt;
&lt;br /&gt;
Let us take another example and see how we can emulate Multiple inheritance in Java. We are developing a paint application. We have separate tools to draw each primitive shapes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
 class Shape&lt;br /&gt;
 {&lt;br /&gt;
    //...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Square : Shape&lt;br /&gt;
 {&lt;br /&gt;
   // ..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Circle : Shape&lt;br /&gt;
 {&lt;br /&gt;
    //..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Triangle : Shape&lt;br /&gt;
 {&lt;br /&gt;
    //..&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
 class Painter&lt;br /&gt;
 {&lt;br /&gt;
     //..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class SquarePainter : Painter&lt;br /&gt;
 {&lt;br /&gt;
     void draw(Square s) { }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class CirclePainter : Painter&lt;br /&gt;
 {&lt;br /&gt;
    void draw(Circle c) { }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class TrianglePainter : Painter&lt;br /&gt;
 {&lt;br /&gt;
    void draw(Triangle t) { }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we need a canvas painter, which should have the capability to draw any of the above shape. In C++, this is pretty straight forward, you define a new class which extends all painters.&lt;br /&gt;
&lt;br /&gt;
 class CanvasPainter : SquarePainter, CirclePainter, TrianglePainter&lt;br /&gt;
 {&lt;br /&gt;
     // ..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
How can do the same in Java? Ofcourse, we will use composition, but how will you know which version of draw function to call? May be we can write some thing like this:&lt;br /&gt;
&lt;br /&gt;
 if (shape instanceof Triangle)&lt;br /&gt;
 {&lt;br /&gt;
       trianglePainter.draw((Triangle) shape);&lt;br /&gt;
 }&lt;br /&gt;
 else if(shape instanceof Circle)&lt;br /&gt;
 {&lt;br /&gt;
       circlePainter.draw((Circle) shape);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Unfortunately this is not a scalable solution and for every new tool we add to our paint program, we will have to change this code. This is where design patterns like [http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern Chain of Responsibility] proves helpful.&lt;br /&gt;
&lt;br /&gt;
Implementing the above code in Java using Chain of Responsibility design pattern.&lt;br /&gt;
&lt;br /&gt;
 interface IPainter&lt;br /&gt;
 {&lt;br /&gt;
     boolean canDraw(Shape s);&lt;br /&gt;
     void draw(Shape s);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
All painters will implement the above function:&lt;br /&gt;
&lt;br /&gt;
 class TrianglePainter implements IPainter&lt;br /&gt;
 {&lt;br /&gt;
    public boolean canDraw(Shape s) { return s instanceof Triangle; }&lt;br /&gt;
    public void draw(Shape s) { // ... }  &lt;br /&gt;
 } &lt;br /&gt;
&lt;br /&gt;
 class CanvasPainter implements IPainter&lt;br /&gt;
 {&lt;br /&gt;
    // List&amp;lt;IPainter&amp;gt; = ....&lt;br /&gt;
    // function to add and remove painters&lt;br /&gt;
    public void draw(Shape s)&lt;br /&gt;
    {&lt;br /&gt;
       for(Paint p : painters)&lt;br /&gt;
       { &lt;br /&gt;
            if(p.canDraw(s)) { p.draw(s); return; }&lt;br /&gt;
       }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
We can see that, chain of responsibility design pattern provides a very neat way to handle multiple inheritance in languages which don't support them completely.&lt;br /&gt;
&lt;br /&gt;
= Alternates to Multiple Inheritance =&lt;br /&gt;
* Composition &amp;amp; Interfaces&lt;br /&gt;
Compositions provides a neat way of emulating Multiple Inheritance. With Multiple Inheritance, we bring functionality from different classes to the class which extends them. We can achieve the same using Composition in which one class contains instance of other class, there by importing the capability of all those classes. Since a class is allowed to implement Multiple Interfaces, the class behaves as though it extended from Multiple classes.&lt;br /&gt;
&lt;br /&gt;
* Mixins&lt;br /&gt;
Some languages provide [http://en.wikipedia.org/wiki/Mixin mixins] as a language feature. Mixins provide another elegant mechanism which mimics Multiple Inheritance. The common problems with Mixin is that it pollutes the class namespace as it imports functions from different modules. This [http://www.artima.com/weblogs/viewpost.jsp?thread=246341 article] talks about the limitations of mixins in greater detail.&lt;br /&gt;
&lt;br /&gt;
* Extended Dynamic Proxies&lt;br /&gt;
Dynamic Proxy is a technique in which we can dynamically synthesis a new object from existing objects using reflection.  With Dynamic Proxies, we can dynamically make a class implement different interfaces. This is achieved through reflection mechanism. This [http://java.sys-con.com/node/37748 article] talks about using Extended Dynamic Proxies to achieve Multiple Inheritance in Java. But this rather a verbose and a complicated solution. This [http://www.java2s.com/Code/Java/Reflection/Aninvocationhandlerthatcountsthenumberofcallsforallmethodsinthetargetclass.htm article] discusses using Dynamic proxies in greater detail.&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
We discussed briefly about Inheritance, Multiple Inheritance, its advantages and disadvantages. Multiple Inheritance surely offers more flexibility in designing, but languages which don't support Multiple Inheritance has different ways of simulating Multiple Inheritance if required to do so.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table border = &amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;Single Inheritance&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;Multiple Inheritance&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Flexibility&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Single Inheritance does have some restriction in terms of modeling real world objects, but mitigate this problem with careful design&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Multiple Inheritance provides lots more flexibility for designing software&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Easy of Use&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Single Inheritance is much simpler to use&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Multiple Inheritance sometimes leaves the programmer confused. One of the common problem with Multiple inheritance in Diamond Problem&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Availability&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Single Inheritance is supported by all the Object Oriented Programming Languages&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Multiple Inheritance is not supported by few of the OOP Languages, because the designer of the language thought it makes the languauge complicated to use and not necessary for most scenarios.&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Efficiency&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;This actually language specific. But generally it takes less time to figure which version of the function to call in a single inheritance&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Virtual Function dispatch in C++ is more time consuming in Multiple Inheritance. For example Multiple Inheritance in C++, needs multiple vtable for dispatching virtual functions. [http://www.cs.ucsb.edu/~urs/oocsb/papers/oopsla96.pdf (1)] discusses about this problem in great detail.&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Further Reading =&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Virtual_inheritance Virtual Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_composition Composition]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Mixin Mixins]&lt;br /&gt;
&lt;br /&gt;
= Reference =&lt;br /&gt;
*[http://www.cs.ucsb.edu/~urs/oocsb/papers/oopsla96.pdf (1) Cost of Virtual Functions C++]&lt;br /&gt;
*[http://www.artima.com/weblogs/viewpost.jsp?thread=246341 (2) Mixins considered Harmful]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Diamond_problem (3) Diamond Problem with Multiple Inheritance]&lt;br /&gt;
*[http://java.sun.com/docs/white/langenv/ (4) The Java Language]&lt;/div&gt;</summary>
		<author><name>Cgoogc</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_25_ag&amp;diff=35994</id>
		<title>CSC/ECE 517 Fall 2010/ch1 25 ag</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_25_ag&amp;diff=35994"/>
		<updated>2010-09-22T14:31:49Z</updated>

		<summary type="html">&lt;p&gt;Cgoogc: /* Conclusion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
= What is Inheritance? =&lt;br /&gt;
Inheritance, along with polymorphism and encapsulation, is one of the pillar features of [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch1_1e_bb#Object_Oriented_Programming Object Oriented Programming] (OOP). Inheritance allows a class to reuse, extend or modify the behavior of another class. The term inheritance generally refers to single inheritance, where a sub-class or a derived class inherits methods and attributes from only one parent class. &lt;br /&gt;
&lt;br /&gt;
For example, consider a general class of electrical appliances. Switching an appliance on and off can be considered a standard feature of every appliance.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 class ElectricAppliance&lt;br /&gt;
 {&lt;br /&gt;
     public void on() { ... }&lt;br /&gt;
     public void off() { ... }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class TempControlAppliance : public ElectricAppliance&lt;br /&gt;
 {&lt;br /&gt;
     public void increaseTemp() { .. }&lt;br /&gt;
     public void decreaseTemp() { .. }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Heater : public TempControlAppliance &lt;br /&gt;
 {&lt;br /&gt;
     public void heat() { .. }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Cooler : public TempControlAppliance &lt;br /&gt;
 {&lt;br /&gt;
     public void cool() { .. }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
If we don't have inheritance, we might have to duplicate all the functions in both Heater and Cooler class. Inheritance is very important to achieve DRY (Don't Repeat Yourself). In the above class hierarchy, TempControlAppliance is super (parent) class for both Cooler and Heater subclass (child).&lt;br /&gt;
&lt;br /&gt;
= What is Multiple Inheritance =&lt;br /&gt;
When a class inherits from more than one class i.e., a child class having more than one parent class, is called multiple inheritance. Though this feature offers more flexibility, it seem to overwhelm most of the developer, so some of the object oriented language designer decided not to support Multiple Inheritance. Java and C# are some of the most famous language which don't support multiple inheritance. We need careful design to take proper advantage of Multiple inheritance, otherwise you might end up in spaghetti code, with classes inheritancing from multiple classes.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Building upon the above example, let us say we have a new device called Air Conditioner which can both heat and cool air. We might have a new class called AirConditioner as follows:&lt;br /&gt;
&lt;br /&gt;
 class AirConditioner : public Heater, public Cooler&lt;br /&gt;
 {&lt;br /&gt;
      public void setThresholdTemperature(int temp) { ... }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
= Good and bad of Multiple Inheritance =&lt;br /&gt;
Good:&lt;br /&gt;
* Multiple Inheritance strongly supports DRY principle, which leads to better software which is extensible, cohesive, less coupled and modular. &lt;br /&gt;
* Multiple Inheritance is very common in real world, in which object takes attributes from different objects.&lt;br /&gt;
* Multiple Inheritance is more expressive than Single Inheritane, so it gives flexibility in designing software with complex object relationships.&lt;br /&gt;
&lt;br /&gt;
Bad:&lt;br /&gt;
* To take complete advantage of Multiple Inheritance needs a thoughtful design. Without a good design, Multiple Inheritance infact might lead to bad code.&lt;br /&gt;
* Multiple Inheritance is overwhelming to developers. Requires more experience to properly use Multiple Inheritance.&lt;br /&gt;
* Multiple Inheritance is hard to use and make the language complicated. Some example like what happens when you Inherit from multiple classes which has same method?&lt;br /&gt;
&lt;br /&gt;
== Implementation issues ==&lt;br /&gt;
One of the most common problem with Multiple Inheritance is called the [http://en.wikipedia.org/wiki/Diamond_problem Diamond Problem(3)]. This is encountered when a derived class inherits from multiple parent classes, which in turn inherit from one super class. Let us try to understand this problem using an example.&lt;br /&gt;
&lt;br /&gt;
Consider a task of modelling a car. Now a car can broadly be divided into two subclasses, mechanical cars and electric cars. &lt;br /&gt;
&lt;br /&gt;
 class Car&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
        void start() { cout &amp;lt;&amp;lt; &amp;quot;Start with loud noise&amp;quot;;&lt;br /&gt;
        void steer();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class MechCar : public Car&lt;br /&gt;
 {&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class ElectricCar : public Car&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
       void start() { cout &amp;lt;&amp;lt; &amp;quot;start without noise&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Now considering modern advancements in vehicle design, a new subclass of hybrid cars have emerged in the market. These cars have traits of both mechanical and electric cars. Here is how this is achieved in C++.&lt;br /&gt;
&lt;br /&gt;
 class HybridCar : public MechCar, ElectricCar&lt;br /&gt;
 {&lt;br /&gt;
      // This has two copies of start()&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Here is an illustration of the inheritance hierarchy.&lt;br /&gt;
&lt;br /&gt;
[[Image:Car-hybrid.jpg|frame|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now if an object of the HybridCar class calls a method of class Car, an ambiguous situation arises. Assuming the method has been [http://en.wikipedia.org/wiki/Method_overriding overridden] by class MechCar and ElectricCar, then it is not clear which version of the method the object is referring to. This is what is known as the Diamond Problem. This problem is addressed in C++ solves this problem through the use of  [http://en.wikipedia.org/wiki/Virtual_inheritance Virtual Inheritance]. &lt;br /&gt;
&lt;br /&gt;
 HybridCar h;&lt;br /&gt;
 h.ElectricCar::start();&lt;br /&gt;
&lt;br /&gt;
There are also ambiguities during the type casting. For more details about Virtual Inheritance refer [http://en.wikipedia.org/wiki/Virtual_inheritance here]&lt;br /&gt;
&lt;br /&gt;
== Multiple Inheritance - Do we really need this? ==&lt;br /&gt;
Many languages which don't support multiple inheritance completely, has some ways of emulating multiple inheritance. We will consider Java as an example which does not support Multiple Inheritance completely and see how we can emulate multiple inheritance in Java. It is not completely true to say that Java does not support multiple inheritance, in fact with interfaces we can have multiple inheritance. Since, Interface just defines the behaviour, it does not have any attributes to describe it, Interfaces are not considered as a Class.&lt;br /&gt;
&lt;br /&gt;
Let us consider the previous example of an AirConditioner:&lt;br /&gt;
&lt;br /&gt;
 class AirConditioner extends [______________]&lt;br /&gt;
&lt;br /&gt;
We cannot fit both the classes there since a class in Java can extend from a single class, unlike what we did in C++. But Java classes can implement multiple interfaces.&lt;br /&gt;
&lt;br /&gt;
 public interface IHeater {&lt;br /&gt;
    void heat();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public interface ICooler {&lt;br /&gt;
    void cool();&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public Heater extends TempControllerAppliance implements IHeater {&lt;br /&gt;
    public void heat() { .. }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public Cooler extends TempControllerAppliance implements ICooler{&lt;br /&gt;
    public void cool() { .. }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public IAirConditioner {&lt;br /&gt;
      public void setThresholdTemp(int t);&lt;br /&gt;
 } &lt;br /&gt;
&lt;br /&gt;
 public class AirConditioner extends TempControllerAppliance implements IHeater, ICooler, IAirConditioner {&lt;br /&gt;
    private ICooler cooler = new Cooler();&lt;br /&gt;
    private IHeater heater = new Heater();&lt;br /&gt;
 &lt;br /&gt;
    public void cool() {&lt;br /&gt;
        cooler.cool();&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void heat() {&lt;br /&gt;
        heater.heat();&lt;br /&gt;
    }  &lt;br /&gt;
    &lt;br /&gt;
    public void setThreshold(int t) { ... }   &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
To implement multiple inheritance in Java, we need to the following:&lt;br /&gt;
1. Make every class implement an Interface&lt;br /&gt;
2. Classes extends these interfaces to add logic&lt;br /&gt;
3. When a class needs to extend from Multiple interfaces, you can implement those interfaces and delegate the call to the actual implementation. This is achieved through Composition.&lt;br /&gt;
&lt;br /&gt;
We can see that it is possible to emulate multiple inheritance through [http://en.wikipedia.org/wiki/Object_composition composition]. &lt;br /&gt;
&lt;br /&gt;
Let us take another example and see how we can emulate Multiple inheritance in Java. We are developing a paint application. We have separate tools to draw each primitive shapes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
 class Shape&lt;br /&gt;
 {&lt;br /&gt;
    //...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Square : Shape&lt;br /&gt;
 {&lt;br /&gt;
   // ..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Circle : Shape&lt;br /&gt;
 {&lt;br /&gt;
    //..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Triangle : Shape&lt;br /&gt;
 {&lt;br /&gt;
    //..&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
 class Painter&lt;br /&gt;
 {&lt;br /&gt;
     //..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class SquarePainter : Painter&lt;br /&gt;
 {&lt;br /&gt;
     void draw(Square s) { }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class CirclePainter : Painter&lt;br /&gt;
 {&lt;br /&gt;
    void draw(Circle c) { }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class TrianglePainter : Painter&lt;br /&gt;
 {&lt;br /&gt;
    void draw(Triangle t) { }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we need a canvas painter, which should have the capability to draw any of the above shape. In C++, this is pretty straight forward, you define a new class which extends all painters.&lt;br /&gt;
&lt;br /&gt;
 class CanvasPainter : SquarePainter, CirclePainter, TrianglePainter&lt;br /&gt;
 {&lt;br /&gt;
     // ..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
How can do the same in Java? Ofcourse, we will use composition, but how will you know which version of draw function to call? May be we can write some thing like this:&lt;br /&gt;
&lt;br /&gt;
 if (shape instanceof Triangle)&lt;br /&gt;
 {&lt;br /&gt;
       trianglePainter.draw((Triangle) shape);&lt;br /&gt;
 }&lt;br /&gt;
 else if(shape instanceof Circle)&lt;br /&gt;
 {&lt;br /&gt;
       circlePainter.draw((Circle) shape);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Unfortunately this is not a scalable solution and for every new tool we add to our paint program, we will have to change this code. This is where design patterns like [http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern Chain of Responsibility] proves helpful.&lt;br /&gt;
&lt;br /&gt;
Implementing the above code in Java using Chain of Responsibility design pattern.&lt;br /&gt;
&lt;br /&gt;
 interface IPainter&lt;br /&gt;
 {&lt;br /&gt;
     boolean canDraw(Shape s);&lt;br /&gt;
     void draw(Shape s);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
All painters will implement the above function:&lt;br /&gt;
&lt;br /&gt;
 class TrianglePainter implements IPainter&lt;br /&gt;
 {&lt;br /&gt;
    public boolean canDraw(Shape s) { return s instanceof Triangle; }&lt;br /&gt;
    public void draw(Shape s) { // ... }  &lt;br /&gt;
 } &lt;br /&gt;
&lt;br /&gt;
 class CanvasPainter implements IPainter&lt;br /&gt;
 {&lt;br /&gt;
    // List&amp;lt;IPainter&amp;gt; = ....&lt;br /&gt;
    // function to add and remove painters&lt;br /&gt;
    public void draw(Shape s)&lt;br /&gt;
    {&lt;br /&gt;
       for(Paint p : painters)&lt;br /&gt;
       { &lt;br /&gt;
            if(p.canDraw(s)) { p.draw(s); return; }&lt;br /&gt;
       }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
We can see that, chain of responsibility design pattern provides a very neat way to handle multiple inheritance in languages which don't support them completely.&lt;br /&gt;
&lt;br /&gt;
= Alternates to Multiple Inheritance =&lt;br /&gt;
* Composition &amp;amp; Interfaces&lt;br /&gt;
Compositions provides a neat way of emulating Multiple Inheritance. With Multiple Inheritance, we bring functionality from different classes to the class which extends them. We can achieve the same using Composition in which one class contains instance of other class, there by importing the capability of all those classes. Since a class is allowed to implement Multiple Interfaces, the class behaves as though it extended from Multiple classes.&lt;br /&gt;
&lt;br /&gt;
* Mixins&lt;br /&gt;
Some languages provide [http://en.wikipedia.org/wiki/Mixin mixins] as a language feature. Mixins provide another elegant mechanism which mimics Multiple Inheritance. The common problems with Mixin is that it pollutes the class namespace as it imports functions from different modules. This [http://www.artima.com/weblogs/viewpost.jsp?thread=246341 article] talks about the limitations of mixins in greater detail.&lt;br /&gt;
&lt;br /&gt;
* Extended Dynamic Proxies&lt;br /&gt;
Dynamic Proxy is a technique in which we can dynamically synthesis a new object from existing objects using reflection.  With Dynamic Proxies, we can dynamically make a class implement different interfaces. This is achieved through reflection mechanism. This [http://java.sys-con.com/node/37748 article] talks about using Extended Dynamic Proxies to achieve Multiple Inheritance in Java. But this rather a verbose and a complicated solution. This [http://www.java2s.com/Code/Java/Reflection/Aninvocationhandlerthatcountsthenumberofcallsforallmethodsinthetargetclass.htm article] discusses using Dynamic proxies in greater detail.&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
&amp;lt;table border = &amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;Single Inheritance&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;Multiple Inheritance&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Flexibility&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Single Inheritance does have some restriction in terms of modeling real world objects, but mitigate this problem with careful design&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Multiple Inheritance provides lots more flexibility for designing software&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Easy of Use&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Single Inheritance is much simpler to use&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Multiple Inheritance sometimes leaves the programmer confused. One of the common problem with Multiple inheritance in Diamond Problem&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Availability&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Single Inheritance is supported by all the Object Oriented Programming Languages&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Multiple Inheritance is not supported by few of the OOP Languages, because the designer of the language thought it makes the languauge complicated to use and not necessary for most scenarios.&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Efficiency&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;This actually language specific. But generally it takes less time to figure which version of the function to call in a single inheritance&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Virtual Function dispatch in C++ is more time consuming in Multiple Inheritance. For example Multiple Inheritance in C++, needs multiple vtable for dispatching virtual functions. [http://www.cs.ucsb.edu/~urs/oocsb/papers/oopsla96.pdf (1)] discusses about this problem in great detail.&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Further Reading =&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Virtual_inheritance Virtual Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_composition Composition]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Mixin Mixins]&lt;br /&gt;
&lt;br /&gt;
= Reference =&lt;br /&gt;
*[http://www.cs.ucsb.edu/~urs/oocsb/papers/oopsla96.pdf (1) Cost of Virtual Functions C++]&lt;br /&gt;
*[http://www.artima.com/weblogs/viewpost.jsp?thread=246341 (2) Mixins considered Harmful]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Diamond_problem (3) Diamond Problem with Multiple Inheritance]&lt;br /&gt;
*[http://java.sun.com/docs/white/langenv/ (4) The Java Language]&lt;/div&gt;</summary>
		<author><name>Cgoogc</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_25_ag&amp;diff=35992</id>
		<title>CSC/ECE 517 Fall 2010/ch1 25 ag</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_25_ag&amp;diff=35992"/>
		<updated>2010-09-22T14:30:54Z</updated>

		<summary type="html">&lt;p&gt;Cgoogc: /* Alternates to Multiple Inheritance */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
= What is Inheritance? =&lt;br /&gt;
Inheritance, along with polymorphism and encapsulation, is one of the pillar features of [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch1_1e_bb#Object_Oriented_Programming Object Oriented Programming] (OOP). Inheritance allows a class to reuse, extend or modify the behavior of another class. The term inheritance generally refers to single inheritance, where a sub-class or a derived class inherits methods and attributes from only one parent class. &lt;br /&gt;
&lt;br /&gt;
For example, consider a general class of electrical appliances. Switching an appliance on and off can be considered a standard feature of every appliance.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 class ElectricAppliance&lt;br /&gt;
 {&lt;br /&gt;
     public void on() { ... }&lt;br /&gt;
     public void off() { ... }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class TempControlAppliance : public ElectricAppliance&lt;br /&gt;
 {&lt;br /&gt;
     public void increaseTemp() { .. }&lt;br /&gt;
     public void decreaseTemp() { .. }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Heater : public TempControlAppliance &lt;br /&gt;
 {&lt;br /&gt;
     public void heat() { .. }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Cooler : public TempControlAppliance &lt;br /&gt;
 {&lt;br /&gt;
     public void cool() { .. }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
If we don't have inheritance, we might have to duplicate all the functions in both Heater and Cooler class. Inheritance is very important to achieve DRY (Don't Repeat Yourself). In the above class hierarchy, TempControlAppliance is super (parent) class for both Cooler and Heater subclass (child).&lt;br /&gt;
&lt;br /&gt;
= What is Multiple Inheritance =&lt;br /&gt;
When a class inherits from more than one class i.e., a child class having more than one parent class, is called multiple inheritance. Though this feature offers more flexibility, it seem to overwhelm most of the developer, so some of the object oriented language designer decided not to support Multiple Inheritance. Java and C# are some of the most famous language which don't support multiple inheritance. We need careful design to take proper advantage of Multiple inheritance, otherwise you might end up in spaghetti code, with classes inheritancing from multiple classes.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Building upon the above example, let us say we have a new device called Air Conditioner which can both heat and cool air. We might have a new class called AirConditioner as follows:&lt;br /&gt;
&lt;br /&gt;
 class AirConditioner : public Heater, public Cooler&lt;br /&gt;
 {&lt;br /&gt;
      public void setThresholdTemperature(int temp) { ... }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
= Good and bad of Multiple Inheritance =&lt;br /&gt;
Good:&lt;br /&gt;
* Multiple Inheritance strongly supports DRY principle, which leads to better software which is extensible, cohesive, less coupled and modular. &lt;br /&gt;
* Multiple Inheritance is very common in real world, in which object takes attributes from different objects.&lt;br /&gt;
* Multiple Inheritance is more expressive than Single Inheritane, so it gives flexibility in designing software with complex object relationships.&lt;br /&gt;
&lt;br /&gt;
Bad:&lt;br /&gt;
* To take complete advantage of Multiple Inheritance needs a thoughtful design. Without a good design, Multiple Inheritance infact might lead to bad code.&lt;br /&gt;
* Multiple Inheritance is overwhelming to developers. Requires more experience to properly use Multiple Inheritance.&lt;br /&gt;
* Multiple Inheritance is hard to use and make the language complicated. Some example like what happens when you Inherit from multiple classes which has same method?&lt;br /&gt;
&lt;br /&gt;
== Implementation issues ==&lt;br /&gt;
One of the most common problem with Multiple Inheritance is called the [http://en.wikipedia.org/wiki/Diamond_problem Diamond Problem(3)]. This is encountered when a derived class inherits from multiple parent classes, which in turn inherit from one super class. Let us try to understand this problem using an example.&lt;br /&gt;
&lt;br /&gt;
Consider a task of modelling a car. Now a car can broadly be divided into two subclasses, mechanical cars and electric cars. &lt;br /&gt;
&lt;br /&gt;
 class Car&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
        void start() { cout &amp;lt;&amp;lt; &amp;quot;Start with loud noise&amp;quot;;&lt;br /&gt;
        void steer();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class MechCar : public Car&lt;br /&gt;
 {&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class ElectricCar : public Car&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
       void start() { cout &amp;lt;&amp;lt; &amp;quot;start without noise&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Now considering modern advancements in vehicle design, a new subclass of hybrid cars have emerged in the market. These cars have traits of both mechanical and electric cars. Here is how this is achieved in C++.&lt;br /&gt;
&lt;br /&gt;
 class HybridCar : public MechCar, ElectricCar&lt;br /&gt;
 {&lt;br /&gt;
      // This has two copies of start()&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Here is an illustration of the inheritance hierarchy.&lt;br /&gt;
&lt;br /&gt;
[[Image:Car-hybrid.jpg|frame|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now if an object of the HybridCar class calls a method of class Car, an ambiguous situation arises. Assuming the method has been [http://en.wikipedia.org/wiki/Method_overriding overridden] by class MechCar and ElectricCar, then it is not clear which version of the method the object is referring to. This is what is known as the Diamond Problem. This problem is addressed in C++ solves this problem through the use of  [http://en.wikipedia.org/wiki/Virtual_inheritance Virtual Inheritance]. &lt;br /&gt;
&lt;br /&gt;
 HybridCar h;&lt;br /&gt;
 h.ElectricCar::start();&lt;br /&gt;
&lt;br /&gt;
There are also ambiguities during the type casting. For more details about Virtual Inheritance refer [http://en.wikipedia.org/wiki/Virtual_inheritance here]&lt;br /&gt;
&lt;br /&gt;
== Multiple Inheritance - Do we really need this? ==&lt;br /&gt;
Many languages which don't support multiple inheritance completely, has some ways of emulating multiple inheritance. We will consider Java as an example which does not support Multiple Inheritance completely and see how we can emulate multiple inheritance in Java. It is not completely true to say that Java does not support multiple inheritance, in fact with interfaces we can have multiple inheritance. Since, Interface just defines the behaviour, it does not have any attributes to describe it, Interfaces are not considered as a Class.&lt;br /&gt;
&lt;br /&gt;
Let us consider the previous example of an AirConditioner:&lt;br /&gt;
&lt;br /&gt;
 class AirConditioner extends [______________]&lt;br /&gt;
&lt;br /&gt;
We cannot fit both the classes there since a class in Java can extend from a single class, unlike what we did in C++. But Java classes can implement multiple interfaces.&lt;br /&gt;
&lt;br /&gt;
 public interface IHeater {&lt;br /&gt;
    void heat();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public interface ICooler {&lt;br /&gt;
    void cool();&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public Heater extends TempControllerAppliance implements IHeater {&lt;br /&gt;
    public void heat() { .. }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public Cooler extends TempControllerAppliance implements ICooler{&lt;br /&gt;
    public void cool() { .. }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public IAirConditioner {&lt;br /&gt;
      public void setThresholdTemp(int t);&lt;br /&gt;
 } &lt;br /&gt;
&lt;br /&gt;
 public class AirConditioner extends TempControllerAppliance implements IHeater, ICooler, IAirConditioner {&lt;br /&gt;
    private ICooler cooler = new Cooler();&lt;br /&gt;
    private IHeater heater = new Heater();&lt;br /&gt;
 &lt;br /&gt;
    public void cool() {&lt;br /&gt;
        cooler.cool();&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void heat() {&lt;br /&gt;
        heater.heat();&lt;br /&gt;
    }  &lt;br /&gt;
    &lt;br /&gt;
    public void setThreshold(int t) { ... }   &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
To implement multiple inheritance in Java, we need to the following:&lt;br /&gt;
1. Make every class implement an Interface&lt;br /&gt;
2. Classes extends these interfaces to add logic&lt;br /&gt;
3. When a class needs to extend from Multiple interfaces, you can implement those interfaces and delegate the call to the actual implementation. This is achieved through Composition.&lt;br /&gt;
&lt;br /&gt;
We can see that it is possible to emulate multiple inheritance through [http://en.wikipedia.org/wiki/Object_composition composition]. &lt;br /&gt;
&lt;br /&gt;
Let us take another example and see how we can emulate Multiple inheritance in Java. We are developing a paint application. We have separate tools to draw each primitive shapes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
 class Shape&lt;br /&gt;
 {&lt;br /&gt;
    //...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Square : Shape&lt;br /&gt;
 {&lt;br /&gt;
   // ..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Circle : Shape&lt;br /&gt;
 {&lt;br /&gt;
    //..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Triangle : Shape&lt;br /&gt;
 {&lt;br /&gt;
    //..&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
 class Painter&lt;br /&gt;
 {&lt;br /&gt;
     //..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class SquarePainter : Painter&lt;br /&gt;
 {&lt;br /&gt;
     void draw(Square s) { }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class CirclePainter : Painter&lt;br /&gt;
 {&lt;br /&gt;
    void draw(Circle c) { }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class TrianglePainter : Painter&lt;br /&gt;
 {&lt;br /&gt;
    void draw(Triangle t) { }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we need a canvas painter, which should have the capability to draw any of the above shape. In C++, this is pretty straight forward, you define a new class which extends all painters.&lt;br /&gt;
&lt;br /&gt;
 class CanvasPainter : SquarePainter, CirclePainter, TrianglePainter&lt;br /&gt;
 {&lt;br /&gt;
     // ..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
How can do the same in Java? Ofcourse, we will use composition, but how will you know which version of draw function to call? May be we can write some thing like this:&lt;br /&gt;
&lt;br /&gt;
 if (shape instanceof Triangle)&lt;br /&gt;
 {&lt;br /&gt;
       trianglePainter.draw((Triangle) shape);&lt;br /&gt;
 }&lt;br /&gt;
 else if(shape instanceof Circle)&lt;br /&gt;
 {&lt;br /&gt;
       circlePainter.draw((Circle) shape);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Unfortunately this is not a scalable solution and for every new tool we add to our paint program, we will have to change this code. This is where design patterns like [http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern Chain of Responsibility] proves helpful.&lt;br /&gt;
&lt;br /&gt;
Implementing the above code in Java using Chain of Responsibility design pattern.&lt;br /&gt;
&lt;br /&gt;
 interface IPainter&lt;br /&gt;
 {&lt;br /&gt;
     boolean canDraw(Shape s);&lt;br /&gt;
     void draw(Shape s);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
All painters will implement the above function:&lt;br /&gt;
&lt;br /&gt;
 class TrianglePainter implements IPainter&lt;br /&gt;
 {&lt;br /&gt;
    public boolean canDraw(Shape s) { return s instanceof Triangle; }&lt;br /&gt;
    public void draw(Shape s) { // ... }  &lt;br /&gt;
 } &lt;br /&gt;
&lt;br /&gt;
 class CanvasPainter implements IPainter&lt;br /&gt;
 {&lt;br /&gt;
    // List&amp;lt;IPainter&amp;gt; = ....&lt;br /&gt;
    // function to add and remove painters&lt;br /&gt;
    public void draw(Shape s)&lt;br /&gt;
    {&lt;br /&gt;
       for(Paint p : painters)&lt;br /&gt;
       { &lt;br /&gt;
            if(p.canDraw(s)) { p.draw(s); return; }&lt;br /&gt;
       }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
We can see that, chain of responsibility design pattern provides a very neat way to handle multiple inheritance in languages which don't support them completely.&lt;br /&gt;
&lt;br /&gt;
= Alternates to Multiple Inheritance =&lt;br /&gt;
* Composition &amp;amp; Interfaces&lt;br /&gt;
Compositions provides a neat way of emulating Multiple Inheritance. With Multiple Inheritance, we bring functionality from different classes to the class which extends them. We can achieve the same using Composition in which one class contains instance of other class, there by importing the capability of all those classes. Since a class is allowed to implement Multiple Interfaces, the class behaves as though it extended from Multiple classes.&lt;br /&gt;
&lt;br /&gt;
* Mixins&lt;br /&gt;
Some languages provide [http://en.wikipedia.org/wiki/Mixin mixins] as a language feature. Mixins provide another elegant mechanism which mimics Multiple Inheritance. The common problems with Mixin is that it pollutes the class namespace as it imports functions from different modules. This [http://www.artima.com/weblogs/viewpost.jsp?thread=246341 article] talks about the limitations of mixins in greater detail.&lt;br /&gt;
&lt;br /&gt;
* Extended Dynamic Proxies&lt;br /&gt;
Dynamic Proxy is a technique in which we can dynamically synthesis a new object from existing objects using reflection.  With Dynamic Proxies, we can dynamically make a class implement different interfaces. This is achieved through reflection mechanism. This [http://java.sys-con.com/node/37748 article] talks about using Extended Dynamic Proxies to achieve Multiple Inheritance in Java. But this rather a verbose and a complicated solution. This [http://www.java2s.com/Code/Java/Reflection/Aninvocationhandlerthatcountsthenumberofcallsforallmethodsinthetargetclass.htm article] discusses using Dynamic proxies in greater detail.&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
&amp;lt;table border = &amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;Single Inheritance&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;Multiple Inheritance&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Flexibility&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Single Inheritance does have some restriction in terms of modeling real world objects, but mitigate this problem with careful design&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Multiple Inheritance provides lots more flexibility for designing&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Easy of Use&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Single Inheritance is much simpler to use&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Multiple Inheritance sometimes leaves the programmer confused. One of the common problem with Multiple inheritance in Diamond Problem&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Availability&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Single Inheritance is supported by all the Object Oriented Programming Languages&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Multiple Inheritance is not supported by few of the OOP Languages, because the designer of the language thought it is hard to use and not necessary for common scenarios.&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Efficiency&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;This actually language specific. But generally it takes less time to figure which version of the function to call in a single inheritance&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Virtual Function dispatch in C++ is more time consuming in Multiple Inheritance. For example Multiple Inheritance in C++, needs multiple vtable for dispatching virtual functions. [http://www.cs.ucsb.edu/~urs/oocsb/papers/oopsla96.pdf (1)] discusses about this problem in great detail.&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Further Reading =&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Virtual_inheritance Virtual Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_composition Composition]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Mixin Mixins]&lt;br /&gt;
&lt;br /&gt;
= Reference =&lt;br /&gt;
*[http://www.cs.ucsb.edu/~urs/oocsb/papers/oopsla96.pdf (1) Cost of Virtual Functions C++]&lt;br /&gt;
*[http://www.artima.com/weblogs/viewpost.jsp?thread=246341 (2) Mixins considered Harmful]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Diamond_problem (3) Diamond Problem with Multiple Inheritance]&lt;br /&gt;
*[http://java.sun.com/docs/white/langenv/ (4) The Java Language]&lt;/div&gt;</summary>
		<author><name>Cgoogc</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_25_ag&amp;diff=35991</id>
		<title>CSC/ECE 517 Fall 2010/ch1 25 ag</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_25_ag&amp;diff=35991"/>
		<updated>2010-09-22T14:30:48Z</updated>

		<summary type="html">&lt;p&gt;Cgoogc: /* Alternates to Multiple Inheritance */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
= What is Inheritance? =&lt;br /&gt;
Inheritance, along with polymorphism and encapsulation, is one of the pillar features of [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch1_1e_bb#Object_Oriented_Programming Object Oriented Programming] (OOP). Inheritance allows a class to reuse, extend or modify the behavior of another class. The term inheritance generally refers to single inheritance, where a sub-class or a derived class inherits methods and attributes from only one parent class. &lt;br /&gt;
&lt;br /&gt;
For example, consider a general class of electrical appliances. Switching an appliance on and off can be considered a standard feature of every appliance.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 class ElectricAppliance&lt;br /&gt;
 {&lt;br /&gt;
     public void on() { ... }&lt;br /&gt;
     public void off() { ... }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class TempControlAppliance : public ElectricAppliance&lt;br /&gt;
 {&lt;br /&gt;
     public void increaseTemp() { .. }&lt;br /&gt;
     public void decreaseTemp() { .. }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Heater : public TempControlAppliance &lt;br /&gt;
 {&lt;br /&gt;
     public void heat() { .. }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Cooler : public TempControlAppliance &lt;br /&gt;
 {&lt;br /&gt;
     public void cool() { .. }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
If we don't have inheritance, we might have to duplicate all the functions in both Heater and Cooler class. Inheritance is very important to achieve DRY (Don't Repeat Yourself). In the above class hierarchy, TempControlAppliance is super (parent) class for both Cooler and Heater subclass (child).&lt;br /&gt;
&lt;br /&gt;
= What is Multiple Inheritance =&lt;br /&gt;
When a class inherits from more than one class i.e., a child class having more than one parent class, is called multiple inheritance. Though this feature offers more flexibility, it seem to overwhelm most of the developer, so some of the object oriented language designer decided not to support Multiple Inheritance. Java and C# are some of the most famous language which don't support multiple inheritance. We need careful design to take proper advantage of Multiple inheritance, otherwise you might end up in spaghetti code, with classes inheritancing from multiple classes.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Building upon the above example, let us say we have a new device called Air Conditioner which can both heat and cool air. We might have a new class called AirConditioner as follows:&lt;br /&gt;
&lt;br /&gt;
 class AirConditioner : public Heater, public Cooler&lt;br /&gt;
 {&lt;br /&gt;
      public void setThresholdTemperature(int temp) { ... }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
= Good and bad of Multiple Inheritance =&lt;br /&gt;
Good:&lt;br /&gt;
* Multiple Inheritance strongly supports DRY principle, which leads to better software which is extensible, cohesive, less coupled and modular. &lt;br /&gt;
* Multiple Inheritance is very common in real world, in which object takes attributes from different objects.&lt;br /&gt;
* Multiple Inheritance is more expressive than Single Inheritane, so it gives flexibility in designing software with complex object relationships.&lt;br /&gt;
&lt;br /&gt;
Bad:&lt;br /&gt;
* To take complete advantage of Multiple Inheritance needs a thoughtful design. Without a good design, Multiple Inheritance infact might lead to bad code.&lt;br /&gt;
* Multiple Inheritance is overwhelming to developers. Requires more experience to properly use Multiple Inheritance.&lt;br /&gt;
* Multiple Inheritance is hard to use and make the language complicated. Some example like what happens when you Inherit from multiple classes which has same method?&lt;br /&gt;
&lt;br /&gt;
== Implementation issues ==&lt;br /&gt;
One of the most common problem with Multiple Inheritance is called the [http://en.wikipedia.org/wiki/Diamond_problem Diamond Problem(3)]. This is encountered when a derived class inherits from multiple parent classes, which in turn inherit from one super class. Let us try to understand this problem using an example.&lt;br /&gt;
&lt;br /&gt;
Consider a task of modelling a car. Now a car can broadly be divided into two subclasses, mechanical cars and electric cars. &lt;br /&gt;
&lt;br /&gt;
 class Car&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
        void start() { cout &amp;lt;&amp;lt; &amp;quot;Start with loud noise&amp;quot;;&lt;br /&gt;
        void steer();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class MechCar : public Car&lt;br /&gt;
 {&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class ElectricCar : public Car&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
       void start() { cout &amp;lt;&amp;lt; &amp;quot;start without noise&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Now considering modern advancements in vehicle design, a new subclass of hybrid cars have emerged in the market. These cars have traits of both mechanical and electric cars. Here is how this is achieved in C++.&lt;br /&gt;
&lt;br /&gt;
 class HybridCar : public MechCar, ElectricCar&lt;br /&gt;
 {&lt;br /&gt;
      // This has two copies of start()&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Here is an illustration of the inheritance hierarchy.&lt;br /&gt;
&lt;br /&gt;
[[Image:Car-hybrid.jpg|frame|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now if an object of the HybridCar class calls a method of class Car, an ambiguous situation arises. Assuming the method has been [http://en.wikipedia.org/wiki/Method_overriding overridden] by class MechCar and ElectricCar, then it is not clear which version of the method the object is referring to. This is what is known as the Diamond Problem. This problem is addressed in C++ solves this problem through the use of  [http://en.wikipedia.org/wiki/Virtual_inheritance Virtual Inheritance]. &lt;br /&gt;
&lt;br /&gt;
 HybridCar h;&lt;br /&gt;
 h.ElectricCar::start();&lt;br /&gt;
&lt;br /&gt;
There are also ambiguities during the type casting. For more details about Virtual Inheritance refer [http://en.wikipedia.org/wiki/Virtual_inheritance here]&lt;br /&gt;
&lt;br /&gt;
== Multiple Inheritance - Do we really need this? ==&lt;br /&gt;
Many languages which don't support multiple inheritance completely, has some ways of emulating multiple inheritance. We will consider Java as an example which does not support Multiple Inheritance completely and see how we can emulate multiple inheritance in Java. It is not completely true to say that Java does not support multiple inheritance, in fact with interfaces we can have multiple inheritance. Since, Interface just defines the behaviour, it does not have any attributes to describe it, Interfaces are not considered as a Class.&lt;br /&gt;
&lt;br /&gt;
Let us consider the previous example of an AirConditioner:&lt;br /&gt;
&lt;br /&gt;
 class AirConditioner extends [______________]&lt;br /&gt;
&lt;br /&gt;
We cannot fit both the classes there since a class in Java can extend from a single class, unlike what we did in C++. But Java classes can implement multiple interfaces.&lt;br /&gt;
&lt;br /&gt;
 public interface IHeater {&lt;br /&gt;
    void heat();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public interface ICooler {&lt;br /&gt;
    void cool();&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public Heater extends TempControllerAppliance implements IHeater {&lt;br /&gt;
    public void heat() { .. }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public Cooler extends TempControllerAppliance implements ICooler{&lt;br /&gt;
    public void cool() { .. }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public IAirConditioner {&lt;br /&gt;
      public void setThresholdTemp(int t);&lt;br /&gt;
 } &lt;br /&gt;
&lt;br /&gt;
 public class AirConditioner extends TempControllerAppliance implements IHeater, ICooler, IAirConditioner {&lt;br /&gt;
    private ICooler cooler = new Cooler();&lt;br /&gt;
    private IHeater heater = new Heater();&lt;br /&gt;
 &lt;br /&gt;
    public void cool() {&lt;br /&gt;
        cooler.cool();&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void heat() {&lt;br /&gt;
        heater.heat();&lt;br /&gt;
    }  &lt;br /&gt;
    &lt;br /&gt;
    public void setThreshold(int t) { ... }   &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
To implement multiple inheritance in Java, we need to the following:&lt;br /&gt;
1. Make every class implement an Interface&lt;br /&gt;
2. Classes extends these interfaces to add logic&lt;br /&gt;
3. When a class needs to extend from Multiple interfaces, you can implement those interfaces and delegate the call to the actual implementation. This is achieved through Composition.&lt;br /&gt;
&lt;br /&gt;
We can see that it is possible to emulate multiple inheritance through [http://en.wikipedia.org/wiki/Object_composition composition]. &lt;br /&gt;
&lt;br /&gt;
Let us take another example and see how we can emulate Multiple inheritance in Java. We are developing a paint application. We have separate tools to draw each primitive shapes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
 class Shape&lt;br /&gt;
 {&lt;br /&gt;
    //...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Square : Shape&lt;br /&gt;
 {&lt;br /&gt;
   // ..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Circle : Shape&lt;br /&gt;
 {&lt;br /&gt;
    //..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Triangle : Shape&lt;br /&gt;
 {&lt;br /&gt;
    //..&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
 class Painter&lt;br /&gt;
 {&lt;br /&gt;
     //..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class SquarePainter : Painter&lt;br /&gt;
 {&lt;br /&gt;
     void draw(Square s) { }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class CirclePainter : Painter&lt;br /&gt;
 {&lt;br /&gt;
    void draw(Circle c) { }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class TrianglePainter : Painter&lt;br /&gt;
 {&lt;br /&gt;
    void draw(Triangle t) { }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we need a canvas painter, which should have the capability to draw any of the above shape. In C++, this is pretty straight forward, you define a new class which extends all painters.&lt;br /&gt;
&lt;br /&gt;
 class CanvasPainter : SquarePainter, CirclePainter, TrianglePainter&lt;br /&gt;
 {&lt;br /&gt;
     // ..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
How can do the same in Java? Ofcourse, we will use composition, but how will you know which version of draw function to call? May be we can write some thing like this:&lt;br /&gt;
&lt;br /&gt;
 if (shape instanceof Triangle)&lt;br /&gt;
 {&lt;br /&gt;
       trianglePainter.draw((Triangle) shape);&lt;br /&gt;
 }&lt;br /&gt;
 else if(shape instanceof Circle)&lt;br /&gt;
 {&lt;br /&gt;
       circlePainter.draw((Circle) shape);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Unfortunately this is not a scalable solution and for every new tool we add to our paint program, we will have to change this code. This is where design patterns like [http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern Chain of Responsibility] proves helpful.&lt;br /&gt;
&lt;br /&gt;
Implementing the above code in Java using Chain of Responsibility design pattern.&lt;br /&gt;
&lt;br /&gt;
 interface IPainter&lt;br /&gt;
 {&lt;br /&gt;
     boolean canDraw(Shape s);&lt;br /&gt;
     void draw(Shape s);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
All painters will implement the above function:&lt;br /&gt;
&lt;br /&gt;
 class TrianglePainter implements IPainter&lt;br /&gt;
 {&lt;br /&gt;
    public boolean canDraw(Shape s) { return s instanceof Triangle; }&lt;br /&gt;
    public void draw(Shape s) { // ... }  &lt;br /&gt;
 } &lt;br /&gt;
&lt;br /&gt;
 class CanvasPainter implements IPainter&lt;br /&gt;
 {&lt;br /&gt;
    // List&amp;lt;IPainter&amp;gt; = ....&lt;br /&gt;
    // function to add and remove painters&lt;br /&gt;
    public void draw(Shape s)&lt;br /&gt;
    {&lt;br /&gt;
       for(Paint p : painters)&lt;br /&gt;
       { &lt;br /&gt;
            if(p.canDraw(s)) { p.draw(s); return; }&lt;br /&gt;
       }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
We can see that, chain of responsibility design pattern provides a very neat way to handle multiple inheritance in languages which don't support them completely.&lt;br /&gt;
&lt;br /&gt;
= Alternates to Multiple Inheritance =&lt;br /&gt;
* Composition &amp;amp; Interfaces&lt;br /&gt;
Compositions provides a neat way of emulating Multiple Inheritance. With Multiple Inheritance, we bring functionality from different classes to the class which extends them. We can achieve the same using Composition in which one class contains instance of other class, there by importing the capability of all those classes. Since a class is allowed to implement Multiple Interfaces, the class behaves as though it extended from Multiple classes.&lt;br /&gt;
&lt;br /&gt;
* Mixins&lt;br /&gt;
Some languages provide [http://en.wikipedia.org/wiki/Mixin mixins] as a language feature. Mixins provide another elegant mechanism which mimics Multiple Inheritance. The common problems with Mixin is that it pollutes the class namespace as it imports functions from different modules. This [http://www.artima.com/weblogs/viewpost.jsp?thread=246341 article] talks about the limitations of mixins in greater detail.&lt;br /&gt;
&lt;br /&gt;
* Extended Dynamic Proxies&lt;br /&gt;
Dynamic Proxy is a technique in which we can dynamically synthesis a new object from existing objects using reflection.  With Dynamic Proxies, we can dynamically make a class implement different interfaces. This is achieved through reflection mechanism. This [http://java.sys-con.com/node/37748 article] talks about using Extended Dynamic Proxies to achieve Multiple Inheritance in Java. But this rather a verbose and a complicated solution. This [http://www.java2s.com/Code/Java/Reflection/Aninvocationhandlerthatcountsthenumberofcallsforallmethodsinthetargetclass.htm article] talks discusses using Dynamic proxies in greater detail.&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
&amp;lt;table border = &amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;Single Inheritance&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;Multiple Inheritance&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Flexibility&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Single Inheritance does have some restriction in terms of modeling real world objects, but mitigate this problem with careful design&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Multiple Inheritance provides lots more flexibility for designing&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Easy of Use&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Single Inheritance is much simpler to use&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Multiple Inheritance sometimes leaves the programmer confused. One of the common problem with Multiple inheritance in Diamond Problem&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Availability&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Single Inheritance is supported by all the Object Oriented Programming Languages&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Multiple Inheritance is not supported by few of the OOP Languages, because the designer of the language thought it is hard to use and not necessary for common scenarios.&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Efficiency&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;This actually language specific. But generally it takes less time to figure which version of the function to call in a single inheritance&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Virtual Function dispatch in C++ is more time consuming in Multiple Inheritance. For example Multiple Inheritance in C++, needs multiple vtable for dispatching virtual functions. [http://www.cs.ucsb.edu/~urs/oocsb/papers/oopsla96.pdf (1)] discusses about this problem in great detail.&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Further Reading =&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Virtual_inheritance Virtual Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_composition Composition]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Mixin Mixins]&lt;br /&gt;
&lt;br /&gt;
= Reference =&lt;br /&gt;
*[http://www.cs.ucsb.edu/~urs/oocsb/papers/oopsla96.pdf (1) Cost of Virtual Functions C++]&lt;br /&gt;
*[http://www.artima.com/weblogs/viewpost.jsp?thread=246341 (2) Mixins considered Harmful]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Diamond_problem (3) Diamond Problem with Multiple Inheritance]&lt;br /&gt;
*[http://java.sun.com/docs/white/langenv/ (4) The Java Language]&lt;/div&gt;</summary>
		<author><name>Cgoogc</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_25_ag&amp;diff=35990</id>
		<title>CSC/ECE 517 Fall 2010/ch1 25 ag</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_25_ag&amp;diff=35990"/>
		<updated>2010-09-22T14:30:20Z</updated>

		<summary type="html">&lt;p&gt;Cgoogc: /* Alternates to Multiple Inheritance */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
= What is Inheritance? =&lt;br /&gt;
Inheritance, along with polymorphism and encapsulation, is one of the pillar features of [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch1_1e_bb#Object_Oriented_Programming Object Oriented Programming] (OOP). Inheritance allows a class to reuse, extend or modify the behavior of another class. The term inheritance generally refers to single inheritance, where a sub-class or a derived class inherits methods and attributes from only one parent class. &lt;br /&gt;
&lt;br /&gt;
For example, consider a general class of electrical appliances. Switching an appliance on and off can be considered a standard feature of every appliance.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 class ElectricAppliance&lt;br /&gt;
 {&lt;br /&gt;
     public void on() { ... }&lt;br /&gt;
     public void off() { ... }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class TempControlAppliance : public ElectricAppliance&lt;br /&gt;
 {&lt;br /&gt;
     public void increaseTemp() { .. }&lt;br /&gt;
     public void decreaseTemp() { .. }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Heater : public TempControlAppliance &lt;br /&gt;
 {&lt;br /&gt;
     public void heat() { .. }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Cooler : public TempControlAppliance &lt;br /&gt;
 {&lt;br /&gt;
     public void cool() { .. }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
If we don't have inheritance, we might have to duplicate all the functions in both Heater and Cooler class. Inheritance is very important to achieve DRY (Don't Repeat Yourself). In the above class hierarchy, TempControlAppliance is super (parent) class for both Cooler and Heater subclass (child).&lt;br /&gt;
&lt;br /&gt;
= What is Multiple Inheritance =&lt;br /&gt;
When a class inherits from more than one class i.e., a child class having more than one parent class, is called multiple inheritance. Though this feature offers more flexibility, it seem to overwhelm most of the developer, so some of the object oriented language designer decided not to support Multiple Inheritance. Java and C# are some of the most famous language which don't support multiple inheritance. We need careful design to take proper advantage of Multiple inheritance, otherwise you might end up in spaghetti code, with classes inheritancing from multiple classes.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Building upon the above example, let us say we have a new device called Air Conditioner which can both heat and cool air. We might have a new class called AirConditioner as follows:&lt;br /&gt;
&lt;br /&gt;
 class AirConditioner : public Heater, public Cooler&lt;br /&gt;
 {&lt;br /&gt;
      public void setThresholdTemperature(int temp) { ... }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
= Good and bad of Multiple Inheritance =&lt;br /&gt;
Good:&lt;br /&gt;
* Multiple Inheritance strongly supports DRY principle, which leads to better software which is extensible, cohesive, less coupled and modular. &lt;br /&gt;
* Multiple Inheritance is very common in real world, in which object takes attributes from different objects.&lt;br /&gt;
* Multiple Inheritance is more expressive than Single Inheritane, so it gives flexibility in designing software with complex object relationships.&lt;br /&gt;
&lt;br /&gt;
Bad:&lt;br /&gt;
* To take complete advantage of Multiple Inheritance needs a thoughtful design. Without a good design, Multiple Inheritance infact might lead to bad code.&lt;br /&gt;
* Multiple Inheritance is overwhelming to developers. Requires more experience to properly use Multiple Inheritance.&lt;br /&gt;
* Multiple Inheritance is hard to use and make the language complicated. Some example like what happens when you Inherit from multiple classes which has same method?&lt;br /&gt;
&lt;br /&gt;
== Implementation issues ==&lt;br /&gt;
One of the most common problem with Multiple Inheritance is called the [http://en.wikipedia.org/wiki/Diamond_problem Diamond Problem(3)]. This is encountered when a derived class inherits from multiple parent classes, which in turn inherit from one super class. Let us try to understand this problem using an example.&lt;br /&gt;
&lt;br /&gt;
Consider a task of modelling a car. Now a car can broadly be divided into two subclasses, mechanical cars and electric cars. &lt;br /&gt;
&lt;br /&gt;
 class Car&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
        void start() { cout &amp;lt;&amp;lt; &amp;quot;Start with loud noise&amp;quot;;&lt;br /&gt;
        void steer();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class MechCar : public Car&lt;br /&gt;
 {&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class ElectricCar : public Car&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
       void start() { cout &amp;lt;&amp;lt; &amp;quot;start without noise&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Now considering modern advancements in vehicle design, a new subclass of hybrid cars have emerged in the market. These cars have traits of both mechanical and electric cars. Here is how this is achieved in C++.&lt;br /&gt;
&lt;br /&gt;
 class HybridCar : public MechCar, ElectricCar&lt;br /&gt;
 {&lt;br /&gt;
      // This has two copies of start()&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Here is an illustration of the inheritance hierarchy.&lt;br /&gt;
&lt;br /&gt;
[[Image:Car-hybrid.jpg|frame|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now if an object of the HybridCar class calls a method of class Car, an ambiguous situation arises. Assuming the method has been [http://en.wikipedia.org/wiki/Method_overriding overridden] by class MechCar and ElectricCar, then it is not clear which version of the method the object is referring to. This is what is known as the Diamond Problem. This problem is addressed in C++ solves this problem through the use of  [http://en.wikipedia.org/wiki/Virtual_inheritance Virtual Inheritance]. &lt;br /&gt;
&lt;br /&gt;
 HybridCar h;&lt;br /&gt;
 h.ElectricCar::start();&lt;br /&gt;
&lt;br /&gt;
There are also ambiguities during the type casting. For more details about Virtual Inheritance refer [http://en.wikipedia.org/wiki/Virtual_inheritance here]&lt;br /&gt;
&lt;br /&gt;
== Multiple Inheritance - Do we really need this? ==&lt;br /&gt;
Many languages which don't support multiple inheritance completely, has some ways of emulating multiple inheritance. We will consider Java as an example which does not support Multiple Inheritance completely and see how we can emulate multiple inheritance in Java. It is not completely true to say that Java does not support multiple inheritance, in fact with interfaces we can have multiple inheritance. Since, Interface just defines the behaviour, it does not have any attributes to describe it, Interfaces are not considered as a Class.&lt;br /&gt;
&lt;br /&gt;
Let us consider the previous example of an AirConditioner:&lt;br /&gt;
&lt;br /&gt;
 class AirConditioner extends [______________]&lt;br /&gt;
&lt;br /&gt;
We cannot fit both the classes there since a class in Java can extend from a single class, unlike what we did in C++. But Java classes can implement multiple interfaces.&lt;br /&gt;
&lt;br /&gt;
 public interface IHeater {&lt;br /&gt;
    void heat();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public interface ICooler {&lt;br /&gt;
    void cool();&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public Heater extends TempControllerAppliance implements IHeater {&lt;br /&gt;
    public void heat() { .. }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public Cooler extends TempControllerAppliance implements ICooler{&lt;br /&gt;
    public void cool() { .. }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public IAirConditioner {&lt;br /&gt;
      public void setThresholdTemp(int t);&lt;br /&gt;
 } &lt;br /&gt;
&lt;br /&gt;
 public class AirConditioner extends TempControllerAppliance implements IHeater, ICooler, IAirConditioner {&lt;br /&gt;
    private ICooler cooler = new Cooler();&lt;br /&gt;
    private IHeater heater = new Heater();&lt;br /&gt;
 &lt;br /&gt;
    public void cool() {&lt;br /&gt;
        cooler.cool();&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void heat() {&lt;br /&gt;
        heater.heat();&lt;br /&gt;
    }  &lt;br /&gt;
    &lt;br /&gt;
    public void setThreshold(int t) { ... }   &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
To implement multiple inheritance in Java, we need to the following:&lt;br /&gt;
1. Make every class implement an Interface&lt;br /&gt;
2. Classes extends these interfaces to add logic&lt;br /&gt;
3. When a class needs to extend from Multiple interfaces, you can implement those interfaces and delegate the call to the actual implementation. This is achieved through Composition.&lt;br /&gt;
&lt;br /&gt;
We can see that it is possible to emulate multiple inheritance through [http://en.wikipedia.org/wiki/Object_composition composition]. &lt;br /&gt;
&lt;br /&gt;
Let us take another example and see how we can emulate Multiple inheritance in Java. We are developing a paint application. We have separate tools to draw each primitive shapes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
 class Shape&lt;br /&gt;
 {&lt;br /&gt;
    //...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Square : Shape&lt;br /&gt;
 {&lt;br /&gt;
   // ..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Circle : Shape&lt;br /&gt;
 {&lt;br /&gt;
    //..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Triangle : Shape&lt;br /&gt;
 {&lt;br /&gt;
    //..&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
 class Painter&lt;br /&gt;
 {&lt;br /&gt;
     //..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class SquarePainter : Painter&lt;br /&gt;
 {&lt;br /&gt;
     void draw(Square s) { }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class CirclePainter : Painter&lt;br /&gt;
 {&lt;br /&gt;
    void draw(Circle c) { }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class TrianglePainter : Painter&lt;br /&gt;
 {&lt;br /&gt;
    void draw(Triangle t) { }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we need a canvas painter, which should have the capability to draw any of the above shape. In C++, this is pretty straight forward, you define a new class which extends all painters.&lt;br /&gt;
&lt;br /&gt;
 class CanvasPainter : SquarePainter, CirclePainter, TrianglePainter&lt;br /&gt;
 {&lt;br /&gt;
     // ..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
How can do the same in Java? Ofcourse, we will use composition, but how will you know which version of draw function to call? May be we can write some thing like this:&lt;br /&gt;
&lt;br /&gt;
 if (shape instanceof Triangle)&lt;br /&gt;
 {&lt;br /&gt;
       trianglePainter.draw((Triangle) shape);&lt;br /&gt;
 }&lt;br /&gt;
 else if(shape instanceof Circle)&lt;br /&gt;
 {&lt;br /&gt;
       circlePainter.draw((Circle) shape);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Unfortunately this is not a scalable solution and for every new tool we add to our paint program, we will have to change this code. This is where design patterns like [http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern Chain of Responsibility] proves helpful.&lt;br /&gt;
&lt;br /&gt;
Implementing the above code in Java using Chain of Responsibility design pattern.&lt;br /&gt;
&lt;br /&gt;
 interface IPainter&lt;br /&gt;
 {&lt;br /&gt;
     boolean canDraw(Shape s);&lt;br /&gt;
     void draw(Shape s);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
All painters will implement the above function:&lt;br /&gt;
&lt;br /&gt;
 class TrianglePainter implements IPainter&lt;br /&gt;
 {&lt;br /&gt;
    public boolean canDraw(Shape s) { return s instanceof Triangle; }&lt;br /&gt;
    public void draw(Shape s) { // ... }  &lt;br /&gt;
 } &lt;br /&gt;
&lt;br /&gt;
 class CanvasPainter implements IPainter&lt;br /&gt;
 {&lt;br /&gt;
    // List&amp;lt;IPainter&amp;gt; = ....&lt;br /&gt;
    // function to add and remove painters&lt;br /&gt;
    public void draw(Shape s)&lt;br /&gt;
    {&lt;br /&gt;
       for(Paint p : painters)&lt;br /&gt;
       { &lt;br /&gt;
            if(p.canDraw(s)) { p.draw(s); return; }&lt;br /&gt;
       }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
We can see that, chain of responsibility design pattern provides a very neat way to handle multiple inheritance in languages which don't support them completely.&lt;br /&gt;
&lt;br /&gt;
= Alternates to Multiple Inheritance =&lt;br /&gt;
* Composition &amp;amp; Interfaces&lt;br /&gt;
Compositions provides a neat way of emulating Multiple Inheritance. With Multiple Inheritance, we bring functionality from different classes to the class which extends them. We can achieve the same using Composition in which one class contains instance of other class, there by importing the capability of all those classes. Since a class is allowed to implement Multiple Interfaces, the class behaves as though it extended from Multiple classes.&lt;br /&gt;
&lt;br /&gt;
* Mixins&lt;br /&gt;
Some languages provide [http://en.wikipedia.org/wiki/Mixin mixins] as a language feature. Mixins provide another elegant mechanism which mimics Multiple Inheritance. The common problems with Mixin is that it pollutes the class namespace as it imports functions from different modules. This [http://www.artima.com/weblogs/viewpost.jsp?thread=246341 article] talks about the limitations of mixins in greater detail.&lt;br /&gt;
&lt;br /&gt;
* Extended Dynamic Proxies&lt;br /&gt;
Dynamic Proxy is a technique in which we can dynamically synthesis a new object from existing objects using reflection.  With Dynamic Proxies, we can dynamically make a class implement different interfaces. This is achieved through reflection mechanism. This [http://java.sys-con.com/node/37748 article] talks about using Extended Dynamic Proxies to achieve Multiple Inheritance in Java. But this rather a verbose and a complicated solution. This [http://www.java2s.com/Code/Java/Reflection/Aninvocationhandlerthatcountsthenumberofcallsforallmethodsinthetargetclass.htm article] talks about using Dynamic proxies.&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
&amp;lt;table border = &amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;Single Inheritance&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;Multiple Inheritance&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Flexibility&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Single Inheritance does have some restriction in terms of modeling real world objects, but mitigate this problem with careful design&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Multiple Inheritance provides lots more flexibility for designing&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Easy of Use&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Single Inheritance is much simpler to use&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Multiple Inheritance sometimes leaves the programmer confused. One of the common problem with Multiple inheritance in Diamond Problem&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Availability&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Single Inheritance is supported by all the Object Oriented Programming Languages&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Multiple Inheritance is not supported by few of the OOP Languages, because the designer of the language thought it is hard to use and not necessary for common scenarios.&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Efficiency&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;This actually language specific. But generally it takes less time to figure which version of the function to call in a single inheritance&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Virtual Function dispatch in C++ is more time consuming in Multiple Inheritance. For example Multiple Inheritance in C++, needs multiple vtable for dispatching virtual functions. [http://www.cs.ucsb.edu/~urs/oocsb/papers/oopsla96.pdf (1)] discusses about this problem in great detail.&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Further Reading =&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Virtual_inheritance Virtual Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_composition Composition]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Mixin Mixins]&lt;br /&gt;
&lt;br /&gt;
= Reference =&lt;br /&gt;
*[http://www.cs.ucsb.edu/~urs/oocsb/papers/oopsla96.pdf (1) Cost of Virtual Functions C++]&lt;br /&gt;
*[http://www.artima.com/weblogs/viewpost.jsp?thread=246341 (2) Mixins considered Harmful]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Diamond_problem (3) Diamond Problem with Multiple Inheritance]&lt;br /&gt;
*[http://java.sun.com/docs/white/langenv/ (4) The Java Language]&lt;/div&gt;</summary>
		<author><name>Cgoogc</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_25_ag&amp;diff=35981</id>
		<title>CSC/ECE 517 Fall 2010/ch1 25 ag</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_25_ag&amp;diff=35981"/>
		<updated>2010-09-22T14:22:58Z</updated>

		<summary type="html">&lt;p&gt;Cgoogc: /* Reference */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
= What is Inheritance? =&lt;br /&gt;
Inheritance, along with polymorphism and encapsulation, is one of the pillar features of [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch1_1e_bb#Object_Oriented_Programming Object Oriented Programming] (OOP). Inheritance allows a class to reuse, extend or modify the behavior of another class. The term inheritance generally refers to single inheritance, where a sub-class or a derived class inherits methods and attributes from only one parent class. &lt;br /&gt;
&lt;br /&gt;
For example, consider a general class of electrical appliances. Switching an appliance on and off can be considered a standard feature of every appliance.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 class ElectricAppliance&lt;br /&gt;
 {&lt;br /&gt;
     public void on() { ... }&lt;br /&gt;
     public void off() { ... }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class TempControlAppliance : public ElectricAppliance&lt;br /&gt;
 {&lt;br /&gt;
     public void increaseTemp() { .. }&lt;br /&gt;
     public void decreaseTemp() { .. }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Heater : public TempControlAppliance &lt;br /&gt;
 {&lt;br /&gt;
     public void heat() { .. }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Cooler : public TempControlAppliance &lt;br /&gt;
 {&lt;br /&gt;
     public void cool() { .. }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
If we don't have inheritance, we might have to duplicate all the functions in both Heater and Cooler class. Inheritance is very important to achieve DRY (Don't Repeat Yourself). In the above class hierarchy, TempControlAppliance is super (parent) class for both Cooler and Heater subclass (child).&lt;br /&gt;
&lt;br /&gt;
= What is Multiple Inheritance =&lt;br /&gt;
When a class inherits from more than one class i.e., a child class having more than one parent class, is called multiple inheritance. Though this feature offers more flexibility, it seem to overwhelm most of the developer, so some of the object oriented language designer decided not to support Multiple Inheritance. Java and C# are some of the most famous language which don't support multiple inheritance. We need careful design to take proper advantage of Multiple inheritance, otherwise you might end up in spaghetti code, with classes inheritancing from multiple classes.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Building upon the above example, let us say we have a new device called Air Conditioner which can both heat and cool air. We might have a new class called AirConditioner as follows:&lt;br /&gt;
&lt;br /&gt;
 class AirConditioner : public Heater, public Cooler&lt;br /&gt;
 {&lt;br /&gt;
      public void setThresholdTemperature(int temp) { ... }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
= Good and bad of Multiple Inheritance =&lt;br /&gt;
Good:&lt;br /&gt;
* Multiple Inheritance strongly supports DRY principle, which leads to better software which is extensible, cohesive, less coupled and modular. &lt;br /&gt;
* Multiple Inheritance is very common in real world, in which object takes attributes from different objects.&lt;br /&gt;
* Multiple Inheritance is more expressive than Single Inheritane, so it gives flexibility in designing software with complex object relationships.&lt;br /&gt;
&lt;br /&gt;
Bad:&lt;br /&gt;
* To take complete advantage of Multiple Inheritance needs a thoughtful design. Without a good design, Multiple Inheritance infact might lead to bad code.&lt;br /&gt;
* Multiple Inheritance is overwhelming to developers. Requires more experience to properly use Multiple Inheritance.&lt;br /&gt;
* Multiple Inheritance is hard to use and make the language complicated. Some example like what happens when you Inherit from multiple classes which has same method?&lt;br /&gt;
&lt;br /&gt;
== Implementation issues ==&lt;br /&gt;
One of the most common problem with Multiple Inheritance is called the [http://en.wikipedia.org/wiki/Diamond_problem Diamond Problem(3)]. This is encountered when a derived class inherits from multiple parent classes, which in turn inherit from one super class. Let us try to understand this problem using an example.&lt;br /&gt;
&lt;br /&gt;
Consider a task of modelling a car. Now a car can broadly be divided into two subclasses, mechanical cars and electric cars. &lt;br /&gt;
&lt;br /&gt;
 class Car&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
        void start() { cout &amp;lt;&amp;lt; &amp;quot;Start with loud noise&amp;quot;;&lt;br /&gt;
        void steer();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class MechCar : public Car&lt;br /&gt;
 {&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class ElectricCar : public Car&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
       void start() { cout &amp;lt;&amp;lt; &amp;quot;start without noise&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Now considering modern advancements in vehicle design, a new subclass of hybrid cars have emerged in the market. These cars have traits of both mechanical and electric cars. Here is how this is achieved in C++.&lt;br /&gt;
&lt;br /&gt;
 class HybridCar : public MechCar, ElectricCar&lt;br /&gt;
 {&lt;br /&gt;
      // This has two copies of start()&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Here is an illustration of the inheritance hierarchy.&lt;br /&gt;
&lt;br /&gt;
[[Image:Car-hybrid.jpg|frame|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now if an object of the HybridCar class calls a method of class Car, an ambiguous situation arises. Assuming the method has been [http://en.wikipedia.org/wiki/Method_overriding overridden] by class MechCar and ElectricCar, then it is not clear which version of the method the object is referring to. This is what is known as the Diamond Problem. This problem is addressed in C++ solves this problem through the use of  [http://en.wikipedia.org/wiki/Virtual_inheritance Virtual Inheritance]. &lt;br /&gt;
&lt;br /&gt;
 HybridCar h;&lt;br /&gt;
 h.ElectricCar::start();&lt;br /&gt;
&lt;br /&gt;
There are also ambiguities during the type casting. For more details about Virtual Inheritance refer [http://en.wikipedia.org/wiki/Virtual_inheritance here]&lt;br /&gt;
&lt;br /&gt;
== Multiple Inheritance - Do we really need this? ==&lt;br /&gt;
Many languages which don't support multiple inheritance completely, has some ways of emulating multiple inheritance. We will consider Java as an example which does not support Multiple Inheritance completely and see how we can emulate multiple inheritance in Java. It is not completely true to say that Java does not support multiple inheritance, in fact with interfaces we can have multiple inheritance. Since, Interface just defines the behaviour, it does not have any attributes to describe it, Interfaces are not considered as a Class.&lt;br /&gt;
&lt;br /&gt;
Let us consider the previous example of an AirConditioner:&lt;br /&gt;
&lt;br /&gt;
 class AirConditioner extends [______________]&lt;br /&gt;
&lt;br /&gt;
We cannot fit both the classes there since a class in Java can extend from a single class, unlike what we did in C++. But Java classes can implement multiple interfaces.&lt;br /&gt;
&lt;br /&gt;
 public interface IHeater {&lt;br /&gt;
    void heat();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public interface ICooler {&lt;br /&gt;
    void cool();&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public Heater extends TempControllerAppliance implements IHeater {&lt;br /&gt;
    public void heat() { .. }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public Cooler extends TempControllerAppliance implements ICooler{&lt;br /&gt;
    public void cool() { .. }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public IAirConditioner {&lt;br /&gt;
      public void setThresholdTemp(int t);&lt;br /&gt;
 } &lt;br /&gt;
&lt;br /&gt;
 public class AirConditioner extends TempControllerAppliance implements IHeater, ICooler, IAirConditioner {&lt;br /&gt;
    private ICooler cooler = new Cooler();&lt;br /&gt;
    private IHeater heater = new Heater();&lt;br /&gt;
 &lt;br /&gt;
    public void cool() {&lt;br /&gt;
        cooler.cool();&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void heat() {&lt;br /&gt;
        heater.heat();&lt;br /&gt;
    }  &lt;br /&gt;
    &lt;br /&gt;
    public void setThreshold(int t) { ... }   &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
To implement multiple inheritance in Java, we need to the following:&lt;br /&gt;
1. Make every class implement an Interface&lt;br /&gt;
2. Classes extends these interfaces to add logic&lt;br /&gt;
3. When a class needs to extend from Multiple interfaces, you can implement those interfaces and delegate the call to the actual implementation. This is achieved through Composition.&lt;br /&gt;
&lt;br /&gt;
We can see that it is possible to emulate multiple inheritance through [http://en.wikipedia.org/wiki/Object_composition composition]. &lt;br /&gt;
&lt;br /&gt;
Let us take another example and see how we can emulate Multiple inheritance in Java. We are developing a paint application. We have separate tools to draw each primitive shapes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
 class Shape&lt;br /&gt;
 {&lt;br /&gt;
    //...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Square : Shape&lt;br /&gt;
 {&lt;br /&gt;
   // ..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Circle : Shape&lt;br /&gt;
 {&lt;br /&gt;
    //..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Triangle : Shape&lt;br /&gt;
 {&lt;br /&gt;
    //..&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
 class Painter&lt;br /&gt;
 {&lt;br /&gt;
     //..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class SquarePainter : Painter&lt;br /&gt;
 {&lt;br /&gt;
     void draw(Square s) { }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class CirclePainter : Painter&lt;br /&gt;
 {&lt;br /&gt;
    void draw(Circle c) { }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class TrianglePainter : Painter&lt;br /&gt;
 {&lt;br /&gt;
    void draw(Triangle t) { }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we need a canvas painter, which should have the capability to draw any of the above shape. In C++, this is pretty straight forward, you define a new class which extends all painters.&lt;br /&gt;
&lt;br /&gt;
 class CanvasPainter : SquarePainter, CirclePainter, TrianglePainter&lt;br /&gt;
 {&lt;br /&gt;
     // ..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
How can do the same in Java? Ofcourse, we will use composition, but how will you know which version of draw function to call? May be we can write some thing like this:&lt;br /&gt;
&lt;br /&gt;
 if (shape instanceof Triangle)&lt;br /&gt;
 {&lt;br /&gt;
       trianglePainter.draw((Triangle) shape);&lt;br /&gt;
 }&lt;br /&gt;
 else if(shape instanceof Circle)&lt;br /&gt;
 {&lt;br /&gt;
       circlePainter.draw((Circle) shape);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Unfortunately this is not a scalable solution and for every new tool we add to our paint program, we will have to change this code. This is where design patterns like [http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern Chain of Responsibility] proves helpful.&lt;br /&gt;
&lt;br /&gt;
Implementing the above code in Java using Chain of Responsibility design pattern.&lt;br /&gt;
&lt;br /&gt;
 interface IPainter&lt;br /&gt;
 {&lt;br /&gt;
     boolean canDraw(Shape s);&lt;br /&gt;
     void draw(Shape s);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
All painters will implement the above function:&lt;br /&gt;
&lt;br /&gt;
 class TrianglePainter implements IPainter&lt;br /&gt;
 {&lt;br /&gt;
    public boolean canDraw(Shape s) { return s instanceof Triangle; }&lt;br /&gt;
    public void draw(Shape s) { // ... }  &lt;br /&gt;
 } &lt;br /&gt;
&lt;br /&gt;
 class CanvasPainter implements IPainter&lt;br /&gt;
 {&lt;br /&gt;
    // List&amp;lt;IPainter&amp;gt; = ....&lt;br /&gt;
    // function to add and remove painters&lt;br /&gt;
    public void draw(Shape s)&lt;br /&gt;
    {&lt;br /&gt;
       for(Paint p : painters)&lt;br /&gt;
       { &lt;br /&gt;
            if(p.canDraw(s)) { p.draw(s); return; }&lt;br /&gt;
       }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
We can see that, chain of responsibility design pattern provides a very neat way to handle multiple inheritance in languages which don't support them completely.&lt;br /&gt;
&lt;br /&gt;
= Alternates to Multiple Inheritance =&lt;br /&gt;
* Composition &amp;amp; Interfaces&lt;br /&gt;
Compositions provides a neat way of emulating Multiple Inheritance. With Multiple Inheritance, we bring functionality from different classes to the class which extends them. We can achieve the same using Composition in which one class contains instance of other class, there by importing the capability of all those classes. Since a class is allowed to implement Multiple Interfaces, the class behaves as though it extended from Multiple classes.&lt;br /&gt;
&lt;br /&gt;
* Mixins&lt;br /&gt;
Some languages provide [http://en.wikipedia.org/wiki/Mixin mixins] as a language feature. Mixins provide another elegant mechanism which mimics Multiple Inheritance. The common problems with Mixin is that it pollutes the class namespace as it imports functions from different modules. This [http://www.artima.com/weblogs/viewpost.jsp?thread=246341 article] talks about the limitations of mixins in greater detail.&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
&amp;lt;table border = &amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;Single Inheritance&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;Multiple Inheritance&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Flexibility&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Single Inheritance does have some restriction in terms of modeling real world objects, but mitigate this problem with careful design&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Multiple Inheritance provides lots more flexibility for designing&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Easy of Use&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Single Inheritance is much simpler to use&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Multiple Inheritance sometimes leaves the programmer confused. One of the common problem with Multiple inheritance in Diamond Problem&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Availability&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Single Inheritance is supported by all the Object Oriented Programming Languages&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Multiple Inheritance is not supported by few of the OOP Languages, because the designer of the language thought it is hard to use and not necessary for common scenarios.&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Efficiency&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;This actually language specific. But generally it takes less time to figure which version of the function to call in a single inheritance&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Virtual Function dispatch in C++ is more time consuming in Multiple Inheritance. For example Multiple Inheritance in C++, needs multiple vtable for dispatching virtual functions. [http://www.cs.ucsb.edu/~urs/oocsb/papers/oopsla96.pdf (1)] discusses about this problem in great detail.&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Further Reading =&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Virtual_inheritance Virtual Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_composition Composition]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Mixin Mixins]&lt;br /&gt;
&lt;br /&gt;
= Reference =&lt;br /&gt;
*[http://www.cs.ucsb.edu/~urs/oocsb/papers/oopsla96.pdf (1) Cost of Virtual Functions C++]&lt;br /&gt;
*[http://www.artima.com/weblogs/viewpost.jsp?thread=246341 (2) Mixins considered Harmful]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Diamond_problem (3) Diamond Problem with Multiple Inheritance]&lt;br /&gt;
*[http://java.sun.com/docs/white/langenv/ (4) The Java Language]&lt;/div&gt;</summary>
		<author><name>Cgoogc</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_25_ag&amp;diff=35980</id>
		<title>CSC/ECE 517 Fall 2010/ch1 25 ag</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_25_ag&amp;diff=35980"/>
		<updated>2010-09-22T14:20:43Z</updated>

		<summary type="html">&lt;p&gt;Cgoogc: /* What is Inheritance? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
= What is Inheritance? =&lt;br /&gt;
Inheritance, along with polymorphism and encapsulation, is one of the pillar features of [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch1_1e_bb#Object_Oriented_Programming Object Oriented Programming] (OOP). Inheritance allows a class to reuse, extend or modify the behavior of another class. The term inheritance generally refers to single inheritance, where a sub-class or a derived class inherits methods and attributes from only one parent class. &lt;br /&gt;
&lt;br /&gt;
For example, consider a general class of electrical appliances. Switching an appliance on and off can be considered a standard feature of every appliance.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 class ElectricAppliance&lt;br /&gt;
 {&lt;br /&gt;
     public void on() { ... }&lt;br /&gt;
     public void off() { ... }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class TempControlAppliance : public ElectricAppliance&lt;br /&gt;
 {&lt;br /&gt;
     public void increaseTemp() { .. }&lt;br /&gt;
     public void decreaseTemp() { .. }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Heater : public TempControlAppliance &lt;br /&gt;
 {&lt;br /&gt;
     public void heat() { .. }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Cooler : public TempControlAppliance &lt;br /&gt;
 {&lt;br /&gt;
     public void cool() { .. }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
If we don't have inheritance, we might have to duplicate all the functions in both Heater and Cooler class. Inheritance is very important to achieve DRY (Don't Repeat Yourself). In the above class hierarchy, TempControlAppliance is super (parent) class for both Cooler and Heater subclass (child).&lt;br /&gt;
&lt;br /&gt;
= What is Multiple Inheritance =&lt;br /&gt;
When a class inherits from more than one class i.e., a child class having more than one parent class, is called multiple inheritance. Though this feature offers more flexibility, it seem to overwhelm most of the developer, so some of the object oriented language designer decided not to support Multiple Inheritance. Java and C# are some of the most famous language which don't support multiple inheritance. We need careful design to take proper advantage of Multiple inheritance, otherwise you might end up in spaghetti code, with classes inheritancing from multiple classes.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Building upon the above example, let us say we have a new device called Air Conditioner which can both heat and cool air. We might have a new class called AirConditioner as follows:&lt;br /&gt;
&lt;br /&gt;
 class AirConditioner : public Heater, public Cooler&lt;br /&gt;
 {&lt;br /&gt;
      public void setThresholdTemperature(int temp) { ... }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
= Good and bad of Multiple Inheritance =&lt;br /&gt;
Good:&lt;br /&gt;
* Multiple Inheritance strongly supports DRY principle, which leads to better software which is extensible, cohesive, less coupled and modular. &lt;br /&gt;
* Multiple Inheritance is very common in real world, in which object takes attributes from different objects.&lt;br /&gt;
* Multiple Inheritance is more expressive than Single Inheritane, so it gives flexibility in designing software with complex object relationships.&lt;br /&gt;
&lt;br /&gt;
Bad:&lt;br /&gt;
* To take complete advantage of Multiple Inheritance needs a thoughtful design. Without a good design, Multiple Inheritance infact might lead to bad code.&lt;br /&gt;
* Multiple Inheritance is overwhelming to developers. Requires more experience to properly use Multiple Inheritance.&lt;br /&gt;
* Multiple Inheritance is hard to use and make the language complicated. Some example like what happens when you Inherit from multiple classes which has same method?&lt;br /&gt;
&lt;br /&gt;
== Implementation issues ==&lt;br /&gt;
One of the most common problem with Multiple Inheritance is called the [http://en.wikipedia.org/wiki/Diamond_problem Diamond Problem(3)]. This is encountered when a derived class inherits from multiple parent classes, which in turn inherit from one super class. Let us try to understand this problem using an example.&lt;br /&gt;
&lt;br /&gt;
Consider a task of modelling a car. Now a car can broadly be divided into two subclasses, mechanical cars and electric cars. &lt;br /&gt;
&lt;br /&gt;
 class Car&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
        void start() { cout &amp;lt;&amp;lt; &amp;quot;Start with loud noise&amp;quot;;&lt;br /&gt;
        void steer();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class MechCar : public Car&lt;br /&gt;
 {&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class ElectricCar : public Car&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
       void start() { cout &amp;lt;&amp;lt; &amp;quot;start without noise&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Now considering modern advancements in vehicle design, a new subclass of hybrid cars have emerged in the market. These cars have traits of both mechanical and electric cars. Here is how this is achieved in C++.&lt;br /&gt;
&lt;br /&gt;
 class HybridCar : public MechCar, ElectricCar&lt;br /&gt;
 {&lt;br /&gt;
      // This has two copies of start()&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Here is an illustration of the inheritance hierarchy.&lt;br /&gt;
&lt;br /&gt;
[[Image:Car-hybrid.jpg|frame|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now if an object of the HybridCar class calls a method of class Car, an ambiguous situation arises. Assuming the method has been [http://en.wikipedia.org/wiki/Method_overriding overridden] by class MechCar and ElectricCar, then it is not clear which version of the method the object is referring to. This is what is known as the Diamond Problem. This problem is addressed in C++ solves this problem through the use of  [http://en.wikipedia.org/wiki/Virtual_inheritance Virtual Inheritance]. &lt;br /&gt;
&lt;br /&gt;
 HybridCar h;&lt;br /&gt;
 h.ElectricCar::start();&lt;br /&gt;
&lt;br /&gt;
There are also ambiguities during the type casting. For more details about Virtual Inheritance refer [http://en.wikipedia.org/wiki/Virtual_inheritance here]&lt;br /&gt;
&lt;br /&gt;
== Multiple Inheritance - Do we really need this? ==&lt;br /&gt;
Many languages which don't support multiple inheritance completely, has some ways of emulating multiple inheritance. We will consider Java as an example which does not support Multiple Inheritance completely and see how we can emulate multiple inheritance in Java. It is not completely true to say that Java does not support multiple inheritance, in fact with interfaces we can have multiple inheritance. Since, Interface just defines the behaviour, it does not have any attributes to describe it, Interfaces are not considered as a Class.&lt;br /&gt;
&lt;br /&gt;
Let us consider the previous example of an AirConditioner:&lt;br /&gt;
&lt;br /&gt;
 class AirConditioner extends [______________]&lt;br /&gt;
&lt;br /&gt;
We cannot fit both the classes there since a class in Java can extend from a single class, unlike what we did in C++. But Java classes can implement multiple interfaces.&lt;br /&gt;
&lt;br /&gt;
 public interface IHeater {&lt;br /&gt;
    void heat();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public interface ICooler {&lt;br /&gt;
    void cool();&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public Heater extends TempControllerAppliance implements IHeater {&lt;br /&gt;
    public void heat() { .. }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public Cooler extends TempControllerAppliance implements ICooler{&lt;br /&gt;
    public void cool() { .. }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public IAirConditioner {&lt;br /&gt;
      public void setThresholdTemp(int t);&lt;br /&gt;
 } &lt;br /&gt;
&lt;br /&gt;
 public class AirConditioner extends TempControllerAppliance implements IHeater, ICooler, IAirConditioner {&lt;br /&gt;
    private ICooler cooler = new Cooler();&lt;br /&gt;
    private IHeater heater = new Heater();&lt;br /&gt;
 &lt;br /&gt;
    public void cool() {&lt;br /&gt;
        cooler.cool();&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void heat() {&lt;br /&gt;
        heater.heat();&lt;br /&gt;
    }  &lt;br /&gt;
    &lt;br /&gt;
    public void setThreshold(int t) { ... }   &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
To implement multiple inheritance in Java, we need to the following:&lt;br /&gt;
1. Make every class implement an Interface&lt;br /&gt;
2. Classes extends these interfaces to add logic&lt;br /&gt;
3. When a class needs to extend from Multiple interfaces, you can implement those interfaces and delegate the call to the actual implementation. This is achieved through Composition.&lt;br /&gt;
&lt;br /&gt;
We can see that it is possible to emulate multiple inheritance through [http://en.wikipedia.org/wiki/Object_composition composition]. &lt;br /&gt;
&lt;br /&gt;
Let us take another example and see how we can emulate Multiple inheritance in Java. We are developing a paint application. We have separate tools to draw each primitive shapes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
 class Shape&lt;br /&gt;
 {&lt;br /&gt;
    //...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Square : Shape&lt;br /&gt;
 {&lt;br /&gt;
   // ..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Circle : Shape&lt;br /&gt;
 {&lt;br /&gt;
    //..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Triangle : Shape&lt;br /&gt;
 {&lt;br /&gt;
    //..&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
 class Painter&lt;br /&gt;
 {&lt;br /&gt;
     //..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class SquarePainter : Painter&lt;br /&gt;
 {&lt;br /&gt;
     void draw(Square s) { }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class CirclePainter : Painter&lt;br /&gt;
 {&lt;br /&gt;
    void draw(Circle c) { }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class TrianglePainter : Painter&lt;br /&gt;
 {&lt;br /&gt;
    void draw(Triangle t) { }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we need a canvas painter, which should have the capability to draw any of the above shape. In C++, this is pretty straight forward, you define a new class which extends all painters.&lt;br /&gt;
&lt;br /&gt;
 class CanvasPainter : SquarePainter, CirclePainter, TrianglePainter&lt;br /&gt;
 {&lt;br /&gt;
     // ..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
How can do the same in Java? Ofcourse, we will use composition, but how will you know which version of draw function to call? May be we can write some thing like this:&lt;br /&gt;
&lt;br /&gt;
 if (shape instanceof Triangle)&lt;br /&gt;
 {&lt;br /&gt;
       trianglePainter.draw((Triangle) shape);&lt;br /&gt;
 }&lt;br /&gt;
 else if(shape instanceof Circle)&lt;br /&gt;
 {&lt;br /&gt;
       circlePainter.draw((Circle) shape);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Unfortunately this is not a scalable solution and for every new tool we add to our paint program, we will have to change this code. This is where design patterns like [http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern Chain of Responsibility] proves helpful.&lt;br /&gt;
&lt;br /&gt;
Implementing the above code in Java using Chain of Responsibility design pattern.&lt;br /&gt;
&lt;br /&gt;
 interface IPainter&lt;br /&gt;
 {&lt;br /&gt;
     boolean canDraw(Shape s);&lt;br /&gt;
     void draw(Shape s);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
All painters will implement the above function:&lt;br /&gt;
&lt;br /&gt;
 class TrianglePainter implements IPainter&lt;br /&gt;
 {&lt;br /&gt;
    public boolean canDraw(Shape s) { return s instanceof Triangle; }&lt;br /&gt;
    public void draw(Shape s) { // ... }  &lt;br /&gt;
 } &lt;br /&gt;
&lt;br /&gt;
 class CanvasPainter implements IPainter&lt;br /&gt;
 {&lt;br /&gt;
    // List&amp;lt;IPainter&amp;gt; = ....&lt;br /&gt;
    // function to add and remove painters&lt;br /&gt;
    public void draw(Shape s)&lt;br /&gt;
    {&lt;br /&gt;
       for(Paint p : painters)&lt;br /&gt;
       { &lt;br /&gt;
            if(p.canDraw(s)) { p.draw(s); return; }&lt;br /&gt;
       }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
We can see that, chain of responsibility design pattern provides a very neat way to handle multiple inheritance in languages which don't support them completely.&lt;br /&gt;
&lt;br /&gt;
= Alternates to Multiple Inheritance =&lt;br /&gt;
* Composition &amp;amp; Interfaces&lt;br /&gt;
Compositions provides a neat way of emulating Multiple Inheritance. With Multiple Inheritance, we bring functionality from different classes to the class which extends them. We can achieve the same using Composition in which one class contains instance of other class, there by importing the capability of all those classes. Since a class is allowed to implement Multiple Interfaces, the class behaves as though it extended from Multiple classes.&lt;br /&gt;
&lt;br /&gt;
* Mixins&lt;br /&gt;
Some languages provide [http://en.wikipedia.org/wiki/Mixin mixins] as a language feature. Mixins provide another elegant mechanism which mimics Multiple Inheritance. The common problems with Mixin is that it pollutes the class namespace as it imports functions from different modules. This [http://www.artima.com/weblogs/viewpost.jsp?thread=246341 article] talks about the limitations of mixins in greater detail.&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
&amp;lt;table border = &amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;Single Inheritance&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;Multiple Inheritance&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Flexibility&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Single Inheritance does have some restriction in terms of modeling real world objects, but mitigate this problem with careful design&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Multiple Inheritance provides lots more flexibility for designing&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Easy of Use&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Single Inheritance is much simpler to use&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Multiple Inheritance sometimes leaves the programmer confused. One of the common problem with Multiple inheritance in Diamond Problem&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Availability&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Single Inheritance is supported by all the Object Oriented Programming Languages&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Multiple Inheritance is not supported by few of the OOP Languages, because the designer of the language thought it is hard to use and not necessary for common scenarios.&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Efficiency&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;This actually language specific. But generally it takes less time to figure which version of the function to call in a single inheritance&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Virtual Function dispatch in C++ is more time consuming in Multiple Inheritance. For example Multiple Inheritance in C++, needs multiple vtable for dispatching virtual functions. [http://www.cs.ucsb.edu/~urs/oocsb/papers/oopsla96.pdf (1)] discusses about this problem in great detail.&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Further Reading =&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Virtual_inheritance Virtual Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_composition Composition]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Mixin Mixins]&lt;br /&gt;
&lt;br /&gt;
= Reference =&lt;br /&gt;
*[http://www.cs.ucsb.edu/~urs/oocsb/papers/oopsla96.pdf (1) Cost of Virtual Functions C++]&lt;br /&gt;
*[http://www.artima.com/weblogs/viewpost.jsp?thread=246341 (2) Mixins considered Harmful]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Diamond_problem (3) Diamond Problem with Multiple Inheritance]&lt;/div&gt;</summary>
		<author><name>Cgoogc</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_25_ag&amp;diff=35972</id>
		<title>CSC/ECE 517 Fall 2010/ch1 25 ag</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_25_ag&amp;diff=35972"/>
		<updated>2010-09-22T13:41:59Z</updated>

		<summary type="html">&lt;p&gt;Cgoogc: /* Multiple Inheritance - Do we really need this? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
= What is Inheritance? =&lt;br /&gt;
Inheritance, along with polymorphism and encapsulation, is one of the pillar features of [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch1_1e_bb#Object_Oriented_Programming Object Oriented Programming] (OOP). Inheritance allows a class to reuse, extend or modify the behavior of another class. The term inheritance generally refers to single inheritance, where a sub-class or a derived class inherits methods and attributes from only one parent class. &lt;br /&gt;
&lt;br /&gt;
#For example, consider a general class of electrical appliances. Switching an appliance on and off can be considered a standard feature #of every appliance.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 class ElectricAppliance&lt;br /&gt;
 {&lt;br /&gt;
     public void on() { ... }&lt;br /&gt;
     public void off() { ... }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class TempControlAppliance : public ElectricAppliance&lt;br /&gt;
 {&lt;br /&gt;
     public void increaseTemp() { .. }&lt;br /&gt;
     public void decreaseTemp() { .. }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Heater : public TempControlAppliance &lt;br /&gt;
 {&lt;br /&gt;
     public void heat() { .. }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Cooler : public TempControlAppliance &lt;br /&gt;
 {&lt;br /&gt;
     public void cool() { .. }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
If we don't have inheritance, we might have to duplicate all the functions in both Heater and Cooler class. Inheritance is very important to achieve DRY (Don't Repeat Yourself). In the above class hierarchy, TempControlAppliance is super (parent) class for both Cooler and Heater subclass (child).&lt;br /&gt;
&lt;br /&gt;
= What is Multiple Inheritance =&lt;br /&gt;
When a class inherits from more than one class i.e., a child class having more than one parent class, is called multiple inheritance. Though this feature offers more flexibility, it seem to overwhelm most of the developer, so some of the object oriented language designer decided not to support Multiple Inheritance. Java and C# are some of the most famous language which don't support multiple inheritance. We need careful design to take proper advantage of Multiple inheritance, otherwise you might end up in spaghetti code, with classes inheritancing from multiple classes.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Building upon the above example, let us say we have a new device called Air Conditioner which can both heat and cool air. We might have a new class called AirConditioner as follows:&lt;br /&gt;
&lt;br /&gt;
 class AirConditioner : public Heater, public Cooler&lt;br /&gt;
 {&lt;br /&gt;
      public void setThresholdTemperature(int temp) { ... }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
= Good and bad of Multiple Inheritance =&lt;br /&gt;
Good:&lt;br /&gt;
* Multiple Inheritance strongly supports DRY principle, which leads to better software which is extensible, cohesive, less coupled and modular. &lt;br /&gt;
* Multiple Inheritance is very common in real world, in which object takes attributes from different objects.&lt;br /&gt;
* Multiple Inheritance is more expressive than Single Inheritane, so it gives flexibility in designing software with complex object relationships.&lt;br /&gt;
&lt;br /&gt;
Bad:&lt;br /&gt;
* To take complete advantage of Multiple Inheritance needs a thoughtful design. Without a good design, Multiple Inheritance infact might lead to bad code.&lt;br /&gt;
* Multiple Inheritance is overwhelming to developers. Requires more experience to properly use Multiple Inheritance.&lt;br /&gt;
* Multiple Inheritance is hard to use and make the language complicated. Some example like what happens when you Inherit from multiple classes which has same method?&lt;br /&gt;
&lt;br /&gt;
== Implementation issues ==&lt;br /&gt;
One of the most common problem with Multiple Inheritance is called the [http://en.wikipedia.org/wiki/Diamond_problem Diamond Problem(3)]. This is encountered when a derived class inherits from multiple parent classes, which in turn inherit from one super class. Let us try to understand this problem using an example.&lt;br /&gt;
&lt;br /&gt;
Consider a task of modelling a car. Now a car can broadly be divided into two subclasses, mechanical cars and electric cars. &lt;br /&gt;
&lt;br /&gt;
 class Car&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
        void start() { cout &amp;lt;&amp;lt; &amp;quot;Start with loud noise&amp;quot;;&lt;br /&gt;
        void steer();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class MechCar : public Car&lt;br /&gt;
 {&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class ElectricCar : public Car&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
       void start() { cout &amp;lt;&amp;lt; &amp;quot;start without noise&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Now considering modern advancements in vehicle design, a new subclass of hybrid cars have emerged in the market. These cars have traits of both mechanical and electric cars. Here is how this is achieved in C++.&lt;br /&gt;
&lt;br /&gt;
 class HybridCar : public MechCar, ElectricCar&lt;br /&gt;
 {&lt;br /&gt;
      // This has two copies of start()&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Here is an illustration of the inheritance hierarchy.&lt;br /&gt;
&lt;br /&gt;
[[Image:Car-hybrid.jpg|frame|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now if an object of the HybridCar class calls a method of class Car, an ambiguous situation arises. Assuming the method has been [http://en.wikipedia.org/wiki/Method_overriding overridden] by class MechCar and ElectricCar, then it is not clear which version of the method the object is referring to. This is what is known as the Diamond Problem. This problem is addressed in C++ solves this problem through the use of  [http://en.wikipedia.org/wiki/Virtual_inheritance Virtual Inheritance]. &lt;br /&gt;
&lt;br /&gt;
 HybridCar h;&lt;br /&gt;
 h.ElectricCar::start();&lt;br /&gt;
&lt;br /&gt;
There are also ambiguities during the type casting. For more details about Virtual Inheritance refer [http://en.wikipedia.org/wiki/Virtual_inheritance here]&lt;br /&gt;
&lt;br /&gt;
== Multiple Inheritance - Do we really need this? ==&lt;br /&gt;
Many languages which don't support multiple inheritance completely, has some ways of emulating multiple inheritance. We will consider Java as an example which does not support Multiple Inheritance completely and see how we can emulate multiple inheritance in Java. It is not completely true to say that Java does not support multiple inheritance, in fact with interfaces we can have multiple inheritance. Since, Interface just defines the behaviour, it does not have any attributes to describe it, Interfaces are not considered as a Class.&lt;br /&gt;
&lt;br /&gt;
Let us consider the previous example of an AirConditioner:&lt;br /&gt;
&lt;br /&gt;
 class AirConditioner extends [______________]&lt;br /&gt;
&lt;br /&gt;
We cannot fit both the classes there since a class in Java can extend from a single class, unlike what we did in C++. But Java classes can implement multiple interfaces.&lt;br /&gt;
&lt;br /&gt;
 public interface IHeater {&lt;br /&gt;
    void heat();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public interface ICooler {&lt;br /&gt;
    void cool();&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public Heater extends TempControllerAppliance implements IHeater {&lt;br /&gt;
    public void heat() { .. }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public Cooler extends TempControllerAppliance implements ICooler{&lt;br /&gt;
    public void cool() { .. }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public IAirConditioner {&lt;br /&gt;
      public void setThresholdTemp(int t);&lt;br /&gt;
 } &lt;br /&gt;
&lt;br /&gt;
 public class AirConditioner extends TempControllerAppliance implements IHeater, ICooler, IAirConditioner {&lt;br /&gt;
    private ICooler cooler = new Cooler();&lt;br /&gt;
    private IHeater heater = new Heater();&lt;br /&gt;
 &lt;br /&gt;
    public void cool() {&lt;br /&gt;
        cooler.cool();&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void heat() {&lt;br /&gt;
        heater.heat();&lt;br /&gt;
    }  &lt;br /&gt;
    &lt;br /&gt;
    public void setThreshold(int t) { ... }   &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
To implement multiple inheritance in Java, we need to the following:&lt;br /&gt;
1. Make every class implement an Interface&lt;br /&gt;
2. Classes extends these interfaces to add logic&lt;br /&gt;
3. When a class needs to extend from Multiple interfaces, you can implement those interfaces and delegate the call to the actual implementation. This is achieved through Composition.&lt;br /&gt;
&lt;br /&gt;
We can see that it is possible to emulate multiple inheritance through [http://en.wikipedia.org/wiki/Object_composition composition]. &lt;br /&gt;
&lt;br /&gt;
Let us take another example and see how we can emulate Multiple inheritance in Java. We are developing a paint application. We have separate tools to draw each primitive shapes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
 class Shape&lt;br /&gt;
 {&lt;br /&gt;
    //...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Square : Shape&lt;br /&gt;
 {&lt;br /&gt;
   // ..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Circle : Shape&lt;br /&gt;
 {&lt;br /&gt;
    //..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Triangle : Shape&lt;br /&gt;
 {&lt;br /&gt;
    //..&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
 class Painter&lt;br /&gt;
 {&lt;br /&gt;
     //..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class SquarePainter : Painter&lt;br /&gt;
 {&lt;br /&gt;
     void draw(Square s) { }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class CirclePainter : Painter&lt;br /&gt;
 {&lt;br /&gt;
    void draw(Circle c) { }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class TrianglePainter : Painter&lt;br /&gt;
 {&lt;br /&gt;
    void draw(Triangle t) { }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we need a canvas painter, which should have the capability to draw any of the above shape. In C++, this is pretty straight forward, you define a new class which extends all painters.&lt;br /&gt;
&lt;br /&gt;
 class CanvasPainter : SquarePainter, CirclePainter, TrianglePainter&lt;br /&gt;
 {&lt;br /&gt;
     // ..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
How can do the same in Java? Ofcourse, we will use composition, but how will you know which version of draw function to call? May be we can write some thing like this:&lt;br /&gt;
&lt;br /&gt;
 if (shape instanceof Triangle)&lt;br /&gt;
 {&lt;br /&gt;
       trianglePainter.draw((Triangle) shape);&lt;br /&gt;
 }&lt;br /&gt;
 else if(shape instanceof Circle)&lt;br /&gt;
 {&lt;br /&gt;
       circlePainter.draw((Circle) shape);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Unfortunately this is not a scalable solution and for every new tool we add to our paint program, we will have to change this code. This is where design patterns like [http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern Chain of Responsibility] proves helpful.&lt;br /&gt;
&lt;br /&gt;
Implementing the above code in Java using Chain of Responsibility design pattern.&lt;br /&gt;
&lt;br /&gt;
 interface IPainter&lt;br /&gt;
 {&lt;br /&gt;
     boolean canDraw(Shape s);&lt;br /&gt;
     void draw(Shape s);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
All painters will implement the above function:&lt;br /&gt;
&lt;br /&gt;
 class TrianglePainter implements IPainter&lt;br /&gt;
 {&lt;br /&gt;
    public boolean canDraw(Shape s) { return s instanceof Triangle; }&lt;br /&gt;
    public void draw(Shape s) { // ... }  &lt;br /&gt;
 } &lt;br /&gt;
&lt;br /&gt;
 class CanvasPainter implements IPainter&lt;br /&gt;
 {&lt;br /&gt;
    // List&amp;lt;IPainter&amp;gt; = ....&lt;br /&gt;
    // function to add and remove painters&lt;br /&gt;
    public void draw(Shape s)&lt;br /&gt;
    {&lt;br /&gt;
       for(Paint p : painters)&lt;br /&gt;
       { &lt;br /&gt;
            if(p.canDraw(s)) { p.draw(s); return; }&lt;br /&gt;
       }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
We can see that, chain of responsibility design pattern provides a very neat way to handle multiple inheritance in languages which don't support them completely.&lt;br /&gt;
&lt;br /&gt;
= Alternates to Multiple Inheritance =&lt;br /&gt;
* Composition &amp;amp; Interfaces&lt;br /&gt;
Compositions provides a neat way of emulating Multiple Inheritance. With Multiple Inheritance, we bring functionality from different classes to the class which extends them. We can achieve the same using Composition in which one class contains instance of other class, there by importing the capability of all those classes. Since a class is allowed to implement Multiple Interfaces, the class behaves as though it extended from Multiple classes.&lt;br /&gt;
&lt;br /&gt;
* Mixins&lt;br /&gt;
Some languages provide [http://en.wikipedia.org/wiki/Mixin mixins] as a language feature. Mixins provide another elegant mechanism which mimics Multiple Inheritance. The common problems with Mixin is that it pollutes the class namespace as it imports functions from different modules. This [http://www.artima.com/weblogs/viewpost.jsp?thread=246341 article] talks about the limitations of mixins in greater detail.&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
&amp;lt;table border = &amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;Single Inheritance&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;Multiple Inheritance&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Flexibility&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Single Inheritance does have some restriction in terms of modeling real world objects, but mitigate this problem with careful design&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Multiple Inheritance provides lots more flexibility for designing&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Easy of Use&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Single Inheritance is much simpler to use&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Multiple Inheritance sometimes leaves the programmer confused. One of the common problem with Multiple inheritance in Diamond Problem&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Availability&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Single Inheritance is supported by all the Object Oriented Programming Languages&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Multiple Inheritance is not supported by few of the OOP Languages, because the designer of the language thought it is hard to use and not necessary for common scenarios.&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Efficiency&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;This actually language specific. But generally it takes less time to figure which version of the function to call in a single inheritance&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Virtual Function dispatch in C++ is more time consuming in Multiple Inheritance. For example Multiple Inheritance in C++, needs multiple vtable for dispatching virtual functions. [http://www.cs.ucsb.edu/~urs/oocsb/papers/oopsla96.pdf (1)] discusses about this problem in great detail.&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Further Reading =&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Virtual_inheritance Virtual Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_composition Composition]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Mixin Mixins]&lt;br /&gt;
&lt;br /&gt;
= Reference =&lt;br /&gt;
*[http://www.cs.ucsb.edu/~urs/oocsb/papers/oopsla96.pdf (1) Cost of Virtual Functions C++]&lt;br /&gt;
*[http://www.artima.com/weblogs/viewpost.jsp?thread=246341 (2) Mixins considered Harmful]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Diamond_problem (3) Diamond Problem with Multiple Inheritance]&lt;/div&gt;</summary>
		<author><name>Cgoogc</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_25_ag&amp;diff=35883</id>
		<title>CSC/ECE 517 Fall 2010/ch1 25 ag</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_25_ag&amp;diff=35883"/>
		<updated>2010-09-21T20:40:51Z</updated>

		<summary type="html">&lt;p&gt;Cgoogc: /* Multiple Inheritance - Do we really need this? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
= What is Inheritance? =&lt;br /&gt;
Inheritance, along with polymorphism and encapsulation, is one of the pillar features of [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch1_1e_bb#Object_Oriented_Programming Object Oriented Programming] (OOP). Inheritance allows a class to reuse, extend or modify the behavior of another class. The term inheritance generally refers to single inheritance, where a sub-class or a derived class inherits methods and attributes from only one parent class. &lt;br /&gt;
&lt;br /&gt;
#For example, consider a general class of electrical appliances. Switching an appliance on and off can be considered a standard feature #of every appliance.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 class ElectricAppliance&lt;br /&gt;
 {&lt;br /&gt;
     public void on() { ... }&lt;br /&gt;
     public void off() { ... }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class TempControlAppliance : public ElectricAppliance&lt;br /&gt;
 {&lt;br /&gt;
     public void increaseTemp() { .. }&lt;br /&gt;
     public void decreaseTemp() { .. }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Heater : public TempControlAppliance &lt;br /&gt;
 {&lt;br /&gt;
     public void heat() { .. }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Cooler : public TempControlAppliance &lt;br /&gt;
 {&lt;br /&gt;
     public void cool() { .. }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
If we don't have inheritance, we might have to duplicate all the functions in both Heater and Cooler class. Inheritance is very important to achieve DRY (Don't Repeat Yourself). In the above class hierarchy, TempControlAppliance is super (parent) class for both Cooler and Heater subclass (child).&lt;br /&gt;
&lt;br /&gt;
= What is Multiple Inheritance =&lt;br /&gt;
When a class inherits from more than one class i.e., a child class having more than one parent class, is called multiple inheritance. Though this feature offers more flexibility, it seem to overwhelm most of the developer, so some of the object oriented language designer decided not to support Multiple Inheritance. Java and C# are some of the most famous language which don't support multiple inheritance. We need careful design to take proper advantage of Multiple inheritance, otherwise you might end up in spaghetti code, with classes inheritancing from multiple classes.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Building upon the above example, let us say we have a new device called Air Conditioner which can both heat and cool air. We might have a new class called AirConditioner as follows:&lt;br /&gt;
&lt;br /&gt;
 class AirConditioner : public Heater, public Cooler&lt;br /&gt;
 {&lt;br /&gt;
      public void setThresholdTemperature(int temp) { ... }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
= Good and bad of Multiple Inheritance =&lt;br /&gt;
Good:&lt;br /&gt;
* Multiple Inheritance strongly supports DRY principle, which leads to better software which is extensible, cohesive, less coupled and modular. &lt;br /&gt;
* Multiple Inheritance is very common in real world, in which object takes attributes from different objects.&lt;br /&gt;
* Multiple Inheritance is more expressive than Single Inheritane, so it gives flexibility in designing software with complex object relationships.&lt;br /&gt;
&lt;br /&gt;
Bad:&lt;br /&gt;
* To take complete advantage of Multiple Inheritance needs a thoughtful design. Without a good design, Multiple Inheritance infact might lead to bad code.&lt;br /&gt;
* Multiple Inheritance is overwhelming to developers. Requires more experience to properly use Multiple Inheritance.&lt;br /&gt;
* Multiple Inheritance is hard to use and make the language complicated. Some example like what happens when you Inherit from multiple classes which has same method?&lt;br /&gt;
&lt;br /&gt;
== Implementation issues ==&lt;br /&gt;
One of the most common problem with Multiple Inheritance is called the [http://en.wikipedia.org/wiki/Diamond_problem Diamond Problem(3)]. This is encountered when a derived class inherits from multiple parent classes, which in turn inherit from one super class. Let us try to understand this problem using an example.&lt;br /&gt;
&lt;br /&gt;
Consider a task of modelling a car. Now a car can broadly be divided into two subclasses, mechanical cars and electric cars. &lt;br /&gt;
&lt;br /&gt;
 class Car&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
        void start() { cout &amp;lt;&amp;lt; &amp;quot;Start with loud noise&amp;quot;;&lt;br /&gt;
        void steer();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class MechCar : public Car&lt;br /&gt;
 {&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class ElectricCar : public Car&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
       void start() { cout &amp;lt;&amp;lt; &amp;quot;start without noise&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Now considering modern advancements in vehicle design, a new subclass of hybrid cars have emerged in the market. These cars have traits of both mechanical and electric cars. Here is how this is achieved in C++.&lt;br /&gt;
&lt;br /&gt;
 class HybridCar : public MechCar, ElectricCar&lt;br /&gt;
 {&lt;br /&gt;
      // This has two copies of start()&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Here is an illustration of the inheritance hierarchy.&lt;br /&gt;
&lt;br /&gt;
[[Image:Car-hybrid.jpg|frame|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now if an object of the HybridCar class calls a method of class Car, an ambiguous situation arises. Assuming the method has been [http://en.wikipedia.org/wiki/Method_overriding overridden] by class MechCar and ElectricCar, then it is not clear which version of the method the object is referring to. This is what is known as the Diamond Problem. This problem is addressed in C++ solves this problem through the use of  [http://en.wikipedia.org/wiki/Virtual_inheritance Virtual Inheritance]. &lt;br /&gt;
&lt;br /&gt;
 HybridCar h;&lt;br /&gt;
 h.ElectricCar::start();&lt;br /&gt;
&lt;br /&gt;
There are also ambiguities during the type casting. For more details about Virtual Inheritance refer [http://en.wikipedia.org/wiki/Virtual_inheritance here]&lt;br /&gt;
&lt;br /&gt;
== Multiple Inheritance - Do we really need this? ==&lt;br /&gt;
Many languages which don't support multiple inheritance completely, has some ways of emulating multiple inheritance. We will consider Java as an example which does not support Multiple Inheritance completely and see how we can emulate multiple inheritance in Java. It is not completely true to say that Java does not support multiple inheritance, in fact with interfaces we can have multiple inheritance. Since, Interface just defines the behaviour, it does not have any attributes to describe it, Interfaces are not considered as a Class.&lt;br /&gt;
&lt;br /&gt;
Let us consider the previous example of an AirConditioner:&lt;br /&gt;
&lt;br /&gt;
 class AirConditioner extends [______________]&lt;br /&gt;
&lt;br /&gt;
We cannot fit both the classes there since a class in Java can extend from a single class. But Java classes can implement multiple interfaces.&lt;br /&gt;
&lt;br /&gt;
 public interface IHeater {&lt;br /&gt;
    void heat();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public interface ICooler {&lt;br /&gt;
    void cool();&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public Heater extends TempControllerAppliance implements IHeater {&lt;br /&gt;
    public void heat() { .. }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public Cooler extends TempControllerAppliance implements ICooler{&lt;br /&gt;
    public void cool() { .. }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public class AirConditioner extends TempControllerAppliance implements IHeater, ICooler, IAirConditioner {&lt;br /&gt;
    private ICooler cooler = new Cooler();&lt;br /&gt;
    private IHeater heater = new Heater();&lt;br /&gt;
 &lt;br /&gt;
    public void cool() {&lt;br /&gt;
        cooler.cool();&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void heat() {&lt;br /&gt;
        heater.heat();&lt;br /&gt;
    }  &lt;br /&gt;
    &lt;br /&gt;
    public void setThreshold(int t) { ... }   &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
To implement multiple inheritance in Java, we need to the following:&lt;br /&gt;
1. Make every class implement an Interface&lt;br /&gt;
2. Classes extends these interfaces to add logic&lt;br /&gt;
3. When a class needs to extend from Multiple interfaces, you can implement those interfaces and delegate the call to the actual implementation. This is achieved through Composition.&lt;br /&gt;
&lt;br /&gt;
We can see that it is possible to emulate multiple inheritance through [http://en.wikipedia.org/wiki/Object_composition composition]. &lt;br /&gt;
&lt;br /&gt;
Let us take another example and see how we can emulate Multiple inheritance in Java. We are developing a paint application. We have separate tools to draw each primitive shapes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
 class Shape&lt;br /&gt;
 {&lt;br /&gt;
    //...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Square : Shape&lt;br /&gt;
 {&lt;br /&gt;
   // ..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Circle : Shape&lt;br /&gt;
 {&lt;br /&gt;
    //..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Triangle : Shape&lt;br /&gt;
 {&lt;br /&gt;
    //..&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
 class Painter&lt;br /&gt;
 {&lt;br /&gt;
     //..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class SquarePainter : Painter&lt;br /&gt;
 {&lt;br /&gt;
     void draw(Square s) { }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class CirclePainter : Painter&lt;br /&gt;
 {&lt;br /&gt;
    void draw(Circle c) { }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class TrianglePainter : Painter&lt;br /&gt;
 {&lt;br /&gt;
    void draw(Triangle t) { }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we need a canvas painter, which should have the capability to draw any of the above shape. In C++, this is pretty straight forward, you define a new class which extends all painters.&lt;br /&gt;
&lt;br /&gt;
 class CanvasPainter : SquarePainter, CirclePainter, TrianglePainter&lt;br /&gt;
 {&lt;br /&gt;
     // ..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
How can do the same in Java? Ofcourse, we will use composition, but how will you know which version of draw function to call? May be we can write some thing like this:&lt;br /&gt;
&lt;br /&gt;
 if (shape instanceof Triangle)&lt;br /&gt;
 {&lt;br /&gt;
       trianglePainter.draw((Triangle) shape);&lt;br /&gt;
 }&lt;br /&gt;
 else if(shape instanceof Circle)&lt;br /&gt;
 {&lt;br /&gt;
       circlePainter.draw((Circle) shape);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Unfortunately this is not a scalable solution and for every new tool we add to our paint program, we will have to change this code. This is where design patterns like [http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern Chain of Responsibility] proves helpful.&lt;br /&gt;
&lt;br /&gt;
Implementing the above code in Java using Chain of Responsibility design pattern.&lt;br /&gt;
&lt;br /&gt;
 interface IPainter&lt;br /&gt;
 {&lt;br /&gt;
     boolean canDraw(Shape s);&lt;br /&gt;
     void draw(Shape s);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
All painters will implement the above function:&lt;br /&gt;
&lt;br /&gt;
 class TrianglePainter implements IPainter&lt;br /&gt;
 {&lt;br /&gt;
    public boolean canDraw(Shape s) { return s instanceof Triangle; }&lt;br /&gt;
    public void draw(Shape s) { // ... }  &lt;br /&gt;
 } &lt;br /&gt;
&lt;br /&gt;
 class CanvasPainter implements IPainter&lt;br /&gt;
 {&lt;br /&gt;
    // List&amp;lt;IPainter&amp;gt; = ....&lt;br /&gt;
    // function to add and remove painters&lt;br /&gt;
    public void draw(Shape s)&lt;br /&gt;
    {&lt;br /&gt;
       for(Paint p : painters)&lt;br /&gt;
       { &lt;br /&gt;
            if(p.canDraw(s)) { p.draw(s); return; }&lt;br /&gt;
       }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
We can see that, chain of responsibility design pattern provides a very neat way to handle multiple inheritance in languages which don't support them completely.&lt;br /&gt;
&lt;br /&gt;
= Alternates to Multiple Inheritance =&lt;br /&gt;
* Composition &amp;amp; Interfaces&lt;br /&gt;
Compositions provides a neat way of emulating Multiple Inheritance. With Multiple Inheritance, we bring functionality from different classes to the class which extends them. We can achieve the same using Composition in which one class contains instance of other class, there by importing the capability of all those classes. Since a class is allowed to implement Multiple Interfaces, the class behaves as though it extended from Multiple classes.&lt;br /&gt;
&lt;br /&gt;
* Mixins&lt;br /&gt;
Some languages provide [http://en.wikipedia.org/wiki/Mixin mixins] as a language feature. Mixins provide another elegant mechanism which mimics Multiple Inheritance. The common problems with Mixin is that it pollutes the class namespace as it imports functions from different modules. This [http://www.artima.com/weblogs/viewpost.jsp?thread=246341 article] talks about the limitations of mixins in greater detail.&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
&amp;lt;table border = &amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;Single Inheritance&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;Multiple Inheritance&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Flexibility&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Single Inheritance does have some restriction in terms of modeling real world objects, but mitigate this problem with careful design&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Multiple Inheritance provides lots more flexibility for designing&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Easy of Use&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Single Inheritance is much simpler to use&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Multiple Inheritance sometimes leaves the programmer confused. One of the common problem with Multiple inheritance in Diamond Problem&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Availability&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Single Inheritance is supported by all the Object Oriented Programming Languages&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Multiple Inheritance is not supported by few of the OOP Languages, because the designer of the language thought it is hard to use and not necessary for common scenarios.&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Efficiency&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;This actually language specific. But generally it takes less time to figure which version of the function to call in a single inheritance&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Virtual Function dispatch in C++ is more time consuming in Multiple Inheritance. For example Multiple Inheritance in C++, needs multiple vtable for dispatching virtual functions. [http://www.cs.ucsb.edu/~urs/oocsb/papers/oopsla96.pdf (1)] discusses about this problem in great detail.&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Further Reading =&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Virtual_inheritance Virtual Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_composition Composition]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Mixin Mixins]&lt;br /&gt;
&lt;br /&gt;
= Reference =&lt;br /&gt;
*[http://www.cs.ucsb.edu/~urs/oocsb/papers/oopsla96.pdf (1) Cost of Virtual Functions C++]&lt;br /&gt;
*[http://www.artima.com/weblogs/viewpost.jsp?thread=246341 (2) Mixins considered Harmful]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Diamond_problem (3) Diamond Problem with Multiple Inheritance]&lt;/div&gt;</summary>
		<author><name>Cgoogc</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_25_ag&amp;diff=35882</id>
		<title>CSC/ECE 517 Fall 2010/ch1 25 ag</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_25_ag&amp;diff=35882"/>
		<updated>2010-09-21T20:40:40Z</updated>

		<summary type="html">&lt;p&gt;Cgoogc: /* Implementation issues */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
= What is Inheritance? =&lt;br /&gt;
Inheritance, along with polymorphism and encapsulation, is one of the pillar features of [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch1_1e_bb#Object_Oriented_Programming Object Oriented Programming] (OOP). Inheritance allows a class to reuse, extend or modify the behavior of another class. The term inheritance generally refers to single inheritance, where a sub-class or a derived class inherits methods and attributes from only one parent class. &lt;br /&gt;
&lt;br /&gt;
#For example, consider a general class of electrical appliances. Switching an appliance on and off can be considered a standard feature #of every appliance.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 class ElectricAppliance&lt;br /&gt;
 {&lt;br /&gt;
     public void on() { ... }&lt;br /&gt;
     public void off() { ... }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class TempControlAppliance : public ElectricAppliance&lt;br /&gt;
 {&lt;br /&gt;
     public void increaseTemp() { .. }&lt;br /&gt;
     public void decreaseTemp() { .. }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Heater : public TempControlAppliance &lt;br /&gt;
 {&lt;br /&gt;
     public void heat() { .. }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Cooler : public TempControlAppliance &lt;br /&gt;
 {&lt;br /&gt;
     public void cool() { .. }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
If we don't have inheritance, we might have to duplicate all the functions in both Heater and Cooler class. Inheritance is very important to achieve DRY (Don't Repeat Yourself). In the above class hierarchy, TempControlAppliance is super (parent) class for both Cooler and Heater subclass (child).&lt;br /&gt;
&lt;br /&gt;
= What is Multiple Inheritance =&lt;br /&gt;
When a class inherits from more than one class i.e., a child class having more than one parent class, is called multiple inheritance. Though this feature offers more flexibility, it seem to overwhelm most of the developer, so some of the object oriented language designer decided not to support Multiple Inheritance. Java and C# are some of the most famous language which don't support multiple inheritance. We need careful design to take proper advantage of Multiple inheritance, otherwise you might end up in spaghetti code, with classes inheritancing from multiple classes.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Building upon the above example, let us say we have a new device called Air Conditioner which can both heat and cool air. We might have a new class called AirConditioner as follows:&lt;br /&gt;
&lt;br /&gt;
 class AirConditioner : public Heater, public Cooler&lt;br /&gt;
 {&lt;br /&gt;
      public void setThresholdTemperature(int temp) { ... }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
= Good and bad of Multiple Inheritance =&lt;br /&gt;
Good:&lt;br /&gt;
* Multiple Inheritance strongly supports DRY principle, which leads to better software which is extensible, cohesive, less coupled and modular. &lt;br /&gt;
* Multiple Inheritance is very common in real world, in which object takes attributes from different objects.&lt;br /&gt;
* Multiple Inheritance is more expressive than Single Inheritane, so it gives flexibility in designing software with complex object relationships.&lt;br /&gt;
&lt;br /&gt;
Bad:&lt;br /&gt;
* To take complete advantage of Multiple Inheritance needs a thoughtful design. Without a good design, Multiple Inheritance infact might lead to bad code.&lt;br /&gt;
* Multiple Inheritance is overwhelming to developers. Requires more experience to properly use Multiple Inheritance.&lt;br /&gt;
* Multiple Inheritance is hard to use and make the language complicated. Some example like what happens when you Inherit from multiple classes which has same method?&lt;br /&gt;
&lt;br /&gt;
== Implementation issues ==&lt;br /&gt;
One of the most common problem with Multiple Inheritance is called the [http://en.wikipedia.org/wiki/Diamond_problem Diamond Problem(3)]. This is encountered when a derived class inherits from multiple parent classes, which in turn inherit from one super class. Let us try to understand this problem using an example.&lt;br /&gt;
&lt;br /&gt;
Consider a task of modelling a car. Now a car can broadly be divided into two subclasses, mechanical cars and electric cars. &lt;br /&gt;
&lt;br /&gt;
 class Car&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
        void start() { cout &amp;lt;&amp;lt; &amp;quot;Start with loud noise&amp;quot;;&lt;br /&gt;
        void steer();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class MechCar : public Car&lt;br /&gt;
 {&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class ElectricCar : public Car&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
       void start() { cout &amp;lt;&amp;lt; &amp;quot;start without noise&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Now considering modern advancements in vehicle design, a new subclass of hybrid cars have emerged in the market. These cars have traits of both mechanical and electric cars. Here is how this is achieved in C++.&lt;br /&gt;
&lt;br /&gt;
 class HybridCar : public MechCar, ElectricCar&lt;br /&gt;
 {&lt;br /&gt;
      // This has two copies of start()&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Here is an illustration of the inheritance hierarchy.&lt;br /&gt;
&lt;br /&gt;
[[Image:Car-hybrid.jpg|frame|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now if an object of the HybridCar class calls a method of class Car, an ambiguous situation arises. Assuming the method has been [http://en.wikipedia.org/wiki/Method_overriding overridden] by class MechCar and ElectricCar, then it is not clear which version of the method the object is referring to. This is what is known as the Diamond Problem. This problem is addressed in C++ solves this problem through the use of  [http://en.wikipedia.org/wiki/Virtual_inheritance Virtual Inheritance]. &lt;br /&gt;
&lt;br /&gt;
 HybridCar h;&lt;br /&gt;
 h.ElectricCar::start();&lt;br /&gt;
&lt;br /&gt;
There are also ambiguities during the type casting. For more details about Virtual Inheritance refer [http://en.wikipedia.org/wiki/Virtual_inheritance here]&lt;br /&gt;
&lt;br /&gt;
= Multiple Inheritance - Do we really need this? =&lt;br /&gt;
Many languages which don't support multiple inheritance completely, has some ways of emulating multiple inheritance. We will consider Java as an example which does not support Multiple Inheritance completely and see how we can emulate multiple inheritance in Java. It is not completely true to say that Java does not support multiple inheritance, in fact with interfaces we can have multiple inheritance. Since, Interface just defines the behaviour, it does not have any attributes to describe it, Interfaces are not considered as a Class.&lt;br /&gt;
&lt;br /&gt;
Let us consider the previous example of an AirConditioner:&lt;br /&gt;
&lt;br /&gt;
 class AirConditioner extends [______________]&lt;br /&gt;
&lt;br /&gt;
We cannot fit both the classes there since a class in Java can extend from a single class. But Java classes can implement multiple interfaces.&lt;br /&gt;
&lt;br /&gt;
 public interface IHeater {&lt;br /&gt;
    void heat();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public interface ICooler {&lt;br /&gt;
    void cool();&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public Heater extends TempControllerAppliance implements IHeater {&lt;br /&gt;
    public void heat() { .. }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public Cooler extends TempControllerAppliance implements ICooler{&lt;br /&gt;
    public void cool() { .. }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public class AirConditioner extends TempControllerAppliance implements IHeater, ICooler, IAirConditioner {&lt;br /&gt;
    private ICooler cooler = new Cooler();&lt;br /&gt;
    private IHeater heater = new Heater();&lt;br /&gt;
 &lt;br /&gt;
    public void cool() {&lt;br /&gt;
        cooler.cool();&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void heat() {&lt;br /&gt;
        heater.heat();&lt;br /&gt;
    }  &lt;br /&gt;
    &lt;br /&gt;
    public void setThreshold(int t) { ... }   &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
To implement multiple inheritance in Java, we need to the following:&lt;br /&gt;
1. Make every class implement an Interface&lt;br /&gt;
2. Classes extends these interfaces to add logic&lt;br /&gt;
3. When a class needs to extend from Multiple interfaces, you can implement those interfaces and delegate the call to the actual implementation. This is achieved through Composition.&lt;br /&gt;
&lt;br /&gt;
We can see that it is possible to emulate multiple inheritance through [http://en.wikipedia.org/wiki/Object_composition composition]. &lt;br /&gt;
&lt;br /&gt;
Let us take another example and see how we can emulate Multiple inheritance in Java. We are developing a paint application. We have separate tools to draw each primitive shapes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
 class Shape&lt;br /&gt;
 {&lt;br /&gt;
    //...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Square : Shape&lt;br /&gt;
 {&lt;br /&gt;
   // ..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Circle : Shape&lt;br /&gt;
 {&lt;br /&gt;
    //..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Triangle : Shape&lt;br /&gt;
 {&lt;br /&gt;
    //..&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
 class Painter&lt;br /&gt;
 {&lt;br /&gt;
     //..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class SquarePainter : Painter&lt;br /&gt;
 {&lt;br /&gt;
     void draw(Square s) { }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class CirclePainter : Painter&lt;br /&gt;
 {&lt;br /&gt;
    void draw(Circle c) { }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class TrianglePainter : Painter&lt;br /&gt;
 {&lt;br /&gt;
    void draw(Triangle t) { }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we need a canvas painter, which should have the capability to draw any of the above shape. In C++, this is pretty straight forward, you define a new class which extends all painters.&lt;br /&gt;
&lt;br /&gt;
 class CanvasPainter : SquarePainter, CirclePainter, TrianglePainter&lt;br /&gt;
 {&lt;br /&gt;
     // ..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
How can do the same in Java? Ofcourse, we will use composition, but how will you know which version of draw function to call? May be we can write some thing like this:&lt;br /&gt;
&lt;br /&gt;
 if (shape instanceof Triangle)&lt;br /&gt;
 {&lt;br /&gt;
       trianglePainter.draw((Triangle) shape);&lt;br /&gt;
 }&lt;br /&gt;
 else if(shape instanceof Circle)&lt;br /&gt;
 {&lt;br /&gt;
       circlePainter.draw((Circle) shape);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Unfortunately this is not a scalable solution and for every new tool we add to our paint program, we will have to change this code. This is where design patterns like [http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern Chain of Responsibility] proves helpful.&lt;br /&gt;
&lt;br /&gt;
Implementing the above code in Java using Chain of Responsibility design pattern.&lt;br /&gt;
&lt;br /&gt;
 interface IPainter&lt;br /&gt;
 {&lt;br /&gt;
     boolean canDraw(Shape s);&lt;br /&gt;
     void draw(Shape s);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
All painters will implement the above function:&lt;br /&gt;
&lt;br /&gt;
 class TrianglePainter implements IPainter&lt;br /&gt;
 {&lt;br /&gt;
    public boolean canDraw(Shape s) { return s instanceof Triangle; }&lt;br /&gt;
    public void draw(Shape s) { // ... }  &lt;br /&gt;
 } &lt;br /&gt;
&lt;br /&gt;
 class CanvasPainter implements IPainter&lt;br /&gt;
 {&lt;br /&gt;
    // List&amp;lt;IPainter&amp;gt; = ....&lt;br /&gt;
    // function to add and remove painters&lt;br /&gt;
    public void draw(Shape s)&lt;br /&gt;
    {&lt;br /&gt;
       for(Paint p : painters)&lt;br /&gt;
       { &lt;br /&gt;
            if(p.canDraw(s)) { p.draw(s); return; }&lt;br /&gt;
       }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
We can see that, chain of responsibility design pattern provides a very neat way to handle multiple inheritance in languages which don't support them completely.&lt;br /&gt;
&lt;br /&gt;
= Alternates to Multiple Inheritance =&lt;br /&gt;
* Composition &amp;amp; Interfaces&lt;br /&gt;
Compositions provides a neat way of emulating Multiple Inheritance. With Multiple Inheritance, we bring functionality from different classes to the class which extends them. We can achieve the same using Composition in which one class contains instance of other class, there by importing the capability of all those classes. Since a class is allowed to implement Multiple Interfaces, the class behaves as though it extended from Multiple classes.&lt;br /&gt;
&lt;br /&gt;
* Mixins&lt;br /&gt;
Some languages provide [http://en.wikipedia.org/wiki/Mixin mixins] as a language feature. Mixins provide another elegant mechanism which mimics Multiple Inheritance. The common problems with Mixin is that it pollutes the class namespace as it imports functions from different modules. This [http://www.artima.com/weblogs/viewpost.jsp?thread=246341 article] talks about the limitations of mixins in greater detail.&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
&amp;lt;table border = &amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;Single Inheritance&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;Multiple Inheritance&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Flexibility&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Single Inheritance does have some restriction in terms of modeling real world objects, but mitigate this problem with careful design&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Multiple Inheritance provides lots more flexibility for designing&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Easy of Use&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Single Inheritance is much simpler to use&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Multiple Inheritance sometimes leaves the programmer confused. One of the common problem with Multiple inheritance in Diamond Problem&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Availability&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Single Inheritance is supported by all the Object Oriented Programming Languages&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Multiple Inheritance is not supported by few of the OOP Languages, because the designer of the language thought it is hard to use and not necessary for common scenarios.&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Efficiency&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;This actually language specific. But generally it takes less time to figure which version of the function to call in a single inheritance&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Virtual Function dispatch in C++ is more time consuming in Multiple Inheritance. For example Multiple Inheritance in C++, needs multiple vtable for dispatching virtual functions. [http://www.cs.ucsb.edu/~urs/oocsb/papers/oopsla96.pdf (1)] discusses about this problem in great detail.&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Further Reading =&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Virtual_inheritance Virtual Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_composition Composition]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Mixin Mixins]&lt;br /&gt;
&lt;br /&gt;
= Reference =&lt;br /&gt;
*[http://www.cs.ucsb.edu/~urs/oocsb/papers/oopsla96.pdf (1) Cost of Virtual Functions C++]&lt;br /&gt;
*[http://www.artima.com/weblogs/viewpost.jsp?thread=246341 (2) Mixins considered Harmful]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Diamond_problem (3) Diamond Problem with Multiple Inheritance]&lt;/div&gt;</summary>
		<author><name>Cgoogc</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_25_ag&amp;diff=35880</id>
		<title>CSC/ECE 517 Fall 2010/ch1 25 ag</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_25_ag&amp;diff=35880"/>
		<updated>2010-09-21T20:38:32Z</updated>

		<summary type="html">&lt;p&gt;Cgoogc: /* Further Reading */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
= What is Inheritance? =&lt;br /&gt;
Inheritance, along with polymorphism and encapsulation, is one of the pillar features of Object Oriented Programming (OOP). Inheritance allows a class to reuse, extend or modify the behavior of another class. The term inheritance generally refers to single inheritance, where a sub-class or a derived class inherits methods and attributes from only one parent class. &lt;br /&gt;
&lt;br /&gt;
#For example, consider a general class of electrical appliances. Switching an appliance on and off can be considered a standard feature #of every appliance.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 class ElectricAppliance&lt;br /&gt;
 {&lt;br /&gt;
     public void on() { ... }&lt;br /&gt;
     public void off() { ... }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class TempControlAppliance : public ElectricAppliance&lt;br /&gt;
 {&lt;br /&gt;
     public void increaseTemp() { .. }&lt;br /&gt;
     public void decreaseTemp() { .. }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Heater : public TempControlAppliance &lt;br /&gt;
 {&lt;br /&gt;
     public void heat() { .. }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Cooler : public TempControlAppliance &lt;br /&gt;
 {&lt;br /&gt;
     public void cool() { .. }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
If we don't have inheritance, we might have to duplicate all the functions in both Heater and Cooler class. Inheritance is very important to achieve DRY (Don't Repeat Yourself). In the above class hierarchy, TempControlAppliance is super (parent) class for both Cooler and Heater subclass (child).&lt;br /&gt;
&lt;br /&gt;
= What is Multiple Inheritance =&lt;br /&gt;
When a class inherits from more than one class i.e., a child class having more than one parent class, is called multiple inheritance. Though this feature offers more flexibility, it seem to overwhelm most of the developer, so some of the object oriented language designer decided not to support Multiple Inheritance. Java and C# are some of the most famous language which don't support multiple inheritance. We need careful design to take proper advantage of Multiple inheritance, otherwise you might end up in spaghetti code, with classes inheritancing from multiple classes.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Building upon the above example, let us say we have a new device called Air Conditioner which can both heat and cool air. We might have a new class called AirConditioner as follows:&lt;br /&gt;
&lt;br /&gt;
 class AirConditioner : public Heater, public Cooler&lt;br /&gt;
 {&lt;br /&gt;
      public void setThresholdTemperature(int temp) { ... }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
= Good and bad of Multiple Inheritance =&lt;br /&gt;
Good:&lt;br /&gt;
* Multiple Inheritance strongly supports DRY principle, which leads to better software which is extensible, cohesive, less coupled and modular. &lt;br /&gt;
* Multiple Inheritance is very common in real world, in which object takes attributes from different objects.&lt;br /&gt;
* Multiple Inheritance is more expressive than Single Inheritane, so it gives flexibility in designing software with complex object relationships.&lt;br /&gt;
&lt;br /&gt;
Bad:&lt;br /&gt;
* To take complete advantage of Multiple Inheritance needs a thoughtful design. Without a good design, Multiple Inheritance infact might lead to bad code.&lt;br /&gt;
* Multiple Inheritance is overwhelming to developers. Requires more experience to properly use Multiple Inheritance.&lt;br /&gt;
* Multiple Inheritance is hard to use and make the language complicated. Some example like what happens when you Inherit from multiple classes which has same method?&lt;br /&gt;
&lt;br /&gt;
= Implementation issues =&lt;br /&gt;
One of the most common problem with Multiple Inheritance is called the [http://en.wikipedia.org/wiki/Diamond_problem Diamond Problem(3)]. This is encountered when a derived class inherits from multiple parent classes, which in turn inherit from one super class. Let us try to understand this problem using an example.&lt;br /&gt;
&lt;br /&gt;
Consider a task of modelling a car. Now a car can broadly be divided into two subclasses, mechanical cars and electric cars. &lt;br /&gt;
&lt;br /&gt;
 class Car&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
        void start() { cout &amp;lt;&amp;lt; &amp;quot;Start with loud noise&amp;quot;;&lt;br /&gt;
        void steer();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class MechCar : public Car&lt;br /&gt;
 {&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class ElectricCar : public Car&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
       void start() { cout &amp;lt;&amp;lt; &amp;quot;start without noise&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Now considering modern advancements in vehicle design, a new subclass of hybrid cars have emerged in the market. These cars have traits of both mechanical and electric cars. Here is how this is achieved in C++.&lt;br /&gt;
&lt;br /&gt;
 class HybridCar : public MechCar, ElectricCar&lt;br /&gt;
 {&lt;br /&gt;
      // This has two copies of start()&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Here is an illustration of the inheritance hierarchy.&lt;br /&gt;
&lt;br /&gt;
[[Image:Car-hybrid.jpg|frame|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now if an object of the HybridCar class calls a method of class Car, an ambiguous situation arises. Assuming the method has been [http://en.wikipedia.org/wiki/Method_overriding overridden] by class MechCar and ElectricCar, then it is not clear which version of the method the object is referring to. This is what is known as the Diamond Problem. This problem is addressed in C++ solves this problem through the use of  [http://en.wikipedia.org/wiki/Virtual_inheritance Virtual Inheritance]. &lt;br /&gt;
&lt;br /&gt;
 HybridCar h;&lt;br /&gt;
 h.ElectricCar::start();&lt;br /&gt;
&lt;br /&gt;
There are also ambiguities during the type casting. For more details about Virtual Inheritance refer [http://en.wikipedia.org/wiki/Virtual_inheritance here]&lt;br /&gt;
&lt;br /&gt;
= Multiple Inheritance - Do we really need this? =&lt;br /&gt;
Many languages which don't support multiple inheritance completely, has some ways of emulating multiple inheritance. We will consider Java as an example which does not support Multiple Inheritance completely and see how we can emulate multiple inheritance in Java. It is not completely true to say that Java does not support multiple inheritance, in fact with interfaces we can have multiple inheritance. Since, Interface just defines the behaviour, it does not have any attributes to describe it, Interfaces are not considered as a Class.&lt;br /&gt;
&lt;br /&gt;
Let us consider the previous example of an AirConditioner:&lt;br /&gt;
&lt;br /&gt;
 class AirConditioner extends [______________]&lt;br /&gt;
&lt;br /&gt;
We cannot fit both the classes there since a class in Java can extend from a single class. But Java classes can implement multiple interfaces.&lt;br /&gt;
&lt;br /&gt;
 public interface IHeater {&lt;br /&gt;
    void heat();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public interface ICooler {&lt;br /&gt;
    void cool();&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public Heater extends TempControllerAppliance implements IHeater {&lt;br /&gt;
    public void heat() { .. }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public Cooler extends TempControllerAppliance implements ICooler{&lt;br /&gt;
    public void cool() { .. }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public class AirConditioner extends TempControllerAppliance implements IHeater, ICooler, IAirConditioner {&lt;br /&gt;
    private ICooler cooler = new Cooler();&lt;br /&gt;
    private IHeater heater = new Heater();&lt;br /&gt;
 &lt;br /&gt;
    public void cool() {&lt;br /&gt;
        cooler.cool();&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void heat() {&lt;br /&gt;
        heater.heat();&lt;br /&gt;
    }  &lt;br /&gt;
    &lt;br /&gt;
    public void setThreshold(int t) { ... }   &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
To implement multiple inheritance in Java, we need to the following:&lt;br /&gt;
1. Make every class implement an Interface&lt;br /&gt;
2. Classes extends these interfaces to add logic&lt;br /&gt;
3. When a class needs to extend from Multiple interfaces, you can implement those interfaces and delegate the call to the actual implementation. This is achieved through Composition.&lt;br /&gt;
&lt;br /&gt;
We can see that it is possible to emulate multiple inheritance through [http://en.wikipedia.org/wiki/Object_composition composition]. &lt;br /&gt;
&lt;br /&gt;
Let us take another example and see how we can emulate Multiple inheritance in Java. We are developing a paint application. We have separate tools to draw each primitive shapes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
 class Shape&lt;br /&gt;
 {&lt;br /&gt;
    //...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Square : Shape&lt;br /&gt;
 {&lt;br /&gt;
   // ..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Circle : Shape&lt;br /&gt;
 {&lt;br /&gt;
    //..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Triangle : Shape&lt;br /&gt;
 {&lt;br /&gt;
    //..&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
 class Painter&lt;br /&gt;
 {&lt;br /&gt;
     //..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class SquarePainter : Painter&lt;br /&gt;
 {&lt;br /&gt;
     void draw(Square s) { }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class CirclePainter : Painter&lt;br /&gt;
 {&lt;br /&gt;
    void draw(Circle c) { }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class TrianglePainter : Painter&lt;br /&gt;
 {&lt;br /&gt;
    void draw(Triangle t) { }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we need a canvas painter, which should have the capability to draw any of the above shape. In C++, this is pretty straight forward, you define a new class which extends all painters.&lt;br /&gt;
&lt;br /&gt;
 class CanvasPainter : SquarePainter, CirclePainter, TrianglePainter&lt;br /&gt;
 {&lt;br /&gt;
     // ..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
How can do the same in Java? Ofcourse, we will use composition, but how will you know which version of draw function to call? May be we can write some thing like this:&lt;br /&gt;
&lt;br /&gt;
 if (shape instanceof Triangle)&lt;br /&gt;
 {&lt;br /&gt;
       trianglePainter.draw((Triangle) shape);&lt;br /&gt;
 }&lt;br /&gt;
 else if(shape instanceof Circle)&lt;br /&gt;
 {&lt;br /&gt;
       circlePainter.draw((Circle) shape);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Unfortunately this is not a scalable solution and for every new tool we add to our paint program, we will have to change this code. This is where design patterns like [http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern Chain of Responsibility] proves helpful.&lt;br /&gt;
&lt;br /&gt;
Implementing the above code in Java using Chain of Responsibility design pattern.&lt;br /&gt;
&lt;br /&gt;
 interface IPainter&lt;br /&gt;
 {&lt;br /&gt;
     boolean canDraw(Shape s);&lt;br /&gt;
     void draw(Shape s);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
All painters will implement the above function:&lt;br /&gt;
&lt;br /&gt;
 class TrianglePainter implements IPainter&lt;br /&gt;
 {&lt;br /&gt;
    public boolean canDraw(Shape s) { return s instanceof Triangle; }&lt;br /&gt;
    public void draw(Shape s) { // ... }  &lt;br /&gt;
 } &lt;br /&gt;
&lt;br /&gt;
 class CanvasPainter implements IPainter&lt;br /&gt;
 {&lt;br /&gt;
    // List&amp;lt;IPainter&amp;gt; = ....&lt;br /&gt;
    // function to add and remove painters&lt;br /&gt;
    public void draw(Shape s)&lt;br /&gt;
    {&lt;br /&gt;
       for(Paint p : painters)&lt;br /&gt;
       { &lt;br /&gt;
            if(p.canDraw(s)) { p.draw(s); return; }&lt;br /&gt;
       }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
We can see that, chain of responsibility design pattern provides a very neat way to handle multiple inheritance in languages which don't support them completely.&lt;br /&gt;
&lt;br /&gt;
= Alternates to Multiple Inheritance =&lt;br /&gt;
* Composition &amp;amp; Interfaces&lt;br /&gt;
Compositions provides a neat way of emulating Multiple Inheritance. With Multiple Inheritance, we bring functionality from different classes to the class which extends them. We can achieve the same using Composition in which one class contains instance of other class, there by importing the capability of all those classes. Since a class is allowed to implement Multiple Interfaces, the class behaves as though it extended from Multiple classes.&lt;br /&gt;
&lt;br /&gt;
* Mixins&lt;br /&gt;
Some languages provide [http://en.wikipedia.org/wiki/Mixin mixins] as a language feature. Mixins provide another elegant mechanism which mimics Multiple Inheritance. The common problems with Mixin is that it pollutes the class namespace as it imports functions from different modules. This [http://www.artima.com/weblogs/viewpost.jsp?thread=246341 article] talks about the limitations of mixins in greater detail.&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
&amp;lt;table border = &amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;Single Inheritance&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;Multiple Inheritance&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Flexibility&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Single Inheritance does have some restriction in terms of modeling real world objects, but mitigate this problem with careful design&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Multiple Inheritance provides lots more flexibility for designing&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Easy of Use&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Single Inheritance is much simpler to use&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Multiple Inheritance sometimes leaves the programmer confused. One of the common problem with Multiple inheritance in Diamond Problem&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Availability&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Single Inheritance is supported by all the Object Oriented Programming Languages&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Multiple Inheritance is not supported by few of the OOP Languages, because the designer of the language thought it is hard to use and not necessary for common scenarios.&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Efficiency&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;This actually language specific. But generally it takes less time to figure which version of the function to call in a single inheritance&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Virtual Function dispatch in C++ is more time consuming in Multiple Inheritance. For example Multiple Inheritance in C++, needs multiple vtable for dispatching virtual functions. [http://www.cs.ucsb.edu/~urs/oocsb/papers/oopsla96.pdf (1)] discusses about this problem in great detail.&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Further Reading =&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Virtual_inheritance Virtual Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_composition Composition]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Mixin Mixins]&lt;br /&gt;
&lt;br /&gt;
= Reference =&lt;br /&gt;
*[http://www.cs.ucsb.edu/~urs/oocsb/papers/oopsla96.pdf (1) Cost of Virtual Functions C++]&lt;br /&gt;
*[http://www.artima.com/weblogs/viewpost.jsp?thread=246341 (2) Mixins considered Harmful]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Diamond_problem (3) Diamond Problem with Multiple Inheritance]&lt;/div&gt;</summary>
		<author><name>Cgoogc</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_25_ag&amp;diff=35879</id>
		<title>CSC/ECE 517 Fall 2010/ch1 25 ag</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_25_ag&amp;diff=35879"/>
		<updated>2010-09-21T20:36:00Z</updated>

		<summary type="html">&lt;p&gt;Cgoogc: /* Reference */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
= What is Inheritance? =&lt;br /&gt;
Inheritance, along with polymorphism and encapsulation, is one of the pillar features of Object Oriented Programming (OOP). Inheritance allows a class to reuse, extend or modify the behavior of another class. The term inheritance generally refers to single inheritance, where a sub-class or a derived class inherits methods and attributes from only one parent class. &lt;br /&gt;
&lt;br /&gt;
#For example, consider a general class of electrical appliances. Switching an appliance on and off can be considered a standard feature #of every appliance.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 class ElectricAppliance&lt;br /&gt;
 {&lt;br /&gt;
     public void on() { ... }&lt;br /&gt;
     public void off() { ... }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class TempControlAppliance : public ElectricAppliance&lt;br /&gt;
 {&lt;br /&gt;
     public void increaseTemp() { .. }&lt;br /&gt;
     public void decreaseTemp() { .. }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Heater : public TempControlAppliance &lt;br /&gt;
 {&lt;br /&gt;
     public void heat() { .. }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Cooler : public TempControlAppliance &lt;br /&gt;
 {&lt;br /&gt;
     public void cool() { .. }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
If we don't have inheritance, we might have to duplicate all the functions in both Heater and Cooler class. Inheritance is very important to achieve DRY (Don't Repeat Yourself). In the above class hierarchy, TempControlAppliance is super (parent) class for both Cooler and Heater subclass (child).&lt;br /&gt;
&lt;br /&gt;
= What is Multiple Inheritance =&lt;br /&gt;
When a class inherits from more than one class i.e., a child class having more than one parent class, is called multiple inheritance. Though this feature offers more flexibility, it seem to overwhelm most of the developer, so some of the object oriented language designer decided not to support Multiple Inheritance. Java and C# are some of the most famous language which don't support multiple inheritance. We need careful design to take proper advantage of Multiple inheritance, otherwise you might end up in spaghetti code, with classes inheritancing from multiple classes.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Building upon the above example, let us say we have a new device called Air Conditioner which can both heat and cool air. We might have a new class called AirConditioner as follows:&lt;br /&gt;
&lt;br /&gt;
 class AirConditioner : public Heater, public Cooler&lt;br /&gt;
 {&lt;br /&gt;
      public void setThresholdTemperature(int temp) { ... }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
= Good and bad of Multiple Inheritance =&lt;br /&gt;
Good:&lt;br /&gt;
* Multiple Inheritance strongly supports DRY principle, which leads to better software which is extensible, cohesive, less coupled and modular. &lt;br /&gt;
* Multiple Inheritance is very common in real world, in which object takes attributes from different objects.&lt;br /&gt;
* Multiple Inheritance is more expressive than Single Inheritane, so it gives flexibility in designing software with complex object relationships.&lt;br /&gt;
&lt;br /&gt;
Bad:&lt;br /&gt;
* To take complete advantage of Multiple Inheritance needs a thoughtful design. Without a good design, Multiple Inheritance infact might lead to bad code.&lt;br /&gt;
* Multiple Inheritance is overwhelming to developers. Requires more experience to properly use Multiple Inheritance.&lt;br /&gt;
* Multiple Inheritance is hard to use and make the language complicated. Some example like what happens when you Inherit from multiple classes which has same method?&lt;br /&gt;
&lt;br /&gt;
= Implementation issues =&lt;br /&gt;
One of the most common problem with Multiple Inheritance is called the [http://en.wikipedia.org/wiki/Diamond_problem Diamond Problem(3)]. This is encountered when a derived class inherits from multiple parent classes, which in turn inherit from one super class. Let us try to understand this problem using an example.&lt;br /&gt;
&lt;br /&gt;
Consider a task of modelling a car. Now a car can broadly be divided into two subclasses, mechanical cars and electric cars. &lt;br /&gt;
&lt;br /&gt;
 class Car&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
        void start() { cout &amp;lt;&amp;lt; &amp;quot;Start with loud noise&amp;quot;;&lt;br /&gt;
        void steer();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class MechCar : public Car&lt;br /&gt;
 {&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class ElectricCar : public Car&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
       void start() { cout &amp;lt;&amp;lt; &amp;quot;start without noise&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Now considering modern advancements in vehicle design, a new subclass of hybrid cars have emerged in the market. These cars have traits of both mechanical and electric cars. Here is how this is achieved in C++.&lt;br /&gt;
&lt;br /&gt;
 class HybridCar : public MechCar, ElectricCar&lt;br /&gt;
 {&lt;br /&gt;
      // This has two copies of start()&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Here is an illustration of the inheritance hierarchy.&lt;br /&gt;
&lt;br /&gt;
[[Image:Car-hybrid.jpg|frame|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now if an object of the HybridCar class calls a method of class Car, an ambiguous situation arises. Assuming the method has been [http://en.wikipedia.org/wiki/Method_overriding overridden] by class MechCar and ElectricCar, then it is not clear which version of the method the object is referring to. This is what is known as the Diamond Problem. This problem is addressed in C++ solves this problem through the use of  [http://en.wikipedia.org/wiki/Virtual_inheritance Virtual Inheritance]. &lt;br /&gt;
&lt;br /&gt;
 HybridCar h;&lt;br /&gt;
 h.ElectricCar::start();&lt;br /&gt;
&lt;br /&gt;
There are also ambiguities during the type casting. For more details about Virtual Inheritance refer [http://en.wikipedia.org/wiki/Virtual_inheritance here]&lt;br /&gt;
&lt;br /&gt;
= Multiple Inheritance - Do we really need this? =&lt;br /&gt;
Many languages which don't support multiple inheritance completely, has some ways of emulating multiple inheritance. We will consider Java as an example which does not support Multiple Inheritance completely and see how we can emulate multiple inheritance in Java. It is not completely true to say that Java does not support multiple inheritance, in fact with interfaces we can have multiple inheritance. Since, Interface just defines the behaviour, it does not have any attributes to describe it, Interfaces are not considered as a Class.&lt;br /&gt;
&lt;br /&gt;
Let us consider the previous example of an AirConditioner:&lt;br /&gt;
&lt;br /&gt;
 class AirConditioner extends [______________]&lt;br /&gt;
&lt;br /&gt;
We cannot fit both the classes there since a class in Java can extend from a single class. But Java classes can implement multiple interfaces.&lt;br /&gt;
&lt;br /&gt;
 public interface IHeater {&lt;br /&gt;
    void heat();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public interface ICooler {&lt;br /&gt;
    void cool();&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public Heater extends TempControllerAppliance implements IHeater {&lt;br /&gt;
    public void heat() { .. }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public Cooler extends TempControllerAppliance implements ICooler{&lt;br /&gt;
    public void cool() { .. }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public class AirConditioner extends TempControllerAppliance implements IHeater, ICooler, IAirConditioner {&lt;br /&gt;
    private ICooler cooler = new Cooler();&lt;br /&gt;
    private IHeater heater = new Heater();&lt;br /&gt;
 &lt;br /&gt;
    public void cool() {&lt;br /&gt;
        cooler.cool();&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void heat() {&lt;br /&gt;
        heater.heat();&lt;br /&gt;
    }  &lt;br /&gt;
    &lt;br /&gt;
    public void setThreshold(int t) { ... }   &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
To implement multiple inheritance in Java, we need to the following:&lt;br /&gt;
1. Make every class implement an Interface&lt;br /&gt;
2. Classes extends these interfaces to add logic&lt;br /&gt;
3. When a class needs to extend from Multiple interfaces, you can implement those interfaces and delegate the call to the actual implementation. This is achieved through Composition.&lt;br /&gt;
&lt;br /&gt;
We can see that it is possible to emulate multiple inheritance through [http://en.wikipedia.org/wiki/Object_composition composition]. &lt;br /&gt;
&lt;br /&gt;
Let us take another example and see how we can emulate Multiple inheritance in Java. We are developing a paint application. We have separate tools to draw each primitive shapes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
 class Shape&lt;br /&gt;
 {&lt;br /&gt;
    //...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Square : Shape&lt;br /&gt;
 {&lt;br /&gt;
   // ..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Circle : Shape&lt;br /&gt;
 {&lt;br /&gt;
    //..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Triangle : Shape&lt;br /&gt;
 {&lt;br /&gt;
    //..&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
 class Painter&lt;br /&gt;
 {&lt;br /&gt;
     //..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class SquarePainter : Painter&lt;br /&gt;
 {&lt;br /&gt;
     void draw(Square s) { }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class CirclePainter : Painter&lt;br /&gt;
 {&lt;br /&gt;
    void draw(Circle c) { }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class TrianglePainter : Painter&lt;br /&gt;
 {&lt;br /&gt;
    void draw(Triangle t) { }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we need a canvas painter, which should have the capability to draw any of the above shape. In C++, this is pretty straight forward, you define a new class which extends all painters.&lt;br /&gt;
&lt;br /&gt;
 class CanvasPainter : SquarePainter, CirclePainter, TrianglePainter&lt;br /&gt;
 {&lt;br /&gt;
     // ..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
How can do the same in Java? Ofcourse, we will use composition, but how will you know which version of draw function to call? May be we can write some thing like this:&lt;br /&gt;
&lt;br /&gt;
 if (shape instanceof Triangle)&lt;br /&gt;
 {&lt;br /&gt;
       trianglePainter.draw((Triangle) shape);&lt;br /&gt;
 }&lt;br /&gt;
 else if(shape instanceof Circle)&lt;br /&gt;
 {&lt;br /&gt;
       circlePainter.draw((Circle) shape);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Unfortunately this is not a scalable solution and for every new tool we add to our paint program, we will have to change this code. This is where design patterns like [http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern Chain of Responsibility] proves helpful.&lt;br /&gt;
&lt;br /&gt;
Implementing the above code in Java using Chain of Responsibility design pattern.&lt;br /&gt;
&lt;br /&gt;
 interface IPainter&lt;br /&gt;
 {&lt;br /&gt;
     boolean canDraw(Shape s);&lt;br /&gt;
     void draw(Shape s);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
All painters will implement the above function:&lt;br /&gt;
&lt;br /&gt;
 class TrianglePainter implements IPainter&lt;br /&gt;
 {&lt;br /&gt;
    public boolean canDraw(Shape s) { return s instanceof Triangle; }&lt;br /&gt;
    public void draw(Shape s) { // ... }  &lt;br /&gt;
 } &lt;br /&gt;
&lt;br /&gt;
 class CanvasPainter implements IPainter&lt;br /&gt;
 {&lt;br /&gt;
    // List&amp;lt;IPainter&amp;gt; = ....&lt;br /&gt;
    // function to add and remove painters&lt;br /&gt;
    public void draw(Shape s)&lt;br /&gt;
    {&lt;br /&gt;
       for(Paint p : painters)&lt;br /&gt;
       { &lt;br /&gt;
            if(p.canDraw(s)) { p.draw(s); return; }&lt;br /&gt;
       }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
We can see that, chain of responsibility design pattern provides a very neat way to handle multiple inheritance in languages which don't support them completely.&lt;br /&gt;
&lt;br /&gt;
= Alternates to Multiple Inheritance =&lt;br /&gt;
* Composition &amp;amp; Interfaces&lt;br /&gt;
Compositions provides a neat way of emulating Multiple Inheritance. With Multiple Inheritance, we bring functionality from different classes to the class which extends them. We can achieve the same using Composition in which one class contains instance of other class, there by importing the capability of all those classes. Since a class is allowed to implement Multiple Interfaces, the class behaves as though it extended from Multiple classes.&lt;br /&gt;
&lt;br /&gt;
* Mixins&lt;br /&gt;
Some languages provide [http://en.wikipedia.org/wiki/Mixin mixins] as a language feature. Mixins provide another elegant mechanism which mimics Multiple Inheritance. The common problems with Mixin is that it pollutes the class namespace as it imports functions from different modules. This [http://www.artima.com/weblogs/viewpost.jsp?thread=246341 article] talks about the limitations of mixins in greater detail.&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
&amp;lt;table border = &amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;Single Inheritance&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;Multiple Inheritance&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Flexibility&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Single Inheritance does have some restriction in terms of modeling real world objects, but mitigate this problem with careful design&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Multiple Inheritance provides lots more flexibility for designing&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Easy of Use&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Single Inheritance is much simpler to use&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Multiple Inheritance sometimes leaves the programmer confused. One of the common problem with Multiple inheritance in Diamond Problem&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Availability&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Single Inheritance is supported by all the Object Oriented Programming Languages&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Multiple Inheritance is not supported by few of the OOP Languages, because the designer of the language thought it is hard to use and not necessary for common scenarios.&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Efficiency&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;This actually language specific. But generally it takes less time to figure which version of the function to call in a single inheritance&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Virtual Function dispatch in C++ is more time consuming in Multiple Inheritance. For example Multiple Inheritance in C++, needs multiple vtable for dispatching virtual functions. [http://www.cs.ucsb.edu/~urs/oocsb/papers/oopsla96.pdf (1)] discusses about this problem in great detail.&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Further Reading =&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Virtual_inheritance Virtual Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_composition Composition]&lt;br /&gt;
&lt;br /&gt;
= Reference =&lt;br /&gt;
*[http://www.cs.ucsb.edu/~urs/oocsb/papers/oopsla96.pdf (1) Cost of Virtual Functions C++]&lt;br /&gt;
*[http://www.artima.com/weblogs/viewpost.jsp?thread=246341 (2) Mixins considered Harmful]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Diamond_problem (3) Diamond Problem with Multiple Inheritance]&lt;/div&gt;</summary>
		<author><name>Cgoogc</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_25_ag&amp;diff=35878</id>
		<title>CSC/ECE 517 Fall 2010/ch1 25 ag</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_25_ag&amp;diff=35878"/>
		<updated>2010-09-21T20:35:28Z</updated>

		<summary type="html">&lt;p&gt;Cgoogc: /* Implementation issues */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
= What is Inheritance? =&lt;br /&gt;
Inheritance, along with polymorphism and encapsulation, is one of the pillar features of Object Oriented Programming (OOP). Inheritance allows a class to reuse, extend or modify the behavior of another class. The term inheritance generally refers to single inheritance, where a sub-class or a derived class inherits methods and attributes from only one parent class. &lt;br /&gt;
&lt;br /&gt;
#For example, consider a general class of electrical appliances. Switching an appliance on and off can be considered a standard feature #of every appliance.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 class ElectricAppliance&lt;br /&gt;
 {&lt;br /&gt;
     public void on() { ... }&lt;br /&gt;
     public void off() { ... }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class TempControlAppliance : public ElectricAppliance&lt;br /&gt;
 {&lt;br /&gt;
     public void increaseTemp() { .. }&lt;br /&gt;
     public void decreaseTemp() { .. }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Heater : public TempControlAppliance &lt;br /&gt;
 {&lt;br /&gt;
     public void heat() { .. }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Cooler : public TempControlAppliance &lt;br /&gt;
 {&lt;br /&gt;
     public void cool() { .. }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
If we don't have inheritance, we might have to duplicate all the functions in both Heater and Cooler class. Inheritance is very important to achieve DRY (Don't Repeat Yourself). In the above class hierarchy, TempControlAppliance is super (parent) class for both Cooler and Heater subclass (child).&lt;br /&gt;
&lt;br /&gt;
= What is Multiple Inheritance =&lt;br /&gt;
When a class inherits from more than one class i.e., a child class having more than one parent class, is called multiple inheritance. Though this feature offers more flexibility, it seem to overwhelm most of the developer, so some of the object oriented language designer decided not to support Multiple Inheritance. Java and C# are some of the most famous language which don't support multiple inheritance. We need careful design to take proper advantage of Multiple inheritance, otherwise you might end up in spaghetti code, with classes inheritancing from multiple classes.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Building upon the above example, let us say we have a new device called Air Conditioner which can both heat and cool air. We might have a new class called AirConditioner as follows:&lt;br /&gt;
&lt;br /&gt;
 class AirConditioner : public Heater, public Cooler&lt;br /&gt;
 {&lt;br /&gt;
      public void setThresholdTemperature(int temp) { ... }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
= Good and bad of Multiple Inheritance =&lt;br /&gt;
Good:&lt;br /&gt;
* Multiple Inheritance strongly supports DRY principle, which leads to better software which is extensible, cohesive, less coupled and modular. &lt;br /&gt;
* Multiple Inheritance is very common in real world, in which object takes attributes from different objects.&lt;br /&gt;
* Multiple Inheritance is more expressive than Single Inheritane, so it gives flexibility in designing software with complex object relationships.&lt;br /&gt;
&lt;br /&gt;
Bad:&lt;br /&gt;
* To take complete advantage of Multiple Inheritance needs a thoughtful design. Without a good design, Multiple Inheritance infact might lead to bad code.&lt;br /&gt;
* Multiple Inheritance is overwhelming to developers. Requires more experience to properly use Multiple Inheritance.&lt;br /&gt;
* Multiple Inheritance is hard to use and make the language complicated. Some example like what happens when you Inherit from multiple classes which has same method?&lt;br /&gt;
&lt;br /&gt;
= Implementation issues =&lt;br /&gt;
One of the most common problem with Multiple Inheritance is called the [http://en.wikipedia.org/wiki/Diamond_problem Diamond Problem(3)]. This is encountered when a derived class inherits from multiple parent classes, which in turn inherit from one super class. Let us try to understand this problem using an example.&lt;br /&gt;
&lt;br /&gt;
Consider a task of modelling a car. Now a car can broadly be divided into two subclasses, mechanical cars and electric cars. &lt;br /&gt;
&lt;br /&gt;
 class Car&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
        void start() { cout &amp;lt;&amp;lt; &amp;quot;Start with loud noise&amp;quot;;&lt;br /&gt;
        void steer();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class MechCar : public Car&lt;br /&gt;
 {&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class ElectricCar : public Car&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
       void start() { cout &amp;lt;&amp;lt; &amp;quot;start without noise&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Now considering modern advancements in vehicle design, a new subclass of hybrid cars have emerged in the market. These cars have traits of both mechanical and electric cars. Here is how this is achieved in C++.&lt;br /&gt;
&lt;br /&gt;
 class HybridCar : public MechCar, ElectricCar&lt;br /&gt;
 {&lt;br /&gt;
      // This has two copies of start()&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Here is an illustration of the inheritance hierarchy.&lt;br /&gt;
&lt;br /&gt;
[[Image:Car-hybrid.jpg|frame|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now if an object of the HybridCar class calls a method of class Car, an ambiguous situation arises. Assuming the method has been [http://en.wikipedia.org/wiki/Method_overriding overridden] by class MechCar and ElectricCar, then it is not clear which version of the method the object is referring to. This is what is known as the Diamond Problem. This problem is addressed in C++ solves this problem through the use of  [http://en.wikipedia.org/wiki/Virtual_inheritance Virtual Inheritance]. &lt;br /&gt;
&lt;br /&gt;
 HybridCar h;&lt;br /&gt;
 h.ElectricCar::start();&lt;br /&gt;
&lt;br /&gt;
There are also ambiguities during the type casting. For more details about Virtual Inheritance refer [http://en.wikipedia.org/wiki/Virtual_inheritance here]&lt;br /&gt;
&lt;br /&gt;
= Multiple Inheritance - Do we really need this? =&lt;br /&gt;
Many languages which don't support multiple inheritance completely, has some ways of emulating multiple inheritance. We will consider Java as an example which does not support Multiple Inheritance completely and see how we can emulate multiple inheritance in Java. It is not completely true to say that Java does not support multiple inheritance, in fact with interfaces we can have multiple inheritance. Since, Interface just defines the behaviour, it does not have any attributes to describe it, Interfaces are not considered as a Class.&lt;br /&gt;
&lt;br /&gt;
Let us consider the previous example of an AirConditioner:&lt;br /&gt;
&lt;br /&gt;
 class AirConditioner extends [______________]&lt;br /&gt;
&lt;br /&gt;
We cannot fit both the classes there since a class in Java can extend from a single class. But Java classes can implement multiple interfaces.&lt;br /&gt;
&lt;br /&gt;
 public interface IHeater {&lt;br /&gt;
    void heat();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public interface ICooler {&lt;br /&gt;
    void cool();&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public Heater extends TempControllerAppliance implements IHeater {&lt;br /&gt;
    public void heat() { .. }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public Cooler extends TempControllerAppliance implements ICooler{&lt;br /&gt;
    public void cool() { .. }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public class AirConditioner extends TempControllerAppliance implements IHeater, ICooler, IAirConditioner {&lt;br /&gt;
    private ICooler cooler = new Cooler();&lt;br /&gt;
    private IHeater heater = new Heater();&lt;br /&gt;
 &lt;br /&gt;
    public void cool() {&lt;br /&gt;
        cooler.cool();&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void heat() {&lt;br /&gt;
        heater.heat();&lt;br /&gt;
    }  &lt;br /&gt;
    &lt;br /&gt;
    public void setThreshold(int t) { ... }   &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
To implement multiple inheritance in Java, we need to the following:&lt;br /&gt;
1. Make every class implement an Interface&lt;br /&gt;
2. Classes extends these interfaces to add logic&lt;br /&gt;
3. When a class needs to extend from Multiple interfaces, you can implement those interfaces and delegate the call to the actual implementation. This is achieved through Composition.&lt;br /&gt;
&lt;br /&gt;
We can see that it is possible to emulate multiple inheritance through [http://en.wikipedia.org/wiki/Object_composition composition]. &lt;br /&gt;
&lt;br /&gt;
Let us take another example and see how we can emulate Multiple inheritance in Java. We are developing a paint application. We have separate tools to draw each primitive shapes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
 class Shape&lt;br /&gt;
 {&lt;br /&gt;
    //...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Square : Shape&lt;br /&gt;
 {&lt;br /&gt;
   // ..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Circle : Shape&lt;br /&gt;
 {&lt;br /&gt;
    //..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Triangle : Shape&lt;br /&gt;
 {&lt;br /&gt;
    //..&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
 class Painter&lt;br /&gt;
 {&lt;br /&gt;
     //..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class SquarePainter : Painter&lt;br /&gt;
 {&lt;br /&gt;
     void draw(Square s) { }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class CirclePainter : Painter&lt;br /&gt;
 {&lt;br /&gt;
    void draw(Circle c) { }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class TrianglePainter : Painter&lt;br /&gt;
 {&lt;br /&gt;
    void draw(Triangle t) { }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we need a canvas painter, which should have the capability to draw any of the above shape. In C++, this is pretty straight forward, you define a new class which extends all painters.&lt;br /&gt;
&lt;br /&gt;
 class CanvasPainter : SquarePainter, CirclePainter, TrianglePainter&lt;br /&gt;
 {&lt;br /&gt;
     // ..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
How can do the same in Java? Ofcourse, we will use composition, but how will you know which version of draw function to call? May be we can write some thing like this:&lt;br /&gt;
&lt;br /&gt;
 if (shape instanceof Triangle)&lt;br /&gt;
 {&lt;br /&gt;
       trianglePainter.draw((Triangle) shape);&lt;br /&gt;
 }&lt;br /&gt;
 else if(shape instanceof Circle)&lt;br /&gt;
 {&lt;br /&gt;
       circlePainter.draw((Circle) shape);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Unfortunately this is not a scalable solution and for every new tool we add to our paint program, we will have to change this code. This is where design patterns like [http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern Chain of Responsibility] proves helpful.&lt;br /&gt;
&lt;br /&gt;
Implementing the above code in Java using Chain of Responsibility design pattern.&lt;br /&gt;
&lt;br /&gt;
 interface IPainter&lt;br /&gt;
 {&lt;br /&gt;
     boolean canDraw(Shape s);&lt;br /&gt;
     void draw(Shape s);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
All painters will implement the above function:&lt;br /&gt;
&lt;br /&gt;
 class TrianglePainter implements IPainter&lt;br /&gt;
 {&lt;br /&gt;
    public boolean canDraw(Shape s) { return s instanceof Triangle; }&lt;br /&gt;
    public void draw(Shape s) { // ... }  &lt;br /&gt;
 } &lt;br /&gt;
&lt;br /&gt;
 class CanvasPainter implements IPainter&lt;br /&gt;
 {&lt;br /&gt;
    // List&amp;lt;IPainter&amp;gt; = ....&lt;br /&gt;
    // function to add and remove painters&lt;br /&gt;
    public void draw(Shape s)&lt;br /&gt;
    {&lt;br /&gt;
       for(Paint p : painters)&lt;br /&gt;
       { &lt;br /&gt;
            if(p.canDraw(s)) { p.draw(s); return; }&lt;br /&gt;
       }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
We can see that, chain of responsibility design pattern provides a very neat way to handle multiple inheritance in languages which don't support them completely.&lt;br /&gt;
&lt;br /&gt;
= Alternates to Multiple Inheritance =&lt;br /&gt;
* Composition &amp;amp; Interfaces&lt;br /&gt;
Compositions provides a neat way of emulating Multiple Inheritance. With Multiple Inheritance, we bring functionality from different classes to the class which extends them. We can achieve the same using Composition in which one class contains instance of other class, there by importing the capability of all those classes. Since a class is allowed to implement Multiple Interfaces, the class behaves as though it extended from Multiple classes.&lt;br /&gt;
&lt;br /&gt;
* Mixins&lt;br /&gt;
Some languages provide [http://en.wikipedia.org/wiki/Mixin mixins] as a language feature. Mixins provide another elegant mechanism which mimics Multiple Inheritance. The common problems with Mixin is that it pollutes the class namespace as it imports functions from different modules. This [http://www.artima.com/weblogs/viewpost.jsp?thread=246341 article] talks about the limitations of mixins in greater detail.&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
&amp;lt;table border = &amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;Single Inheritance&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;Multiple Inheritance&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Flexibility&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Single Inheritance does have some restriction in terms of modeling real world objects, but mitigate this problem with careful design&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Multiple Inheritance provides lots more flexibility for designing&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Easy of Use&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Single Inheritance is much simpler to use&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Multiple Inheritance sometimes leaves the programmer confused. One of the common problem with Multiple inheritance in Diamond Problem&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Availability&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Single Inheritance is supported by all the Object Oriented Programming Languages&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Multiple Inheritance is not supported by few of the OOP Languages, because the designer of the language thought it is hard to use and not necessary for common scenarios.&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Efficiency&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;This actually language specific. But generally it takes less time to figure which version of the function to call in a single inheritance&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Virtual Function dispatch in C++ is more time consuming in Multiple Inheritance. For example Multiple Inheritance in C++, needs multiple vtable for dispatching virtual functions. [http://www.cs.ucsb.edu/~urs/oocsb/papers/oopsla96.pdf (1)] discusses about this problem in great detail.&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Further Reading =&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Virtual_inheritance Virtual Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_composition Composition]&lt;br /&gt;
&lt;br /&gt;
= Reference =&lt;br /&gt;
*[http://www.cs.ucsb.edu/~urs/oocsb/papers/oopsla96.pdf (1) Cost of Virtual Functions C++]&lt;br /&gt;
*[http://www.artima.com/weblogs/viewpost.jsp?thread=246341 (2) Mixins considered Harmful]&lt;/div&gt;</summary>
		<author><name>Cgoogc</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_25_ag&amp;diff=35876</id>
		<title>CSC/ECE 517 Fall 2010/ch1 25 ag</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_25_ag&amp;diff=35876"/>
		<updated>2010-09-21T20:32:57Z</updated>

		<summary type="html">&lt;p&gt;Cgoogc: /* Reference */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
= What is Inheritance? =&lt;br /&gt;
Inheritance, along with polymorphism and encapsulation, is one of the pillar features of Object Oriented Programming (OOP). Inheritance allows a class to reuse, extend or modify the behavior of another class. The term inheritance generally refers to single inheritance, where a sub-class or a derived class inherits methods and attributes from only one parent class. &lt;br /&gt;
&lt;br /&gt;
#For example, consider a general class of electrical appliances. Switching an appliance on and off can be considered a standard feature #of every appliance.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 class ElectricAppliance&lt;br /&gt;
 {&lt;br /&gt;
     public void on() { ... }&lt;br /&gt;
     public void off() { ... }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class TempControlAppliance : public ElectricAppliance&lt;br /&gt;
 {&lt;br /&gt;
     public void increaseTemp() { .. }&lt;br /&gt;
     public void decreaseTemp() { .. }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Heater : public TempControlAppliance &lt;br /&gt;
 {&lt;br /&gt;
     public void heat() { .. }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Cooler : public TempControlAppliance &lt;br /&gt;
 {&lt;br /&gt;
     public void cool() { .. }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
If we don't have inheritance, we might have to duplicate all the functions in both Heater and Cooler class. Inheritance is very important to achieve DRY (Don't Repeat Yourself). In the above class hierarchy, TempControlAppliance is super (parent) class for both Cooler and Heater subclass (child).&lt;br /&gt;
&lt;br /&gt;
= What is Multiple Inheritance =&lt;br /&gt;
When a class inherits from more than one class i.e., a child class having more than one parent class, is called multiple inheritance. Though this feature offers more flexibility, it seem to overwhelm most of the developer, so some of the object oriented language designer decided not to support Multiple Inheritance. Java and C# are some of the most famous language which don't support multiple inheritance. We need careful design to take proper advantage of Multiple inheritance, otherwise you might end up in spaghetti code, with classes inheritancing from multiple classes.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Building upon the above example, let us say we have a new device called Air Conditioner which can both heat and cool air. We might have a new class called AirConditioner as follows:&lt;br /&gt;
&lt;br /&gt;
 class AirConditioner : public Heater, public Cooler&lt;br /&gt;
 {&lt;br /&gt;
      public void setThresholdTemperature(int temp) { ... }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
= Good and bad of Multiple Inheritance =&lt;br /&gt;
Good:&lt;br /&gt;
* Multiple Inheritance strongly supports DRY principle, which leads to better software which is extensible, cohesive, less coupled and modular. &lt;br /&gt;
* Multiple Inheritance is very common in real world, in which object takes attributes from different objects.&lt;br /&gt;
* Multiple Inheritance is more expressive than Single Inheritane, so it gives flexibility in designing software with complex object relationships.&lt;br /&gt;
&lt;br /&gt;
Bad:&lt;br /&gt;
* To take complete advantage of Multiple Inheritance needs a thoughtful design. Without a good design, Multiple Inheritance infact might lead to bad code.&lt;br /&gt;
* Multiple Inheritance is overwhelming to developers. Requires more experience to properly use Multiple Inheritance.&lt;br /&gt;
* Multiple Inheritance is hard to use and make the language complicated. Some example like what happens when you Inherit from multiple classes which has same method?&lt;br /&gt;
&lt;br /&gt;
= Implementation issues =&lt;br /&gt;
One of the most common problem with Multiple Inheritance is called the [http://en.wikipedia.org/wiki/Diamond_problem Diamond Problem]. This is encountered when a derived class inherits from multiple parent classes, which in turn inherit from one super class. Let us try to understand this problem using an example.&lt;br /&gt;
&lt;br /&gt;
Consider a task of modelling a car. Now a car can broadly be divided into two subclasses, mechanical cars and electric cars. &lt;br /&gt;
&lt;br /&gt;
 class Car&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
        void start() { cout &amp;lt;&amp;lt; &amp;quot;Start with loud noise&amp;quot;;&lt;br /&gt;
        void steer();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class MechCar : public Car&lt;br /&gt;
 {&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class ElectricCar : public Car&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
       void start() { cout &amp;lt;&amp;lt; &amp;quot;start without noise&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Now considering modern advancements in vehicle design, a new subclass of hybrid cars have emerged in the market. These cars have traits of both mechanical and electric cars. Here is how this is achieved in C++.&lt;br /&gt;
&lt;br /&gt;
 class HybridCar : public MechCar, ElectricCar&lt;br /&gt;
 {&lt;br /&gt;
      // This has two copies of start()&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Here is an illustration of the inheritance hierarchy.&lt;br /&gt;
&lt;br /&gt;
[[Image:Car-hybrid.jpg|frame|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now if an object of the HybridCar class calls a method of class Car, an ambiguous situation arises. Assuming the method has been [http://en.wikipedia.org/wiki/Method_overriding overridden] by class MechCar and ElectricCar, then it is not clear which version of the method the object is referring to. This is what is known as the Diamond Problem. This problem is addressed in C++ solves this problem through the use of  [http://en.wikipedia.org/wiki/Virtual_inheritance Virtual Inheritance]. &lt;br /&gt;
&lt;br /&gt;
 HybridCar h;&lt;br /&gt;
 h.ElectricCar::start();&lt;br /&gt;
&lt;br /&gt;
There are also ambiguities during the type casting. For more details about Virtual Inheritance refer [http://en.wikipedia.org/wiki/Virtual_inheritance here]&lt;br /&gt;
&lt;br /&gt;
= Multiple Inheritance - Do we really need this? =&lt;br /&gt;
Many languages which don't support multiple inheritance completely, has some ways of emulating multiple inheritance. We will consider Java as an example which does not support Multiple Inheritance completely and see how we can emulate multiple inheritance in Java. It is not completely true to say that Java does not support multiple inheritance, in fact with interfaces we can have multiple inheritance. Since, Interface just defines the behaviour, it does not have any attributes to describe it, Interfaces are not considered as a Class.&lt;br /&gt;
&lt;br /&gt;
Let us consider the previous example of an AirConditioner:&lt;br /&gt;
&lt;br /&gt;
 class AirConditioner extends [______________]&lt;br /&gt;
&lt;br /&gt;
We cannot fit both the classes there since a class in Java can extend from a single class. But Java classes can implement multiple interfaces.&lt;br /&gt;
&lt;br /&gt;
 public interface IHeater {&lt;br /&gt;
    void heat();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public interface ICooler {&lt;br /&gt;
    void cool();&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public Heater extends TempControllerAppliance implements IHeater {&lt;br /&gt;
    public void heat() { .. }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public Cooler extends TempControllerAppliance implements ICooler{&lt;br /&gt;
    public void cool() { .. }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public class AirConditioner extends TempControllerAppliance implements IHeater, ICooler, IAirConditioner {&lt;br /&gt;
    private ICooler cooler = new Cooler();&lt;br /&gt;
    private IHeater heater = new Heater();&lt;br /&gt;
 &lt;br /&gt;
    public void cool() {&lt;br /&gt;
        cooler.cool();&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void heat() {&lt;br /&gt;
        heater.heat();&lt;br /&gt;
    }  &lt;br /&gt;
    &lt;br /&gt;
    public void setThreshold(int t) { ... }   &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
To implement multiple inheritance in Java, we need to the following:&lt;br /&gt;
1. Make every class implement an Interface&lt;br /&gt;
2. Classes extends these interfaces to add logic&lt;br /&gt;
3. When a class needs to extend from Multiple interfaces, you can implement those interfaces and delegate the call to the actual implementation. This is achieved through Composition.&lt;br /&gt;
&lt;br /&gt;
We can see that it is possible to emulate multiple inheritance through [http://en.wikipedia.org/wiki/Object_composition composition]. &lt;br /&gt;
&lt;br /&gt;
Let us take another example and see how we can emulate Multiple inheritance in Java. We are developing a paint application. We have separate tools to draw each primitive shapes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
 class Shape&lt;br /&gt;
 {&lt;br /&gt;
    //...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Square : Shape&lt;br /&gt;
 {&lt;br /&gt;
   // ..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Circle : Shape&lt;br /&gt;
 {&lt;br /&gt;
    //..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Triangle : Shape&lt;br /&gt;
 {&lt;br /&gt;
    //..&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
 class Painter&lt;br /&gt;
 {&lt;br /&gt;
     //..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class SquarePainter : Painter&lt;br /&gt;
 {&lt;br /&gt;
     void draw(Square s) { }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class CirclePainter : Painter&lt;br /&gt;
 {&lt;br /&gt;
    void draw(Circle c) { }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class TrianglePainter : Painter&lt;br /&gt;
 {&lt;br /&gt;
    void draw(Triangle t) { }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we need a canvas painter, which should have the capability to draw any of the above shape. In C++, this is pretty straight forward, you define a new class which extends all painters.&lt;br /&gt;
&lt;br /&gt;
 class CanvasPainter : SquarePainter, CirclePainter, TrianglePainter&lt;br /&gt;
 {&lt;br /&gt;
     // ..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
How can do the same in Java? Ofcourse, we will use composition, but how will you know which version of draw function to call? May be we can write some thing like this:&lt;br /&gt;
&lt;br /&gt;
 if (shape instanceof Triangle)&lt;br /&gt;
 {&lt;br /&gt;
       trianglePainter.draw((Triangle) shape);&lt;br /&gt;
 }&lt;br /&gt;
 else if(shape instanceof Circle)&lt;br /&gt;
 {&lt;br /&gt;
       circlePainter.draw((Circle) shape);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Unfortunately this is not a scalable solution and for every new tool we add to our paint program, we will have to change this code. This is where design patterns like [http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern Chain of Responsibility] proves helpful.&lt;br /&gt;
&lt;br /&gt;
Implementing the above code in Java using Chain of Responsibility design pattern.&lt;br /&gt;
&lt;br /&gt;
 interface IPainter&lt;br /&gt;
 {&lt;br /&gt;
     boolean canDraw(Shape s);&lt;br /&gt;
     void draw(Shape s);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
All painters will implement the above function:&lt;br /&gt;
&lt;br /&gt;
 class TrianglePainter implements IPainter&lt;br /&gt;
 {&lt;br /&gt;
    public boolean canDraw(Shape s) { return s instanceof Triangle; }&lt;br /&gt;
    public void draw(Shape s) { // ... }  &lt;br /&gt;
 } &lt;br /&gt;
&lt;br /&gt;
 class CanvasPainter implements IPainter&lt;br /&gt;
 {&lt;br /&gt;
    // List&amp;lt;IPainter&amp;gt; = ....&lt;br /&gt;
    // function to add and remove painters&lt;br /&gt;
    public void draw(Shape s)&lt;br /&gt;
    {&lt;br /&gt;
       for(Paint p : painters)&lt;br /&gt;
       { &lt;br /&gt;
            if(p.canDraw(s)) { p.draw(s); return; }&lt;br /&gt;
       }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
We can see that, chain of responsibility design pattern provides a very neat way to handle multiple inheritance in languages which don't support them completely.&lt;br /&gt;
&lt;br /&gt;
= Alternates to Multiple Inheritance =&lt;br /&gt;
* Composition &amp;amp; Interfaces&lt;br /&gt;
Compositions provides a neat way of emulating Multiple Inheritance. With Multiple Inheritance, we bring functionality from different classes to the class which extends them. We can achieve the same using Composition in which one class contains instance of other class, there by importing the capability of all those classes. Since a class is allowed to implement Multiple Interfaces, the class behaves as though it extended from Multiple classes.&lt;br /&gt;
&lt;br /&gt;
* Mixins&lt;br /&gt;
Some languages provide [http://en.wikipedia.org/wiki/Mixin mixins] as a language feature. Mixins provide another elegant mechanism which mimics Multiple Inheritance. The common problems with Mixin is that it pollutes the class namespace as it imports functions from different modules. This [http://www.artima.com/weblogs/viewpost.jsp?thread=246341 article] talks about the limitations of mixins in greater detail.&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
&amp;lt;table border = &amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;Single Inheritance&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;Multiple Inheritance&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Flexibility&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Single Inheritance does have some restriction in terms of modeling real world objects, but mitigate this problem with careful design&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Multiple Inheritance provides lots more flexibility for designing&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Easy of Use&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Single Inheritance is much simpler to use&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Multiple Inheritance sometimes leaves the programmer confused. One of the common problem with Multiple inheritance in Diamond Problem&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Availability&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Single Inheritance is supported by all the Object Oriented Programming Languages&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Multiple Inheritance is not supported by few of the OOP Languages, because the designer of the language thought it is hard to use and not necessary for common scenarios.&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Efficiency&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;This actually language specific. But generally it takes less time to figure which version of the function to call in a single inheritance&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Virtual Function dispatch in C++ is more time consuming in Multiple Inheritance. For example Multiple Inheritance in C++, needs multiple vtable for dispatching virtual functions. [http://www.cs.ucsb.edu/~urs/oocsb/papers/oopsla96.pdf (1)] discusses about this problem in great detail.&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Further Reading =&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Virtual_inheritance Virtual Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_composition Composition]&lt;br /&gt;
&lt;br /&gt;
= Reference =&lt;br /&gt;
*[http://www.cs.ucsb.edu/~urs/oocsb/papers/oopsla96.pdf (1) Cost of Virtual Functions C++]&lt;br /&gt;
*[http://www.artima.com/weblogs/viewpost.jsp?thread=246341 (2) Mixins considered Harmful]&lt;/div&gt;</summary>
		<author><name>Cgoogc</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_25_ag&amp;diff=35869</id>
		<title>CSC/ECE 517 Fall 2010/ch1 25 ag</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_25_ag&amp;diff=35869"/>
		<updated>2010-09-21T20:22:10Z</updated>

		<summary type="html">&lt;p&gt;Cgoogc: /* Implementation issues */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
= What is Inheritance? =&lt;br /&gt;
Inheritance, along with polymorphism and encapsulation, is one of the pillar features of Object Oriented Programming (OOP). Inheritance allows a class to reuse, extend or modify the behavior of another class. The term inheritance generally refers to single inheritance, where a sub-class or a derived class inherits methods and attributes from only one parent class. &lt;br /&gt;
&lt;br /&gt;
#For example, consider a general class of electrical appliances. Switching an appliance on and off can be considered a standard feature #of every appliance.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 class ElectricAppliance&lt;br /&gt;
 {&lt;br /&gt;
     public void on() { ... }&lt;br /&gt;
     public void off() { ... }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class TempControlAppliance : public ElectricAppliance&lt;br /&gt;
 {&lt;br /&gt;
     public void increaseTemp() { .. }&lt;br /&gt;
     public void decreaseTemp() { .. }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Heater : public TempControlAppliance &lt;br /&gt;
 {&lt;br /&gt;
     public void heat() { .. }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Cooler : public TempControlAppliance &lt;br /&gt;
 {&lt;br /&gt;
     public void cool() { .. }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
If we don't have inheritance, we might have to duplicate all the functions in both Heater and Cooler class. Inheritance is very important to achieve DRY (Don't Repeat Yourself). In the above class hierarchy, TempControlAppliance is super (parent) class for both Cooler and Heater subclass (child).&lt;br /&gt;
&lt;br /&gt;
= What is Multiple Inheritance =&lt;br /&gt;
When a class inherits from more than one class i.e., a child class having more than one parent class, is called multiple inheritance. Though this feature offers more flexibility, it seem to overwhelm most of the developer, so some of the object oriented language designer decided not to support Multiple Inheritance. Java and C# are some of the most famous language which don't support multiple inheritance. We need careful design to take proper advantage of Multiple inheritance, otherwise you might end up in spaghetti code, with classes inheritancing from multiple classes.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Building upon the above example, let us say we have a new device called Air Conditioner which can both heat and cool air. We might have a new class called AirConditioner as follows:&lt;br /&gt;
&lt;br /&gt;
 class AirConditioner : public Heater, public Cooler&lt;br /&gt;
 {&lt;br /&gt;
      public void setThresholdTemperature(int temp) { ... }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
= Good and bad of Multiple Inheritance =&lt;br /&gt;
Good:&lt;br /&gt;
* Multiple Inheritance strongly supports DRY principle, which leads to better software which is extensible, cohesive, less coupled and modular. &lt;br /&gt;
* Multiple Inheritance is very common in real world, in which object takes attributes from different objects.&lt;br /&gt;
* Multiple Inheritance is more expressive than Single Inheritane, so it gives flexibility in designing software with complex object relationships.&lt;br /&gt;
&lt;br /&gt;
Bad:&lt;br /&gt;
* To take complete advantage of Multiple Inheritance needs a thoughtful design. Without a good design, Multiple Inheritance infact might lead to bad code.&lt;br /&gt;
* Multiple Inheritance is overwhelming to developers. Requires more experience to properly use Multiple Inheritance.&lt;br /&gt;
* Multiple Inheritance is hard to use and make the language complicated. Some example like what happens when you Inherit from multiple classes which has same method?&lt;br /&gt;
&lt;br /&gt;
= Implementation issues =&lt;br /&gt;
One of the most common problem with Multiple Inheritance is called the [http://en.wikipedia.org/wiki/Diamond_problem Diamond Problem]. This is encountered when a derived class inherits from multiple parent classes, which in turn inherit from one super class. Let us try to understand this problem using an example.&lt;br /&gt;
&lt;br /&gt;
Consider a task of modelling a car. Now a car can broadly be divided into two subclasses, mechanical cars and electric cars. &lt;br /&gt;
&lt;br /&gt;
 class Car&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
        void start() { cout &amp;lt;&amp;lt; &amp;quot;Start with loud noise&amp;quot;;&lt;br /&gt;
        void steer();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class MechCar : public Car&lt;br /&gt;
 {&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class ElectricCar : public Car&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
       void start() { cout &amp;lt;&amp;lt; &amp;quot;start without noise&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Now considering modern advancements in vehicle design, a new subclass of hybrid cars have emerged in the market. These cars have traits of both mechanical and electric cars. Here is how this is achieved in C++.&lt;br /&gt;
&lt;br /&gt;
 class HybridCar : public MechCar, ElectricCar&lt;br /&gt;
 {&lt;br /&gt;
      // This has two copies of start()&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Here is an illustration of the inheritance hierarchy.&lt;br /&gt;
&lt;br /&gt;
[[Image:Car-hybrid.jpg | frame | center | 50p]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
From the above we can see that there is an ambiguity about which version of start function to use. C++ solves this problem through [http://en.wikipedia.org/wiki/Virtual_inheritance Virtual Inheritance]. &lt;br /&gt;
&lt;br /&gt;
 HybridCar h;&lt;br /&gt;
 h.ElectricCar::start();&lt;br /&gt;
&lt;br /&gt;
There are also ambiguities during the type casting. For more details about Virtual Inheritance refer [http://en.wikipedia.org/wiki/Virtual_inheritance here]&lt;br /&gt;
&lt;br /&gt;
= Multiple Inheritance - Do we really need this? =&lt;br /&gt;
Many languages which don't support multiple inheritance completely, has some ways of emulating multiple inheritance. We will consider Java as an example which does not support Multiple Inheritance completely and see how we can emulate multiple inheritance in Java. It is not completely true to say that Java does not support multiple inheritance, in fact with interfaces we can have multiple inheritance. Since, Interface just defines the behaviour, it does not have any attributes to describe it, Interfaces are not considered as a Class.&lt;br /&gt;
&lt;br /&gt;
Let us consider the previous example of an AirConditioner:&lt;br /&gt;
&lt;br /&gt;
 class AirConditioner extends [______________]&lt;br /&gt;
&lt;br /&gt;
We cannot fit both the classes there since a class in Java can extend from a single class. But Java classes can implement multiple interfaces.&lt;br /&gt;
&lt;br /&gt;
 public interface IHeater {&lt;br /&gt;
    void heat();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public interface ICooler {&lt;br /&gt;
    void cool();&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public Heater extends TempControllerAppliance implements IHeater {&lt;br /&gt;
    public void heat() { .. }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public Cooler extends TempControllerAppliance implements ICooler{&lt;br /&gt;
    public void cool() { .. }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public class AirConditioner extends TempControllerAppliance implements IHeater, ICooler, IAirConditioner {&lt;br /&gt;
    private ICooler cooler = new Cooler();&lt;br /&gt;
    private IHeater heater = new Heater();&lt;br /&gt;
 &lt;br /&gt;
    public void cool() {&lt;br /&gt;
        cooler.cool();&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void heat() {&lt;br /&gt;
        heater.heat();&lt;br /&gt;
    }  &lt;br /&gt;
    &lt;br /&gt;
    public void setThreshold(int t) { ... }   &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
To implement multiple inheritance in Java, we need to the following:&lt;br /&gt;
1. Make every class implement an Interface&lt;br /&gt;
2. Classes extends these interfaces to add logic&lt;br /&gt;
3. When a class needs to extend from Multiple interfaces, you can implement those interfaces and delegate the call to the actual implementation. This is achieved through Composition.&lt;br /&gt;
&lt;br /&gt;
We can see that it is possible to emulate multiple inheritance through [http://en.wikipedia.org/wiki/Object_composition composition]. &lt;br /&gt;
&lt;br /&gt;
Let us take another example and see how we can emulate Multiple inheritance in Java. We are developing a paint application. We have separate tools to draw each primitive shapes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
 class Shape&lt;br /&gt;
 {&lt;br /&gt;
    //...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Square : Shape&lt;br /&gt;
 {&lt;br /&gt;
   // ..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Circle : Shape&lt;br /&gt;
 {&lt;br /&gt;
    //..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Triangle : Shape&lt;br /&gt;
 {&lt;br /&gt;
    //..&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
 class Painter&lt;br /&gt;
 {&lt;br /&gt;
     //..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class SquarePainter : Painter&lt;br /&gt;
 {&lt;br /&gt;
     void draw(Square s) { }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class CirclePainter : Painter&lt;br /&gt;
 {&lt;br /&gt;
    void draw(Circle c) { }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class TrianglePainter : Painter&lt;br /&gt;
 {&lt;br /&gt;
    void draw(Triangle t) { }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we need a canvas painter, which should have the capability to draw any of the above shape. In C++, this is pretty straight forward, you define a new class which extends all painters.&lt;br /&gt;
&lt;br /&gt;
 class CanvasPainter : SquarePainter, CirclePainter, TrianglePainter&lt;br /&gt;
 {&lt;br /&gt;
     // ..&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
How can do the same in Java? Ofcourse, we will use composition, but how will you know which version of draw function to call? May be we can write some thing like this:&lt;br /&gt;
&lt;br /&gt;
 if (shape instanceof Triangle)&lt;br /&gt;
 {&lt;br /&gt;
       trianglePainter.draw((Triangle) shape);&lt;br /&gt;
 }&lt;br /&gt;
 else if(shape instanceof Circle)&lt;br /&gt;
 {&lt;br /&gt;
       circlePainter.draw((Circle) shape);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Unfortunately this is not a scalable solution and for every new tool we add to our paint program, we will have to change this code. This is where design patterns like [http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern Chain of Responsibility] proves helpful.&lt;br /&gt;
&lt;br /&gt;
Implementing the above code in Java using Chain of Responsibility design pattern.&lt;br /&gt;
&lt;br /&gt;
 interface IPainter&lt;br /&gt;
 {&lt;br /&gt;
     boolean canDraw(Shape s);&lt;br /&gt;
     void draw(Shape s);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
All painters will implement the above function:&lt;br /&gt;
&lt;br /&gt;
 class TrianglePainter implements IPainter&lt;br /&gt;
 {&lt;br /&gt;
    public boolean canDraw(Shape s) { return s instanceof Triangle; }&lt;br /&gt;
    public void draw(Shape s) { // ... }  &lt;br /&gt;
 } &lt;br /&gt;
&lt;br /&gt;
 class CanvasPainter implements IPainter&lt;br /&gt;
 {&lt;br /&gt;
    // List&amp;lt;IPainter&amp;gt; = ....&lt;br /&gt;
    // function to add and remove painters&lt;br /&gt;
    public void draw(Shape s)&lt;br /&gt;
    {&lt;br /&gt;
       for(Paint p : painters)&lt;br /&gt;
       { &lt;br /&gt;
            if(p.canDraw(s)) { p.draw(s); return; }&lt;br /&gt;
       }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
We can see that, chain of responsibility design pattern provides a very neat way to handle multiple inheritance in languages which don't support them completely.&lt;br /&gt;
&lt;br /&gt;
= Alternates to Multiple Inheritance =&lt;br /&gt;
* Composition &amp;amp; Interfaces&lt;br /&gt;
Compositions provides a neat way of emulating Multiple Inheritance. With Multiple Inheritance, we bring functionality from different classes to the class which extends them. We can achieve the same using Composition in which one class contains instance of other class, there by importing the capability of all those classes. Since a class is allowed to implement Multiple Interfaces, the class behaves as though it extended from Multiple classes.&lt;br /&gt;
&lt;br /&gt;
* Mixins&lt;br /&gt;
Some languages provide [http://en.wikipedia.org/wiki/Mixin mixins] as a language feature. Mixins provide another elegant mechanism which mimics Multiple Inheritance. The common problems with Mixin is that it pollutes the class namespace as it imports functions from different modules. This [http://www.artima.com/weblogs/viewpost.jsp?thread=246341 article] talks about the limitations of mixins in greater detail.&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
&amp;lt;table border = &amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;Single Inheritance&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;Multiple Inheritance&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Flexibility&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Single Inheritance does have some restriction in terms of modeling real world objects, but mitigate this problem with careful design&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Multiple Inheritance provides lots more flexibility for designing&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Easy of Use&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Single Inheritance is much simpler to use&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Multiple Inheritance sometimes leaves the programmer confused. One of the common problem with Multiple inheritance in Diamond Problem&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Availability&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Single Inheritance is supported by all the Object Oriented Programming Languages&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Multiple Inheritance is not supported by few of the OOP Languages, because the designer of the language thought it is hard to use and not necessary for common scenarios.&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Efficiency&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;This actually language specific. But generally it takes less time to figure which version of the function to call in a single inheritance&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;Virtual Function dispatch in C++ is more time consuming in Multiple Inheritance. For example Multiple Inheritance in C++, needs multiple vtable for dispatching virtual functions. [http://www.cs.ucsb.edu/~urs/oocsb/papers/oopsla96.pdf (1)] discusses about this problem in great detail.&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Further Reading =&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Virtual_inheritance Virtual Inheritance]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_composition Composition]&lt;br /&gt;
&lt;br /&gt;
= Reference =&lt;br /&gt;
*[http://www.cs.ucsb.edu/~urs/oocsb/papers/oopsla96.pdf (1) Cost of Virtual Functions C++]&lt;/div&gt;</summary>
		<author><name>Cgoogc</name></author>
	</entry>
</feed>