<?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=Rgovind2</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=Rgovind2"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Rgovind2"/>
	<updated>2026-07-20T12:00:55Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1a_1w16_br&amp;diff=65501</id>
		<title>CSC/ECE 517 Fall 2012/ch1a 1w16 br</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1a_1w16_br&amp;diff=65501"/>
		<updated>2012-09-21T23:51:08Z</updated>

		<summary type="html">&lt;p&gt;Rgovind2: /* Mixins */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Overview==&lt;br /&gt;
&lt;br /&gt;
Mixins are [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby] modules. A module is a grouping of objects under a single name. The objects may be constants, methods, classes, or other modules. Modules are defined in Ruby using the keyword module. They can be incorporated into a class with Ruby’s include statement. This is called a mixin.&lt;br /&gt;
Modules are for sharing behavior (methods), while classes are for modeling relationship between objects. Ruby classes can Mixin a module and receive all its methods for free.&lt;br /&gt;
An Interface in [http://en.wikipedia.org/wiki/Java_(programming_language) Java] programming language is an abstract type that classes must implement. Interfaces are declared using the interface keyword, and may only contain method signature and constant declarations.&lt;br /&gt;
A class that implements an interface should implement all of the methods described in the interface, or be an [http://en.wikipedia.org/wiki/Abstract_type abstract] class.&lt;br /&gt;
&lt;br /&gt;
==Definitions==&lt;br /&gt;
===Mixins===&lt;br /&gt;
	Modules provide a structure to collect classes, methods, and constants into a single, separately defined unit. They provide the simplest and an elegant way to adhere to the Don't repeat yourself&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Don't_repeat_yourself Don't_repeat_yourself]&amp;lt;/ref&amp;gt; principle. This is useful so as to avoid clashes with existing classes, methods, and constants.&lt;br /&gt;
Modules are defined in Ruby using the module keyword.&lt;br /&gt;
A Mixin is a class that is mixed with a [http://www.tutorialspoint.com/ruby/ruby_modules.htm module] or a set of modules i.e. the implementation of the modules and the class are intertwined and combined together &amp;lt;ref&amp;gt;[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html Mixins] &amp;lt;/ref&amp;gt;.&lt;br /&gt;
This gives us a neat and controlled way of adding new functionality to classes. [http://blog.rubybestpractices.com/posts/gregory/037-issue-8-uses-for-modules.html Modules] are similar to classes in that they hold a collection of methods, variables constants and other modules and class definitions. However, the real usage of a Mixin is exploited when the code in the Mixin starts to interact with code in the class that uses it.&lt;br /&gt;
Below is an example demonstrating usage of modules and Mixins:&lt;br /&gt;
The module ParkingSlot consists of the methods park and unpark&lt;br /&gt;
 &lt;br /&gt;
 module ParkingSlot&lt;br /&gt;
       def park&lt;br /&gt;
          #code to park the car&lt;br /&gt;
       end&lt;br /&gt;
       def unpark&lt;br /&gt;
          #code to unpark the car&lt;br /&gt;
       end&lt;br /&gt;
 end&lt;br /&gt;
The module ParkingMeter consists of the methods calculate and printreceipt&lt;br /&gt;
&lt;br /&gt;
 module ParkingMeter&lt;br /&gt;
       def calculate&lt;br /&gt;
          #code to calculate the parking fee&lt;br /&gt;
       end&lt;br /&gt;
       def printreceipt&lt;br /&gt;
          #code to print the receipt&lt;br /&gt;
       end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
When a class includes a module via include Module Name, all the methods on that module become instance methods on the class.&lt;br /&gt;
&lt;br /&gt;
 class Parking&lt;br /&gt;
      include ParkingSlot&lt;br /&gt;
      include ParkingMeter&lt;br /&gt;
      def printcardetails&lt;br /&gt;
          #code to print the details and type of car&lt;br /&gt;
      end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
The modules ParkingSlot and ParkingMeter are “included” in the Parking class. All the variables and the instance methods of the modules ParkingSlot and ParkingMeter are now said to be “mixed in” into the class. It is now possible for class instances to use these methods defined in the modules as and when required.&lt;br /&gt;
 p = Parking.new   &lt;br /&gt;
 p.park            # -&amp;gt; Method park from module ParkingSlot&lt;br /&gt;
 p.unpark          # -&amp;gt; Method unpark from module ParkingSlot&lt;br /&gt;
 p.calculate       # -&amp;gt; Method calculate from module ParkingMeter&lt;br /&gt;
 p.printreceipt    # -&amp;gt; Method printreceipt from module ParkingMeter&lt;br /&gt;
 p.printcardetails # -&amp;gt; Calls printCarDetails from class&lt;br /&gt;
The class Parking inherits from both the modules and the module methods are now available in Parking. Hence, it is now possible to use the methods with an instance of the Parking class p.&lt;br /&gt;
Hence, Mixins can be thought of taking different methods and variables defined in different modules making them available as instance methods in the class as well, thereby extending the class’ functionality. Effectively mixed in modules behave as superclass. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:ParkingLot.PNG| center]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Interfaces===&lt;br /&gt;
In object-oriented programming languages such as Java, C#, an interface&amp;lt;ref&amp;gt;[http://download.oracle.com/javase/tutorial/ Interfaces] &amp;lt;/ref&amp;gt; is a reference type, similar to a class that can contain only constants, method signatures, and nested types. There are no method bodies. [http://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html Interfaces] cannot be instantiated and they can only be implemented by classes or extended by other interfaces. All methods are public by default and any fields declared in an interface are by default static and final.&lt;br /&gt;
Example:&lt;br /&gt;
 public interface ParkingLot {&lt;br /&gt;
       boolean add(Car car, int slot);&lt;br /&gt;
       boolean remove(Car car);&lt;br /&gt;
       boolean findCar(Int number);&lt;br /&gt;
       int freeSlotsCount();&lt;br /&gt;
       int occupiedSlots();&lt;br /&gt;
       int parkingType();&lt;br /&gt;
       int calculateParkingFee(int hours, int charge, int duration);&lt;br /&gt;
       int findParkingSlot();&lt;br /&gt;
 }&lt;br /&gt;
 public class Mall implements ParkingLot {&lt;br /&gt;
       boolean add(Car car, int slot) { //... Code to add a car into the parking lot... }&lt;br /&gt;
       boolean remove(Car car){ // do something }&lt;br /&gt;
       boolean findCar(Int number){ // do something }&lt;br /&gt;
       int freeSlotsCount(){ // do something }&lt;br /&gt;
       int occupiedSlots(){ // do something }&lt;br /&gt;
       int parkingType(){ // do something }&lt;br /&gt;
       int calculateParkingFee(int hours, int charge, int duration){ // do something }&lt;br /&gt;
       int findParkingSlot(){ // do something }&lt;br /&gt;
 }&lt;br /&gt;
Interfaces play another important role in the object-oriented programming language. Interfaces are not part of the class hierarchy, although they work in combination with classes.&lt;br /&gt;
&lt;br /&gt;
==Mixins vs. Interfaces==&lt;br /&gt;
===Similarities and Differences===&lt;br /&gt;
	Mixins and Interfaces can be considered as a block of code having some variables and methods that can be used by a class. The difference between them is that a Mixin can contain method definitions whereas an Interface cannot.&lt;br /&gt;
&lt;br /&gt;
====Multiple method definition====&lt;br /&gt;
	In a class, when more than one module is mixed in where some modules have overloaded methods, the method from the module that was last mixed in into the class will be executed.&lt;br /&gt;
 module Frog&lt;br /&gt;
    def sound()&lt;br /&gt;
      puts &amp;quot;croak...&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Dinosaur&lt;br /&gt;
   def sound()&lt;br /&gt;
     puts &amp;quot;roar...&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
       include Frog&lt;br /&gt;
       include Dinosaur&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 Frogosaur.new.sound &amp;lt;br&amp;gt;	&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 roar...&amp;lt;br&amp;gt;&lt;br /&gt;
This is because the class Frogosaur can contain only one definition for each method. The users have the flexibility to make use of methods from different modules by rearranging the order of include.&lt;br /&gt;
Interfaces, on the other hand, do not offer the flexibility to have different implementation for a same method. This is because the class implementing the interface can provide only one implementation for each method. Hence they offer only one method definition for the user.&lt;br /&gt;
 public interface Frog {&lt;br /&gt;
     void sound();&lt;br /&gt;
 }&lt;br /&gt;
 public interface Dinasour {&lt;br /&gt;
     void sound();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
 public void sound()&lt;br /&gt;
 {&lt;br /&gt;
     System.out.println(&amp;quot;barks......&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public static void main(String args[])&lt;br /&gt;
 {&lt;br /&gt;
     new Frogosaur().sound();;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 barks...&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Multiple object behavior====&lt;br /&gt;
In Ruby, a class’s instance can extend a module and exhibit different behavior than the other instances of the same class.&lt;br /&gt;
&lt;br /&gt;
 module Frog&lt;br /&gt;
  def sound()&lt;br /&gt;
     puts &amp;quot;croak...&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Dinasour&lt;br /&gt;
  def sound()&lt;br /&gt;
    puts &amp;quot;roar...&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
  def sound()&lt;br /&gt;
    puts &amp;quot;barks...&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 beast1= Frogosaur.new&lt;br /&gt;
 beast2= Frogosaur.new&lt;br /&gt;
 beast1.extend(Frog)&lt;br /&gt;
 beast1.sound&lt;br /&gt;
 beast2.sound&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 croak...&amp;lt;br&amp;gt;&lt;br /&gt;
 barks...&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Interfaces are implemented by a class and all instances of the class behave in the same way. They can only use the methods of the class to which they belong to.&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
  public void sound()&lt;br /&gt;
  {&lt;br /&gt;
      System.out.println(&amp;quot;Sounds...&amp;quot;);&lt;br /&gt;
  }  &lt;br /&gt;
  public static void main(String args[])&lt;br /&gt;
  {&lt;br /&gt;
      Frogosaur beast1 = new Frogosaur();&lt;br /&gt;
      Frogosaur beast2 = new Frogosaur();&lt;br /&gt;
      object1.sound();&lt;br /&gt;
      object2.sound();&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 Sounds...&amp;lt;br&amp;gt;&lt;br /&gt;
 Sounds...&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Enumerable – module and interface====&lt;br /&gt;
&lt;br /&gt;
Ruby’s Arrays have several methods defined in them. To use them, the classes need not be a subclass of Array. The classes can include Enumerable &amp;lt;ref&amp;gt;[http://mattenoble.com/2011/05/30/ruby-enumerables/ Enumerables]&amp;lt;/ref&amp;gt; and define the each method to use the array’s methods for free.&lt;br /&gt;
&lt;br /&gt;
 class EnumExer&lt;br /&gt;
  include Enumerable&lt;br /&gt;
  def initialize(*arrays)&lt;br /&gt;
    @arrays = arrays&lt;br /&gt;
  end&lt;br /&gt;
  def each&lt;br /&gt;
    @arrays.each { |a| a.each { |x| yield x } }&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 ma = EnumExer.new([4, 1], [3], [2])&lt;br /&gt;
 puts ma.sort&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 1&amp;lt;br&amp;gt;&lt;br /&gt;
 2&amp;lt;br&amp;gt;&lt;br /&gt;
 3&amp;lt;br&amp;gt;&lt;br /&gt;
 4&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can observe that although the arguments passed to the ma instance did not have a similar structure, the sort method was able to identify each entry and sort them. This is because the each method was defined to identify and serve all the entries to the sort method.&lt;br /&gt;
The methods that are defined for Enumerable modules are:&lt;br /&gt;
 [&amp;quot;all?&amp;quot;, &amp;quot;any?&amp;quot;, &amp;quot;collect&amp;quot;, &amp;quot;detect&amp;quot;, &amp;quot;each_with_index&amp;quot;, &amp;quot;entries&amp;quot;,&amp;quot;find&amp;quot;, &amp;quot;find_all&amp;quot;, &amp;quot;grep&amp;quot;, &amp;quot;include?&amp;quot;, &amp;quot;inject&amp;quot;,&lt;br /&gt;
 &amp;quot;map&amp;quot;, &amp;quot;max&amp;quot;,&amp;quot;member?&amp;quot;, &amp;quot;min&amp;quot;, &amp;quot;partition&amp;quot;, &amp;quot;reject&amp;quot;, &amp;quot;select&amp;quot;, &amp;quot;sort&amp;quot;, &amp;quot;sort_by&amp;quot;,&amp;quot;to_a&amp;quot;, &amp;quot;zip&amp;quot;]&lt;br /&gt;
In Java, to use the methods of Enumerable interface, the class should be a subclass of Array.&lt;br /&gt;
&lt;br /&gt;
 public class EnumExer {&lt;br /&gt;
     static String content_string = &amp;quot;zyxwvutsrqponmlkjihgfedcba&amp;quot;;&lt;br /&gt;
     String a=&amp;quot;1&amp;quot;, b=&amp;quot;2&amp;quot;;&lt;br /&gt;
     public static void main(String args[])&lt;br /&gt;
     {&lt;br /&gt;
         char[] content_array = content_string.toCharArray();&lt;br /&gt;
         java.util.Arrays.sort(content_array);&lt;br /&gt;
         content_string=new String(content_array);&lt;br /&gt;
         System.out.println(content_string);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 Abcdefghijklmnopqrstuvwxyz&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, the string object was converted to a character array to be able to use with the sort method.&lt;br /&gt;
&lt;br /&gt;
====Inheritance====&lt;br /&gt;
Java interfaces can inherit other interfaces whereas Ruby modules cannot although they can contain classes and methods.&lt;br /&gt;
 module Wrapper&lt;br /&gt;
  class Base&lt;br /&gt;
     # Class Body&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 public interface Dinasour extends Frog{&lt;br /&gt;
     //variables and method Declarations&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
==== Achieving Multiple Inheritance ====&lt;br /&gt;
Modules and Interfaces help acheive multiple inheritance.  &amp;lt;br&amp;gt;&lt;br /&gt;
Interfaces are used in Java to achieve multiple inheritance&amp;lt;ref&amp;gt; [http://csis.pace.edu/~bergin/patterns/multipleinheritance.html  Mulitple Inheritance] &amp;lt;/ref&amp;gt;. Similarly, modules in Ruby help achieve multiple inheritance.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Ruby,&lt;br /&gt;
&lt;br /&gt;
 module Frog&lt;br /&gt;
   def jump()&lt;br /&gt;
     //Method definition&lt;br /&gt;
    end&lt;br /&gt;
 end &amp;lt;br&amp;gt;&lt;br /&gt;
 module Dinosaur&lt;br /&gt;
  def strength()&lt;br /&gt;
    //Method definition&lt;br /&gt;
  end&lt;br /&gt;
 end &amp;lt;br&amp;gt;&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
      include Frog&lt;br /&gt;
      include Dinosaur&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
In Java,&lt;br /&gt;
 public interface Frog {&lt;br /&gt;
     void jump();&lt;br /&gt;
 } &amp;lt;br&amp;gt;&lt;br /&gt;
 public interface Dinasour {&lt;br /&gt;
     void strength();&lt;br /&gt;
 } &amp;lt;br&amp;gt;&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
  //Method Implementations&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Frogosaur inherits the methods of both frog and dinosaur.&lt;br /&gt;
&lt;br /&gt;
===Comparison===&lt;br /&gt;
{|border=&amp;quot;2&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Mixins&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Interfaces&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|The number of lines of code is lesser since the &amp;lt;br&amp;gt;&lt;br /&gt;
modules contain method definitions. &lt;br /&gt;
|The number of lines of code is bigger since the &amp;lt;br&amp;gt;&lt;br /&gt;
classes have to define the methods.&lt;br /&gt;
|-&lt;br /&gt;
|More modules in a class makes code less readable.&lt;br /&gt;
|The code is more readable since the class contain the &amp;lt;br&amp;gt;&lt;br /&gt;
method definition.&lt;br /&gt;
|-&lt;br /&gt;
|Modules cannot be inherited and cannot form is-a hierarchy.&lt;br /&gt;
|Interfaces can extend another interface and hence can form &amp;lt;br&amp;gt;&lt;br /&gt;
is-a hierarchy.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Drawbacks===&lt;br /&gt;
The drawbacks of the technique of Mixins are very much debated. Though Mixins provide us with an easy way to write flexible and decoupled code they pose their own problems. In large programs there could be large number of modules and each module could have loads of methods each performing a certain task. To trace the origin of the methods and to remember the hierarchy is difficult. Mixins suffer from silent method overriding. Silent method overriding refers to a condition when different modules are included in a class and each module have a method with the same definition. In such a case, the method which gets executed depends upon the way it is included in the class and this is done without any sort of message to the user (silently). In large scale applications this could certainly be an issue considering the number of modules that would be mixed in and it would be difficult to keep track of the hierarchy in which the modules were included.&lt;br /&gt;
Although Java provides polymorphic behavior with the use of interfaces, they sometimes tend to be very slow. The implementation of interfaces is also limited to public methods and constants with no implementation.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Modules and Interfaces are similar in the fact that they enable additional features in object-oriented programming languages. They are dissimilar in the way they are implemented but add more flexibility to object-oriented programming and are easy to maintain.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
1. http://ruby.about.com/od/beginningruby/a/mixin.htm/ &amp;lt;br&amp;gt;&lt;br /&gt;
2. http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/ &amp;lt;br&amp;gt;&lt;br /&gt;
3. http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-frameworks/ &amp;lt;br&amp;gt;&lt;br /&gt;
4. http://pg-server.csc.ncsu.edu/mediawiki/index.php?title=CSC/ECE_517_Fall_2010/ch3_3b_sv&amp;amp;printable=yes &amp;lt;br&amp;gt;&lt;br /&gt;
5. http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2c_ds &amp;lt;br&amp;gt;&lt;br /&gt;
6. Lucas Carlson; Leonard Richardson (July 2006), [http://shop.oreilly.com/product/9780596523695.do Ruby Cookbook], O'Reilly Media,  ISBN 10:0-596-52369-6&lt;/div&gt;</summary>
		<author><name>Rgovind2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1a_1w16_br&amp;diff=65500</id>
		<title>CSC/ECE 517 Fall 2012/ch1a 1w16 br</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1a_1w16_br&amp;diff=65500"/>
		<updated>2012-09-21T23:45:35Z</updated>

		<summary type="html">&lt;p&gt;Rgovind2: /* Overview */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Overview==&lt;br /&gt;
&lt;br /&gt;
Mixins are [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby] modules. A module is a grouping of objects under a single name. The objects may be constants, methods, classes, or other modules. Modules are defined in Ruby using the keyword module. They can be incorporated into a class with Ruby’s include statement. This is called a mixin.&lt;br /&gt;
Modules are for sharing behavior (methods), while classes are for modeling relationship between objects. Ruby classes can Mixin a module and receive all its methods for free.&lt;br /&gt;
An Interface in [http://en.wikipedia.org/wiki/Java_(programming_language) Java] programming language is an abstract type that classes must implement. Interfaces are declared using the interface keyword, and may only contain method signature and constant declarations.&lt;br /&gt;
A class that implements an interface should implement all of the methods described in the interface, or be an [http://en.wikipedia.org/wiki/Abstract_type abstract] class.&lt;br /&gt;
&lt;br /&gt;
==Definitions==&lt;br /&gt;
===Mixins===&lt;br /&gt;
	Modules provide a structure to collect classes, methods, and constants into a single, separately defined unit. They provide the simplest and an elegant way to adhere to the Don't repeat yourself&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Don't_repeat_yourself Don't_repeat_yourself]&amp;lt;/ref&amp;gt; principle. This is useful so as to avoid clashes with existing classes, methods, and constants.&lt;br /&gt;
Modules are defined in Ruby using the module keyword.&lt;br /&gt;
A Mixin is a class that is mixed with a [http://www.tutorialspoint.com/ruby/ruby_modules.htm module] or a set of modules i.e. the implementation of the modules and the class are intertwined and combined together &amp;lt;ref&amp;gt;[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html Mixins] &amp;lt;/ref&amp;gt;.&lt;br /&gt;
This gives us a neat and controlled way of adding new functionality to classes. [http://blog.rubybestpractices.com/posts/gregory/037-issue-8-uses-for-modules.html Modules] are similar to classes in that they hold a collection of methods, variables constants and other modules and class definitions. However, the real usage of a Mixin is exploited when the code in the Mixin starts to interact with code in the class that uses it.&lt;br /&gt;
Below is an example for modules and Mixins:&lt;br /&gt;
The module ParkingSlot consists of the methods park and unpark&lt;br /&gt;
 &lt;br /&gt;
 module ParkingSlot&lt;br /&gt;
       def park&lt;br /&gt;
          #code to park the car&lt;br /&gt;
       end&lt;br /&gt;
       def unpark&lt;br /&gt;
          #code to unpark the car&lt;br /&gt;
       end&lt;br /&gt;
 end&lt;br /&gt;
The module ParkingMeter consists of the methods calculate and printreceipt&lt;br /&gt;
&lt;br /&gt;
 module ParkingMeter&lt;br /&gt;
       def calculate&lt;br /&gt;
          #code to calculate the parking fee&lt;br /&gt;
       end&lt;br /&gt;
       def printreceipt&lt;br /&gt;
          #code to print the receipt&lt;br /&gt;
       end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
When a class includes a module via include Module Name, all the methods on that module become instance methods on the class.&lt;br /&gt;
&lt;br /&gt;
 class Parking&lt;br /&gt;
      include ParkingSlot&lt;br /&gt;
      include ParkingMeter&lt;br /&gt;
      def printcardetails&lt;br /&gt;
          #code to print the details and type of car&lt;br /&gt;
      end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
The modules ParkingSlot and ParkingMeter are “included” in the Parking class. All the variables and the instance methods of the modules ParkingSlot and ParkingMeter are now said to be “mixed in” into the class. It is now possible for class instances to use these methods defined in the modules as and when required.&lt;br /&gt;
 p=Parking.new   &lt;br /&gt;
 p.park            # -&amp;gt; Method park from module ParkingSlot&lt;br /&gt;
 p.unpark          # -&amp;gt; Method unpark from module ParkingSlot&lt;br /&gt;
 p.calculate       # -&amp;gt; Method calculate from module ParkingMeter&lt;br /&gt;
 p.printreceipt    # -&amp;gt; Method printreceipt from module ParkingMeter&lt;br /&gt;
 p.printcardetails # -&amp;gt; Calls printCarDetails from class&lt;br /&gt;
The class Parking inherits from both the modules and the module methods are now available in Parking. Hence, it is now possible to use the methods with an instance of the Parking class p.&lt;br /&gt;
Hence, Mixins can be thought of taking different methods and variables defined in different modules making them available as instance methods in the class as well, thereby extending the class’ functionality. Effectively mixed in modules behave as superclass. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:ParkingLot.PNG| center]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Interfaces===&lt;br /&gt;
In object-oriented programming languages such as Java, C#, an interface&amp;lt;ref&amp;gt;[http://download.oracle.com/javase/tutorial/ Interfaces] &amp;lt;/ref&amp;gt; is a reference type, similar to a class that can contain only constants, method signatures, and nested types. There are no method bodies. [http://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html Interfaces] cannot be instantiated and they can only be implemented by classes or extended by other interfaces. All methods are public by default and any fields declared in an interface are by default static and final.&lt;br /&gt;
Example:&lt;br /&gt;
 public interface ParkingLot {&lt;br /&gt;
       boolean add(Car car, int slot);&lt;br /&gt;
       boolean remove(Car car);&lt;br /&gt;
       boolean findCar(Int number);&lt;br /&gt;
       int freeSlotsCount();&lt;br /&gt;
       int occupiedSlots();&lt;br /&gt;
       int parkingType();&lt;br /&gt;
       int calculateParkingFee(int hours, int charge, int duration);&lt;br /&gt;
       int findParkingSlot();&lt;br /&gt;
 }&lt;br /&gt;
 public class Mall implements ParkingLot {&lt;br /&gt;
       boolean add(Car car, int slot) { //... Code to add a car into the parking lot... }&lt;br /&gt;
       boolean remove(Car car){ // do something }&lt;br /&gt;
       boolean findCar(Int number){ // do something }&lt;br /&gt;
       int freeSlotsCount(){ // do something }&lt;br /&gt;
       int occupiedSlots(){ // do something }&lt;br /&gt;
       int parkingType(){ // do something }&lt;br /&gt;
       int calculateParkingFee(int hours, int charge, int duration){ // do something }&lt;br /&gt;
       int findParkingSlot(){ // do something }&lt;br /&gt;
 }&lt;br /&gt;
Interfaces play another important role in the object-oriented programming language. Interfaces are not part of the class hierarchy, although they work in combination with classes.&lt;br /&gt;
&lt;br /&gt;
==Mixins vs. Interfaces==&lt;br /&gt;
===Similarities and Differences===&lt;br /&gt;
	Mixins and Interfaces can be considered as a block of code having some variables and methods that can be used by a class. The difference between them is that a Mixin can contain method definitions whereas an Interface cannot.&lt;br /&gt;
&lt;br /&gt;
====Multiple method definition====&lt;br /&gt;
	In a class, when more than one module is mixed in where some modules have overloaded methods, the method from the module that was last mixed in into the class will be executed.&lt;br /&gt;
 module Frog&lt;br /&gt;
    def sound()&lt;br /&gt;
      puts &amp;quot;croak...&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Dinosaur&lt;br /&gt;
   def sound()&lt;br /&gt;
     puts &amp;quot;roar...&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
       include Frog&lt;br /&gt;
       include Dinosaur&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 Frogosaur.new.sound &amp;lt;br&amp;gt;	&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 roar...&amp;lt;br&amp;gt;&lt;br /&gt;
This is because the class Frogosaur can contain only one definition for each method. The users have the flexibility to make use of methods from different modules by rearranging the order of include.&lt;br /&gt;
Interfaces, on the other hand, do not offer the flexibility to have different implementation for a same method. This is because the class implementing the interface can provide only one implementation for each method. Hence they offer only one method definition for the user.&lt;br /&gt;
 public interface Frog {&lt;br /&gt;
     void sound();&lt;br /&gt;
 }&lt;br /&gt;
 public interface Dinasour {&lt;br /&gt;
     void sound();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
 public void sound()&lt;br /&gt;
 {&lt;br /&gt;
     System.out.println(&amp;quot;barks......&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public static void main(String args[])&lt;br /&gt;
 {&lt;br /&gt;
     new Frogosaur().sound();;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 barks...&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Multiple object behavior====&lt;br /&gt;
In Ruby, a class’s instance can extend a module and exhibit different behavior than the other instances of the same class.&lt;br /&gt;
&lt;br /&gt;
 module Frog&lt;br /&gt;
  def sound()&lt;br /&gt;
     puts &amp;quot;croak...&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Dinasour&lt;br /&gt;
  def sound()&lt;br /&gt;
    puts &amp;quot;roar...&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
  def sound()&lt;br /&gt;
    puts &amp;quot;barks...&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 beast1= Frogosaur.new&lt;br /&gt;
 beast2= Frogosaur.new&lt;br /&gt;
 beast1.extend(Frog)&lt;br /&gt;
 beast1.sound&lt;br /&gt;
 beast2.sound&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 croak...&amp;lt;br&amp;gt;&lt;br /&gt;
 barks...&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Interfaces are implemented by a class and all instances of the class behave in the same way. They can only use the methods of the class to which they belong to.&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
  public void sound()&lt;br /&gt;
  {&lt;br /&gt;
      System.out.println(&amp;quot;Sounds...&amp;quot;);&lt;br /&gt;
  }  &lt;br /&gt;
  public static void main(String args[])&lt;br /&gt;
  {&lt;br /&gt;
      Frogosaur beast1 = new Frogosaur();&lt;br /&gt;
      Frogosaur beast2 = new Frogosaur();&lt;br /&gt;
      object1.sound();&lt;br /&gt;
      object2.sound();&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 Sounds...&amp;lt;br&amp;gt;&lt;br /&gt;
 Sounds...&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Enumerable – module and interface====&lt;br /&gt;
&lt;br /&gt;
Ruby’s Arrays have several methods defined in them. To use them, the classes need not be a subclass of Array. The classes can include Enumerable &amp;lt;ref&amp;gt;[http://mattenoble.com/2011/05/30/ruby-enumerables/ Enumerables]&amp;lt;/ref&amp;gt; and define the each method to use the array’s methods for free.&lt;br /&gt;
&lt;br /&gt;
 class EnumExer&lt;br /&gt;
  include Enumerable&lt;br /&gt;
  def initialize(*arrays)&lt;br /&gt;
    @arrays = arrays&lt;br /&gt;
  end&lt;br /&gt;
  def each&lt;br /&gt;
    @arrays.each { |a| a.each { |x| yield x } }&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 ma = EnumExer.new([4, 1], [3], [2])&lt;br /&gt;
 puts ma.sort&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 1&amp;lt;br&amp;gt;&lt;br /&gt;
 2&amp;lt;br&amp;gt;&lt;br /&gt;
 3&amp;lt;br&amp;gt;&lt;br /&gt;
 4&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can observe that although the arguments passed to the ma instance did not have a similar structure, the sort method was able to identify each entry and sort them. This is because the each method was defined to identify and serve all the entries to the sort method.&lt;br /&gt;
The methods that are defined for Enumerable modules are:&lt;br /&gt;
 [&amp;quot;all?&amp;quot;, &amp;quot;any?&amp;quot;, &amp;quot;collect&amp;quot;, &amp;quot;detect&amp;quot;, &amp;quot;each_with_index&amp;quot;, &amp;quot;entries&amp;quot;,&amp;quot;find&amp;quot;, &amp;quot;find_all&amp;quot;, &amp;quot;grep&amp;quot;, &amp;quot;include?&amp;quot;, &amp;quot;inject&amp;quot;,&lt;br /&gt;
 &amp;quot;map&amp;quot;, &amp;quot;max&amp;quot;,&amp;quot;member?&amp;quot;, &amp;quot;min&amp;quot;, &amp;quot;partition&amp;quot;, &amp;quot;reject&amp;quot;, &amp;quot;select&amp;quot;, &amp;quot;sort&amp;quot;, &amp;quot;sort_by&amp;quot;,&amp;quot;to_a&amp;quot;, &amp;quot;zip&amp;quot;]&lt;br /&gt;
In Java, to use the methods of Enumerable interface, the class should be a subclass of Array.&lt;br /&gt;
&lt;br /&gt;
 public class EnumExer {&lt;br /&gt;
     static String content_string = &amp;quot;zyxwvutsrqponmlkjihgfedcba&amp;quot;;&lt;br /&gt;
     String a=&amp;quot;1&amp;quot;, b=&amp;quot;2&amp;quot;;&lt;br /&gt;
     public static void main(String args[])&lt;br /&gt;
     {&lt;br /&gt;
         char[] content_array = content_string.toCharArray();&lt;br /&gt;
         java.util.Arrays.sort(content_array);&lt;br /&gt;
         content_string=new String(content_array);&lt;br /&gt;
         System.out.println(content_string);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 Abcdefghijklmnopqrstuvwxyz&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, the string object was converted to a character array to be able to use with the sort method.&lt;br /&gt;
&lt;br /&gt;
====Inheritance====&lt;br /&gt;
Java interfaces can inherit other interfaces whereas Ruby modules cannot although they can contain classes and methods.&lt;br /&gt;
 module Wrapper&lt;br /&gt;
  class Base&lt;br /&gt;
     # Class Body&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 public interface Dinasour extends Frog{&lt;br /&gt;
     //variables and method Declarations&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
==== Achieving Multiple Inheritance ====&lt;br /&gt;
Modules and Interfaces help acheive multiple inheritance.  &amp;lt;br&amp;gt;&lt;br /&gt;
Interfaces are used in Java to achieve multiple inheritance&amp;lt;ref&amp;gt; [http://csis.pace.edu/~bergin/patterns/multipleinheritance.html  Mulitple Inheritance] &amp;lt;/ref&amp;gt;. Similarly, modules in Ruby help achieve multiple inheritance.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Ruby,&lt;br /&gt;
&lt;br /&gt;
 module Frog&lt;br /&gt;
   def jump()&lt;br /&gt;
     //Method definition&lt;br /&gt;
    end&lt;br /&gt;
 end &amp;lt;br&amp;gt;&lt;br /&gt;
 module Dinosaur&lt;br /&gt;
  def strength()&lt;br /&gt;
    //Method definition&lt;br /&gt;
  end&lt;br /&gt;
 end &amp;lt;br&amp;gt;&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
      include Frog&lt;br /&gt;
      include Dinosaur&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
In Java,&lt;br /&gt;
 public interface Frog {&lt;br /&gt;
     void jump();&lt;br /&gt;
 } &amp;lt;br&amp;gt;&lt;br /&gt;
 public interface Dinasour {&lt;br /&gt;
     void strength();&lt;br /&gt;
 } &amp;lt;br&amp;gt;&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
  //Method Implementations&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Frogosaur inherits the methods of both frog and dinosaur.&lt;br /&gt;
&lt;br /&gt;
===Comparison===&lt;br /&gt;
{|border=&amp;quot;2&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Mixins&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Interfaces&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|The number of lines of code is lesser since the &amp;lt;br&amp;gt;&lt;br /&gt;
modules contain method definitions. &lt;br /&gt;
|The number of lines of code is bigger since the &amp;lt;br&amp;gt;&lt;br /&gt;
classes have to define the methods.&lt;br /&gt;
|-&lt;br /&gt;
|More modules in a class makes code less readable.&lt;br /&gt;
|The code is more readable since the class contain the &amp;lt;br&amp;gt;&lt;br /&gt;
method definition.&lt;br /&gt;
|-&lt;br /&gt;
|Modules cannot be inherited and cannot form is-a hierarchy.&lt;br /&gt;
|Interfaces can extend another interface and hence can form &amp;lt;br&amp;gt;&lt;br /&gt;
is-a hierarchy.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Drawbacks===&lt;br /&gt;
The drawbacks of the technique of Mixins are very much debated. Though Mixins provide us with an easy way to write flexible and decoupled code they pose their own problems. In large programs there could be large number of modules and each module could have loads of methods each performing a certain task. To trace the origin of the methods and to remember the hierarchy is difficult. Mixins suffer from silent method overriding. Silent method overriding refers to a condition when different modules are included in a class and each module have a method with the same definition. In such a case, the method which gets executed depends upon the way it is included in the class and this is done without any sort of message to the user (silently). In large scale applications this could certainly be an issue considering the number of modules that would be mixed in and it would be difficult to keep track of the hierarchy in which the modules were included.&lt;br /&gt;
Although Java provides polymorphic behavior with the use of interfaces, they sometimes tend to be very slow. The implementation of interfaces is also limited to public methods and constants with no implementation.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Modules and Interfaces are similar in the fact that they enable additional features in object-oriented programming languages. They are dissimilar in the way they are implemented but add more flexibility to object-oriented programming and are easy to maintain.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
1. http://ruby.about.com/od/beginningruby/a/mixin.htm/ &amp;lt;br&amp;gt;&lt;br /&gt;
2. http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/ &amp;lt;br&amp;gt;&lt;br /&gt;
3. http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-frameworks/ &amp;lt;br&amp;gt;&lt;br /&gt;
4. http://pg-server.csc.ncsu.edu/mediawiki/index.php?title=CSC/ECE_517_Fall_2010/ch3_3b_sv&amp;amp;printable=yes &amp;lt;br&amp;gt;&lt;br /&gt;
5. http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2c_ds &amp;lt;br&amp;gt;&lt;br /&gt;
6. Lucas Carlson; Leonard Richardson (July 2006), [http://shop.oreilly.com/product/9780596523695.do Ruby Cookbook], O'Reilly Media,  ISBN 10:0-596-52369-6&lt;/div&gt;</summary>
		<author><name>Rgovind2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1a_1w16_br&amp;diff=63751</id>
		<title>CSC/ECE 517 Fall 2012/ch1a 1w16 br</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1a_1w16_br&amp;diff=63751"/>
		<updated>2012-09-12T22:21:02Z</updated>

		<summary type="html">&lt;p&gt;Rgovind2: /* Enumerable – module and interface */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Overview==&lt;br /&gt;
&lt;br /&gt;
Mixins are Ruby modules. A module is a grouping of objects under a single name. The objects may be constants, methods, classes, or other modules. Modules are defined in Ruby using the keyword module. They can be incorporated into a class with Ruby’s include statement. This is called a mixin.&lt;br /&gt;
Modules are for sharing behavior (methods), while classes are for modeling relationship between objects. Ruby classes can Mixin a module and receive all its methods for free.&lt;br /&gt;
An Interface in Java programming language is an abstract type that classes must implement. Interfaces are declared using the interface keyword, and may only contain method signature and constant declarations.&lt;br /&gt;
A class that implements an interface must implement all of the methods described in the interface, or be an abstract class.&lt;br /&gt;
&lt;br /&gt;
==Definitions==&lt;br /&gt;
===Mixins===&lt;br /&gt;
	Modules provide a structure to collect classes, methods, and constants into a single, separately defined unit. They provide the simplest and an elegant way to adhere to the Don't repeat yourself&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Don't_repeat_yourself Don't_repeat_yourself]&amp;lt;/ref&amp;gt; principle. This is useful so as to avoid clashes with existing classes, methods, and constants.&lt;br /&gt;
Modules are defined in Ruby using the module keyword.&lt;br /&gt;
A Mixin is a class that is mixed with a module or a set of modules i.e. the implementation of the modules and the class are intertwined and combined together &amp;lt;ref&amp;gt;[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html Mixins] &amp;lt;/ref&amp;gt;.&lt;br /&gt;
This gives us a neat and controlled way of adding new functionality to classes. Modules are similar to classes in that they hold a collection of methods, variables constants and other modules and class definitions. However, the real usage of a Mixin is exploited when the code in the Mixin starts to interact with code in the class that uses it.&lt;br /&gt;
Below is an example for modules and Mixins:&lt;br /&gt;
The module ParkingSlot consists of the methods park and unpark&lt;br /&gt;
 &lt;br /&gt;
 module ParkingSlot&lt;br /&gt;
       def park&lt;br /&gt;
          #code to park the car&lt;br /&gt;
       end&lt;br /&gt;
       def unpark&lt;br /&gt;
          #code to unpark the car&lt;br /&gt;
       end&lt;br /&gt;
 end&lt;br /&gt;
The module ParkingMeter consists of the methods calculate and printreceipt&lt;br /&gt;
&lt;br /&gt;
 module ParkingMeter&lt;br /&gt;
       def calculate&lt;br /&gt;
          #code to calculate the parking fee&lt;br /&gt;
       end&lt;br /&gt;
       def printreceipt&lt;br /&gt;
          #code to print the receipt&lt;br /&gt;
       end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
When a class includes a module via include Module Name, all the methods on that module become instance methods on the class.&lt;br /&gt;
&lt;br /&gt;
 class Parking&lt;br /&gt;
      include ParkingSlot&lt;br /&gt;
      include ParkingMeter&lt;br /&gt;
      def printcardetails&lt;br /&gt;
          #code to print the details and type of car&lt;br /&gt;
      end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
The modules ParkingSlot and ParkingMeter are “included” in the Parking class. All the variables and the instance methods of the modules ParkingSlot and ParkingMeter are now said to be “mixed in” into the class. It is now possible for class instances to use these methods defined in the modules as and when required.&lt;br /&gt;
 p=Parking.new   &lt;br /&gt;
 p.park            # -&amp;gt; Method park from module ParkingSlot&lt;br /&gt;
 p.unpark          # -&amp;gt; Method unpark from module ParkingSlot&lt;br /&gt;
 p.calculate       # -&amp;gt; Method calculate from module ParkingMeter&lt;br /&gt;
 p.printreceipt    # -&amp;gt; Method printreceipt from module ParkingMeter&lt;br /&gt;
 p.printcardetails # -&amp;gt; Calls printCarDetails from class&lt;br /&gt;
The class Parking inherits from both the modules and the module methods are now available in Parking. Hence, it is now possible to use the methods with an instance of the Parking class p.&lt;br /&gt;
Hence, Mixins can be thought of taking different methods and variables defined in different modules making them available as instance methods in the class as well, thereby extending the class’ functionality. Effectively mixed in modules behave as superclass. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:ParkingLot.PNG| center]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Interfaces===&lt;br /&gt;
In an object-oriented programming languages such as Java, C#, an interface&amp;lt;ref&amp;gt;[http://download.oracle.com/javase/tutorial/ Interfaces] &amp;lt;/ref&amp;gt; is a reference type, similar to a class that can contain only constants, method signatures, and nested types. There are no method bodies. Interfaces cannot be instantiated and they can only be implemented by classes or extended by other interfaces. All methods are public by default and any fields declared in an interface are by default static and final.&lt;br /&gt;
Example:&lt;br /&gt;
 public interface ParkingLot {&lt;br /&gt;
       boolean add(Car car, int slot);&lt;br /&gt;
       boolean remove(Car car);&lt;br /&gt;
       boolean findCar(Int number);&lt;br /&gt;
       int freeSlotsCount();&lt;br /&gt;
       int occupiedSlots();&lt;br /&gt;
       int parkingType();&lt;br /&gt;
       int calculateParkingFee(int hours, int charge, int duration);&lt;br /&gt;
       int findParkingSlot();&lt;br /&gt;
 }&lt;br /&gt;
 public class Mall implements ParkingLot {&lt;br /&gt;
       boolean add(Car car, int slot) { //... Code to add a car into the parking lot... }&lt;br /&gt;
       boolean remove(Car car){ // do something }&lt;br /&gt;
       boolean findCar(Int number){ // do something }&lt;br /&gt;
       int freeSlotsCount(){ // do something }&lt;br /&gt;
       int occupiedSlots(){ // do something }&lt;br /&gt;
       int parkingType(){ // do something }&lt;br /&gt;
       int calculateParkingFee(int hours, int charge, int duration){ // do something }&lt;br /&gt;
       int findParkingSlot(){ // do something }&lt;br /&gt;
 }&lt;br /&gt;
Interfaces play another important role in the object-oriented programming language. Interfaces are not part of the class hierarchy, although they work in combination with classes. Interfaces provide an alternative for languages like Java that does not allow multiple inheritance .&lt;br /&gt;
&lt;br /&gt;
==Mixins vs. Interfaces==&lt;br /&gt;
===Similarities and Differences===&lt;br /&gt;
	Mixins and Interfaces can be considered as a block of code having some variables and methods that are used in a class. The key difference between them is that a Mixin can contain method definitions whereas an Interface cannot.&lt;br /&gt;
&lt;br /&gt;
====Multiple method definitions:====&lt;br /&gt;
	In a class, when more than one module is Mixed in where some modules have overloaded methods, the method from the module that was last Mixed in into the class will be executed.&lt;br /&gt;
 module Frog&lt;br /&gt;
    def sound()&lt;br /&gt;
      puts &amp;quot;croak...&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Dinosaur&lt;br /&gt;
   def sound()&lt;br /&gt;
     puts &amp;quot;roar...&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
       include Frog&lt;br /&gt;
       include Dinosaur&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 Frogosaur.new.sound &amp;lt;br&amp;gt;	&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 roar...&amp;lt;br&amp;gt;&lt;br /&gt;
This is because the class Frogosaur will have only one definition for each method. The users have the flexibility to make use of methods from different modules by rearranging the order of include.&lt;br /&gt;
Interfaces, on the other hand, do not offer the flexibility to have different implementation for a same method. This is because the class implementing the interface can provide only one implementation for each method. Hence there exists only one method definition.&lt;br /&gt;
 public interface Frog {&lt;br /&gt;
     void sound();&lt;br /&gt;
 }&lt;br /&gt;
 public interface Dinasour {&lt;br /&gt;
     void sound();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
 public void sound()&lt;br /&gt;
 {&lt;br /&gt;
     System.out.println(&amp;quot;barks......&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public static void main(String args[])&lt;br /&gt;
 {&lt;br /&gt;
     new Frogosaur().sound();;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 barks...&amp;lt;br&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
====Multiple object behavior:====&lt;br /&gt;
In Ruby, a class’s instance can extend a module and exhibit different behavior than the other instances of the same class.&lt;br /&gt;
&lt;br /&gt;
 module Frog&lt;br /&gt;
  def sound()&lt;br /&gt;
     puts &amp;quot;croak...&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Dinasour&lt;br /&gt;
  def sound()&lt;br /&gt;
    puts &amp;quot;roar...&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
  def sound()&lt;br /&gt;
    puts &amp;quot;barks...&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 beast1= Frogosaur.new&lt;br /&gt;
 beast2= Frogosaur.new&lt;br /&gt;
 beast1.extend(Frog)&lt;br /&gt;
 beast1.sound&lt;br /&gt;
 beast2.sound&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 croak...&amp;lt;br&amp;gt;&lt;br /&gt;
 barks...&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Interfaces are implemented by a class and all instances of the class behave in the same way. They can use only the methods of the class from which they were instantiated.&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
  public void sound()&lt;br /&gt;
  {&lt;br /&gt;
      System.out.println(&amp;quot;Sounds...&amp;quot;);&lt;br /&gt;
  }  &lt;br /&gt;
  public static void main(String args[])&lt;br /&gt;
  {&lt;br /&gt;
      Frogosaur beast1 = new Frogosaur();&lt;br /&gt;
      Frogosaur beast2 = new Frogosaur();&lt;br /&gt;
      object1.sound();&lt;br /&gt;
      object2.sound();&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 Sounds...&amp;lt;br&amp;gt;&lt;br /&gt;
 Sounds...&amp;lt;br&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
====Enumerable – module and interface====&lt;br /&gt;
&lt;br /&gt;
Ruby’s Arrays have several methods defined in them. To use them, the classes need not be a subclass of Array. The classes can include Enumerable &amp;lt;ref&amp;gt;[http://mattenoble.com/2011/05/30/ruby-enumerables/ Enumerables]&amp;lt;/ref&amp;gt; and define the each method to use the array’s methods for free.&lt;br /&gt;
&lt;br /&gt;
 class EnumExer&lt;br /&gt;
  include Enumerable&lt;br /&gt;
  def initialize(*arrays)&lt;br /&gt;
    @arrays = arrays&lt;br /&gt;
  end&lt;br /&gt;
  def each&lt;br /&gt;
    @arrays.each { |a| a.each { |x| yield x } }&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 ma = EnumExer.new([4, 1], [3], [2])&lt;br /&gt;
 puts ma.sort&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 1&amp;lt;br&amp;gt;&lt;br /&gt;
 2&amp;lt;br&amp;gt;&lt;br /&gt;
 3&amp;lt;br&amp;gt;&lt;br /&gt;
 4&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can observe that although the arguments passed to the ma instance did not have a similar structure, the sort method was able to identify each entry and sort them. This is because the each method was defined to identify and serve all the entries to the sort method.&lt;br /&gt;
The methods that are defined for Enumerable modules are:&lt;br /&gt;
 [&amp;quot;all?&amp;quot;, &amp;quot;any?&amp;quot;, &amp;quot;collect&amp;quot;, &amp;quot;detect&amp;quot;, &amp;quot;each_with_index&amp;quot;, &amp;quot;entries&amp;quot;,&lt;br /&gt;
 &amp;quot;find&amp;quot;, &amp;quot;find_all&amp;quot;, &amp;quot;grep&amp;quot;, &amp;quot;include?&amp;quot;, &amp;quot;inject&amp;quot;, &amp;quot;map&amp;quot;, &amp;quot;max&amp;quot;,&lt;br /&gt;
 &amp;quot;member?&amp;quot;, &amp;quot;min&amp;quot;, &amp;quot;partition&amp;quot;, &amp;quot;reject&amp;quot;, &amp;quot;select&amp;quot;, &amp;quot;sort&amp;quot;, &amp;quot;sort_by&amp;quot;,&lt;br /&gt;
 &amp;quot;to_a&amp;quot;, &amp;quot;zip&amp;quot;]&lt;br /&gt;
In Java, to use the methods of Enumerable interface, the class should be a subclass of Array.&lt;br /&gt;
&lt;br /&gt;
 public class EnumExer {&lt;br /&gt;
     static String content_string = &amp;quot;zyxwvutsrqponmlkjihgfedcba&amp;quot;;&lt;br /&gt;
     String a=&amp;quot;1&amp;quot;, b=&amp;quot;2&amp;quot;;&lt;br /&gt;
     public static void main(String args[])&lt;br /&gt;
     {&lt;br /&gt;
         char[] content_array = content_string.toCharArray();&lt;br /&gt;
         java.util.Arrays.sort(content_array);&lt;br /&gt;
         content_string=new String(content_array);&lt;br /&gt;
         System.out.println(content_string);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 Abcdefghijklmnopqrstuvwxyz&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, the string object was converted to a character array to be able to use with the sort method.&lt;br /&gt;
&lt;br /&gt;
====Inheritance:====&lt;br /&gt;
Java interfaces can inherit other interfaces but Ruby modules cannot inherit other modules but can contain classes and methods.&lt;br /&gt;
 module Wrapper&lt;br /&gt;
  class Base&lt;br /&gt;
     # Class Body&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Java,&lt;br /&gt;
 public interface Dinasour extends Frog{&lt;br /&gt;
     //variables and method Declarations&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
==== Achieving Multiple Inheritance ====&lt;br /&gt;
Modules and Interfaces help acheive multiple inheritance. Programming languages such as Ruby and Java do not support multiple inheritance. &amp;lt;br&amp;gt;&lt;br /&gt;
Interfaces are used in Java to achieve multiple inheritance.&amp;lt;ref&amp;gt; [http://csis.pace.edu/~bergin/patterns/multipleinheritance.html  Mulitple Inheritance] &amp;lt;/ref&amp;gt; Similarly, modules in Ruby help achieve multiple inheritance.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Ruby,&lt;br /&gt;
&lt;br /&gt;
 module Frog&lt;br /&gt;
   def jump()&lt;br /&gt;
     //Method definition&lt;br /&gt;
    end&lt;br /&gt;
 end &amp;lt;br&amp;gt;&lt;br /&gt;
 module Dinosaur&lt;br /&gt;
  def strength()&lt;br /&gt;
    //Method definition&lt;br /&gt;
  end&lt;br /&gt;
 end &amp;lt;br&amp;gt;&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
      include Frog&lt;br /&gt;
      include Dinosaur&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
In Java,&lt;br /&gt;
 public interface Frog {&lt;br /&gt;
     void jump();&lt;br /&gt;
 } &amp;lt;br&amp;gt;&lt;br /&gt;
 public interface Dinasour {&lt;br /&gt;
     void strength();&lt;br /&gt;
 } &amp;lt;br&amp;gt;&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
  //Method Implementations&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Frogosaur inherits the methods of both frog and dinosaur. &lt;br /&gt;
&lt;br /&gt;
===Tabular Comparison===&lt;br /&gt;
{|border=&amp;quot;2&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Mixins&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Interfaces&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|The number of lines of code is lesser since the &amp;lt;br&amp;gt;&lt;br /&gt;
modules contain the method definition. &lt;br /&gt;
|The number of lines of code is bigger since the &amp;lt;br&amp;gt;&lt;br /&gt;
classes have to define the methods.&lt;br /&gt;
|-&lt;br /&gt;
|More modules in a class makes code less readable.&lt;br /&gt;
|The code is more readable since the class contain the &amp;lt;br&amp;gt;&lt;br /&gt;
method definition.&lt;br /&gt;
|-&lt;br /&gt;
|Modules cannot be inherited and cannot form is-a hierarchy.&lt;br /&gt;
|Interfaces can extend another interface and hence can form &amp;lt;br&amp;gt;&lt;br /&gt;
is-a hierarchy.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Drawbacks===&lt;br /&gt;
The drawbacks of the technique of Mixins are very much debated. Though Mixins provide us with an easy way to write flexible and decoupled code and also help us solve the diamond problem, they pose their own problems. In large programs there could be large number of modules and each module could have loads of methods each performing a certain task. To trace the origin of the methods and to keep in mind the hierarchy is practically impossible. Another issue associated with Mixins pose is silent method overriding. Silent method overriding occurs when different modules are included in a class and each module have a method with the same definition. In such a case, the method which gets executed depends upon the way it is included in the class and this is done without any sort of message to the user (silently). In large scale applications this could certainly be an issue considering the number of modules that would be mixed in and it would be difficult to keep track of the hierarchy in which the modules were included.&lt;br /&gt;
Although Java provides polymorphic behavior with the use of interfaces, they sometimes tend to be very slow. The implementation of interfaces is also limited to public methods and constants with no implementation.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Modules and Interfaces offer many advantages. However, they have their own disadvantages. Modules add more flexibility to object-oriented programming by the use of DRY principle and are easy to maintain.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
1. http://ruby.about.com/od/beginningruby/a/mixin.htm/ &amp;lt;br&amp;gt;&lt;br /&gt;
2. http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/ &amp;lt;br&amp;gt;&lt;br /&gt;
3. http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-frameworks/ &amp;lt;br&amp;gt;&lt;br /&gt;
4. http://pg-server.csc.ncsu.edu/mediawiki/index.php?title=CSC/ECE_517_Fall_2010/ch3_3b_sv&amp;amp;printable=yes &amp;lt;br&amp;gt;&lt;br /&gt;
5. http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2c_ds &amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rgovind2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1a_1w16_br&amp;diff=63750</id>
		<title>CSC/ECE 517 Fall 2012/ch1a 1w16 br</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1a_1w16_br&amp;diff=63750"/>
		<updated>2012-09-12T22:18:52Z</updated>

		<summary type="html">&lt;p&gt;Rgovind2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Overview==&lt;br /&gt;
&lt;br /&gt;
Mixins are Ruby modules. A module is a grouping of objects under a single name. The objects may be constants, methods, classes, or other modules. Modules are defined in Ruby using the keyword module. They can be incorporated into a class with Ruby’s include statement. This is called a mixin.&lt;br /&gt;
Modules are for sharing behavior (methods), while classes are for modeling relationship between objects. Ruby classes can Mixin a module and receive all its methods for free.&lt;br /&gt;
An Interface in Java programming language is an abstract type that classes must implement. Interfaces are declared using the interface keyword, and may only contain method signature and constant declarations.&lt;br /&gt;
A class that implements an interface must implement all of the methods described in the interface, or be an abstract class.&lt;br /&gt;
&lt;br /&gt;
==Definitions==&lt;br /&gt;
===Mixins===&lt;br /&gt;
	Modules provide a structure to collect classes, methods, and constants into a single, separately defined unit. They provide the simplest and an elegant way to adhere to the Don't repeat yourself&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Don't_repeat_yourself Don't_repeat_yourself]&amp;lt;/ref&amp;gt; principle. This is useful so as to avoid clashes with existing classes, methods, and constants.&lt;br /&gt;
Modules are defined in Ruby using the module keyword.&lt;br /&gt;
A Mixin is a class that is mixed with a module or a set of modules i.e. the implementation of the modules and the class are intertwined and combined together &amp;lt;ref&amp;gt;[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html Mixins] &amp;lt;/ref&amp;gt;.&lt;br /&gt;
This gives us a neat and controlled way of adding new functionality to classes. Modules are similar to classes in that they hold a collection of methods, variables constants and other modules and class definitions. However, the real usage of a Mixin is exploited when the code in the Mixin starts to interact with code in the class that uses it.&lt;br /&gt;
Below is an example for modules and Mixins:&lt;br /&gt;
The module ParkingSlot consists of the methods park and unpark&lt;br /&gt;
 &lt;br /&gt;
 module ParkingSlot&lt;br /&gt;
       def park&lt;br /&gt;
          #code to park the car&lt;br /&gt;
       end&lt;br /&gt;
       def unpark&lt;br /&gt;
          #code to unpark the car&lt;br /&gt;
       end&lt;br /&gt;
 end&lt;br /&gt;
The module ParkingMeter consists of the methods calculate and printreceipt&lt;br /&gt;
&lt;br /&gt;
 module ParkingMeter&lt;br /&gt;
       def calculate&lt;br /&gt;
          #code to calculate the parking fee&lt;br /&gt;
       end&lt;br /&gt;
       def printreceipt&lt;br /&gt;
          #code to print the receipt&lt;br /&gt;
       end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
When a class includes a module via include Module Name, all the methods on that module become instance methods on the class.&lt;br /&gt;
&lt;br /&gt;
 class Parking&lt;br /&gt;
      include ParkingSlot&lt;br /&gt;
      include ParkingMeter&lt;br /&gt;
      def printcardetails&lt;br /&gt;
          #code to print the details and type of car&lt;br /&gt;
      end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
The modules ParkingSlot and ParkingMeter are “included” in the Parking class. All the variables and the instance methods of the modules ParkingSlot and ParkingMeter are now said to be “mixed in” into the class. It is now possible for class instances to use these methods defined in the modules as and when required.&lt;br /&gt;
 p=Parking.new   &lt;br /&gt;
 p.park            # -&amp;gt; Method park from module ParkingSlot&lt;br /&gt;
 p.unpark          # -&amp;gt; Method unpark from module ParkingSlot&lt;br /&gt;
 p.calculate       # -&amp;gt; Method calculate from module ParkingMeter&lt;br /&gt;
 p.printreceipt    # -&amp;gt; Method printreceipt from module ParkingMeter&lt;br /&gt;
 p.printcardetails # -&amp;gt; Calls printCarDetails from class&lt;br /&gt;
The class Parking inherits from both the modules and the module methods are now available in Parking. Hence, it is now possible to use the methods with an instance of the Parking class p.&lt;br /&gt;
Hence, Mixins can be thought of taking different methods and variables defined in different modules making them available as instance methods in the class as well, thereby extending the class’ functionality. Effectively mixed in modules behave as superclass. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:ParkingLot.PNG| center]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Interfaces===&lt;br /&gt;
In an object-oriented programming languages such as Java, C#, an interface&amp;lt;ref&amp;gt;[http://download.oracle.com/javase/tutorial/ Interfaces] &amp;lt;/ref&amp;gt; is a reference type, similar to a class that can contain only constants, method signatures, and nested types. There are no method bodies. Interfaces cannot be instantiated and they can only be implemented by classes or extended by other interfaces. All methods are public by default and any fields declared in an interface are by default static and final.&lt;br /&gt;
Example:&lt;br /&gt;
 public interface ParkingLot {&lt;br /&gt;
       boolean add(Car car, int slot);&lt;br /&gt;
       boolean remove(Car car);&lt;br /&gt;
       boolean findCar(Int number);&lt;br /&gt;
       int freeSlotsCount();&lt;br /&gt;
       int occupiedSlots();&lt;br /&gt;
       int parkingType();&lt;br /&gt;
       int calculateParkingFee(int hours, int charge, int duration);&lt;br /&gt;
       int findParkingSlot();&lt;br /&gt;
 }&lt;br /&gt;
 public class Mall implements ParkingLot {&lt;br /&gt;
       boolean add(Car car, int slot) { //... Code to add a car into the parking lot... }&lt;br /&gt;
       boolean remove(Car car){ // do something }&lt;br /&gt;
       boolean findCar(Int number){ // do something }&lt;br /&gt;
       int freeSlotsCount(){ // do something }&lt;br /&gt;
       int occupiedSlots(){ // do something }&lt;br /&gt;
       int parkingType(){ // do something }&lt;br /&gt;
       int calculateParkingFee(int hours, int charge, int duration){ // do something }&lt;br /&gt;
       int findParkingSlot(){ // do something }&lt;br /&gt;
 }&lt;br /&gt;
Interfaces play another important role in the object-oriented programming language. Interfaces are not part of the class hierarchy, although they work in combination with classes. Interfaces provide an alternative for languages like Java that does not allow multiple inheritance .&lt;br /&gt;
&lt;br /&gt;
==Mixins vs. Interfaces==&lt;br /&gt;
===Similarities and Differences===&lt;br /&gt;
	Mixins and Interfaces can be considered as a block of code having some variables and methods that are used in a class. The key difference between them is that a Mixin can contain method definitions whereas an Interface cannot.&lt;br /&gt;
&lt;br /&gt;
====Multiple method definitions:====&lt;br /&gt;
	In a class, when more than one module is Mixed in where some modules have overloaded methods, the method from the module that was last Mixed in into the class will be executed.&lt;br /&gt;
 module Frog&lt;br /&gt;
    def sound()&lt;br /&gt;
      puts &amp;quot;croak...&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Dinosaur&lt;br /&gt;
   def sound()&lt;br /&gt;
     puts &amp;quot;roar...&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
       include Frog&lt;br /&gt;
       include Dinosaur&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 Frogosaur.new.sound &amp;lt;br&amp;gt;	&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 roar...&amp;lt;br&amp;gt;&lt;br /&gt;
This is because the class Frogosaur will have only one definition for each method. The users have the flexibility to make use of methods from different modules by rearranging the order of include.&lt;br /&gt;
Interfaces, on the other hand, do not offer the flexibility to have different implementation for a same method. This is because the class implementing the interface can provide only one implementation for each method. Hence there exists only one method definition.&lt;br /&gt;
 public interface Frog {&lt;br /&gt;
     void sound();&lt;br /&gt;
 }&lt;br /&gt;
 public interface Dinasour {&lt;br /&gt;
     void sound();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
 public void sound()&lt;br /&gt;
 {&lt;br /&gt;
     System.out.println(&amp;quot;barks......&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public static void main(String args[])&lt;br /&gt;
 {&lt;br /&gt;
     new Frogosaur().sound();;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 barks...&amp;lt;br&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
====Multiple object behavior:====&lt;br /&gt;
In Ruby, a class’s instance can extend a module and exhibit different behavior than the other instances of the same class.&lt;br /&gt;
&lt;br /&gt;
 module Frog&lt;br /&gt;
  def sound()&lt;br /&gt;
     puts &amp;quot;croak...&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Dinasour&lt;br /&gt;
  def sound()&lt;br /&gt;
    puts &amp;quot;roar...&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
  def sound()&lt;br /&gt;
    puts &amp;quot;barks...&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 beast1= Frogosaur.new&lt;br /&gt;
 beast2= Frogosaur.new&lt;br /&gt;
 beast1.extend(Frog)&lt;br /&gt;
 beast1.sound&lt;br /&gt;
 beast2.sound&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 croak...&amp;lt;br&amp;gt;&lt;br /&gt;
 barks...&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Interfaces are implemented by a class and all instances of the class behave in the same way. They can use only the methods of the class from which they were instantiated.&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
  public void sound()&lt;br /&gt;
  {&lt;br /&gt;
      System.out.println(&amp;quot;Sounds...&amp;quot;);&lt;br /&gt;
  }  &lt;br /&gt;
  public static void main(String args[])&lt;br /&gt;
  {&lt;br /&gt;
      Frogosaur beast1 = new Frogosaur();&lt;br /&gt;
      Frogosaur beast2 = new Frogosaur();&lt;br /&gt;
      object1.sound();&lt;br /&gt;
      object2.sound();&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 Sounds...&amp;lt;br&amp;gt;&lt;br /&gt;
 Sounds...&amp;lt;br&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
====Enumerable – module and interface====&lt;br /&gt;
&lt;br /&gt;
Ruby’s Arrays have several methods defined in them. To use them, the classes need not be a subclass of Array. The classes can include Enumerable and define the each method to use the array’s methods for free.&lt;br /&gt;
&lt;br /&gt;
 class EnumExer&lt;br /&gt;
  include Enumerable&lt;br /&gt;
  def initialize(*arrays)&lt;br /&gt;
    @arrays = arrays&lt;br /&gt;
  end&lt;br /&gt;
  def each&lt;br /&gt;
    @arrays.each { |a| a.each { |x| yield x } }&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 ma = EnumExer.new([4, 1], [3], [2])&lt;br /&gt;
 puts ma.sort&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 1&amp;lt;br&amp;gt;&lt;br /&gt;
 2&amp;lt;br&amp;gt;&lt;br /&gt;
 3&amp;lt;br&amp;gt;&lt;br /&gt;
 4&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can observe that although the arguments passed to the ma instance did not have a similar structure, the sort method was able to identify each entry and sort them. This is because the each method was defined to identify and serve all the entries to the sort method.&lt;br /&gt;
The methods that are defined for Enumerable modules are:&lt;br /&gt;
 [&amp;quot;all?&amp;quot;, &amp;quot;any?&amp;quot;, &amp;quot;collect&amp;quot;, &amp;quot;detect&amp;quot;, &amp;quot;each_with_index&amp;quot;, &amp;quot;entries&amp;quot;,&lt;br /&gt;
 &amp;quot;find&amp;quot;, &amp;quot;find_all&amp;quot;, &amp;quot;grep&amp;quot;, &amp;quot;include?&amp;quot;, &amp;quot;inject&amp;quot;, &amp;quot;map&amp;quot;, &amp;quot;max&amp;quot;,&lt;br /&gt;
 &amp;quot;member?&amp;quot;, &amp;quot;min&amp;quot;, &amp;quot;partition&amp;quot;, &amp;quot;reject&amp;quot;, &amp;quot;select&amp;quot;, &amp;quot;sort&amp;quot;, &amp;quot;sort_by&amp;quot;,&lt;br /&gt;
 &amp;quot;to_a&amp;quot;, &amp;quot;zip&amp;quot;]&lt;br /&gt;
In Java, to use the methods of Enumerable interface, the class should be a subclass of Array.&lt;br /&gt;
&lt;br /&gt;
 public class EnumExer {&lt;br /&gt;
     static String content_string = &amp;quot;zyxwvutsrqponmlkjihgfedcba&amp;quot;;&lt;br /&gt;
     String a=&amp;quot;1&amp;quot;, b=&amp;quot;2&amp;quot;;&lt;br /&gt;
     public static void main(String args[])&lt;br /&gt;
     {&lt;br /&gt;
         char[] content_array = content_string.toCharArray();&lt;br /&gt;
         java.util.Arrays.sort(content_array);&lt;br /&gt;
         content_string=new String(content_array);&lt;br /&gt;
         System.out.println(content_string);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 Abcdefghijklmnopqrstuvwxyz&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, the string object was converted to a character array to be able to use with the sort method.&lt;br /&gt;
&lt;br /&gt;
====Inheritance:====&lt;br /&gt;
Java interfaces can inherit other interfaces but Ruby modules cannot inherit other modules but can contain classes and methods.&lt;br /&gt;
 module Wrapper&lt;br /&gt;
  class Base&lt;br /&gt;
     # Class Body&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Java,&lt;br /&gt;
 public interface Dinasour extends Frog{&lt;br /&gt;
     //variables and method Declarations&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
==== Achieving Multiple Inheritance ====&lt;br /&gt;
Modules and Interfaces help acheive multiple inheritance. Programming languages such as Ruby and Java do not support multiple inheritance. &amp;lt;br&amp;gt;&lt;br /&gt;
Interfaces are used in Java to achieve multiple inheritance.&amp;lt;ref&amp;gt; [http://csis.pace.edu/~bergin/patterns/multipleinheritance.html  Mulitple Inheritance] &amp;lt;/ref&amp;gt; Similarly, modules in Ruby help achieve multiple inheritance.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Ruby,&lt;br /&gt;
&lt;br /&gt;
 module Frog&lt;br /&gt;
   def jump()&lt;br /&gt;
     //Method definition&lt;br /&gt;
    end&lt;br /&gt;
 end &amp;lt;br&amp;gt;&lt;br /&gt;
 module Dinosaur&lt;br /&gt;
  def strength()&lt;br /&gt;
    //Method definition&lt;br /&gt;
  end&lt;br /&gt;
 end &amp;lt;br&amp;gt;&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
      include Frog&lt;br /&gt;
      include Dinosaur&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
In Java,&lt;br /&gt;
 public interface Frog {&lt;br /&gt;
     void jump();&lt;br /&gt;
 } &amp;lt;br&amp;gt;&lt;br /&gt;
 public interface Dinasour {&lt;br /&gt;
     void strength();&lt;br /&gt;
 } &amp;lt;br&amp;gt;&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
  //Method Implementations&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Frogosaur inherits the methods of both frog and dinosaur. &lt;br /&gt;
&lt;br /&gt;
===Tabular Comparison===&lt;br /&gt;
{|border=&amp;quot;2&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Mixins&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Interfaces&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|The number of lines of code is lesser since the &amp;lt;br&amp;gt;&lt;br /&gt;
modules contain the method definition. &lt;br /&gt;
|The number of lines of code is bigger since the &amp;lt;br&amp;gt;&lt;br /&gt;
classes have to define the methods.&lt;br /&gt;
|-&lt;br /&gt;
|More modules in a class makes code less readable.&lt;br /&gt;
|The code is more readable since the class contain the &amp;lt;br&amp;gt;&lt;br /&gt;
method definition.&lt;br /&gt;
|-&lt;br /&gt;
|Modules cannot be inherited and cannot form is-a hierarchy.&lt;br /&gt;
|Interfaces can extend another interface and hence can form &amp;lt;br&amp;gt;&lt;br /&gt;
is-a hierarchy.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Drawbacks===&lt;br /&gt;
The drawbacks of the technique of Mixins are very much debated. Though Mixins provide us with an easy way to write flexible and decoupled code and also help us solve the diamond problem, they pose their own problems. In large programs there could be large number of modules and each module could have loads of methods each performing a certain task. To trace the origin of the methods and to keep in mind the hierarchy is practically impossible. Another issue associated with Mixins pose is silent method overriding. Silent method overriding occurs when different modules are included in a class and each module have a method with the same definition. In such a case, the method which gets executed depends upon the way it is included in the class and this is done without any sort of message to the user (silently). In large scale applications this could certainly be an issue considering the number of modules that would be mixed in and it would be difficult to keep track of the hierarchy in which the modules were included.&lt;br /&gt;
Although Java provides polymorphic behavior with the use of interfaces, they sometimes tend to be very slow. The implementation of interfaces is also limited to public methods and constants with no implementation.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Modules and Interfaces offer many advantages. However, they have their own disadvantages. Modules add more flexibility to object-oriented programming by the use of DRY principle and are easy to maintain.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
1. http://ruby.about.com/od/beginningruby/a/mixin.htm/ &amp;lt;br&amp;gt;&lt;br /&gt;
2. http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/ &amp;lt;br&amp;gt;&lt;br /&gt;
3. http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-frameworks/ &amp;lt;br&amp;gt;&lt;br /&gt;
4. http://pg-server.csc.ncsu.edu/mediawiki/index.php?title=CSC/ECE_517_Fall_2010/ch3_3b_sv&amp;amp;printable=yes &amp;lt;br&amp;gt;&lt;br /&gt;
5. http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2c_ds &amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rgovind2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1a_1w16_br&amp;diff=63749</id>
		<title>CSC/ECE 517 Fall 2012/ch1a 1w16 br</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1a_1w16_br&amp;diff=63749"/>
		<updated>2012-09-12T22:18:16Z</updated>

		<summary type="html">&lt;p&gt;Rgovind2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Overview==&lt;br /&gt;
&lt;br /&gt;
Mixins are Ruby modules. A module is a grouping of objects under a single name. The objects may be constants, methods, classes, or other modules. Modules are defined in Ruby using the keyword module. They can be incorporated into a class with Ruby’s include statement. This is called a mixin.&lt;br /&gt;
Modules are for sharing behavior (methods), while classes are for modeling relationship between objects. Ruby classes can Mixin a module and receive all its methods for free.&lt;br /&gt;
An Interface in Java programming language is an abstract type that classes must implement. Interfaces are declared using the interface keyword, and may only contain method signature and constant declarations.&lt;br /&gt;
A class that implements an interface must implement all of the methods described in the interface, or be an abstract class.&lt;br /&gt;
&lt;br /&gt;
==Definitions==&lt;br /&gt;
===Mixins===&lt;br /&gt;
	Modules provide a structure to collect classes, methods, and constants into a single, separately defined unit. They provide the simplest and an elegant way to adhere to the Don't repeat yourself&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Don't_repeat_yourself Don't_repeat_yourself]&amp;lt;/ref&amp;gt; principle. This is useful so as to avoid clashes with existing classes, methods, and constants.&lt;br /&gt;
Modules are defined in Ruby using the module keyword.&lt;br /&gt;
A Mixin is a class that is mixed with a module or a set of modules i.e. the implementation of the modules and the class are intertwined and combined together &amp;lt;ref&amp;gt;[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html Mixins] &amp;lt;/ref&amp;gt;.&lt;br /&gt;
This gives us a neat and controlled way of adding new functionality to classes. Modules are similar to classes in that they hold a collection of methods, variables constants and other modules and class definitions. However, the real usage of a Mixin is exploited when the code in the Mixin starts to interact with code in the class that uses it.&lt;br /&gt;
Below is an example for modules and Mixins:&lt;br /&gt;
The module ParkingSlot consists of the methods park and unpark&lt;br /&gt;
 &lt;br /&gt;
 module ParkingSlot&lt;br /&gt;
       def park&lt;br /&gt;
          #code to park the car&lt;br /&gt;
       end&lt;br /&gt;
       def unpark&lt;br /&gt;
          #code to unpark the car&lt;br /&gt;
       end&lt;br /&gt;
 end&lt;br /&gt;
The module ParkingMeter consists of the methods calculate and printreceipt&lt;br /&gt;
&lt;br /&gt;
 module ParkingMeter&lt;br /&gt;
       def calculate&lt;br /&gt;
          #code to calculate the parking fee&lt;br /&gt;
       end&lt;br /&gt;
       def printreceipt&lt;br /&gt;
          #code to print the receipt&lt;br /&gt;
       end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
When a class includes a module via include Module Name, all the methods on that module become instance methods on the class.&lt;br /&gt;
&lt;br /&gt;
 class Parking&lt;br /&gt;
      include ParkingSlot&lt;br /&gt;
      include ParkingMeter&lt;br /&gt;
      def printcardetails&lt;br /&gt;
          #code to print the details and type of car&lt;br /&gt;
      end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
The modules ParkingSlot and ParkingMeter are “included” in the Parking class. All the variables and the instance methods of the modules ParkingSlot and ParkingMeter are now said to be “mixed in” into the class. It is now possible for class instances to use these methods defined in the modules as and when required.&lt;br /&gt;
 p=Parking.new   &lt;br /&gt;
 p.park            # -&amp;gt; Method park from module ParkingSlot&lt;br /&gt;
 p.unpark          # -&amp;gt; Method unpark from module ParkingSlot&lt;br /&gt;
 p.calculate       # -&amp;gt; Method calculate from module ParkingMeter&lt;br /&gt;
 p.printreceipt    # -&amp;gt; Method printreceipt from module ParkingMeter&lt;br /&gt;
 p.printcardetails # -&amp;gt; Calls printCarDetails from class&lt;br /&gt;
The class Parking inherits from both the modules and the module methods are now available in Parking. Hence, it is now possible to use the methods with an instance of the Parking class p.&lt;br /&gt;
Hence, Mixins can be thought of taking different methods and variables defined in different modules making them available as instance methods in the class as well, thereby extending the class’ functionality. Effectively mixed in modules behave as superclass. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:ParkingLot.PNG| center]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Interfaces===&lt;br /&gt;
In an object-oriented programming languages such as Java, C#, an interface&amp;lt;ref&amp;gt;[http://download.oracle.com/javase/tutorial/ Interfaces] &amp;lt;/ref&amp;gt; is a reference type, similar to a class that can contain only constants, method signatures, and nested types. There are no method bodies. Interfaces cannot be instantiated and they can only be implemented by classes or extended by other interfaces. All methods are public by default and any fields declared in an interface are by default static and final.&lt;br /&gt;
Example:&lt;br /&gt;
 public interface ParkingLot {&lt;br /&gt;
       boolean add(Car car, int slot);&lt;br /&gt;
       boolean remove(Car car);&lt;br /&gt;
       boolean findCar(Int number);&lt;br /&gt;
       int freeSlotsCount();&lt;br /&gt;
       int occupiedSlots();&lt;br /&gt;
       int parkingType();&lt;br /&gt;
       int calculateParkingFee(int hours, int charge, int duration);&lt;br /&gt;
       int findParkingSlot();&lt;br /&gt;
 }&lt;br /&gt;
 public class Mall implements ParkingLot {&lt;br /&gt;
       boolean add(Car car, int slot) { //... Code to add a car into the parking lot... }&lt;br /&gt;
       boolean remove(Car car){ // do something }&lt;br /&gt;
       boolean findCar(Int number){ // do something }&lt;br /&gt;
       int freeSlotsCount(){ // do something }&lt;br /&gt;
       int occupiedSlots(){ // do something }&lt;br /&gt;
       int parkingType(){ // do something }&lt;br /&gt;
       int calculateParkingFee(int hours, int charge, int duration){ // do something }&lt;br /&gt;
       int findParkingSlot(){ // do something }&lt;br /&gt;
 }&lt;br /&gt;
Interfaces play another important role in the object-oriented programming language. Interfaces are not part of the class hierarchy, although they work in combination with classes. Interfaces provide an alternative for languages like Java that does not allow multiple inheritance .&lt;br /&gt;
&lt;br /&gt;
==Mixins vs. Interfaces==&lt;br /&gt;
===Similarities and Differences===&lt;br /&gt;
	Mixins and Interfaces can be considered as a block of code having some variables and methods that are used in a class. The key difference between them is that a Mixin can contain method definitions whereas an Interface cannot.&lt;br /&gt;
&lt;br /&gt;
====Multiple method definitions:====&lt;br /&gt;
	In a class, when more than one module is Mixed in where some modules have overloaded methods, the method from the module that was last Mixed in into the class will be executed.&lt;br /&gt;
 module Frog&lt;br /&gt;
    def sound()&lt;br /&gt;
      puts &amp;quot;croak...&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Dinosaur&lt;br /&gt;
   def sound()&lt;br /&gt;
     puts &amp;quot;roar...&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
       include Frog&lt;br /&gt;
       include Dinosaur&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 Frogosaur.new.sound &amp;lt;br&amp;gt;	&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 roar...&amp;lt;br&amp;gt;&lt;br /&gt;
This is because the class Frogosaur will have only one definition for each method. The users have the flexibility to make use of methods from different modules by rearranging the order of include.&lt;br /&gt;
Interfaces, on the other hand, do not offer the flexibility to have different implementation for a same method. This is because the class implementing the interface can provide only one implementation for each method. Hence there exists only one method definition.&lt;br /&gt;
 public interface Frog {&lt;br /&gt;
     void sound();&lt;br /&gt;
 }&lt;br /&gt;
 public interface Dinasour {&lt;br /&gt;
     void sound();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
 public void sound()&lt;br /&gt;
 {&lt;br /&gt;
     System.out.println(&amp;quot;barks......&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public static void main(String args[])&lt;br /&gt;
 {&lt;br /&gt;
     new Frogosaur().sound();;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 barks...&amp;lt;br&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
====Multiple object behavior:====&lt;br /&gt;
In Ruby, a class’s instance can extend a module and exhibit different behavior than the other instances of the same class.&lt;br /&gt;
&lt;br /&gt;
 module Frog&lt;br /&gt;
  def sound()&lt;br /&gt;
     puts &amp;quot;croak...&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Dinasour&lt;br /&gt;
  def sound()&lt;br /&gt;
    puts &amp;quot;roar...&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
  def sound()&lt;br /&gt;
    puts &amp;quot;barks...&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 beast1= Frogosaur.new&lt;br /&gt;
 beast2= Frogosaur.new&lt;br /&gt;
 beast1.extend(Frog)&lt;br /&gt;
 beast1.sound&lt;br /&gt;
 beast2.sound&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 croak...&amp;lt;br&amp;gt;&lt;br /&gt;
 barks...&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Interfaces are implemented by a class and all instances of the class behave in the same way. They can use only the methods of the class from which they were instantiated.&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
  public void sound()&lt;br /&gt;
  {&lt;br /&gt;
      System.out.println(&amp;quot;Sounds...&amp;quot;);&lt;br /&gt;
  }  &lt;br /&gt;
  public static void main(String args[])&lt;br /&gt;
  {&lt;br /&gt;
      Frogosaur beast1 = new Frogosaur();&lt;br /&gt;
      Frogosaur beast2 = new Frogosaur();&lt;br /&gt;
      object1.sound();&lt;br /&gt;
      object2.sound();&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 Sounds...&amp;lt;br&amp;gt;&lt;br /&gt;
 Sounds...&amp;lt;br&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
====Enumerable – module and interface====&lt;br /&gt;
&lt;br /&gt;
Ruby’s Arrays have several methods defined in them. To use them, the classes need not be a subclass of Array. The classes can include Enumerable and define the each method to use the array’s methods for free.&lt;br /&gt;
&lt;br /&gt;
 class EnumExer&lt;br /&gt;
  include Enumerable&lt;br /&gt;
  def initialize(*arrays)&lt;br /&gt;
    @arrays = arrays&lt;br /&gt;
  end&lt;br /&gt;
  def each&lt;br /&gt;
    @arrays.each { |a| a.each { |x| yield x } }&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 ma = EnumExer.new([4, 1], [3], [2])&lt;br /&gt;
 puts ma.sort&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 1&amp;lt;br&amp;gt;&lt;br /&gt;
 2&amp;lt;br&amp;gt;&lt;br /&gt;
 3&amp;lt;br&amp;gt;&lt;br /&gt;
 4&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can observe that although the arguments passed to the ma instance did not have a similar structure, the sort method was able to identify each entry and sort them. This is because the each method was defined to identify and serve all the entries to the sort method.&lt;br /&gt;
The methods that are defined for Enumerable modules are:&lt;br /&gt;
 [&amp;quot;all?&amp;quot;, &amp;quot;any?&amp;quot;, &amp;quot;collect&amp;quot;, &amp;quot;detect&amp;quot;, &amp;quot;each_with_index&amp;quot;, &amp;quot;entries&amp;quot;,&lt;br /&gt;
 &amp;quot;find&amp;quot;, &amp;quot;find_all&amp;quot;, &amp;quot;grep&amp;quot;, &amp;quot;include?&amp;quot;, &amp;quot;inject&amp;quot;, &amp;quot;map&amp;quot;, &amp;quot;max&amp;quot;,&lt;br /&gt;
 &amp;quot;member?&amp;quot;, &amp;quot;min&amp;quot;, &amp;quot;partition&amp;quot;, &amp;quot;reject&amp;quot;, &amp;quot;select&amp;quot;, &amp;quot;sort&amp;quot;, &amp;quot;sort_by&amp;quot;,&lt;br /&gt;
 &amp;quot;to_a&amp;quot;, &amp;quot;zip&amp;quot;]&lt;br /&gt;
In Java, to use the methods of Enumerable interface, the class should be a subclass of Array.&lt;br /&gt;
&lt;br /&gt;
 public class EnumExer {&lt;br /&gt;
     static String content_string = &amp;quot;zyxwvutsrqponmlkjihgfedcba&amp;quot;;&lt;br /&gt;
     String a=&amp;quot;1&amp;quot;, b=&amp;quot;2&amp;quot;;&lt;br /&gt;
     public static void main(String args[])&lt;br /&gt;
     {&lt;br /&gt;
         char[] content_array = content_string.toCharArray();&lt;br /&gt;
         java.util.Arrays.sort(content_array);&lt;br /&gt;
         content_string=new String(content_array);&lt;br /&gt;
         System.out.println(content_string);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 Abcdefghijklmnopqrstuvwxyz&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, the string object was converted to a character array to be able to use with the sort method.&lt;br /&gt;
&lt;br /&gt;
====Inheritance:====&lt;br /&gt;
Java interfaces can inherit other interfaces but Ruby modules cannot inherit other modules but can contain classes and methods.&lt;br /&gt;
 module Wrapper&lt;br /&gt;
  class Base&lt;br /&gt;
     # Class Body&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Java,&lt;br /&gt;
 public interface Dinasour extends Frog{&lt;br /&gt;
     //variables and method Declarations&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
==== Achieving Multiple Inheritance ====&lt;br /&gt;
Modules and Interfaces help acheive multiple inheritance. Programming languages such as Ruby and Java do not support multiple inheritance. &amp;lt;br&amp;gt;&lt;br /&gt;
Interfaces are used in Java to achieve multiple inheritance.&amp;lt;ref&amp;gt; [http://csis.pace.edu/~bergin/patterns/multipleinheritance.html | Mulitple Inheritance] &amp;lt;/ref&amp;gt; Similarly, modules in Ruby help achieve multiple inheritance.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Ruby,&lt;br /&gt;
&lt;br /&gt;
 module Frog&lt;br /&gt;
   def jump()&lt;br /&gt;
     //Method definition&lt;br /&gt;
    end&lt;br /&gt;
 end &amp;lt;br&amp;gt;&lt;br /&gt;
 module Dinosaur&lt;br /&gt;
  def strength()&lt;br /&gt;
    //Method definition&lt;br /&gt;
  end&lt;br /&gt;
 end &amp;lt;br&amp;gt;&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
      include Frog&lt;br /&gt;
      include Dinosaur&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
In Java,&lt;br /&gt;
 public interface Frog {&lt;br /&gt;
     void jump();&lt;br /&gt;
 } &amp;lt;br&amp;gt;&lt;br /&gt;
 public interface Dinasour {&lt;br /&gt;
     void strength();&lt;br /&gt;
 } &amp;lt;br&amp;gt;&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
  //Method Implementations&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Frogosaur inherits the methods of both frog and dinosaur. &lt;br /&gt;
&lt;br /&gt;
===Tabular Comparison===&lt;br /&gt;
{|border=&amp;quot;2&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Mixins&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Interfaces&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|The number of lines of code is lesser since the &amp;lt;br&amp;gt;&lt;br /&gt;
modules contain the method definition. &lt;br /&gt;
|The number of lines of code is bigger since the &amp;lt;br&amp;gt;&lt;br /&gt;
classes have to define the methods.&lt;br /&gt;
|-&lt;br /&gt;
|More modules in a class makes code less readable.&lt;br /&gt;
|The code is more readable since the class contain the &amp;lt;br&amp;gt;&lt;br /&gt;
method definition.&lt;br /&gt;
|-&lt;br /&gt;
|Modules cannot be inherited and cannot form is-a hierarchy.&lt;br /&gt;
|Interfaces can extend another interface and hence can form &amp;lt;br&amp;gt;&lt;br /&gt;
is-a hierarchy.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Drawbacks===&lt;br /&gt;
The drawbacks of the technique of Mixins are very much debated. Though Mixins provide us with an easy way to write flexible and decoupled code and also help us solve the diamond problem, they pose their own problems. In large programs there could be large number of modules and each module could have loads of methods each performing a certain task. To trace the origin of the methods and to keep in mind the hierarchy is practically impossible. Another issue associated with Mixins pose is silent method overriding. Silent method overriding occurs when different modules are included in a class and each module have a method with the same definition. In such a case, the method which gets executed depends upon the way it is included in the class and this is done without any sort of message to the user (silently). In large scale applications this could certainly be an issue considering the number of modules that would be mixed in and it would be difficult to keep track of the hierarchy in which the modules were included.&lt;br /&gt;
Although Java provides polymorphic behavior with the use of interfaces, they sometimes tend to be very slow. The implementation of interfaces is also limited to public methods and constants with no implementation.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Modules and Interfaces offer many advantages. However, they have their own disadvantages. Modules add more flexibility to object-oriented programming by the use of DRY principle and are easy to maintain.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
1. http://ruby.about.com/od/beginningruby/a/mixin.htm/ &amp;lt;br&amp;gt;&lt;br /&gt;
2. http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/ &amp;lt;br&amp;gt;&lt;br /&gt;
3. http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-frameworks/ &amp;lt;br&amp;gt;&lt;br /&gt;
4. http://pg-server.csc.ncsu.edu/mediawiki/index.php?title=CSC/ECE_517_Fall_2010/ch3_3b_sv&amp;amp;printable=yes &amp;lt;br&amp;gt;&lt;br /&gt;
5. http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2c_ds &amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rgovind2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1a_1w16_br&amp;diff=63748</id>
		<title>CSC/ECE 517 Fall 2012/ch1a 1w16 br</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1a_1w16_br&amp;diff=63748"/>
		<updated>2012-09-12T22:12:43Z</updated>

		<summary type="html">&lt;p&gt;Rgovind2: /* Mixins */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Overview==&lt;br /&gt;
&lt;br /&gt;
Mixins are Ruby modules. A module is a grouping of objects under a single name. The objects may be constants, methods, classes, or other modules. Modules are defined in Ruby using the keyword module. They can be incorporated into a class with Ruby’s include statement. This is called a mixin.&lt;br /&gt;
Modules are for sharing behavior (methods), while classes are for modeling relationship between objects. Ruby classes can Mixin a module and receive all its methods for free.&lt;br /&gt;
An Interface in Java programming language is an abstract type that classes must implement. Interfaces are declared using the interface keyword, and may only contain method signature and constant declarations.&lt;br /&gt;
A class that implements an interface must implement all of the methods described in the interface, or be an abstract class.&lt;br /&gt;
&lt;br /&gt;
==Definitions==&lt;br /&gt;
===Mixins===&lt;br /&gt;
	Modules provide a structure to collect classes, methods, and constants into a single, separately defined unit. They provide the simplest and an elegant way to adhere to the Don't repeat yourself&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Don't_repeat_yourself Don't_repeat_yourself]&amp;lt;/ref&amp;gt; principle. This is useful so as to avoid clashes with existing classes, methods, and constants.&lt;br /&gt;
Modules are defined in Ruby using the module keyword.&lt;br /&gt;
A Mixin is a class that is mixed with a module or a set of modules i.e. the implementation of the modules and the class are intertwined and combined together &amp;lt;ref&amp;gt;[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html Mixins] &amp;lt;/ref&amp;gt;.&lt;br /&gt;
This gives us a neat and controlled way of adding new functionality to classes. Modules are similar to classes in that they hold a collection of methods, variables constants and other modules and class definitions. However, the real usage of a Mixin is exploited when the code in the Mixin starts to interact with code in the class that uses it.&lt;br /&gt;
Below is an example for modules and Mixins:&lt;br /&gt;
The module ParkingSlot consists of the methods park and unpark&lt;br /&gt;
 &lt;br /&gt;
 module ParkingSlot&lt;br /&gt;
       def park&lt;br /&gt;
          #code to park the car&lt;br /&gt;
       end&lt;br /&gt;
       def unpark&lt;br /&gt;
          #code to unpark the car&lt;br /&gt;
       end&lt;br /&gt;
 end&lt;br /&gt;
The module ParkingMeter consists of the methods calculate and printreceipt&lt;br /&gt;
&lt;br /&gt;
 module ParkingMeter&lt;br /&gt;
       def calculate&lt;br /&gt;
          #code to calculate the parking fee&lt;br /&gt;
       end&lt;br /&gt;
       def printreceipt&lt;br /&gt;
          #code to print the receipt&lt;br /&gt;
       end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
When a class includes a module via include Module Name, all the methods on that module become instance methods on the class.&lt;br /&gt;
&lt;br /&gt;
 class Parking&lt;br /&gt;
      include ParkingSlot&lt;br /&gt;
      include ParkingMeter&lt;br /&gt;
      def printcardetails&lt;br /&gt;
          #code to print the details and type of car&lt;br /&gt;
      end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
The modules ParkingSlot and ParkingMeter are “included” in the Parking class. All the variables and the instance methods of the modules ParkingSlot and ParkingMeter are now said to be “mixed in” into the class. It is now possible for class instances to use these methods defined in the modules as and when required.&lt;br /&gt;
 p=Parking.new   &lt;br /&gt;
 p.park            # -&amp;gt; Method park from module ParkingSlot&lt;br /&gt;
 p.unpark          # -&amp;gt; Method unpark from module ParkingSlot&lt;br /&gt;
 p.calculate       # -&amp;gt; Method calculate from module ParkingMeter&lt;br /&gt;
 p.printreceipt    # -&amp;gt; Method printreceipt from module ParkingMeter&lt;br /&gt;
 p.printcardetails # -&amp;gt; Calls printCarDetails from class&lt;br /&gt;
The class Parking inherits from both the modules and the module methods are now available in Parking. Hence, it is now possible to use the methods with an instance of the Parking class p.&lt;br /&gt;
Hence, Mixins can be thought of taking different methods and variables defined in different modules making them available as instance methods in the class as well, thereby extending the class’ functionality. Effectively mixed in modules behave as superclass. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:ParkingLot.PNG| center]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Interfaces===&lt;br /&gt;
In an object-oriented programming languages such as Java, C#, an interface&amp;lt;ref&amp;gt;[http://download.oracle.com/javase/tutorial/ Interfaces] &amp;lt;/ref&amp;gt; is a reference type, similar to a class that can contain only constants, method signatures, and nested types. There are no method bodies. Interfaces cannot be instantiated and they can only be implemented by classes or extended by other interfaces. All methods are public by default and any fields declared in an interface are by default static and final.&lt;br /&gt;
Example:&lt;br /&gt;
 public interface ParkingLot {&lt;br /&gt;
       boolean add(Car car, int slot);&lt;br /&gt;
       boolean remove(Car car);&lt;br /&gt;
       boolean findCar(Int number);&lt;br /&gt;
       int freeSlotsCount();&lt;br /&gt;
       int occupiedSlots();&lt;br /&gt;
       int parkingType();&lt;br /&gt;
       int calculateParkingFee(int hours, int charge, int duration);&lt;br /&gt;
       int findParkingSlot();&lt;br /&gt;
 }&lt;br /&gt;
 public class Mall implements ParkingLot {&lt;br /&gt;
       boolean add(Car car, int slot) { //... Code to add a car into the parking lot... }&lt;br /&gt;
       boolean remove(Car car){ // do something }&lt;br /&gt;
       boolean findCar(Int number){ // do something }&lt;br /&gt;
       int freeSlotsCount(){ // do something }&lt;br /&gt;
       int occupiedSlots(){ // do something }&lt;br /&gt;
       int parkingType(){ // do something }&lt;br /&gt;
       int calculateParkingFee(int hours, int charge, int duration){ // do something }&lt;br /&gt;
       int findParkingSlot(){ // do something }&lt;br /&gt;
 }&lt;br /&gt;
Interfaces play another important role in the object-oriented programming language. Interfaces are not part of the class hierarchy, although they work in combination with classes. Interfaces provide an alternative for languages like Java that does not allow multiple inheritance .&lt;br /&gt;
&lt;br /&gt;
==Mixins vs. Interfaces==&lt;br /&gt;
===Similarities and Differences===&lt;br /&gt;
	Mixins and Interfaces can be considered as a block of code having some variables and methods that are used in a class. The key difference between them is that a Mixin can contain method definitions whereas an Interface cannot.&lt;br /&gt;
&lt;br /&gt;
====Multiple method definitions:====&lt;br /&gt;
	In a class, when more than one module is Mixed in where some modules have overloaded methods, the method from the module that was last Mixed in into the class will be executed.&lt;br /&gt;
 module Frog&lt;br /&gt;
    def sound()&lt;br /&gt;
      puts &amp;quot;croak...&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Dinosaur&lt;br /&gt;
   def sound()&lt;br /&gt;
     puts &amp;quot;roar...&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
       include Frog&lt;br /&gt;
       include Dinosaur&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 Frogosaur.new.sound &amp;lt;br&amp;gt;	&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 roar...&amp;lt;br&amp;gt;&lt;br /&gt;
This is because the class Frogosaur will have only one definition for each method. The users have the flexibility to make use of methods from different modules by rearranging the order of include.&lt;br /&gt;
Interfaces, on the other hand, do not offer the flexibility to have different implementation for a same method. This is because the class implementing the interface can provide only one implementation for each method. Hence there exists only one method definition.&lt;br /&gt;
 public interface Frog {&lt;br /&gt;
     void sound();&lt;br /&gt;
 }&lt;br /&gt;
 public interface Dinasour {&lt;br /&gt;
     void sound();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
 public void sound()&lt;br /&gt;
 {&lt;br /&gt;
     System.out.println(&amp;quot;barks......&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public static void main(String args[])&lt;br /&gt;
 {&lt;br /&gt;
     new Frogosaur().sound();;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 barks...&amp;lt;br&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
====Multiple object behavior:====&lt;br /&gt;
In Ruby, a class’s instance can extend a module and exhibit different behavior than the other instances of the same class.&lt;br /&gt;
&lt;br /&gt;
 module Frog&lt;br /&gt;
  def sound()&lt;br /&gt;
     puts &amp;quot;croak...&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Dinasour&lt;br /&gt;
  def sound()&lt;br /&gt;
    puts &amp;quot;roar...&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
  def sound()&lt;br /&gt;
    puts &amp;quot;barks...&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 beast1= Frogosaur.new&lt;br /&gt;
 beast2= Frogosaur.new&lt;br /&gt;
 beast1.extend(Frog)&lt;br /&gt;
 beast1.sound&lt;br /&gt;
 beast2.sound&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 croak...&amp;lt;br&amp;gt;&lt;br /&gt;
 barks...&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Interfaces are implemented by a class and all instances of the class behave in the same way. They can use only the methods of the class from which they were instantiated.&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
  public void sound()&lt;br /&gt;
  {&lt;br /&gt;
      System.out.println(&amp;quot;Sounds...&amp;quot;);&lt;br /&gt;
  }  &lt;br /&gt;
  public static void main(String args[])&lt;br /&gt;
  {&lt;br /&gt;
      Frogosaur beast1 = new Frogosaur();&lt;br /&gt;
      Frogosaur beast2 = new Frogosaur();&lt;br /&gt;
      object1.sound();&lt;br /&gt;
      object2.sound();&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 Sounds...&amp;lt;br&amp;gt;&lt;br /&gt;
 Sounds...&amp;lt;br&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
====Enumerable – module and interface====&lt;br /&gt;
&lt;br /&gt;
Ruby’s Arrays have several methods defined in them. To use them, the classes need not be a subclass of Array. The classes can include Enumerable and define the each method to use the array’s methods for free.&lt;br /&gt;
&lt;br /&gt;
 class EnumExer&lt;br /&gt;
  include Enumerable&lt;br /&gt;
  def initialize(*arrays)&lt;br /&gt;
    @arrays = arrays&lt;br /&gt;
  end&lt;br /&gt;
  def each&lt;br /&gt;
    @arrays.each { |a| a.each { |x| yield x } }&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 ma = EnumExer.new([4, 1], [3], [2])&lt;br /&gt;
 puts ma.sort&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 1&amp;lt;br&amp;gt;&lt;br /&gt;
 2&amp;lt;br&amp;gt;&lt;br /&gt;
 3&amp;lt;br&amp;gt;&lt;br /&gt;
 4&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can observe that although the arguments passed to the ma instance did not have a similar structure, the sort method was able to identify each entry and sort them. This is because the each method was defined to identify and serve all the entries to the sort method.&lt;br /&gt;
The methods that are defined for Enumerable modules are:&lt;br /&gt;
 [&amp;quot;all?&amp;quot;, &amp;quot;any?&amp;quot;, &amp;quot;collect&amp;quot;, &amp;quot;detect&amp;quot;, &amp;quot;each_with_index&amp;quot;, &amp;quot;entries&amp;quot;,&lt;br /&gt;
 &amp;quot;find&amp;quot;, &amp;quot;find_all&amp;quot;, &amp;quot;grep&amp;quot;, &amp;quot;include?&amp;quot;, &amp;quot;inject&amp;quot;, &amp;quot;map&amp;quot;, &amp;quot;max&amp;quot;,&lt;br /&gt;
 &amp;quot;member?&amp;quot;, &amp;quot;min&amp;quot;, &amp;quot;partition&amp;quot;, &amp;quot;reject&amp;quot;, &amp;quot;select&amp;quot;, &amp;quot;sort&amp;quot;, &amp;quot;sort_by&amp;quot;,&lt;br /&gt;
 &amp;quot;to_a&amp;quot;, &amp;quot;zip&amp;quot;]&lt;br /&gt;
In Java, to use the methods of Enumerable interface, the class should be a subclass of Array.&lt;br /&gt;
&lt;br /&gt;
 public class EnumExer {&lt;br /&gt;
     static String content_string = &amp;quot;zyxwvutsrqponmlkjihgfedcba&amp;quot;;&lt;br /&gt;
     String a=&amp;quot;1&amp;quot;, b=&amp;quot;2&amp;quot;;&lt;br /&gt;
     public static void main(String args[])&lt;br /&gt;
     {&lt;br /&gt;
         char[] content_array = content_string.toCharArray();&lt;br /&gt;
         java.util.Arrays.sort(content_array);&lt;br /&gt;
         content_string=new String(content_array);&lt;br /&gt;
         System.out.println(content_string);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 Abcdefghijklmnopqrstuvwxyz&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, the string object was converted to a character array to be able to use with the sort method.&lt;br /&gt;
&lt;br /&gt;
====Inheritance:====&lt;br /&gt;
Java interfaces can inherit other interfaces but Ruby modules cannot inherit other modules but can contain classes and methods.&lt;br /&gt;
 module Wrapper&lt;br /&gt;
  class Base&lt;br /&gt;
     # Class Body&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Java,&lt;br /&gt;
 public interface Dinasour extends Frog{&lt;br /&gt;
     //variables and method Declarations&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
==== Achieving Multiple Inheritance ====&lt;br /&gt;
Modules and Interfaces help acheive multiple inheritance. Programming languages such as Ruby and Java do not support multiple inheritance. &amp;lt;br&amp;gt;&lt;br /&gt;
Interfaces are used in Java to achieve multiple inheritance. Similarly, modules in Ruby help achieve multiple inheritance.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Ruby,&lt;br /&gt;
&lt;br /&gt;
 module Frog&lt;br /&gt;
   def jump()&lt;br /&gt;
     //Method definition&lt;br /&gt;
    end&lt;br /&gt;
 end &amp;lt;br&amp;gt;&lt;br /&gt;
 module Dinosaur&lt;br /&gt;
  def strength()&lt;br /&gt;
    //Method definition&lt;br /&gt;
  end&lt;br /&gt;
 end &amp;lt;br&amp;gt;&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
      include Frog&lt;br /&gt;
      include Dinosaur&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
In Java,&lt;br /&gt;
 public interface Frog {&lt;br /&gt;
     void jump();&lt;br /&gt;
 } &amp;lt;br&amp;gt;&lt;br /&gt;
 public interface Dinasour {&lt;br /&gt;
     void strength();&lt;br /&gt;
 } &amp;lt;br&amp;gt;&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
  //Method Implementations&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Frogosaur inherits the methods of both frog and dinosaur. &lt;br /&gt;
&lt;br /&gt;
===Tabular Comparison===&lt;br /&gt;
{|border=&amp;quot;2&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Mixins&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Interfaces&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|The number of lines of code is lesser since the &amp;lt;br&amp;gt;&lt;br /&gt;
modules contain the method definition. &lt;br /&gt;
|The number of lines of code is bigger since the &amp;lt;br&amp;gt;&lt;br /&gt;
classes have to define the methods.&lt;br /&gt;
|-&lt;br /&gt;
|More modules in a class makes code less readable.&lt;br /&gt;
|The code is more readable since the class contain the &amp;lt;br&amp;gt;&lt;br /&gt;
method definition.&lt;br /&gt;
|-&lt;br /&gt;
|Modules cannot be inherited and cannot form is-a hierarchy.&lt;br /&gt;
|Interfaces can extend another interface and hence can form &amp;lt;br&amp;gt;&lt;br /&gt;
is-a hierarchy.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Drawbacks===&lt;br /&gt;
The drawbacks of the technique of Mixins are very much debated. Though Mixins provide us with an easy way to write flexible and decoupled code and also help us solve the diamond problem, they pose their own problems. In large programs there could be large number of modules and each module could have loads of methods each performing a certain task. To trace the origin of the methods and to keep in mind the hierarchy is practically impossible. Another issue associated with Mixins pose is silent method overriding. Silent method overriding occurs when different modules are included in a class and each module have a method with the same definition. In such a case, the method which gets executed depends upon the way it is included in the class and this is done without any sort of message to the user (silently). In large scale applications this could certainly be an issue considering the number of modules that would be mixed in and it would be difficult to keep track of the hierarchy in which the modules were included.&lt;br /&gt;
Although Java provides polymorphic behavior with the use of interfaces, they sometimes tend to be very slow. The implementation of interfaces is also limited to public methods and constants with no implementation.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Modules and Interfaces offer many advantages. However, they have their own disadvantages. Modules add more flexibility to object-oriented programming by the use of DRY principle and are easy to maintain.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
1. http://ruby.about.com/od/beginningruby/a/mixin.htm/ &amp;lt;br&amp;gt;&lt;br /&gt;
2. http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/ &amp;lt;br&amp;gt;&lt;br /&gt;
3. http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-frameworks/ &amp;lt;br&amp;gt;&lt;br /&gt;
4. http://pg-server.csc.ncsu.edu/mediawiki/index.php?title=CSC/ECE_517_Fall_2010/ch3_3b_sv&amp;amp;printable=yes &amp;lt;br&amp;gt;&lt;br /&gt;
5. http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2c_ds &amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rgovind2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1a_1w16_br&amp;diff=63747</id>
		<title>CSC/ECE 517 Fall 2012/ch1a 1w16 br</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1a_1w16_br&amp;diff=63747"/>
		<updated>2012-09-12T22:11:41Z</updated>

		<summary type="html">&lt;p&gt;Rgovind2: /* Interfaces */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Overview==&lt;br /&gt;
&lt;br /&gt;
Mixins are Ruby modules. A module is a grouping of objects under a single name. The objects may be constants, methods, classes, or other modules. Modules are defined in Ruby using the keyword module. They can be incorporated into a class with Ruby’s include statement. This is called a mixin.&lt;br /&gt;
Modules are for sharing behavior (methods), while classes are for modeling relationship between objects. Ruby classes can Mixin a module and receive all its methods for free.&lt;br /&gt;
An Interface in Java programming language is an abstract type that classes must implement. Interfaces are declared using the interface keyword, and may only contain method signature and constant declarations.&lt;br /&gt;
A class that implements an interface must implement all of the methods described in the interface, or be an abstract class.&lt;br /&gt;
&lt;br /&gt;
==Definitions==&lt;br /&gt;
===Mixins===&lt;br /&gt;
	Modules provide a structure to collect classes, methods, and constants into a single, separately defined unit. They provide the simplest and an elegant way to adhere to the Don't repeat yourself&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Don't_repeat_yourself Don't_repeat_yourself]&amp;lt;/ref&amp;gt; principle. This is useful so as to avoid clashes with existing classes, methods, and constants.&lt;br /&gt;
Modules are defined in Ruby using the module keyword.&lt;br /&gt;
A Mixin is a class that is mixed with a module or a set of modules i.e. the implementation of the modules and the class are intertwined and combined together [3].&lt;br /&gt;
This gives us a neat and controlled way of adding new functionality to classes. Modules are similar to classes in that they hold a collection of methods, variables constants and other modules and class definitions. However, the real usage of a Mixin is exploited when the code in the Mixin starts to interact with code in the class that uses it.&lt;br /&gt;
Below is an example for modules and Mixins:&lt;br /&gt;
The module ParkingSlot consists of the methods park and unpark&lt;br /&gt;
 &lt;br /&gt;
 module ParkingSlot&lt;br /&gt;
       def park&lt;br /&gt;
          #code to park the car&lt;br /&gt;
       end&lt;br /&gt;
       def unpark&lt;br /&gt;
          #code to unpark the car&lt;br /&gt;
       end&lt;br /&gt;
 end&lt;br /&gt;
The module ParkingMeter consists of the methods calculate and printreceipt&lt;br /&gt;
&lt;br /&gt;
 module ParkingMeter&lt;br /&gt;
       def calculate&lt;br /&gt;
          #code to calculate the parking fee&lt;br /&gt;
       end&lt;br /&gt;
       def printreceipt&lt;br /&gt;
          #code to print the receipt&lt;br /&gt;
       end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
When a class includes a module via include Module Name, all the methods on that module become instance methods on the class.&lt;br /&gt;
&lt;br /&gt;
 class Parking&lt;br /&gt;
      include ParkingSlot&lt;br /&gt;
      include ParkingMeter&lt;br /&gt;
      def printcardetails&lt;br /&gt;
          #code to print the details and type of car&lt;br /&gt;
      end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
The modules ParkingSlot and ParkingMeter are “included” in the Parking class. All the variables and the instance methods of the modules ParkingSlot and ParkingMeter are now said to be “mixed in” into the class. It is now possible for class instances to use these methods defined in the modules as and when required.&lt;br /&gt;
 p=Parking.new   &lt;br /&gt;
 p.park            # -&amp;gt; Method park from module ParkingSlot&lt;br /&gt;
 p.unpark          # -&amp;gt; Method unpark from module ParkingSlot&lt;br /&gt;
 p.calculate       # -&amp;gt; Method calculate from module ParkingMeter&lt;br /&gt;
 p.printreceipt    # -&amp;gt; Method printreceipt from module ParkingMeter&lt;br /&gt;
 p.printcardetails # -&amp;gt; Calls printCarDetails from class&lt;br /&gt;
The class Parking inherits from both the modules and the module methods are now available in Parking. Hence, it is now possible to use the methods with an instance of the Parking class p.&lt;br /&gt;
Hence, Mixins can be thought of taking different methods and variables defined in different modules making them available as instance methods in the class as well, thereby extending the class’ functionality. Effectively mixed in modules behave as superclass. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:ParkingLot.PNG| center]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Interfaces===&lt;br /&gt;
In an object-oriented programming languages such as Java, C#, an interface&amp;lt;ref&amp;gt;[http://download.oracle.com/javase/tutorial/ Interfaces] &amp;lt;/ref&amp;gt; is a reference type, similar to a class that can contain only constants, method signatures, and nested types. There are no method bodies. Interfaces cannot be instantiated and they can only be implemented by classes or extended by other interfaces. All methods are public by default and any fields declared in an interface are by default static and final.&lt;br /&gt;
Example:&lt;br /&gt;
 public interface ParkingLot {&lt;br /&gt;
       boolean add(Car car, int slot);&lt;br /&gt;
       boolean remove(Car car);&lt;br /&gt;
       boolean findCar(Int number);&lt;br /&gt;
       int freeSlotsCount();&lt;br /&gt;
       int occupiedSlots();&lt;br /&gt;
       int parkingType();&lt;br /&gt;
       int calculateParkingFee(int hours, int charge, int duration);&lt;br /&gt;
       int findParkingSlot();&lt;br /&gt;
 }&lt;br /&gt;
 public class Mall implements ParkingLot {&lt;br /&gt;
       boolean add(Car car, int slot) { //... Code to add a car into the parking lot... }&lt;br /&gt;
       boolean remove(Car car){ // do something }&lt;br /&gt;
       boolean findCar(Int number){ // do something }&lt;br /&gt;
       int freeSlotsCount(){ // do something }&lt;br /&gt;
       int occupiedSlots(){ // do something }&lt;br /&gt;
       int parkingType(){ // do something }&lt;br /&gt;
       int calculateParkingFee(int hours, int charge, int duration){ // do something }&lt;br /&gt;
       int findParkingSlot(){ // do something }&lt;br /&gt;
 }&lt;br /&gt;
Interfaces play another important role in the object-oriented programming language. Interfaces are not part of the class hierarchy, although they work in combination with classes. Interfaces provide an alternative for languages like Java that does not allow multiple inheritance .&lt;br /&gt;
&lt;br /&gt;
==Mixins vs. Interfaces==&lt;br /&gt;
===Similarities and Differences===&lt;br /&gt;
	Mixins and Interfaces can be considered as a block of code having some variables and methods that are used in a class. The key difference between them is that a Mixin can contain method definitions whereas an Interface cannot.&lt;br /&gt;
&lt;br /&gt;
====Multiple method definitions:====&lt;br /&gt;
	In a class, when more than one module is Mixed in where some modules have overloaded methods, the method from the module that was last Mixed in into the class will be executed.&lt;br /&gt;
 module Frog&lt;br /&gt;
    def sound()&lt;br /&gt;
      puts &amp;quot;croak...&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Dinosaur&lt;br /&gt;
   def sound()&lt;br /&gt;
     puts &amp;quot;roar...&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
       include Frog&lt;br /&gt;
       include Dinosaur&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 Frogosaur.new.sound &amp;lt;br&amp;gt;	&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 roar...&amp;lt;br&amp;gt;&lt;br /&gt;
This is because the class Frogosaur will have only one definition for each method. The users have the flexibility to make use of methods from different modules by rearranging the order of include.&lt;br /&gt;
Interfaces, on the other hand, do not offer the flexibility to have different implementation for a same method. This is because the class implementing the interface can provide only one implementation for each method. Hence there exists only one method definition.&lt;br /&gt;
 public interface Frog {&lt;br /&gt;
     void sound();&lt;br /&gt;
 }&lt;br /&gt;
 public interface Dinasour {&lt;br /&gt;
     void sound();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
 public void sound()&lt;br /&gt;
 {&lt;br /&gt;
     System.out.println(&amp;quot;barks......&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public static void main(String args[])&lt;br /&gt;
 {&lt;br /&gt;
     new Frogosaur().sound();;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 barks...&amp;lt;br&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
====Multiple object behavior:====&lt;br /&gt;
In Ruby, a class’s instance can extend a module and exhibit different behavior than the other instances of the same class.&lt;br /&gt;
&lt;br /&gt;
 module Frog&lt;br /&gt;
  def sound()&lt;br /&gt;
     puts &amp;quot;croak...&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Dinasour&lt;br /&gt;
  def sound()&lt;br /&gt;
    puts &amp;quot;roar...&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
  def sound()&lt;br /&gt;
    puts &amp;quot;barks...&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 beast1= Frogosaur.new&lt;br /&gt;
 beast2= Frogosaur.new&lt;br /&gt;
 beast1.extend(Frog)&lt;br /&gt;
 beast1.sound&lt;br /&gt;
 beast2.sound&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 croak...&amp;lt;br&amp;gt;&lt;br /&gt;
 barks...&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Interfaces are implemented by a class and all instances of the class behave in the same way. They can use only the methods of the class from which they were instantiated.&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
  public void sound()&lt;br /&gt;
  {&lt;br /&gt;
      System.out.println(&amp;quot;Sounds...&amp;quot;);&lt;br /&gt;
  }  &lt;br /&gt;
  public static void main(String args[])&lt;br /&gt;
  {&lt;br /&gt;
      Frogosaur beast1 = new Frogosaur();&lt;br /&gt;
      Frogosaur beast2 = new Frogosaur();&lt;br /&gt;
      object1.sound();&lt;br /&gt;
      object2.sound();&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 Sounds...&amp;lt;br&amp;gt;&lt;br /&gt;
 Sounds...&amp;lt;br&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
====Enumerable – module and interface====&lt;br /&gt;
&lt;br /&gt;
Ruby’s Arrays have several methods defined in them. To use them, the classes need not be a subclass of Array. The classes can include Enumerable and define the each method to use the array’s methods for free.&lt;br /&gt;
&lt;br /&gt;
 class EnumExer&lt;br /&gt;
  include Enumerable&lt;br /&gt;
  def initialize(*arrays)&lt;br /&gt;
    @arrays = arrays&lt;br /&gt;
  end&lt;br /&gt;
  def each&lt;br /&gt;
    @arrays.each { |a| a.each { |x| yield x } }&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 ma = EnumExer.new([4, 1], [3], [2])&lt;br /&gt;
 puts ma.sort&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 1&amp;lt;br&amp;gt;&lt;br /&gt;
 2&amp;lt;br&amp;gt;&lt;br /&gt;
 3&amp;lt;br&amp;gt;&lt;br /&gt;
 4&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can observe that although the arguments passed to the ma instance did not have a similar structure, the sort method was able to identify each entry and sort them. This is because the each method was defined to identify and serve all the entries to the sort method.&lt;br /&gt;
The methods that are defined for Enumerable modules are:&lt;br /&gt;
 [&amp;quot;all?&amp;quot;, &amp;quot;any?&amp;quot;, &amp;quot;collect&amp;quot;, &amp;quot;detect&amp;quot;, &amp;quot;each_with_index&amp;quot;, &amp;quot;entries&amp;quot;,&lt;br /&gt;
 &amp;quot;find&amp;quot;, &amp;quot;find_all&amp;quot;, &amp;quot;grep&amp;quot;, &amp;quot;include?&amp;quot;, &amp;quot;inject&amp;quot;, &amp;quot;map&amp;quot;, &amp;quot;max&amp;quot;,&lt;br /&gt;
 &amp;quot;member?&amp;quot;, &amp;quot;min&amp;quot;, &amp;quot;partition&amp;quot;, &amp;quot;reject&amp;quot;, &amp;quot;select&amp;quot;, &amp;quot;sort&amp;quot;, &amp;quot;sort_by&amp;quot;,&lt;br /&gt;
 &amp;quot;to_a&amp;quot;, &amp;quot;zip&amp;quot;]&lt;br /&gt;
In Java, to use the methods of Enumerable interface, the class should be a subclass of Array.&lt;br /&gt;
&lt;br /&gt;
 public class EnumExer {&lt;br /&gt;
     static String content_string = &amp;quot;zyxwvutsrqponmlkjihgfedcba&amp;quot;;&lt;br /&gt;
     String a=&amp;quot;1&amp;quot;, b=&amp;quot;2&amp;quot;;&lt;br /&gt;
     public static void main(String args[])&lt;br /&gt;
     {&lt;br /&gt;
         char[] content_array = content_string.toCharArray();&lt;br /&gt;
         java.util.Arrays.sort(content_array);&lt;br /&gt;
         content_string=new String(content_array);&lt;br /&gt;
         System.out.println(content_string);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 Abcdefghijklmnopqrstuvwxyz&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, the string object was converted to a character array to be able to use with the sort method.&lt;br /&gt;
&lt;br /&gt;
====Inheritance:====&lt;br /&gt;
Java interfaces can inherit other interfaces but Ruby modules cannot inherit other modules but can contain classes and methods.&lt;br /&gt;
 module Wrapper&lt;br /&gt;
  class Base&lt;br /&gt;
     # Class Body&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Java,&lt;br /&gt;
 public interface Dinasour extends Frog{&lt;br /&gt;
     //variables and method Declarations&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
==== Achieving Multiple Inheritance ====&lt;br /&gt;
Modules and Interfaces help acheive multiple inheritance. Programming languages such as Ruby and Java do not support multiple inheritance. &amp;lt;br&amp;gt;&lt;br /&gt;
Interfaces are used in Java to achieve multiple inheritance. Similarly, modules in Ruby help achieve multiple inheritance.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Ruby,&lt;br /&gt;
&lt;br /&gt;
 module Frog&lt;br /&gt;
   def jump()&lt;br /&gt;
     //Method definition&lt;br /&gt;
    end&lt;br /&gt;
 end &amp;lt;br&amp;gt;&lt;br /&gt;
 module Dinosaur&lt;br /&gt;
  def strength()&lt;br /&gt;
    //Method definition&lt;br /&gt;
  end&lt;br /&gt;
 end &amp;lt;br&amp;gt;&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
      include Frog&lt;br /&gt;
      include Dinosaur&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
In Java,&lt;br /&gt;
 public interface Frog {&lt;br /&gt;
     void jump();&lt;br /&gt;
 } &amp;lt;br&amp;gt;&lt;br /&gt;
 public interface Dinasour {&lt;br /&gt;
     void strength();&lt;br /&gt;
 } &amp;lt;br&amp;gt;&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
  //Method Implementations&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Frogosaur inherits the methods of both frog and dinosaur. &lt;br /&gt;
&lt;br /&gt;
===Tabular Comparison===&lt;br /&gt;
{|border=&amp;quot;2&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Mixins&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Interfaces&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|The number of lines of code is lesser since the &amp;lt;br&amp;gt;&lt;br /&gt;
modules contain the method definition. &lt;br /&gt;
|The number of lines of code is bigger since the &amp;lt;br&amp;gt;&lt;br /&gt;
classes have to define the methods.&lt;br /&gt;
|-&lt;br /&gt;
|More modules in a class makes code less readable.&lt;br /&gt;
|The code is more readable since the class contain the &amp;lt;br&amp;gt;&lt;br /&gt;
method definition.&lt;br /&gt;
|-&lt;br /&gt;
|Modules cannot be inherited and cannot form is-a hierarchy.&lt;br /&gt;
|Interfaces can extend another interface and hence can form &amp;lt;br&amp;gt;&lt;br /&gt;
is-a hierarchy.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Drawbacks===&lt;br /&gt;
The drawbacks of the technique of Mixins are very much debated. Though Mixins provide us with an easy way to write flexible and decoupled code and also help us solve the diamond problem, they pose their own problems. In large programs there could be large number of modules and each module could have loads of methods each performing a certain task. To trace the origin of the methods and to keep in mind the hierarchy is practically impossible. Another issue associated with Mixins pose is silent method overriding. Silent method overriding occurs when different modules are included in a class and each module have a method with the same definition. In such a case, the method which gets executed depends upon the way it is included in the class and this is done without any sort of message to the user (silently). In large scale applications this could certainly be an issue considering the number of modules that would be mixed in and it would be difficult to keep track of the hierarchy in which the modules were included.&lt;br /&gt;
Although Java provides polymorphic behavior with the use of interfaces, they sometimes tend to be very slow. The implementation of interfaces is also limited to public methods and constants with no implementation.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Modules and Interfaces offer many advantages. However, they have their own disadvantages. Modules add more flexibility to object-oriented programming by the use of DRY principle and are easy to maintain.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
1. http://ruby.about.com/od/beginningruby/a/mixin.htm/ &amp;lt;br&amp;gt;&lt;br /&gt;
2. http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/ &amp;lt;br&amp;gt;&lt;br /&gt;
3. http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-frameworks/ &amp;lt;br&amp;gt;&lt;br /&gt;
4. http://pg-server.csc.ncsu.edu/mediawiki/index.php?title=CSC/ECE_517_Fall_2010/ch3_3b_sv&amp;amp;printable=yes &amp;lt;br&amp;gt;&lt;br /&gt;
5. http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2c_ds &amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rgovind2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1a_1w16_br&amp;diff=63746</id>
		<title>CSC/ECE 517 Fall 2012/ch1a 1w16 br</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1a_1w16_br&amp;diff=63746"/>
		<updated>2012-09-12T22:06:08Z</updated>

		<summary type="html">&lt;p&gt;Rgovind2: /* See Also */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Overview==&lt;br /&gt;
&lt;br /&gt;
Mixins are Ruby modules. A module is a grouping of objects under a single name. The objects may be constants, methods, classes, or other modules. Modules are defined in Ruby using the keyword module. They can be incorporated into a class with Ruby’s include statement. This is called a mixin.&lt;br /&gt;
Modules are for sharing behavior (methods), while classes are for modeling relationship between objects. Ruby classes can Mixin a module and receive all its methods for free.&lt;br /&gt;
An Interface in Java programming language is an abstract type that classes must implement. Interfaces are declared using the interface keyword, and may only contain method signature and constant declarations.&lt;br /&gt;
A class that implements an interface must implement all of the methods described in the interface, or be an abstract class.&lt;br /&gt;
&lt;br /&gt;
==Definitions==&lt;br /&gt;
===Mixins===&lt;br /&gt;
	Modules provide a structure to collect classes, methods, and constants into a single, separately defined unit. They provide the simplest and an elegant way to adhere to the Don't repeat yourself&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Don't_repeat_yourself Don't_repeat_yourself]&amp;lt;/ref&amp;gt; principle. This is useful so as to avoid clashes with existing classes, methods, and constants.&lt;br /&gt;
Modules are defined in Ruby using the module keyword.&lt;br /&gt;
A Mixin is a class that is mixed with a module or a set of modules i.e. the implementation of the modules and the class are intertwined and combined together [3].&lt;br /&gt;
This gives us a neat and controlled way of adding new functionality to classes. Modules are similar to classes in that they hold a collection of methods, variables constants and other modules and class definitions. However, the real usage of a Mixin is exploited when the code in the Mixin starts to interact with code in the class that uses it.&lt;br /&gt;
Below is an example for modules and Mixins:&lt;br /&gt;
The module ParkingSlot consists of the methods park and unpark&lt;br /&gt;
 &lt;br /&gt;
 module ParkingSlot&lt;br /&gt;
       def park&lt;br /&gt;
          #code to park the car&lt;br /&gt;
       end&lt;br /&gt;
       def unpark&lt;br /&gt;
          #code to unpark the car&lt;br /&gt;
       end&lt;br /&gt;
 end&lt;br /&gt;
The module ParkingMeter consists of the methods calculate and printreceipt&lt;br /&gt;
&lt;br /&gt;
 module ParkingMeter&lt;br /&gt;
       def calculate&lt;br /&gt;
          #code to calculate the parking fee&lt;br /&gt;
       end&lt;br /&gt;
       def printreceipt&lt;br /&gt;
          #code to print the receipt&lt;br /&gt;
       end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
When a class includes a module via include Module Name, all the methods on that module become instance methods on the class.&lt;br /&gt;
&lt;br /&gt;
 class Parking&lt;br /&gt;
      include ParkingSlot&lt;br /&gt;
      include ParkingMeter&lt;br /&gt;
      def printcardetails&lt;br /&gt;
          #code to print the details and type of car&lt;br /&gt;
      end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
The modules ParkingSlot and ParkingMeter are “included” in the Parking class. All the variables and the instance methods of the modules ParkingSlot and ParkingMeter are now said to be “mixed in” into the class. It is now possible for class instances to use these methods defined in the modules as and when required.&lt;br /&gt;
 p=Parking.new   &lt;br /&gt;
 p.park            # -&amp;gt; Method park from module ParkingSlot&lt;br /&gt;
 p.unpark          # -&amp;gt; Method unpark from module ParkingSlot&lt;br /&gt;
 p.calculate       # -&amp;gt; Method calculate from module ParkingMeter&lt;br /&gt;
 p.printreceipt    # -&amp;gt; Method printreceipt from module ParkingMeter&lt;br /&gt;
 p.printcardetails # -&amp;gt; Calls printCarDetails from class&lt;br /&gt;
The class Parking inherits from both the modules and the module methods are now available in Parking. Hence, it is now possible to use the methods with an instance of the Parking class p.&lt;br /&gt;
Hence, Mixins can be thought of taking different methods and variables defined in different modules making them available as instance methods in the class as well, thereby extending the class’ functionality. Effectively mixed in modules behave as superclass. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:ParkingLot.PNG| center]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Interfaces===&lt;br /&gt;
In an object-oriented programming languages such as Java, C#, an interface[2] is a reference type, similar to a class that can contain only constants, method signatures, and nested types. There are no method bodies. Interfaces cannot be instantiated and they can only be implemented by classes or extended by other interfaces. All methods are public by default and any fields declared in an interface are by default static and final.&lt;br /&gt;
Example:&lt;br /&gt;
 public interface ParkingLot {&lt;br /&gt;
       boolean add(Car car, int slot);&lt;br /&gt;
       boolean remove(Car car);&lt;br /&gt;
       boolean findCar(Int number);&lt;br /&gt;
       int freeSlotsCount();&lt;br /&gt;
       int occupiedSlots();&lt;br /&gt;
       int parkingType();&lt;br /&gt;
       int calculateParkingFee(int hours, int charge, int duration);&lt;br /&gt;
       int findParkingSlot();&lt;br /&gt;
 }&lt;br /&gt;
 public class Mall implements ParkingLot {&lt;br /&gt;
       boolean add(Car car, int slot) { //... Code to add a car into the parking lot... }&lt;br /&gt;
       boolean remove(Car car){ // do something }&lt;br /&gt;
       boolean findCar(Int number){ // do something }&lt;br /&gt;
       int freeSlotsCount(){ // do something }&lt;br /&gt;
       int occupiedSlots(){ // do something }&lt;br /&gt;
       int parkingType(){ // do something }&lt;br /&gt;
       int calculateParkingFee(int hours, int charge, int duration){ // do something }&lt;br /&gt;
       int findParkingSlot(){ // do something }&lt;br /&gt;
 }&lt;br /&gt;
Interfaces play another important role in the object-oriented programming language. Interfaces are not part of the class hierarchy, although they work in combination with classes. Interfaces provide an alternative for languages like Java that does not allow multiple inheritance .&lt;br /&gt;
&lt;br /&gt;
==Mixins vs. Interfaces==&lt;br /&gt;
===Similarities and Differences===&lt;br /&gt;
	Mixins and Interfaces can be considered as a block of code having some variables and methods that are used in a class. The key difference between them is that a Mixin can contain method definitions whereas an Interface cannot.&lt;br /&gt;
&lt;br /&gt;
====Multiple method definitions:====&lt;br /&gt;
	In a class, when more than one module is Mixed in where some modules have overloaded methods, the method from the module that was last Mixed in into the class will be executed.&lt;br /&gt;
 module Frog&lt;br /&gt;
    def sound()&lt;br /&gt;
      puts &amp;quot;croak...&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Dinosaur&lt;br /&gt;
   def sound()&lt;br /&gt;
     puts &amp;quot;roar...&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
       include Frog&lt;br /&gt;
       include Dinosaur&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 Frogosaur.new.sound &amp;lt;br&amp;gt;	&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 roar...&amp;lt;br&amp;gt;&lt;br /&gt;
This is because the class Frogosaur will have only one definition for each method. The users have the flexibility to make use of methods from different modules by rearranging the order of include.&lt;br /&gt;
Interfaces, on the other hand, do not offer the flexibility to have different implementation for a same method. This is because the class implementing the interface can provide only one implementation for each method. Hence there exists only one method definition.&lt;br /&gt;
 public interface Frog {&lt;br /&gt;
     void sound();&lt;br /&gt;
 }&lt;br /&gt;
 public interface Dinasour {&lt;br /&gt;
     void sound();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
 public void sound()&lt;br /&gt;
 {&lt;br /&gt;
     System.out.println(&amp;quot;barks......&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public static void main(String args[])&lt;br /&gt;
 {&lt;br /&gt;
     new Frogosaur().sound();;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 barks...&amp;lt;br&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
====Multiple object behavior:====&lt;br /&gt;
In Ruby, a class’s instance can extend a module and exhibit different behavior than the other instances of the same class.&lt;br /&gt;
&lt;br /&gt;
 module Frog&lt;br /&gt;
  def sound()&lt;br /&gt;
     puts &amp;quot;croak...&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Dinasour&lt;br /&gt;
  def sound()&lt;br /&gt;
    puts &amp;quot;roar...&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
  def sound()&lt;br /&gt;
    puts &amp;quot;barks...&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 beast1= Frogosaur.new&lt;br /&gt;
 beast2= Frogosaur.new&lt;br /&gt;
 beast1.extend(Frog)&lt;br /&gt;
 beast1.sound&lt;br /&gt;
 beast2.sound&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 croak...&amp;lt;br&amp;gt;&lt;br /&gt;
 barks...&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Interfaces are implemented by a class and all instances of the class behave in the same way. They can use only the methods of the class from which they were instantiated.&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
  public void sound()&lt;br /&gt;
  {&lt;br /&gt;
      System.out.println(&amp;quot;Sounds...&amp;quot;);&lt;br /&gt;
  }  &lt;br /&gt;
  public static void main(String args[])&lt;br /&gt;
  {&lt;br /&gt;
      Frogosaur beast1 = new Frogosaur();&lt;br /&gt;
      Frogosaur beast2 = new Frogosaur();&lt;br /&gt;
      object1.sound();&lt;br /&gt;
      object2.sound();&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 Sounds...&amp;lt;br&amp;gt;&lt;br /&gt;
 Sounds...&amp;lt;br&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
====Enumerable – module and interface====&lt;br /&gt;
&lt;br /&gt;
Ruby’s Arrays have several methods defined in them. To use them, the classes need not be a subclass of Array. The classes can include Enumerable and define the each method to use the array’s methods for free.&lt;br /&gt;
&lt;br /&gt;
 class EnumExer&lt;br /&gt;
  include Enumerable&lt;br /&gt;
  def initialize(*arrays)&lt;br /&gt;
    @arrays = arrays&lt;br /&gt;
  end&lt;br /&gt;
  def each&lt;br /&gt;
    @arrays.each { |a| a.each { |x| yield x } }&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 ma = EnumExer.new([4, 1], [3], [2])&lt;br /&gt;
 puts ma.sort&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 1&amp;lt;br&amp;gt;&lt;br /&gt;
 2&amp;lt;br&amp;gt;&lt;br /&gt;
 3&amp;lt;br&amp;gt;&lt;br /&gt;
 4&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can observe that although the arguments passed to the ma instance did not have a similar structure, the sort method was able to identify each entry and sort them. This is because the each method was defined to identify and serve all the entries to the sort method.&lt;br /&gt;
The methods that are defined for Enumerable modules are:&lt;br /&gt;
 [&amp;quot;all?&amp;quot;, &amp;quot;any?&amp;quot;, &amp;quot;collect&amp;quot;, &amp;quot;detect&amp;quot;, &amp;quot;each_with_index&amp;quot;, &amp;quot;entries&amp;quot;,&lt;br /&gt;
 &amp;quot;find&amp;quot;, &amp;quot;find_all&amp;quot;, &amp;quot;grep&amp;quot;, &amp;quot;include?&amp;quot;, &amp;quot;inject&amp;quot;, &amp;quot;map&amp;quot;, &amp;quot;max&amp;quot;,&lt;br /&gt;
 &amp;quot;member?&amp;quot;, &amp;quot;min&amp;quot;, &amp;quot;partition&amp;quot;, &amp;quot;reject&amp;quot;, &amp;quot;select&amp;quot;, &amp;quot;sort&amp;quot;, &amp;quot;sort_by&amp;quot;,&lt;br /&gt;
 &amp;quot;to_a&amp;quot;, &amp;quot;zip&amp;quot;]&lt;br /&gt;
In Java, to use the methods of Enumerable interface, the class should be a subclass of Array.&lt;br /&gt;
&lt;br /&gt;
 public class EnumExer {&lt;br /&gt;
     static String content_string = &amp;quot;zyxwvutsrqponmlkjihgfedcba&amp;quot;;&lt;br /&gt;
     String a=&amp;quot;1&amp;quot;, b=&amp;quot;2&amp;quot;;&lt;br /&gt;
     public static void main(String args[])&lt;br /&gt;
     {&lt;br /&gt;
         char[] content_array = content_string.toCharArray();&lt;br /&gt;
         java.util.Arrays.sort(content_array);&lt;br /&gt;
         content_string=new String(content_array);&lt;br /&gt;
         System.out.println(content_string);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 Abcdefghijklmnopqrstuvwxyz&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, the string object was converted to a character array to be able to use with the sort method.&lt;br /&gt;
&lt;br /&gt;
====Inheritance:====&lt;br /&gt;
Java interfaces can inherit other interfaces but Ruby modules cannot inherit other modules but can contain classes and methods.&lt;br /&gt;
 module Wrapper&lt;br /&gt;
  class Base&lt;br /&gt;
     # Class Body&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Java,&lt;br /&gt;
 public interface Dinasour extends Frog{&lt;br /&gt;
     //variables and method Declarations&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
==== Achieving Multiple Inheritance ====&lt;br /&gt;
Modules and Interfaces help acheive multiple inheritance. Programming languages such as Ruby and Java do not support multiple inheritance. &amp;lt;br&amp;gt;&lt;br /&gt;
Interfaces are used in Java to achieve multiple inheritance. Similarly, modules in Ruby help achieve multiple inheritance.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Ruby,&lt;br /&gt;
&lt;br /&gt;
 module Frog&lt;br /&gt;
   def jump()&lt;br /&gt;
     //Method definition&lt;br /&gt;
    end&lt;br /&gt;
 end &amp;lt;br&amp;gt;&lt;br /&gt;
 module Dinosaur&lt;br /&gt;
  def strength()&lt;br /&gt;
    //Method definition&lt;br /&gt;
  end&lt;br /&gt;
 end &amp;lt;br&amp;gt;&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
      include Frog&lt;br /&gt;
      include Dinosaur&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
In Java,&lt;br /&gt;
 public interface Frog {&lt;br /&gt;
     void jump();&lt;br /&gt;
 } &amp;lt;br&amp;gt;&lt;br /&gt;
 public interface Dinasour {&lt;br /&gt;
     void strength();&lt;br /&gt;
 } &amp;lt;br&amp;gt;&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
  //Method Implementations&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Frogosaur inherits the methods of both frog and dinosaur. &lt;br /&gt;
&lt;br /&gt;
===Tabular Comparison===&lt;br /&gt;
{|border=&amp;quot;2&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Mixins&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Interfaces&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|The number of lines of code is lesser since the &amp;lt;br&amp;gt;&lt;br /&gt;
modules contain the method definition. &lt;br /&gt;
|The number of lines of code is bigger since the &amp;lt;br&amp;gt;&lt;br /&gt;
classes have to define the methods.&lt;br /&gt;
|-&lt;br /&gt;
|More modules in a class makes code less readable.&lt;br /&gt;
|The code is more readable since the class contain the &amp;lt;br&amp;gt;&lt;br /&gt;
method definition.&lt;br /&gt;
|-&lt;br /&gt;
|Modules cannot be inherited and cannot form is-a hierarchy.&lt;br /&gt;
|Interfaces can extend another interface and hence can form &amp;lt;br&amp;gt;&lt;br /&gt;
is-a hierarchy.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Drawbacks===&lt;br /&gt;
The drawbacks of the technique of Mixins are very much debated. Though Mixins provide us with an easy way to write flexible and decoupled code and also help us solve the diamond problem, they pose their own problems. In large programs there could be large number of modules and each module could have loads of methods each performing a certain task. To trace the origin of the methods and to keep in mind the hierarchy is practically impossible. Another issue associated with Mixins pose is silent method overriding. Silent method overriding occurs when different modules are included in a class and each module have a method with the same definition. In such a case, the method which gets executed depends upon the way it is included in the class and this is done without any sort of message to the user (silently). In large scale applications this could certainly be an issue considering the number of modules that would be mixed in and it would be difficult to keep track of the hierarchy in which the modules were included.&lt;br /&gt;
Although Java provides polymorphic behavior with the use of interfaces, they sometimes tend to be very slow. The implementation of interfaces is also limited to public methods and constants with no implementation.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Modules and Interfaces offer many advantages. However, they have their own disadvantages. Modules add more flexibility to object-oriented programming by the use of DRY principle and are easy to maintain.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
1. http://ruby.about.com/od/beginningruby/a/mixin.htm/ &amp;lt;br&amp;gt;&lt;br /&gt;
2. http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/ &amp;lt;br&amp;gt;&lt;br /&gt;
3. http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-frameworks/ &amp;lt;br&amp;gt;&lt;br /&gt;
4. http://pg-server.csc.ncsu.edu/mediawiki/index.php?title=CSC/ECE_517_Fall_2010/ch3_3b_sv&amp;amp;printable=yes &amp;lt;br&amp;gt;&lt;br /&gt;
5. http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2c_ds &amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rgovind2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1a_1w16_br&amp;diff=63745</id>
		<title>CSC/ECE 517 Fall 2012/ch1a 1w16 br</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1a_1w16_br&amp;diff=63745"/>
		<updated>2012-09-12T22:05:55Z</updated>

		<summary type="html">&lt;p&gt;Rgovind2: /* See Also */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Overview==&lt;br /&gt;
&lt;br /&gt;
Mixins are Ruby modules. A module is a grouping of objects under a single name. The objects may be constants, methods, classes, or other modules. Modules are defined in Ruby using the keyword module. They can be incorporated into a class with Ruby’s include statement. This is called a mixin.&lt;br /&gt;
Modules are for sharing behavior (methods), while classes are for modeling relationship between objects. Ruby classes can Mixin a module and receive all its methods for free.&lt;br /&gt;
An Interface in Java programming language is an abstract type that classes must implement. Interfaces are declared using the interface keyword, and may only contain method signature and constant declarations.&lt;br /&gt;
A class that implements an interface must implement all of the methods described in the interface, or be an abstract class.&lt;br /&gt;
&lt;br /&gt;
==Definitions==&lt;br /&gt;
===Mixins===&lt;br /&gt;
	Modules provide a structure to collect classes, methods, and constants into a single, separately defined unit. They provide the simplest and an elegant way to adhere to the Don't repeat yourself&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Don't_repeat_yourself Don't_repeat_yourself]&amp;lt;/ref&amp;gt; principle. This is useful so as to avoid clashes with existing classes, methods, and constants.&lt;br /&gt;
Modules are defined in Ruby using the module keyword.&lt;br /&gt;
A Mixin is a class that is mixed with a module or a set of modules i.e. the implementation of the modules and the class are intertwined and combined together [3].&lt;br /&gt;
This gives us a neat and controlled way of adding new functionality to classes. Modules are similar to classes in that they hold a collection of methods, variables constants and other modules and class definitions. However, the real usage of a Mixin is exploited when the code in the Mixin starts to interact with code in the class that uses it.&lt;br /&gt;
Below is an example for modules and Mixins:&lt;br /&gt;
The module ParkingSlot consists of the methods park and unpark&lt;br /&gt;
 &lt;br /&gt;
 module ParkingSlot&lt;br /&gt;
       def park&lt;br /&gt;
          #code to park the car&lt;br /&gt;
       end&lt;br /&gt;
       def unpark&lt;br /&gt;
          #code to unpark the car&lt;br /&gt;
       end&lt;br /&gt;
 end&lt;br /&gt;
The module ParkingMeter consists of the methods calculate and printreceipt&lt;br /&gt;
&lt;br /&gt;
 module ParkingMeter&lt;br /&gt;
       def calculate&lt;br /&gt;
          #code to calculate the parking fee&lt;br /&gt;
       end&lt;br /&gt;
       def printreceipt&lt;br /&gt;
          #code to print the receipt&lt;br /&gt;
       end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
When a class includes a module via include Module Name, all the methods on that module become instance methods on the class.&lt;br /&gt;
&lt;br /&gt;
 class Parking&lt;br /&gt;
      include ParkingSlot&lt;br /&gt;
      include ParkingMeter&lt;br /&gt;
      def printcardetails&lt;br /&gt;
          #code to print the details and type of car&lt;br /&gt;
      end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
The modules ParkingSlot and ParkingMeter are “included” in the Parking class. All the variables and the instance methods of the modules ParkingSlot and ParkingMeter are now said to be “mixed in” into the class. It is now possible for class instances to use these methods defined in the modules as and when required.&lt;br /&gt;
 p=Parking.new   &lt;br /&gt;
 p.park            # -&amp;gt; Method park from module ParkingSlot&lt;br /&gt;
 p.unpark          # -&amp;gt; Method unpark from module ParkingSlot&lt;br /&gt;
 p.calculate       # -&amp;gt; Method calculate from module ParkingMeter&lt;br /&gt;
 p.printreceipt    # -&amp;gt; Method printreceipt from module ParkingMeter&lt;br /&gt;
 p.printcardetails # -&amp;gt; Calls printCarDetails from class&lt;br /&gt;
The class Parking inherits from both the modules and the module methods are now available in Parking. Hence, it is now possible to use the methods with an instance of the Parking class p.&lt;br /&gt;
Hence, Mixins can be thought of taking different methods and variables defined in different modules making them available as instance methods in the class as well, thereby extending the class’ functionality. Effectively mixed in modules behave as superclass. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:ParkingLot.PNG| center]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Interfaces===&lt;br /&gt;
In an object-oriented programming languages such as Java, C#, an interface[2] is a reference type, similar to a class that can contain only constants, method signatures, and nested types. There are no method bodies. Interfaces cannot be instantiated and they can only be implemented by classes or extended by other interfaces. All methods are public by default and any fields declared in an interface are by default static and final.&lt;br /&gt;
Example:&lt;br /&gt;
 public interface ParkingLot {&lt;br /&gt;
       boolean add(Car car, int slot);&lt;br /&gt;
       boolean remove(Car car);&lt;br /&gt;
       boolean findCar(Int number);&lt;br /&gt;
       int freeSlotsCount();&lt;br /&gt;
       int occupiedSlots();&lt;br /&gt;
       int parkingType();&lt;br /&gt;
       int calculateParkingFee(int hours, int charge, int duration);&lt;br /&gt;
       int findParkingSlot();&lt;br /&gt;
 }&lt;br /&gt;
 public class Mall implements ParkingLot {&lt;br /&gt;
       boolean add(Car car, int slot) { //... Code to add a car into the parking lot... }&lt;br /&gt;
       boolean remove(Car car){ // do something }&lt;br /&gt;
       boolean findCar(Int number){ // do something }&lt;br /&gt;
       int freeSlotsCount(){ // do something }&lt;br /&gt;
       int occupiedSlots(){ // do something }&lt;br /&gt;
       int parkingType(){ // do something }&lt;br /&gt;
       int calculateParkingFee(int hours, int charge, int duration){ // do something }&lt;br /&gt;
       int findParkingSlot(){ // do something }&lt;br /&gt;
 }&lt;br /&gt;
Interfaces play another important role in the object-oriented programming language. Interfaces are not part of the class hierarchy, although they work in combination with classes. Interfaces provide an alternative for languages like Java that does not allow multiple inheritance .&lt;br /&gt;
&lt;br /&gt;
==Mixins vs. Interfaces==&lt;br /&gt;
===Similarities and Differences===&lt;br /&gt;
	Mixins and Interfaces can be considered as a block of code having some variables and methods that are used in a class. The key difference between them is that a Mixin can contain method definitions whereas an Interface cannot.&lt;br /&gt;
&lt;br /&gt;
====Multiple method definitions:====&lt;br /&gt;
	In a class, when more than one module is Mixed in where some modules have overloaded methods, the method from the module that was last Mixed in into the class will be executed.&lt;br /&gt;
 module Frog&lt;br /&gt;
    def sound()&lt;br /&gt;
      puts &amp;quot;croak...&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Dinosaur&lt;br /&gt;
   def sound()&lt;br /&gt;
     puts &amp;quot;roar...&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
       include Frog&lt;br /&gt;
       include Dinosaur&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 Frogosaur.new.sound &amp;lt;br&amp;gt;	&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 roar...&amp;lt;br&amp;gt;&lt;br /&gt;
This is because the class Frogosaur will have only one definition for each method. The users have the flexibility to make use of methods from different modules by rearranging the order of include.&lt;br /&gt;
Interfaces, on the other hand, do not offer the flexibility to have different implementation for a same method. This is because the class implementing the interface can provide only one implementation for each method. Hence there exists only one method definition.&lt;br /&gt;
 public interface Frog {&lt;br /&gt;
     void sound();&lt;br /&gt;
 }&lt;br /&gt;
 public interface Dinasour {&lt;br /&gt;
     void sound();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
 public void sound()&lt;br /&gt;
 {&lt;br /&gt;
     System.out.println(&amp;quot;barks......&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public static void main(String args[])&lt;br /&gt;
 {&lt;br /&gt;
     new Frogosaur().sound();;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 barks...&amp;lt;br&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
====Multiple object behavior:====&lt;br /&gt;
In Ruby, a class’s instance can extend a module and exhibit different behavior than the other instances of the same class.&lt;br /&gt;
&lt;br /&gt;
 module Frog&lt;br /&gt;
  def sound()&lt;br /&gt;
     puts &amp;quot;croak...&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Dinasour&lt;br /&gt;
  def sound()&lt;br /&gt;
    puts &amp;quot;roar...&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
  def sound()&lt;br /&gt;
    puts &amp;quot;barks...&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 beast1= Frogosaur.new&lt;br /&gt;
 beast2= Frogosaur.new&lt;br /&gt;
 beast1.extend(Frog)&lt;br /&gt;
 beast1.sound&lt;br /&gt;
 beast2.sound&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 croak...&amp;lt;br&amp;gt;&lt;br /&gt;
 barks...&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Interfaces are implemented by a class and all instances of the class behave in the same way. They can use only the methods of the class from which they were instantiated.&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
  public void sound()&lt;br /&gt;
  {&lt;br /&gt;
      System.out.println(&amp;quot;Sounds...&amp;quot;);&lt;br /&gt;
  }  &lt;br /&gt;
  public static void main(String args[])&lt;br /&gt;
  {&lt;br /&gt;
      Frogosaur beast1 = new Frogosaur();&lt;br /&gt;
      Frogosaur beast2 = new Frogosaur();&lt;br /&gt;
      object1.sound();&lt;br /&gt;
      object2.sound();&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 Sounds...&amp;lt;br&amp;gt;&lt;br /&gt;
 Sounds...&amp;lt;br&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
====Enumerable – module and interface====&lt;br /&gt;
&lt;br /&gt;
Ruby’s Arrays have several methods defined in them. To use them, the classes need not be a subclass of Array. The classes can include Enumerable and define the each method to use the array’s methods for free.&lt;br /&gt;
&lt;br /&gt;
 class EnumExer&lt;br /&gt;
  include Enumerable&lt;br /&gt;
  def initialize(*arrays)&lt;br /&gt;
    @arrays = arrays&lt;br /&gt;
  end&lt;br /&gt;
  def each&lt;br /&gt;
    @arrays.each { |a| a.each { |x| yield x } }&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 ma = EnumExer.new([4, 1], [3], [2])&lt;br /&gt;
 puts ma.sort&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 1&amp;lt;br&amp;gt;&lt;br /&gt;
 2&amp;lt;br&amp;gt;&lt;br /&gt;
 3&amp;lt;br&amp;gt;&lt;br /&gt;
 4&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can observe that although the arguments passed to the ma instance did not have a similar structure, the sort method was able to identify each entry and sort them. This is because the each method was defined to identify and serve all the entries to the sort method.&lt;br /&gt;
The methods that are defined for Enumerable modules are:&lt;br /&gt;
 [&amp;quot;all?&amp;quot;, &amp;quot;any?&amp;quot;, &amp;quot;collect&amp;quot;, &amp;quot;detect&amp;quot;, &amp;quot;each_with_index&amp;quot;, &amp;quot;entries&amp;quot;,&lt;br /&gt;
 &amp;quot;find&amp;quot;, &amp;quot;find_all&amp;quot;, &amp;quot;grep&amp;quot;, &amp;quot;include?&amp;quot;, &amp;quot;inject&amp;quot;, &amp;quot;map&amp;quot;, &amp;quot;max&amp;quot;,&lt;br /&gt;
 &amp;quot;member?&amp;quot;, &amp;quot;min&amp;quot;, &amp;quot;partition&amp;quot;, &amp;quot;reject&amp;quot;, &amp;quot;select&amp;quot;, &amp;quot;sort&amp;quot;, &amp;quot;sort_by&amp;quot;,&lt;br /&gt;
 &amp;quot;to_a&amp;quot;, &amp;quot;zip&amp;quot;]&lt;br /&gt;
In Java, to use the methods of Enumerable interface, the class should be a subclass of Array.&lt;br /&gt;
&lt;br /&gt;
 public class EnumExer {&lt;br /&gt;
     static String content_string = &amp;quot;zyxwvutsrqponmlkjihgfedcba&amp;quot;;&lt;br /&gt;
     String a=&amp;quot;1&amp;quot;, b=&amp;quot;2&amp;quot;;&lt;br /&gt;
     public static void main(String args[])&lt;br /&gt;
     {&lt;br /&gt;
         char[] content_array = content_string.toCharArray();&lt;br /&gt;
         java.util.Arrays.sort(content_array);&lt;br /&gt;
         content_string=new String(content_array);&lt;br /&gt;
         System.out.println(content_string);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 Abcdefghijklmnopqrstuvwxyz&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, the string object was converted to a character array to be able to use with the sort method.&lt;br /&gt;
&lt;br /&gt;
====Inheritance:====&lt;br /&gt;
Java interfaces can inherit other interfaces but Ruby modules cannot inherit other modules but can contain classes and methods.&lt;br /&gt;
 module Wrapper&lt;br /&gt;
  class Base&lt;br /&gt;
     # Class Body&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Java,&lt;br /&gt;
 public interface Dinasour extends Frog{&lt;br /&gt;
     //variables and method Declarations&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
==== Achieving Multiple Inheritance ====&lt;br /&gt;
Modules and Interfaces help acheive multiple inheritance. Programming languages such as Ruby and Java do not support multiple inheritance. &amp;lt;br&amp;gt;&lt;br /&gt;
Interfaces are used in Java to achieve multiple inheritance. Similarly, modules in Ruby help achieve multiple inheritance.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Ruby,&lt;br /&gt;
&lt;br /&gt;
 module Frog&lt;br /&gt;
   def jump()&lt;br /&gt;
     //Method definition&lt;br /&gt;
    end&lt;br /&gt;
 end &amp;lt;br&amp;gt;&lt;br /&gt;
 module Dinosaur&lt;br /&gt;
  def strength()&lt;br /&gt;
    //Method definition&lt;br /&gt;
  end&lt;br /&gt;
 end &amp;lt;br&amp;gt;&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
      include Frog&lt;br /&gt;
      include Dinosaur&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
In Java,&lt;br /&gt;
 public interface Frog {&lt;br /&gt;
     void jump();&lt;br /&gt;
 } &amp;lt;br&amp;gt;&lt;br /&gt;
 public interface Dinasour {&lt;br /&gt;
     void strength();&lt;br /&gt;
 } &amp;lt;br&amp;gt;&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
  //Method Implementations&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Frogosaur inherits the methods of both frog and dinosaur. &lt;br /&gt;
&lt;br /&gt;
===Tabular Comparison===&lt;br /&gt;
{|border=&amp;quot;2&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Mixins&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Interfaces&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|The number of lines of code is lesser since the &amp;lt;br&amp;gt;&lt;br /&gt;
modules contain the method definition. &lt;br /&gt;
|The number of lines of code is bigger since the &amp;lt;br&amp;gt;&lt;br /&gt;
classes have to define the methods.&lt;br /&gt;
|-&lt;br /&gt;
|More modules in a class makes code less readable.&lt;br /&gt;
|The code is more readable since the class contain the &amp;lt;br&amp;gt;&lt;br /&gt;
method definition.&lt;br /&gt;
|-&lt;br /&gt;
|Modules cannot be inherited and cannot form is-a hierarchy.&lt;br /&gt;
|Interfaces can extend another interface and hence can form &amp;lt;br&amp;gt;&lt;br /&gt;
is-a hierarchy.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Drawbacks===&lt;br /&gt;
The drawbacks of the technique of Mixins are very much debated. Though Mixins provide us with an easy way to write flexible and decoupled code and also help us solve the diamond problem, they pose their own problems. In large programs there could be large number of modules and each module could have loads of methods each performing a certain task. To trace the origin of the methods and to keep in mind the hierarchy is practically impossible. Another issue associated with Mixins pose is silent method overriding. Silent method overriding occurs when different modules are included in a class and each module have a method with the same definition. In such a case, the method which gets executed depends upon the way it is included in the class and this is done without any sort of message to the user (silently). In large scale applications this could certainly be an issue considering the number of modules that would be mixed in and it would be difficult to keep track of the hierarchy in which the modules were included.&lt;br /&gt;
Although Java provides polymorphic behavior with the use of interfaces, they sometimes tend to be very slow. The implementation of interfaces is also limited to public methods and constants with no implementation.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Modules and Interfaces offer many advantages. However, they have their own disadvantages. Modules add more flexibility to object-oriented programming by the use of DRY principle and are easy to maintain.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
1. http://ruby.about.com/od/beginningruby/a/mixin.htm/ &amp;lt;br&amp;gt;&lt;br /&gt;
2. http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/ &amp;lt;br&amp;gt;&lt;br /&gt;
3. http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-frameworks/&lt;br /&gt;
4. http://pg-server.csc.ncsu.edu/mediawiki/index.php?title=CSC/ECE_517_Fall_2010/ch3_3b_sv&amp;amp;printable=yes &amp;lt;br&amp;gt;&lt;br /&gt;
5. http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2c_ds &amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rgovind2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1a_1w16_br&amp;diff=63744</id>
		<title>CSC/ECE 517 Fall 2012/ch1a 1w16 br</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1a_1w16_br&amp;diff=63744"/>
		<updated>2012-09-12T22:01:14Z</updated>

		<summary type="html">&lt;p&gt;Rgovind2: /* See Also */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Overview==&lt;br /&gt;
&lt;br /&gt;
Mixins are Ruby modules. A module is a grouping of objects under a single name. The objects may be constants, methods, classes, or other modules. Modules are defined in Ruby using the keyword module. They can be incorporated into a class with Ruby’s include statement. This is called a mixin.&lt;br /&gt;
Modules are for sharing behavior (methods), while classes are for modeling relationship between objects. Ruby classes can Mixin a module and receive all its methods for free.&lt;br /&gt;
An Interface in Java programming language is an abstract type that classes must implement. Interfaces are declared using the interface keyword, and may only contain method signature and constant declarations.&lt;br /&gt;
A class that implements an interface must implement all of the methods described in the interface, or be an abstract class.&lt;br /&gt;
&lt;br /&gt;
==Definitions==&lt;br /&gt;
===Mixins===&lt;br /&gt;
	Modules provide a structure to collect classes, methods, and constants into a single, separately defined unit. They provide the simplest and an elegant way to adhere to the Don't repeat yourself&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Don't_repeat_yourself Don't_repeat_yourself]&amp;lt;/ref&amp;gt; principle. This is useful so as to avoid clashes with existing classes, methods, and constants.&lt;br /&gt;
Modules are defined in Ruby using the module keyword.&lt;br /&gt;
A Mixin is a class that is mixed with a module or a set of modules i.e. the implementation of the modules and the class are intertwined and combined together [3].&lt;br /&gt;
This gives us a neat and controlled way of adding new functionality to classes. Modules are similar to classes in that they hold a collection of methods, variables constants and other modules and class definitions. However, the real usage of a Mixin is exploited when the code in the Mixin starts to interact with code in the class that uses it.&lt;br /&gt;
Below is an example for modules and Mixins:&lt;br /&gt;
The module ParkingSlot consists of the methods park and unpark&lt;br /&gt;
 &lt;br /&gt;
 module ParkingSlot&lt;br /&gt;
       def park&lt;br /&gt;
          #code to park the car&lt;br /&gt;
       end&lt;br /&gt;
       def unpark&lt;br /&gt;
          #code to unpark the car&lt;br /&gt;
       end&lt;br /&gt;
 end&lt;br /&gt;
The module ParkingMeter consists of the methods calculate and printreceipt&lt;br /&gt;
&lt;br /&gt;
 module ParkingMeter&lt;br /&gt;
       def calculate&lt;br /&gt;
          #code to calculate the parking fee&lt;br /&gt;
       end&lt;br /&gt;
       def printreceipt&lt;br /&gt;
          #code to print the receipt&lt;br /&gt;
       end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
When a class includes a module via include Module Name, all the methods on that module become instance methods on the class.&lt;br /&gt;
&lt;br /&gt;
 class Parking&lt;br /&gt;
      include ParkingSlot&lt;br /&gt;
      include ParkingMeter&lt;br /&gt;
      def printcardetails&lt;br /&gt;
          #code to print the details and type of car&lt;br /&gt;
      end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
The modules ParkingSlot and ParkingMeter are “included” in the Parking class. All the variables and the instance methods of the modules ParkingSlot and ParkingMeter are now said to be “mixed in” into the class. It is now possible for class instances to use these methods defined in the modules as and when required.&lt;br /&gt;
 p=Parking.new   &lt;br /&gt;
 p.park            # -&amp;gt; Method park from module ParkingSlot&lt;br /&gt;
 p.unpark          # -&amp;gt; Method unpark from module ParkingSlot&lt;br /&gt;
 p.calculate       # -&amp;gt; Method calculate from module ParkingMeter&lt;br /&gt;
 p.printreceipt    # -&amp;gt; Method printreceipt from module ParkingMeter&lt;br /&gt;
 p.printcardetails # -&amp;gt; Calls printCarDetails from class&lt;br /&gt;
The class Parking inherits from both the modules and the module methods are now available in Parking. Hence, it is now possible to use the methods with an instance of the Parking class p.&lt;br /&gt;
Hence, Mixins can be thought of taking different methods and variables defined in different modules making them available as instance methods in the class as well, thereby extending the class’ functionality. Effectively mixed in modules behave as superclass. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:ParkingLot.PNG| center]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Interfaces===&lt;br /&gt;
In an object-oriented programming languages such as Java, C#, an interface[2] is a reference type, similar to a class that can contain only constants, method signatures, and nested types. There are no method bodies. Interfaces cannot be instantiated and they can only be implemented by classes or extended by other interfaces. All methods are public by default and any fields declared in an interface are by default static and final.&lt;br /&gt;
Example:&lt;br /&gt;
 public interface ParkingLot {&lt;br /&gt;
       boolean add(Car car, int slot);&lt;br /&gt;
       boolean remove(Car car);&lt;br /&gt;
       boolean findCar(Int number);&lt;br /&gt;
       int freeSlotsCount();&lt;br /&gt;
       int occupiedSlots();&lt;br /&gt;
       int parkingType();&lt;br /&gt;
       int calculateParkingFee(int hours, int charge, int duration);&lt;br /&gt;
       int findParkingSlot();&lt;br /&gt;
 }&lt;br /&gt;
 public class Mall implements ParkingLot {&lt;br /&gt;
       boolean add(Car car, int slot) { //... Code to add a car into the parking lot... }&lt;br /&gt;
       boolean remove(Car car){ // do something }&lt;br /&gt;
       boolean findCar(Int number){ // do something }&lt;br /&gt;
       int freeSlotsCount(){ // do something }&lt;br /&gt;
       int occupiedSlots(){ // do something }&lt;br /&gt;
       int parkingType(){ // do something }&lt;br /&gt;
       int calculateParkingFee(int hours, int charge, int duration){ // do something }&lt;br /&gt;
       int findParkingSlot(){ // do something }&lt;br /&gt;
 }&lt;br /&gt;
Interfaces play another important role in the object-oriented programming language. Interfaces are not part of the class hierarchy, although they work in combination with classes. Interfaces provide an alternative for languages like Java that does not allow multiple inheritance .&lt;br /&gt;
&lt;br /&gt;
==Mixins vs. Interfaces==&lt;br /&gt;
===Similarities and Differences===&lt;br /&gt;
	Mixins and Interfaces can be considered as a block of code having some variables and methods that are used in a class. The key difference between them is that a Mixin can contain method definitions whereas an Interface cannot.&lt;br /&gt;
&lt;br /&gt;
====Multiple method definitions:====&lt;br /&gt;
	In a class, when more than one module is Mixed in where some modules have overloaded methods, the method from the module that was last Mixed in into the class will be executed.&lt;br /&gt;
 module Frog&lt;br /&gt;
    def sound()&lt;br /&gt;
      puts &amp;quot;croak...&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Dinosaur&lt;br /&gt;
   def sound()&lt;br /&gt;
     puts &amp;quot;roar...&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
       include Frog&lt;br /&gt;
       include Dinosaur&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 Frogosaur.new.sound &amp;lt;br&amp;gt;	&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 roar...&amp;lt;br&amp;gt;&lt;br /&gt;
This is because the class Frogosaur will have only one definition for each method. The users have the flexibility to make use of methods from different modules by rearranging the order of include.&lt;br /&gt;
Interfaces, on the other hand, do not offer the flexibility to have different implementation for a same method. This is because the class implementing the interface can provide only one implementation for each method. Hence there exists only one method definition.&lt;br /&gt;
 public interface Frog {&lt;br /&gt;
     void sound();&lt;br /&gt;
 }&lt;br /&gt;
 public interface Dinasour {&lt;br /&gt;
     void sound();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
 public void sound()&lt;br /&gt;
 {&lt;br /&gt;
     System.out.println(&amp;quot;barks......&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public static void main(String args[])&lt;br /&gt;
 {&lt;br /&gt;
     new Frogosaur().sound();;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 barks...&amp;lt;br&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
====Multiple object behavior:====&lt;br /&gt;
In Ruby, a class’s instance can extend a module and exhibit different behavior than the other instances of the same class.&lt;br /&gt;
&lt;br /&gt;
 module Frog&lt;br /&gt;
  def sound()&lt;br /&gt;
     puts &amp;quot;croak...&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Dinasour&lt;br /&gt;
  def sound()&lt;br /&gt;
    puts &amp;quot;roar...&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
  def sound()&lt;br /&gt;
    puts &amp;quot;barks...&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 beast1= Frogosaur.new&lt;br /&gt;
 beast2= Frogosaur.new&lt;br /&gt;
 beast1.extend(Frog)&lt;br /&gt;
 beast1.sound&lt;br /&gt;
 beast2.sound&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 croak...&amp;lt;br&amp;gt;&lt;br /&gt;
 barks...&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Interfaces are implemented by a class and all instances of the class behave in the same way. They can use only the methods of the class from which they were instantiated.&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
  public void sound()&lt;br /&gt;
  {&lt;br /&gt;
      System.out.println(&amp;quot;Sounds...&amp;quot;);&lt;br /&gt;
  }  &lt;br /&gt;
  public static void main(String args[])&lt;br /&gt;
  {&lt;br /&gt;
      Frogosaur beast1 = new Frogosaur();&lt;br /&gt;
      Frogosaur beast2 = new Frogosaur();&lt;br /&gt;
      object1.sound();&lt;br /&gt;
      object2.sound();&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 Sounds...&amp;lt;br&amp;gt;&lt;br /&gt;
 Sounds...&amp;lt;br&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
====Enumerable – module and interface====&lt;br /&gt;
&lt;br /&gt;
Ruby’s Arrays have several methods defined in them. To use them, the classes need not be a subclass of Array. The classes can include Enumerable and define the each method to use the array’s methods for free.&lt;br /&gt;
&lt;br /&gt;
 class EnumExer&lt;br /&gt;
  include Enumerable&lt;br /&gt;
  def initialize(*arrays)&lt;br /&gt;
    @arrays = arrays&lt;br /&gt;
  end&lt;br /&gt;
  def each&lt;br /&gt;
    @arrays.each { |a| a.each { |x| yield x } }&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 ma = EnumExer.new([4, 1], [3], [2])&lt;br /&gt;
 puts ma.sort&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 1&amp;lt;br&amp;gt;&lt;br /&gt;
 2&amp;lt;br&amp;gt;&lt;br /&gt;
 3&amp;lt;br&amp;gt;&lt;br /&gt;
 4&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can observe that although the arguments passed to the ma instance did not have a similar structure, the sort method was able to identify each entry and sort them. This is because the each method was defined to identify and serve all the entries to the sort method.&lt;br /&gt;
The methods that are defined for Enumerable modules are:&lt;br /&gt;
 [&amp;quot;all?&amp;quot;, &amp;quot;any?&amp;quot;, &amp;quot;collect&amp;quot;, &amp;quot;detect&amp;quot;, &amp;quot;each_with_index&amp;quot;, &amp;quot;entries&amp;quot;,&lt;br /&gt;
 &amp;quot;find&amp;quot;, &amp;quot;find_all&amp;quot;, &amp;quot;grep&amp;quot;, &amp;quot;include?&amp;quot;, &amp;quot;inject&amp;quot;, &amp;quot;map&amp;quot;, &amp;quot;max&amp;quot;,&lt;br /&gt;
 &amp;quot;member?&amp;quot;, &amp;quot;min&amp;quot;, &amp;quot;partition&amp;quot;, &amp;quot;reject&amp;quot;, &amp;quot;select&amp;quot;, &amp;quot;sort&amp;quot;, &amp;quot;sort_by&amp;quot;,&lt;br /&gt;
 &amp;quot;to_a&amp;quot;, &amp;quot;zip&amp;quot;]&lt;br /&gt;
In Java, to use the methods of Enumerable interface, the class should be a subclass of Array.&lt;br /&gt;
&lt;br /&gt;
 public class EnumExer {&lt;br /&gt;
     static String content_string = &amp;quot;zyxwvutsrqponmlkjihgfedcba&amp;quot;;&lt;br /&gt;
     String a=&amp;quot;1&amp;quot;, b=&amp;quot;2&amp;quot;;&lt;br /&gt;
     public static void main(String args[])&lt;br /&gt;
     {&lt;br /&gt;
         char[] content_array = content_string.toCharArray();&lt;br /&gt;
         java.util.Arrays.sort(content_array);&lt;br /&gt;
         content_string=new String(content_array);&lt;br /&gt;
         System.out.println(content_string);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 Abcdefghijklmnopqrstuvwxyz&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, the string object was converted to a character array to be able to use with the sort method.&lt;br /&gt;
&lt;br /&gt;
====Inheritance:====&lt;br /&gt;
Java interfaces can inherit other interfaces but Ruby modules cannot inherit other modules but can contain classes and methods.&lt;br /&gt;
 module Wrapper&lt;br /&gt;
  class Base&lt;br /&gt;
     # Class Body&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Java,&lt;br /&gt;
 public interface Dinasour extends Frog{&lt;br /&gt;
     //variables and method Declarations&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
==== Achieving Multiple Inheritance ====&lt;br /&gt;
Modules and Interfaces help acheive multiple inheritance. Programming languages such as Ruby and Java do not support multiple inheritance. &amp;lt;br&amp;gt;&lt;br /&gt;
Interfaces are used in Java to achieve multiple inheritance. Similarly, modules in Ruby help achieve multiple inheritance.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Ruby,&lt;br /&gt;
&lt;br /&gt;
 module Frog&lt;br /&gt;
   def jump()&lt;br /&gt;
     //Method definition&lt;br /&gt;
    end&lt;br /&gt;
 end &amp;lt;br&amp;gt;&lt;br /&gt;
 module Dinosaur&lt;br /&gt;
  def strength()&lt;br /&gt;
    //Method definition&lt;br /&gt;
  end&lt;br /&gt;
 end &amp;lt;br&amp;gt;&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
      include Frog&lt;br /&gt;
      include Dinosaur&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
In Java,&lt;br /&gt;
 public interface Frog {&lt;br /&gt;
     void jump();&lt;br /&gt;
 } &amp;lt;br&amp;gt;&lt;br /&gt;
 public interface Dinasour {&lt;br /&gt;
     void strength();&lt;br /&gt;
 } &amp;lt;br&amp;gt;&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
  //Method Implementations&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Frogosaur inherits the methods of both frog and dinosaur. &lt;br /&gt;
&lt;br /&gt;
===Tabular Comparison===&lt;br /&gt;
{|border=&amp;quot;2&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Mixins&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Interfaces&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|The number of lines of code is lesser since the &amp;lt;br&amp;gt;&lt;br /&gt;
modules contain the method definition. &lt;br /&gt;
|The number of lines of code is bigger since the &amp;lt;br&amp;gt;&lt;br /&gt;
classes have to define the methods.&lt;br /&gt;
|-&lt;br /&gt;
|More modules in a class makes code less readable.&lt;br /&gt;
|The code is more readable since the class contain the &amp;lt;br&amp;gt;&lt;br /&gt;
method definition.&lt;br /&gt;
|-&lt;br /&gt;
|Modules cannot be inherited and cannot form is-a hierarchy.&lt;br /&gt;
|Interfaces can extend another interface and hence can form &amp;lt;br&amp;gt;&lt;br /&gt;
is-a hierarchy.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Drawbacks===&lt;br /&gt;
The drawbacks of the technique of Mixins are very much debated. Though Mixins provide us with an easy way to write flexible and decoupled code and also help us solve the diamond problem, they pose their own problems. In large programs there could be large number of modules and each module could have loads of methods each performing a certain task. To trace the origin of the methods and to keep in mind the hierarchy is practically impossible. Another issue associated with Mixins pose is silent method overriding. Silent method overriding occurs when different modules are included in a class and each module have a method with the same definition. In such a case, the method which gets executed depends upon the way it is included in the class and this is done without any sort of message to the user (silently). In large scale applications this could certainly be an issue considering the number of modules that would be mixed in and it would be difficult to keep track of the hierarchy in which the modules were included.&lt;br /&gt;
Although Java provides polymorphic behavior with the use of interfaces, they sometimes tend to be very slow. The implementation of interfaces is also limited to public methods and constants with no implementation.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Modules and Interfaces offer many advantages. However, they have their own disadvantages. Modules add more flexibility to object-oriented programming by the use of DRY principle and are easy to maintain.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
1. http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2c_ds&lt;br /&gt;
2. http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/ &amp;lt;br&amp;gt;&lt;br /&gt;
3. https://www.re-motion.org/blogs/team/2008/02/20/introducing-mixins-finally/ &amp;lt;br&amp;gt;&lt;br /&gt;
4. https://www.cs.washington.edu/education/courses/cse413/11sp/lectures/ruby_interfaces-etc.pdf &amp;lt;br&amp;gt;&lt;br /&gt;
5. http://pg-server.csc.ncsu.edu/mediawiki/index.php?title=CSC/ECE_517_Fall_2010/ch3_3b_sv&amp;amp;printable=yes &amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rgovind2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w16_br&amp;diff=63635</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w16 br</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w16_br&amp;diff=63635"/>
		<updated>2012-09-12T01:19:07Z</updated>

		<summary type="html">&lt;p&gt;Rgovind2: /* Mixins */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Overview==&lt;br /&gt;
&lt;br /&gt;
Mixins are Ruby modules. A module is a grouping of objects under a single name. The objects may be constants, methods, classes, or other modules. Modules are defined in Ruby using the keyword module. They can be incorporated into a class with Ruby’s include statement. This is called a mixin.&lt;br /&gt;
Modules are for sharing behavior (methods), while classes are for modeling relationship between objects. Ruby classes can Mixin a module and receive all its methods for free.&lt;br /&gt;
An Interface in Java programming language is an abstract type that classes must implement. Interfaces are declared using the interface keyword, and may only contain method signature and constant declarations.&lt;br /&gt;
A class that implements an interface must implement all of the methods described in the interface, or be an abstract class.&lt;br /&gt;
&lt;br /&gt;
==Definitions==&lt;br /&gt;
===Mixins===&lt;br /&gt;
	Modules provide a structure to collect classes, methods, and constants into a single, separately defined unit. They provide the simplest and an elegant way to adhere to the Don't repeat yourself&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Don't_repeat_yourself Don't_repeat_yourself]&amp;lt;/ref&amp;gt; principle. This is useful so as to avoid clashes with existing classes, methods, and constants.&lt;br /&gt;
Modules are defined in Ruby using the module keyword.&lt;br /&gt;
A Mixin is a class that is mixed with a module or a set of modules i.e. the implementation of the modules and the class are intertwined and combined together [3].&lt;br /&gt;
This gives us a neat and controlled way of adding new functionality to classes. Modules are similar to classes in that they hold a collection of methods, variables constants and other modules and class definitions. However, the real usage of a Mixin is exploited when the code in the Mixin starts to interact with code in the class that uses it.&lt;br /&gt;
Below is an example for modules and Mixins:&lt;br /&gt;
The module ParkingSlot consists of the methods park and unpark&lt;br /&gt;
 &lt;br /&gt;
 module ParkingSlot&lt;br /&gt;
       def park&lt;br /&gt;
          #code to park the car&lt;br /&gt;
       end&lt;br /&gt;
       def unpark&lt;br /&gt;
          #code to unpark the car&lt;br /&gt;
       end&lt;br /&gt;
 end&lt;br /&gt;
The module ParkingMeter consists of the methods calculate and printreceipt&lt;br /&gt;
&lt;br /&gt;
 module ParkingMeter&lt;br /&gt;
       def calculate&lt;br /&gt;
          #code to calculate the parking fee&lt;br /&gt;
       end&lt;br /&gt;
       def printreceipt&lt;br /&gt;
          #code to print the receipt&lt;br /&gt;
       end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
When a class includes a module via include Module Name, all the methods on that module become instance methods on the class.&lt;br /&gt;
&lt;br /&gt;
 class Parking&lt;br /&gt;
      include ParkingSlot&lt;br /&gt;
      include ParkingMeter&lt;br /&gt;
      def printcardetails&lt;br /&gt;
          #code to print the details and type of car&lt;br /&gt;
      end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
The modules ParkingSlot and ParkingMeter are “included” in the Parking class. All the variables and the instance methods of the modules ParkingSlot and ParkingMeter are now said to be “mixed in” into the class. It is now possible for class instances to use these methods defined in the modules as and when required.&lt;br /&gt;
 p=Parking.new   &lt;br /&gt;
 p.park            # -&amp;gt; Method park from module ParkingSlot&lt;br /&gt;
 p.unpark          # -&amp;gt; Method unpark from module ParkingSlot&lt;br /&gt;
 p.calculate       # -&amp;gt; Method calculate from module ParkingMeter&lt;br /&gt;
 p.printreceipt    # -&amp;gt; Method printreceipt from module ParkingMeter&lt;br /&gt;
 p.printcardetails # -&amp;gt; Calls printCarDetails from class&lt;br /&gt;
The class Parking inherits from both the modules and the module methods are now available in Parking. Hence, it is now possible to use the methods with an instance of the Parking class p.&lt;br /&gt;
Hence, Mixins can be thought of taking different methods and variables defined in different modules making them available as instance methods in the class as well, thereby extending the class’ functionality. Effectively mixed in modules behave as superclass. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:ParkingLot.PNG| center]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Interfaces===&lt;br /&gt;
In an object-oriented programming languages such as Java, C#, an interface[2] is a reference type, similar to a class that can contain only constants, method signatures, and nested types. There are no method bodies. Interfaces cannot be instantiated and they can only be implemented by classes or extended by other interfaces. All methods are public by default and any fields declared in an interface are by default static and final.&lt;br /&gt;
Example:&lt;br /&gt;
 public interface ParkingLot {&lt;br /&gt;
       boolean add(Car car, int slot);&lt;br /&gt;
       boolean remove(Car car);&lt;br /&gt;
       boolean findCar(Int number);&lt;br /&gt;
       int freeSlotsCount();&lt;br /&gt;
       int occupiedSlots();&lt;br /&gt;
       int parkingType();&lt;br /&gt;
       int calculateParkingFee(int hours, int charge, int duration);&lt;br /&gt;
       int findParkingSlot();&lt;br /&gt;
 }&lt;br /&gt;
 public class Mall implements ParkingLot {&lt;br /&gt;
       boolean add(Car car, int slot) { //... Code to add a car into the parking lot... }&lt;br /&gt;
       boolean remove(Car car){ // do something }&lt;br /&gt;
       boolean findCar(Int number){ // do something }&lt;br /&gt;
       int freeSlotsCount(){ // do something }&lt;br /&gt;
       int occupiedSlots(){ // do something }&lt;br /&gt;
       int parkingType(){ // do something }&lt;br /&gt;
       int calculateParkingFee(int hours, int charge, int duration){ // do something }&lt;br /&gt;
       int findParkingSlot(){ // do something }&lt;br /&gt;
 }&lt;br /&gt;
Interfaces play another important role in the object-oriented programming language. Interfaces are not part of the class hierarchy, although they work in combination with classes. Interfaces provide an alternative for languages like Java that does not allow multiple inheritance .&lt;br /&gt;
&lt;br /&gt;
==Mixins vs. Interfaces==&lt;br /&gt;
===Similarities and Differences===&lt;br /&gt;
	Mixins and Interfaces can be considered as a block of code having some variables and methods that are used in a class. The key difference between them is that a Mixin can contain method definitions whereas an Interface cannot.&lt;br /&gt;
&lt;br /&gt;
====Multiple method definitions:====&lt;br /&gt;
	In a class, when more than one module is Mixed in where some modules have overloaded methods, the method from the module that was last Mixed in into the class will be executed.&lt;br /&gt;
 module Frog&lt;br /&gt;
    def sound()&lt;br /&gt;
      puts &amp;quot;croak...&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Dinosaur&lt;br /&gt;
   def sound()&lt;br /&gt;
     puts &amp;quot;roar...&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
       include Frog&lt;br /&gt;
       include Dinosaur&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 Frogosaur.new.sound &amp;lt;br&amp;gt;	&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 roar...&amp;lt;br&amp;gt;&lt;br /&gt;
This is because the class Frogosaur will have only one definition for each method. The users have the flexibility to make use of methods from different modules by rearranging the order of include.&lt;br /&gt;
Interfaces, on the other hand, do not offer the flexibility to have different implementation for a same method. This is because the class implementing the interface can provide only one implementation for each method. Hence there exists only one method definition.&lt;br /&gt;
 public interface Frog {&lt;br /&gt;
     void sound();&lt;br /&gt;
 }&lt;br /&gt;
 public interface Dinasour {&lt;br /&gt;
     void sound();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
 public void sound()&lt;br /&gt;
 {&lt;br /&gt;
     System.out.println(&amp;quot;barks......&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public static void main(String args[])&lt;br /&gt;
 {&lt;br /&gt;
     new Frogosaur().sound();;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 barks...&amp;lt;br&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
====Multiple object behavior:====&lt;br /&gt;
In Ruby, a class’s instance can extend a module and exhibit different behavior than the other instances of the same class.&lt;br /&gt;
&lt;br /&gt;
 module Frog&lt;br /&gt;
  def sound()&lt;br /&gt;
     puts &amp;quot;croak...&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Dinasour&lt;br /&gt;
  def sound()&lt;br /&gt;
    puts &amp;quot;roar...&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
  def sound()&lt;br /&gt;
    puts &amp;quot;barks...&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 beast1= Frogosaur.new&lt;br /&gt;
 beast2= Frogosaur.new&lt;br /&gt;
 beast1.extend(Frog)&lt;br /&gt;
 beast1.sound&lt;br /&gt;
 beast2.sound&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 croak...&amp;lt;br&amp;gt;&lt;br /&gt;
 barks...&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Interfaces are implemented by a class and all instances of the class behave in the same way. They can use only the methods of the class from which they were instantiated.&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
  public void sound()&lt;br /&gt;
  {&lt;br /&gt;
      System.out.println(&amp;quot;Sounds...&amp;quot;);&lt;br /&gt;
  }  &lt;br /&gt;
  public static void main(String args[])&lt;br /&gt;
  {&lt;br /&gt;
      Frogosaur beast1 = new Frogosaur();&lt;br /&gt;
      Frogosaur beast2 = new Frogosaur();&lt;br /&gt;
      object1.sound();&lt;br /&gt;
      object2.sound();&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 Sounds...&amp;lt;br&amp;gt;&lt;br /&gt;
 Sounds...&amp;lt;br&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
====Enumerable – module and interface====&lt;br /&gt;
&lt;br /&gt;
Ruby’s Arrays have several methods defined in them. To use them, the classes need not be a subclass of Array. The classes can include Enumerable and define the each method to use the array’s methods for free.&lt;br /&gt;
&lt;br /&gt;
 class EnumExer&lt;br /&gt;
  include Enumerable&lt;br /&gt;
  def initialize(*arrays)&lt;br /&gt;
    @arrays = arrays&lt;br /&gt;
  end&lt;br /&gt;
  def each&lt;br /&gt;
    @arrays.each { |a| a.each { |x| yield x } }&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 ma = EnumExer.new([4, 1], [3], [2])&lt;br /&gt;
 puts ma.sort&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 1&amp;lt;br&amp;gt;&lt;br /&gt;
 2&amp;lt;br&amp;gt;&lt;br /&gt;
 3&amp;lt;br&amp;gt;&lt;br /&gt;
 4&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can observe that although the arguments passed to the ma instance did not have a similar structure, the sort method was able to identify each entry and sort them. This is because the each method was defined to identify and serve all the entries to the sort method.&lt;br /&gt;
The methods that are defined for Enumerable modules are:&lt;br /&gt;
 [&amp;quot;all?&amp;quot;, &amp;quot;any?&amp;quot;, &amp;quot;collect&amp;quot;, &amp;quot;detect&amp;quot;, &amp;quot;each_with_index&amp;quot;, &amp;quot;entries&amp;quot;,&lt;br /&gt;
 &amp;quot;find&amp;quot;, &amp;quot;find_all&amp;quot;, &amp;quot;grep&amp;quot;, &amp;quot;include?&amp;quot;, &amp;quot;inject&amp;quot;, &amp;quot;map&amp;quot;, &amp;quot;max&amp;quot;,&lt;br /&gt;
 &amp;quot;member?&amp;quot;, &amp;quot;min&amp;quot;, &amp;quot;partition&amp;quot;, &amp;quot;reject&amp;quot;, &amp;quot;select&amp;quot;, &amp;quot;sort&amp;quot;, &amp;quot;sort_by&amp;quot;,&lt;br /&gt;
 &amp;quot;to_a&amp;quot;, &amp;quot;zip&amp;quot;]&lt;br /&gt;
In Java, to use the methods of Enumerable interface, the class should be a subclass of Array.&lt;br /&gt;
&lt;br /&gt;
 public class EnumExer {&lt;br /&gt;
     static String content_string = &amp;quot;zyxwvutsrqponmlkjihgfedcba&amp;quot;;&lt;br /&gt;
     String a=&amp;quot;1&amp;quot;, b=&amp;quot;2&amp;quot;;&lt;br /&gt;
     public static void main(String args[])&lt;br /&gt;
     {&lt;br /&gt;
         char[] content_array = content_string.toCharArray();&lt;br /&gt;
         java.util.Arrays.sort(content_array);&lt;br /&gt;
         content_string=new String(content_array);&lt;br /&gt;
         System.out.println(content_string);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 Abcdefghijklmnopqrstuvwxyz&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, the string object was converted to a character array to be able to use with the sort method.&lt;br /&gt;
&lt;br /&gt;
====Inheritance:====&lt;br /&gt;
Java interfaces can inherit other interfaces but Ruby modules cannot inherit other modules but can contain classes and methods.&lt;br /&gt;
 module Wrapper&lt;br /&gt;
  class Base&lt;br /&gt;
     # Class Body&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Java,&lt;br /&gt;
 public interface Dinasour extends Frog{&lt;br /&gt;
     //variables and method Declarations&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
==== Achieving Multiple Inheritance ====&lt;br /&gt;
Modules and Interfaces help acheive multiple inheritance. Programming languages such as Ruby and Java do not support multiple inheritance. &amp;lt;br&amp;gt;&lt;br /&gt;
Interfaces are used in Java to achieve multiple inheritance. Similarly, modules in Ruby help achieve multiple inheritance.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Ruby,&lt;br /&gt;
&lt;br /&gt;
 module Frog&lt;br /&gt;
   def jump()&lt;br /&gt;
     //Method definition&lt;br /&gt;
    end&lt;br /&gt;
 end &amp;lt;br&amp;gt;&lt;br /&gt;
 module Dinosaur&lt;br /&gt;
  def strength()&lt;br /&gt;
    //Method definition&lt;br /&gt;
  end&lt;br /&gt;
 end &amp;lt;br&amp;gt;&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
      include Frog&lt;br /&gt;
      include Dinosaur&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
In Java,&lt;br /&gt;
 public interface Frog {&lt;br /&gt;
     void jump();&lt;br /&gt;
 } &amp;lt;br&amp;gt;&lt;br /&gt;
 public interface Dinasour {&lt;br /&gt;
     void strength();&lt;br /&gt;
 } &amp;lt;br&amp;gt;&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
  //Method Implementations&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Frogosaur inherits the methods of both frog and dinosaur. &lt;br /&gt;
&lt;br /&gt;
===Tabular Comparison===&lt;br /&gt;
{|border=&amp;quot;2&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Mixins&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Interfaces&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|The number of lines of code is lesser since the &amp;lt;br&amp;gt;&lt;br /&gt;
modules contain the method definition. &lt;br /&gt;
|The number of lines of code is bigger since the &amp;lt;br&amp;gt;&lt;br /&gt;
classes have to define the methods.&lt;br /&gt;
|-&lt;br /&gt;
|More modules in a class makes code less readable.&lt;br /&gt;
|The code is more readable since the class contain the &amp;lt;br&amp;gt;&lt;br /&gt;
method definition.&lt;br /&gt;
|-&lt;br /&gt;
|Modules cannot be inherited and cannot form is-a hierarchy.&lt;br /&gt;
|Interfaces can extend another interface and hence can form &amp;lt;br&amp;gt;&lt;br /&gt;
is-a hierarchy.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Drawbacks===&lt;br /&gt;
The drawbacks of the technique of Mixins are very much debated. Though Mixins provide us with an easy way to write flexible and decoupled code and also help us solve the diamond problem, they pose their own problems. In large programs there could be large number of modules and each module could have loads of methods each performing a certain task. To trace the origin of the methods and to keep in mind the hierarchy is practically impossible. Another issue associated with Mixins pose is silent method overriding. Silent method overriding occurs when different modules are included in a class and each module have a method with the same definition. In such a case, the method which gets executed depends upon the way it is included in the class and this is done without any sort of message to the user (silently). In large scale applications this could certainly be an issue considering the number of modules that would be mixed in and it would be difficult to keep track of the hierarchy in which the modules were included.&lt;br /&gt;
Although Java provides polymorphic behavior with the use of interfaces, they sometimes tend to be very slow. The implementation of interfaces is also limited to public methods and constants with no implementation.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Modules and Interfaces offer many advantages. However, they have their own disadvantages. Modules add more flexibility to object-oriented programming by the use of DRY principle and are easy to maintain.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;/div&gt;</summary>
		<author><name>Rgovind2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w16_br&amp;diff=63634</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w16 br</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w16_br&amp;diff=63634"/>
		<updated>2012-09-12T01:18:33Z</updated>

		<summary type="html">&lt;p&gt;Rgovind2: /* Mixins */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Overview==&lt;br /&gt;
&lt;br /&gt;
Mixins are Ruby modules. A module is a grouping of objects under a single name. The objects may be constants, methods, classes, or other modules. Modules are defined in Ruby using the keyword module. They can be incorporated into a class with Ruby’s include statement. This is called a mixin.&lt;br /&gt;
Modules are for sharing behavior (methods), while classes are for modeling relationship between objects. Ruby classes can Mixin a module and receive all its methods for free.&lt;br /&gt;
An Interface in Java programming language is an abstract type that classes must implement. Interfaces are declared using the interface keyword, and may only contain method signature and constant declarations.&lt;br /&gt;
A class that implements an interface must implement all of the methods described in the interface, or be an abstract class.&lt;br /&gt;
&lt;br /&gt;
==Definitions==&lt;br /&gt;
===Mixins===&lt;br /&gt;
	Modules provide a structure to collect classes, methods, and constants into a single, separately defined unit. They provide the simplest and an elegant way to adhere to the Don't repeat yourself&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Don't_repeat_yourself]&amp;lt;/ref&amp;gt; principle. This is useful so as to avoid clashes with existing classes, methods, and constants.&lt;br /&gt;
Modules are defined in Ruby using the module keyword.&lt;br /&gt;
A Mixin is a class that is mixed with a module or a set of modules i.e. the implementation of the modules and the class are intertwined and combined together [3].&lt;br /&gt;
This gives us a neat and controlled way of adding new functionality to classes. Modules are similar to classes in that they hold a collection of methods, variables constants and other modules and class definitions. However, the real usage of a Mixin is exploited when the code in the Mixin starts to interact with code in the class that uses it.&lt;br /&gt;
Below is an example for modules and Mixins:&lt;br /&gt;
The module ParkingSlot consists of the methods park and unpark&lt;br /&gt;
 &lt;br /&gt;
 module ParkingSlot&lt;br /&gt;
       def park&lt;br /&gt;
          #code to park the car&lt;br /&gt;
       end&lt;br /&gt;
       def unpark&lt;br /&gt;
          #code to unpark the car&lt;br /&gt;
       end&lt;br /&gt;
 end&lt;br /&gt;
The module ParkingMeter consists of the methods calculate and printreceipt&lt;br /&gt;
&lt;br /&gt;
 module ParkingMeter&lt;br /&gt;
       def calculate&lt;br /&gt;
          #code to calculate the parking fee&lt;br /&gt;
       end&lt;br /&gt;
       def printreceipt&lt;br /&gt;
          #code to print the receipt&lt;br /&gt;
       end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
When a class includes a module via include Module Name, all the methods on that module become instance methods on the class.&lt;br /&gt;
&lt;br /&gt;
 class Parking&lt;br /&gt;
      include ParkingSlot&lt;br /&gt;
      include ParkingMeter&lt;br /&gt;
      def printcardetails&lt;br /&gt;
          #code to print the details and type of car&lt;br /&gt;
      end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
The modules ParkingSlot and ParkingMeter are “included” in the Parking class. All the variables and the instance methods of the modules ParkingSlot and ParkingMeter are now said to be “mixed in” into the class. It is now possible for class instances to use these methods defined in the modules as and when required.&lt;br /&gt;
 p=Parking.new   &lt;br /&gt;
 p.park            # -&amp;gt; Method park from module ParkingSlot&lt;br /&gt;
 p.unpark          # -&amp;gt; Method unpark from module ParkingSlot&lt;br /&gt;
 p.calculate       # -&amp;gt; Method calculate from module ParkingMeter&lt;br /&gt;
 p.printreceipt    # -&amp;gt; Method printreceipt from module ParkingMeter&lt;br /&gt;
 p.printcardetails # -&amp;gt; Calls printCarDetails from class&lt;br /&gt;
The class Parking inherits from both the modules and the module methods are now available in Parking. Hence, it is now possible to use the methods with an instance of the Parking class p.&lt;br /&gt;
Hence, Mixins can be thought of taking different methods and variables defined in different modules making them available as instance methods in the class as well, thereby extending the class’ functionality. Effectively mixed in modules behave as superclass. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:ParkingLot.PNG| center]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Interfaces===&lt;br /&gt;
In an object-oriented programming languages such as Java, C#, an interface[2] is a reference type, similar to a class that can contain only constants, method signatures, and nested types. There are no method bodies. Interfaces cannot be instantiated and they can only be implemented by classes or extended by other interfaces. All methods are public by default and any fields declared in an interface are by default static and final.&lt;br /&gt;
Example:&lt;br /&gt;
 public interface ParkingLot {&lt;br /&gt;
       boolean add(Car car, int slot);&lt;br /&gt;
       boolean remove(Car car);&lt;br /&gt;
       boolean findCar(Int number);&lt;br /&gt;
       int freeSlotsCount();&lt;br /&gt;
       int occupiedSlots();&lt;br /&gt;
       int parkingType();&lt;br /&gt;
       int calculateParkingFee(int hours, int charge, int duration);&lt;br /&gt;
       int findParkingSlot();&lt;br /&gt;
 }&lt;br /&gt;
 public class Mall implements ParkingLot {&lt;br /&gt;
       boolean add(Car car, int slot) { //... Code to add a car into the parking lot... }&lt;br /&gt;
       boolean remove(Car car){ // do something }&lt;br /&gt;
       boolean findCar(Int number){ // do something }&lt;br /&gt;
       int freeSlotsCount(){ // do something }&lt;br /&gt;
       int occupiedSlots(){ // do something }&lt;br /&gt;
       int parkingType(){ // do something }&lt;br /&gt;
       int calculateParkingFee(int hours, int charge, int duration){ // do something }&lt;br /&gt;
       int findParkingSlot(){ // do something }&lt;br /&gt;
 }&lt;br /&gt;
Interfaces play another important role in the object-oriented programming language. Interfaces are not part of the class hierarchy, although they work in combination with classes. Interfaces provide an alternative for languages like Java that does not allow multiple inheritance .&lt;br /&gt;
&lt;br /&gt;
==Mixins vs. Interfaces==&lt;br /&gt;
===Similarities and Differences===&lt;br /&gt;
	Mixins and Interfaces can be considered as a block of code having some variables and methods that are used in a class. The key difference between them is that a Mixin can contain method definitions whereas an Interface cannot.&lt;br /&gt;
&lt;br /&gt;
====Multiple method definitions:====&lt;br /&gt;
	In a class, when more than one module is Mixed in where some modules have overloaded methods, the method from the module that was last Mixed in into the class will be executed.&lt;br /&gt;
 module Frog&lt;br /&gt;
    def sound()&lt;br /&gt;
      puts &amp;quot;croak...&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Dinosaur&lt;br /&gt;
   def sound()&lt;br /&gt;
     puts &amp;quot;roar...&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
       include Frog&lt;br /&gt;
       include Dinosaur&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 Frogosaur.new.sound &amp;lt;br&amp;gt;	&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 roar...&amp;lt;br&amp;gt;&lt;br /&gt;
This is because the class Frogosaur will have only one definition for each method. The users have the flexibility to make use of methods from different modules by rearranging the order of include.&lt;br /&gt;
Interfaces, on the other hand, do not offer the flexibility to have different implementation for a same method. This is because the class implementing the interface can provide only one implementation for each method. Hence there exists only one method definition.&lt;br /&gt;
 public interface Frog {&lt;br /&gt;
     void sound();&lt;br /&gt;
 }&lt;br /&gt;
 public interface Dinasour {&lt;br /&gt;
     void sound();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
 public void sound()&lt;br /&gt;
 {&lt;br /&gt;
     System.out.println(&amp;quot;barks......&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public static void main(String args[])&lt;br /&gt;
 {&lt;br /&gt;
     new Frogosaur().sound();;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 barks...&amp;lt;br&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
====Multiple object behavior:====&lt;br /&gt;
In Ruby, a class’s instance can extend a module and exhibit different behavior than the other instances of the same class.&lt;br /&gt;
&lt;br /&gt;
 module Frog&lt;br /&gt;
  def sound()&lt;br /&gt;
     puts &amp;quot;croak...&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Dinasour&lt;br /&gt;
  def sound()&lt;br /&gt;
    puts &amp;quot;roar...&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
  def sound()&lt;br /&gt;
    puts &amp;quot;barks...&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 beast1= Frogosaur.new&lt;br /&gt;
 beast2= Frogosaur.new&lt;br /&gt;
 beast1.extend(Frog)&lt;br /&gt;
 beast1.sound&lt;br /&gt;
 beast2.sound&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 croak...&amp;lt;br&amp;gt;&lt;br /&gt;
 barks...&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Interfaces are implemented by a class and all instances of the class behave in the same way. They can use only the methods of the class from which they were instantiated.&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
  public void sound()&lt;br /&gt;
  {&lt;br /&gt;
      System.out.println(&amp;quot;Sounds...&amp;quot;);&lt;br /&gt;
  }  &lt;br /&gt;
  public static void main(String args[])&lt;br /&gt;
  {&lt;br /&gt;
      Frogosaur beast1 = new Frogosaur();&lt;br /&gt;
      Frogosaur beast2 = new Frogosaur();&lt;br /&gt;
      object1.sound();&lt;br /&gt;
      object2.sound();&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 Sounds...&amp;lt;br&amp;gt;&lt;br /&gt;
 Sounds...&amp;lt;br&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
====Enumerable – module and interface====&lt;br /&gt;
&lt;br /&gt;
Ruby’s Arrays have several methods defined in them. To use them, the classes need not be a subclass of Array. The classes can include Enumerable and define the each method to use the array’s methods for free.&lt;br /&gt;
&lt;br /&gt;
 class EnumExer&lt;br /&gt;
  include Enumerable&lt;br /&gt;
  def initialize(*arrays)&lt;br /&gt;
    @arrays = arrays&lt;br /&gt;
  end&lt;br /&gt;
  def each&lt;br /&gt;
    @arrays.each { |a| a.each { |x| yield x } }&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 ma = EnumExer.new([4, 1], [3], [2])&lt;br /&gt;
 puts ma.sort&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 1&amp;lt;br&amp;gt;&lt;br /&gt;
 2&amp;lt;br&amp;gt;&lt;br /&gt;
 3&amp;lt;br&amp;gt;&lt;br /&gt;
 4&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can observe that although the arguments passed to the ma instance did not have a similar structure, the sort method was able to identify each entry and sort them. This is because the each method was defined to identify and serve all the entries to the sort method.&lt;br /&gt;
The methods that are defined for Enumerable modules are:&lt;br /&gt;
 [&amp;quot;all?&amp;quot;, &amp;quot;any?&amp;quot;, &amp;quot;collect&amp;quot;, &amp;quot;detect&amp;quot;, &amp;quot;each_with_index&amp;quot;, &amp;quot;entries&amp;quot;,&lt;br /&gt;
 &amp;quot;find&amp;quot;, &amp;quot;find_all&amp;quot;, &amp;quot;grep&amp;quot;, &amp;quot;include?&amp;quot;, &amp;quot;inject&amp;quot;, &amp;quot;map&amp;quot;, &amp;quot;max&amp;quot;,&lt;br /&gt;
 &amp;quot;member?&amp;quot;, &amp;quot;min&amp;quot;, &amp;quot;partition&amp;quot;, &amp;quot;reject&amp;quot;, &amp;quot;select&amp;quot;, &amp;quot;sort&amp;quot;, &amp;quot;sort_by&amp;quot;,&lt;br /&gt;
 &amp;quot;to_a&amp;quot;, &amp;quot;zip&amp;quot;]&lt;br /&gt;
In Java, to use the methods of Enumerable interface, the class should be a subclass of Array.&lt;br /&gt;
&lt;br /&gt;
 public class EnumExer {&lt;br /&gt;
     static String content_string = &amp;quot;zyxwvutsrqponmlkjihgfedcba&amp;quot;;&lt;br /&gt;
     String a=&amp;quot;1&amp;quot;, b=&amp;quot;2&amp;quot;;&lt;br /&gt;
     public static void main(String args[])&lt;br /&gt;
     {&lt;br /&gt;
         char[] content_array = content_string.toCharArray();&lt;br /&gt;
         java.util.Arrays.sort(content_array);&lt;br /&gt;
         content_string=new String(content_array);&lt;br /&gt;
         System.out.println(content_string);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 Abcdefghijklmnopqrstuvwxyz&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, the string object was converted to a character array to be able to use with the sort method.&lt;br /&gt;
&lt;br /&gt;
====Inheritance:====&lt;br /&gt;
Java interfaces can inherit other interfaces but Ruby modules cannot inherit other modules but can contain classes and methods.&lt;br /&gt;
 module Wrapper&lt;br /&gt;
  class Base&lt;br /&gt;
     # Class Body&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Java,&lt;br /&gt;
 public interface Dinasour extends Frog{&lt;br /&gt;
     //variables and method Declarations&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
==== Achieving Multiple Inheritance ====&lt;br /&gt;
Modules and Interfaces help acheive multiple inheritance. Programming languages such as Ruby and Java do not support multiple inheritance. &amp;lt;br&amp;gt;&lt;br /&gt;
Interfaces are used in Java to achieve multiple inheritance. Similarly, modules in Ruby help achieve multiple inheritance.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Ruby,&lt;br /&gt;
&lt;br /&gt;
 module Frog&lt;br /&gt;
   def jump()&lt;br /&gt;
     //Method definition&lt;br /&gt;
    end&lt;br /&gt;
 end &amp;lt;br&amp;gt;&lt;br /&gt;
 module Dinosaur&lt;br /&gt;
  def strength()&lt;br /&gt;
    //Method definition&lt;br /&gt;
  end&lt;br /&gt;
 end &amp;lt;br&amp;gt;&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
      include Frog&lt;br /&gt;
      include Dinosaur&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
In Java,&lt;br /&gt;
 public interface Frog {&lt;br /&gt;
     void jump();&lt;br /&gt;
 } &amp;lt;br&amp;gt;&lt;br /&gt;
 public interface Dinasour {&lt;br /&gt;
     void strength();&lt;br /&gt;
 } &amp;lt;br&amp;gt;&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
  //Method Implementations&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Frogosaur inherits the methods of both frog and dinosaur. &lt;br /&gt;
&lt;br /&gt;
===Tabular Comparison===&lt;br /&gt;
{|border=&amp;quot;2&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Mixins&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Interfaces&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|The number of lines of code is lesser since the &amp;lt;br&amp;gt;&lt;br /&gt;
modules contain the method definition. &lt;br /&gt;
|The number of lines of code is bigger since the &amp;lt;br&amp;gt;&lt;br /&gt;
classes have to define the methods.&lt;br /&gt;
|-&lt;br /&gt;
|More modules in a class makes code less readable.&lt;br /&gt;
|The code is more readable since the class contain the &amp;lt;br&amp;gt;&lt;br /&gt;
method definition.&lt;br /&gt;
|-&lt;br /&gt;
|Modules cannot be inherited and cannot form is-a hierarchy.&lt;br /&gt;
|Interfaces can extend another interface and hence can form &amp;lt;br&amp;gt;&lt;br /&gt;
is-a hierarchy.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Drawbacks===&lt;br /&gt;
The drawbacks of the technique of Mixins are very much debated. Though Mixins provide us with an easy way to write flexible and decoupled code and also help us solve the diamond problem, they pose their own problems. In large programs there could be large number of modules and each module could have loads of methods each performing a certain task. To trace the origin of the methods and to keep in mind the hierarchy is practically impossible. Another issue associated with Mixins pose is silent method overriding. Silent method overriding occurs when different modules are included in a class and each module have a method with the same definition. In such a case, the method which gets executed depends upon the way it is included in the class and this is done without any sort of message to the user (silently). In large scale applications this could certainly be an issue considering the number of modules that would be mixed in and it would be difficult to keep track of the hierarchy in which the modules were included.&lt;br /&gt;
Although Java provides polymorphic behavior with the use of interfaces, they sometimes tend to be very slow. The implementation of interfaces is also limited to public methods and constants with no implementation.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Modules and Interfaces offer many advantages. However, they have their own disadvantages. Modules add more flexibility to object-oriented programming by the use of DRY principle and are easy to maintain.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;/div&gt;</summary>
		<author><name>Rgovind2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w16_br&amp;diff=63633</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w16 br</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w16_br&amp;diff=63633"/>
		<updated>2012-09-12T01:18:09Z</updated>

		<summary type="html">&lt;p&gt;Rgovind2: /* Mixins */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Overview==&lt;br /&gt;
&lt;br /&gt;
Mixins are Ruby modules. A module is a grouping of objects under a single name. The objects may be constants, methods, classes, or other modules. Modules are defined in Ruby using the keyword module. They can be incorporated into a class with Ruby’s include statement. This is called a mixin.&lt;br /&gt;
Modules are for sharing behavior (methods), while classes are for modeling relationship between objects. Ruby classes can Mixin a module and receive all its methods for free.&lt;br /&gt;
An Interface in Java programming language is an abstract type that classes must implement. Interfaces are declared using the interface keyword, and may only contain method signature and constant declarations.&lt;br /&gt;
A class that implements an interface must implement all of the methods described in the interface, or be an abstract class.&lt;br /&gt;
&lt;br /&gt;
==Definitions==&lt;br /&gt;
===Mixins===&lt;br /&gt;
	Modules provide a structure to collect classes, methods, and constants into a single, separately defined unit. They provide the simplest and an elegant way to adhere to the &amp;lt;ref&amp;gt;Don't repeat yourself[http://en.wikipedia.org/wiki/Don't_repeat_yourself]&amp;lt;/ref&amp;gt; principle. This is useful so as to avoid clashes with existing classes, methods, and constants.&lt;br /&gt;
Modules are defined in Ruby using the module keyword.&lt;br /&gt;
A Mixin is a class that is mixed with a module or a set of modules i.e. the implementation of the modules and the class are intertwined and combined together [3].&lt;br /&gt;
This gives us a neat and controlled way of adding new functionality to classes. Modules are similar to classes in that they hold a collection of methods, variables constants and other modules and class definitions. However, the real usage of a Mixin is exploited when the code in the Mixin starts to interact with code in the class that uses it.&lt;br /&gt;
Below is an example for modules and Mixins:&lt;br /&gt;
The module ParkingSlot consists of the methods park and unpark&lt;br /&gt;
 &lt;br /&gt;
 module ParkingSlot&lt;br /&gt;
       def park&lt;br /&gt;
          #code to park the car&lt;br /&gt;
       end&lt;br /&gt;
       def unpark&lt;br /&gt;
          #code to unpark the car&lt;br /&gt;
       end&lt;br /&gt;
 end&lt;br /&gt;
The module ParkingMeter consists of the methods calculate and printreceipt&lt;br /&gt;
&lt;br /&gt;
 module ParkingMeter&lt;br /&gt;
       def calculate&lt;br /&gt;
          #code to calculate the parking fee&lt;br /&gt;
       end&lt;br /&gt;
       def printreceipt&lt;br /&gt;
          #code to print the receipt&lt;br /&gt;
       end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
When a class includes a module via include Module Name, all the methods on that module become instance methods on the class.&lt;br /&gt;
&lt;br /&gt;
 class Parking&lt;br /&gt;
      include ParkingSlot&lt;br /&gt;
      include ParkingMeter&lt;br /&gt;
      def printcardetails&lt;br /&gt;
          #code to print the details and type of car&lt;br /&gt;
      end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
The modules ParkingSlot and ParkingMeter are “included” in the Parking class. All the variables and the instance methods of the modules ParkingSlot and ParkingMeter are now said to be “mixed in” into the class. It is now possible for class instances to use these methods defined in the modules as and when required.&lt;br /&gt;
 p=Parking.new   &lt;br /&gt;
 p.park            # -&amp;gt; Method park from module ParkingSlot&lt;br /&gt;
 p.unpark          # -&amp;gt; Method unpark from module ParkingSlot&lt;br /&gt;
 p.calculate       # -&amp;gt; Method calculate from module ParkingMeter&lt;br /&gt;
 p.printreceipt    # -&amp;gt; Method printreceipt from module ParkingMeter&lt;br /&gt;
 p.printcardetails # -&amp;gt; Calls printCarDetails from class&lt;br /&gt;
The class Parking inherits from both the modules and the module methods are now available in Parking. Hence, it is now possible to use the methods with an instance of the Parking class p.&lt;br /&gt;
Hence, Mixins can be thought of taking different methods and variables defined in different modules making them available as instance methods in the class as well, thereby extending the class’ functionality. Effectively mixed in modules behave as superclass. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:ParkingLot.PNG| center]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Interfaces===&lt;br /&gt;
In an object-oriented programming languages such as Java, C#, an interface[2] is a reference type, similar to a class that can contain only constants, method signatures, and nested types. There are no method bodies. Interfaces cannot be instantiated and they can only be implemented by classes or extended by other interfaces. All methods are public by default and any fields declared in an interface are by default static and final.&lt;br /&gt;
Example:&lt;br /&gt;
 public interface ParkingLot {&lt;br /&gt;
       boolean add(Car car, int slot);&lt;br /&gt;
       boolean remove(Car car);&lt;br /&gt;
       boolean findCar(Int number);&lt;br /&gt;
       int freeSlotsCount();&lt;br /&gt;
       int occupiedSlots();&lt;br /&gt;
       int parkingType();&lt;br /&gt;
       int calculateParkingFee(int hours, int charge, int duration);&lt;br /&gt;
       int findParkingSlot();&lt;br /&gt;
 }&lt;br /&gt;
 public class Mall implements ParkingLot {&lt;br /&gt;
       boolean add(Car car, int slot) { //... Code to add a car into the parking lot... }&lt;br /&gt;
       boolean remove(Car car){ // do something }&lt;br /&gt;
       boolean findCar(Int number){ // do something }&lt;br /&gt;
       int freeSlotsCount(){ // do something }&lt;br /&gt;
       int occupiedSlots(){ // do something }&lt;br /&gt;
       int parkingType(){ // do something }&lt;br /&gt;
       int calculateParkingFee(int hours, int charge, int duration){ // do something }&lt;br /&gt;
       int findParkingSlot(){ // do something }&lt;br /&gt;
 }&lt;br /&gt;
Interfaces play another important role in the object-oriented programming language. Interfaces are not part of the class hierarchy, although they work in combination with classes. Interfaces provide an alternative for languages like Java that does not allow multiple inheritance .&lt;br /&gt;
&lt;br /&gt;
==Mixins vs. Interfaces==&lt;br /&gt;
===Similarities and Differences===&lt;br /&gt;
	Mixins and Interfaces can be considered as a block of code having some variables and methods that are used in a class. The key difference between them is that a Mixin can contain method definitions whereas an Interface cannot.&lt;br /&gt;
&lt;br /&gt;
====Multiple method definitions:====&lt;br /&gt;
	In a class, when more than one module is Mixed in where some modules have overloaded methods, the method from the module that was last Mixed in into the class will be executed.&lt;br /&gt;
 module Frog&lt;br /&gt;
    def sound()&lt;br /&gt;
      puts &amp;quot;croak...&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Dinosaur&lt;br /&gt;
   def sound()&lt;br /&gt;
     puts &amp;quot;roar...&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
       include Frog&lt;br /&gt;
       include Dinosaur&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 Frogosaur.new.sound &amp;lt;br&amp;gt;	&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 roar...&amp;lt;br&amp;gt;&lt;br /&gt;
This is because the class Frogosaur will have only one definition for each method. The users have the flexibility to make use of methods from different modules by rearranging the order of include.&lt;br /&gt;
Interfaces, on the other hand, do not offer the flexibility to have different implementation for a same method. This is because the class implementing the interface can provide only one implementation for each method. Hence there exists only one method definition.&lt;br /&gt;
 public interface Frog {&lt;br /&gt;
     void sound();&lt;br /&gt;
 }&lt;br /&gt;
 public interface Dinasour {&lt;br /&gt;
     void sound();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
 public void sound()&lt;br /&gt;
 {&lt;br /&gt;
     System.out.println(&amp;quot;barks......&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public static void main(String args[])&lt;br /&gt;
 {&lt;br /&gt;
     new Frogosaur().sound();;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 barks...&amp;lt;br&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
====Multiple object behavior:====&lt;br /&gt;
In Ruby, a class’s instance can extend a module and exhibit different behavior than the other instances of the same class.&lt;br /&gt;
&lt;br /&gt;
 module Frog&lt;br /&gt;
  def sound()&lt;br /&gt;
     puts &amp;quot;croak...&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Dinasour&lt;br /&gt;
  def sound()&lt;br /&gt;
    puts &amp;quot;roar...&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
  def sound()&lt;br /&gt;
    puts &amp;quot;barks...&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 beast1= Frogosaur.new&lt;br /&gt;
 beast2= Frogosaur.new&lt;br /&gt;
 beast1.extend(Frog)&lt;br /&gt;
 beast1.sound&lt;br /&gt;
 beast2.sound&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 croak...&amp;lt;br&amp;gt;&lt;br /&gt;
 barks...&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Interfaces are implemented by a class and all instances of the class behave in the same way. They can use only the methods of the class from which they were instantiated.&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
  public void sound()&lt;br /&gt;
  {&lt;br /&gt;
      System.out.println(&amp;quot;Sounds...&amp;quot;);&lt;br /&gt;
  }  &lt;br /&gt;
  public static void main(String args[])&lt;br /&gt;
  {&lt;br /&gt;
      Frogosaur beast1 = new Frogosaur();&lt;br /&gt;
      Frogosaur beast2 = new Frogosaur();&lt;br /&gt;
      object1.sound();&lt;br /&gt;
      object2.sound();&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 Sounds...&amp;lt;br&amp;gt;&lt;br /&gt;
 Sounds...&amp;lt;br&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
====Enumerable – module and interface====&lt;br /&gt;
&lt;br /&gt;
Ruby’s Arrays have several methods defined in them. To use them, the classes need not be a subclass of Array. The classes can include Enumerable and define the each method to use the array’s methods for free.&lt;br /&gt;
&lt;br /&gt;
 class EnumExer&lt;br /&gt;
  include Enumerable&lt;br /&gt;
  def initialize(*arrays)&lt;br /&gt;
    @arrays = arrays&lt;br /&gt;
  end&lt;br /&gt;
  def each&lt;br /&gt;
    @arrays.each { |a| a.each { |x| yield x } }&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 ma = EnumExer.new([4, 1], [3], [2])&lt;br /&gt;
 puts ma.sort&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 1&amp;lt;br&amp;gt;&lt;br /&gt;
 2&amp;lt;br&amp;gt;&lt;br /&gt;
 3&amp;lt;br&amp;gt;&lt;br /&gt;
 4&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can observe that although the arguments passed to the ma instance did not have a similar structure, the sort method was able to identify each entry and sort them. This is because the each method was defined to identify and serve all the entries to the sort method.&lt;br /&gt;
The methods that are defined for Enumerable modules are:&lt;br /&gt;
 [&amp;quot;all?&amp;quot;, &amp;quot;any?&amp;quot;, &amp;quot;collect&amp;quot;, &amp;quot;detect&amp;quot;, &amp;quot;each_with_index&amp;quot;, &amp;quot;entries&amp;quot;,&lt;br /&gt;
 &amp;quot;find&amp;quot;, &amp;quot;find_all&amp;quot;, &amp;quot;grep&amp;quot;, &amp;quot;include?&amp;quot;, &amp;quot;inject&amp;quot;, &amp;quot;map&amp;quot;, &amp;quot;max&amp;quot;,&lt;br /&gt;
 &amp;quot;member?&amp;quot;, &amp;quot;min&amp;quot;, &amp;quot;partition&amp;quot;, &amp;quot;reject&amp;quot;, &amp;quot;select&amp;quot;, &amp;quot;sort&amp;quot;, &amp;quot;sort_by&amp;quot;,&lt;br /&gt;
 &amp;quot;to_a&amp;quot;, &amp;quot;zip&amp;quot;]&lt;br /&gt;
In Java, to use the methods of Enumerable interface, the class should be a subclass of Array.&lt;br /&gt;
&lt;br /&gt;
 public class EnumExer {&lt;br /&gt;
     static String content_string = &amp;quot;zyxwvutsrqponmlkjihgfedcba&amp;quot;;&lt;br /&gt;
     String a=&amp;quot;1&amp;quot;, b=&amp;quot;2&amp;quot;;&lt;br /&gt;
     public static void main(String args[])&lt;br /&gt;
     {&lt;br /&gt;
         char[] content_array = content_string.toCharArray();&lt;br /&gt;
         java.util.Arrays.sort(content_array);&lt;br /&gt;
         content_string=new String(content_array);&lt;br /&gt;
         System.out.println(content_string);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 Abcdefghijklmnopqrstuvwxyz&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, the string object was converted to a character array to be able to use with the sort method.&lt;br /&gt;
&lt;br /&gt;
====Inheritance:====&lt;br /&gt;
Java interfaces can inherit other interfaces but Ruby modules cannot inherit other modules but can contain classes and methods.&lt;br /&gt;
 module Wrapper&lt;br /&gt;
  class Base&lt;br /&gt;
     # Class Body&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Java,&lt;br /&gt;
 public interface Dinasour extends Frog{&lt;br /&gt;
     //variables and method Declarations&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
==== Achieving Multiple Inheritance ====&lt;br /&gt;
Modules and Interfaces help acheive multiple inheritance. Programming languages such as Ruby and Java do not support multiple inheritance. &amp;lt;br&amp;gt;&lt;br /&gt;
Interfaces are used in Java to achieve multiple inheritance. Similarly, modules in Ruby help achieve multiple inheritance.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Ruby,&lt;br /&gt;
&lt;br /&gt;
 module Frog&lt;br /&gt;
   def jump()&lt;br /&gt;
     //Method definition&lt;br /&gt;
    end&lt;br /&gt;
 end &amp;lt;br&amp;gt;&lt;br /&gt;
 module Dinosaur&lt;br /&gt;
  def strength()&lt;br /&gt;
    //Method definition&lt;br /&gt;
  end&lt;br /&gt;
 end &amp;lt;br&amp;gt;&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
      include Frog&lt;br /&gt;
      include Dinosaur&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
In Java,&lt;br /&gt;
 public interface Frog {&lt;br /&gt;
     void jump();&lt;br /&gt;
 } &amp;lt;br&amp;gt;&lt;br /&gt;
 public interface Dinasour {&lt;br /&gt;
     void strength();&lt;br /&gt;
 } &amp;lt;br&amp;gt;&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
  //Method Implementations&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Frogosaur inherits the methods of both frog and dinosaur. &lt;br /&gt;
&lt;br /&gt;
===Tabular Comparison===&lt;br /&gt;
{|border=&amp;quot;2&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Mixins&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Interfaces&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|The number of lines of code is lesser since the &amp;lt;br&amp;gt;&lt;br /&gt;
modules contain the method definition. &lt;br /&gt;
|The number of lines of code is bigger since the &amp;lt;br&amp;gt;&lt;br /&gt;
classes have to define the methods.&lt;br /&gt;
|-&lt;br /&gt;
|More modules in a class makes code less readable.&lt;br /&gt;
|The code is more readable since the class contain the &amp;lt;br&amp;gt;&lt;br /&gt;
method definition.&lt;br /&gt;
|-&lt;br /&gt;
|Modules cannot be inherited and cannot form is-a hierarchy.&lt;br /&gt;
|Interfaces can extend another interface and hence can form &amp;lt;br&amp;gt;&lt;br /&gt;
is-a hierarchy.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Drawbacks===&lt;br /&gt;
The drawbacks of the technique of Mixins are very much debated. Though Mixins provide us with an easy way to write flexible and decoupled code and also help us solve the diamond problem, they pose their own problems. In large programs there could be large number of modules and each module could have loads of methods each performing a certain task. To trace the origin of the methods and to keep in mind the hierarchy is practically impossible. Another issue associated with Mixins pose is silent method overriding. Silent method overriding occurs when different modules are included in a class and each module have a method with the same definition. In such a case, the method which gets executed depends upon the way it is included in the class and this is done without any sort of message to the user (silently). In large scale applications this could certainly be an issue considering the number of modules that would be mixed in and it would be difficult to keep track of the hierarchy in which the modules were included.&lt;br /&gt;
Although Java provides polymorphic behavior with the use of interfaces, they sometimes tend to be very slow. The implementation of interfaces is also limited to public methods and constants with no implementation.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Modules and Interfaces offer many advantages. However, they have their own disadvantages. Modules add more flexibility to object-oriented programming by the use of DRY principle and are easy to maintain.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;/div&gt;</summary>
		<author><name>Rgovind2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w16_br&amp;diff=63632</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w16 br</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w16_br&amp;diff=63632"/>
		<updated>2012-09-12T01:16:15Z</updated>

		<summary type="html">&lt;p&gt;Rgovind2: /* Mixins */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Overview==&lt;br /&gt;
&lt;br /&gt;
Mixins are Ruby modules. A module is a grouping of objects under a single name. The objects may be constants, methods, classes, or other modules. Modules are defined in Ruby using the keyword module. They can be incorporated into a class with Ruby’s include statement. This is called a mixin.&lt;br /&gt;
Modules are for sharing behavior (methods), while classes are for modeling relationship between objects. Ruby classes can Mixin a module and receive all its methods for free.&lt;br /&gt;
An Interface in Java programming language is an abstract type that classes must implement. Interfaces are declared using the interface keyword, and may only contain method signature and constant declarations.&lt;br /&gt;
A class that implements an interface must implement all of the methods described in the interface, or be an abstract class.&lt;br /&gt;
&lt;br /&gt;
==Definitions==&lt;br /&gt;
===Mixins===&lt;br /&gt;
	Modules provide a structure to collect classes, methods, and constants into a single, separately defined unit. They provide the simplest and an elegant way to adhere to the &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Don't_repeat_yourself Don't repeat yourself]&amp;lt;/ref&amp;gt; principle. This is useful so as to avoid clashes with existing classes, methods, and constants.&lt;br /&gt;
Modules are defined in Ruby using the module keyword.&lt;br /&gt;
A Mixin is a class that is mixed with a module or a set of modules i.e. the implementation of the modules and the class are intertwined and combined together [3].&lt;br /&gt;
This gives us a neat and controlled way of adding new functionality to classes. Modules are similar to classes in that they hold a collection of methods, variables constants and other modules and class definitions. However, the real usage of a Mixin is exploited when the code in the Mixin starts to interact with code in the class that uses it.&lt;br /&gt;
Below is an example for modules and Mixins:&lt;br /&gt;
The module ParkingSlot consists of the methods park and unpark&lt;br /&gt;
 &lt;br /&gt;
 module ParkingSlot&lt;br /&gt;
       def park&lt;br /&gt;
          #code to park the car&lt;br /&gt;
       end&lt;br /&gt;
       def unpark&lt;br /&gt;
          #code to unpark the car&lt;br /&gt;
       end&lt;br /&gt;
 end&lt;br /&gt;
The module ParkingMeter consists of the methods calculate and printreceipt&lt;br /&gt;
&lt;br /&gt;
 module ParkingMeter&lt;br /&gt;
       def calculate&lt;br /&gt;
          #code to calculate the parking fee&lt;br /&gt;
       end&lt;br /&gt;
       def printreceipt&lt;br /&gt;
          #code to print the receipt&lt;br /&gt;
       end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
When a class includes a module via include Module Name, all the methods on that module become instance methods on the class.&lt;br /&gt;
&lt;br /&gt;
 class Parking&lt;br /&gt;
      include ParkingSlot&lt;br /&gt;
      include ParkingMeter&lt;br /&gt;
      def printcardetails&lt;br /&gt;
          #code to print the details and type of car&lt;br /&gt;
      end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
The modules ParkingSlot and ParkingMeter are “included” in the Parking class. All the variables and the instance methods of the modules ParkingSlot and ParkingMeter are now said to be “mixed in” into the class. It is now possible for class instances to use these methods defined in the modules as and when required.&lt;br /&gt;
 p=Parking.new   &lt;br /&gt;
 p.park            # -&amp;gt; Method park from module ParkingSlot&lt;br /&gt;
 p.unpark          # -&amp;gt; Method unpark from module ParkingSlot&lt;br /&gt;
 p.calculate       # -&amp;gt; Method calculate from module ParkingMeter&lt;br /&gt;
 p.printreceipt    # -&amp;gt; Method printreceipt from module ParkingMeter&lt;br /&gt;
 p.printcardetails # -&amp;gt; Calls printCarDetails from class&lt;br /&gt;
The class Parking inherits from both the modules and the module methods are now available in Parking. Hence, it is now possible to use the methods with an instance of the Parking class p.&lt;br /&gt;
Hence, Mixins can be thought of taking different methods and variables defined in different modules making them available as instance methods in the class as well, thereby extending the class’ functionality. Effectively mixed in modules behave as superclass. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:ParkingLot.PNG| center]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Interfaces===&lt;br /&gt;
In an object-oriented programming languages such as Java, C#, an interface[2] is a reference type, similar to a class that can contain only constants, method signatures, and nested types. There are no method bodies. Interfaces cannot be instantiated and they can only be implemented by classes or extended by other interfaces. All methods are public by default and any fields declared in an interface are by default static and final.&lt;br /&gt;
Example:&lt;br /&gt;
 public interface ParkingLot {&lt;br /&gt;
       boolean add(Car car, int slot);&lt;br /&gt;
       boolean remove(Car car);&lt;br /&gt;
       boolean findCar(Int number);&lt;br /&gt;
       int freeSlotsCount();&lt;br /&gt;
       int occupiedSlots();&lt;br /&gt;
       int parkingType();&lt;br /&gt;
       int calculateParkingFee(int hours, int charge, int duration);&lt;br /&gt;
       int findParkingSlot();&lt;br /&gt;
 }&lt;br /&gt;
 public class Mall implements ParkingLot {&lt;br /&gt;
       boolean add(Car car, int slot) { //... Code to add a car into the parking lot... }&lt;br /&gt;
       boolean remove(Car car){ // do something }&lt;br /&gt;
       boolean findCar(Int number){ // do something }&lt;br /&gt;
       int freeSlotsCount(){ // do something }&lt;br /&gt;
       int occupiedSlots(){ // do something }&lt;br /&gt;
       int parkingType(){ // do something }&lt;br /&gt;
       int calculateParkingFee(int hours, int charge, int duration){ // do something }&lt;br /&gt;
       int findParkingSlot(){ // do something }&lt;br /&gt;
 }&lt;br /&gt;
Interfaces play another important role in the object-oriented programming language. Interfaces are not part of the class hierarchy, although they work in combination with classes. Interfaces provide an alternative for languages like Java that does not allow multiple inheritance .&lt;br /&gt;
&lt;br /&gt;
==Mixins vs. Interfaces==&lt;br /&gt;
===Similarities and Differences===&lt;br /&gt;
	Mixins and Interfaces can be considered as a block of code having some variables and methods that are used in a class. The key difference between them is that a Mixin can contain method definitions whereas an Interface cannot.&lt;br /&gt;
&lt;br /&gt;
====Multiple method definitions:====&lt;br /&gt;
	In a class, when more than one module is Mixed in where some modules have overloaded methods, the method from the module that was last Mixed in into the class will be executed.&lt;br /&gt;
 module Frog&lt;br /&gt;
    def sound()&lt;br /&gt;
      puts &amp;quot;croak...&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Dinosaur&lt;br /&gt;
   def sound()&lt;br /&gt;
     puts &amp;quot;roar...&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
       include Frog&lt;br /&gt;
       include Dinosaur&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 Frogosaur.new.sound &amp;lt;br&amp;gt;	&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 roar...&amp;lt;br&amp;gt;&lt;br /&gt;
This is because the class Frogosaur will have only one definition for each method. The users have the flexibility to make use of methods from different modules by rearranging the order of include.&lt;br /&gt;
Interfaces, on the other hand, do not offer the flexibility to have different implementation for a same method. This is because the class implementing the interface can provide only one implementation for each method. Hence there exists only one method definition.&lt;br /&gt;
 public interface Frog {&lt;br /&gt;
     void sound();&lt;br /&gt;
 }&lt;br /&gt;
 public interface Dinasour {&lt;br /&gt;
     void sound();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
 public void sound()&lt;br /&gt;
 {&lt;br /&gt;
     System.out.println(&amp;quot;barks......&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public static void main(String args[])&lt;br /&gt;
 {&lt;br /&gt;
     new Frogosaur().sound();;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 barks...&amp;lt;br&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
====Multiple object behavior:====&lt;br /&gt;
In Ruby, a class’s instance can extend a module and exhibit different behavior than the other instances of the same class.&lt;br /&gt;
&lt;br /&gt;
 module Frog&lt;br /&gt;
  def sound()&lt;br /&gt;
     puts &amp;quot;croak...&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Dinasour&lt;br /&gt;
  def sound()&lt;br /&gt;
    puts &amp;quot;roar...&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
  def sound()&lt;br /&gt;
    puts &amp;quot;barks...&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 beast1= Frogosaur.new&lt;br /&gt;
 beast2= Frogosaur.new&lt;br /&gt;
 beast1.extend(Frog)&lt;br /&gt;
 beast1.sound&lt;br /&gt;
 beast2.sound&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 croak...&amp;lt;br&amp;gt;&lt;br /&gt;
 barks...&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Interfaces are implemented by a class and all instances of the class behave in the same way. They can use only the methods of the class from which they were instantiated.&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
  public void sound()&lt;br /&gt;
  {&lt;br /&gt;
      System.out.println(&amp;quot;Sounds...&amp;quot;);&lt;br /&gt;
  }  &lt;br /&gt;
  public static void main(String args[])&lt;br /&gt;
  {&lt;br /&gt;
      Frogosaur beast1 = new Frogosaur();&lt;br /&gt;
      Frogosaur beast2 = new Frogosaur();&lt;br /&gt;
      object1.sound();&lt;br /&gt;
      object2.sound();&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 Sounds...&amp;lt;br&amp;gt;&lt;br /&gt;
 Sounds...&amp;lt;br&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
====Enumerable – module and interface====&lt;br /&gt;
&lt;br /&gt;
Ruby’s Arrays have several methods defined in them. To use them, the classes need not be a subclass of Array. The classes can include Enumerable and define the each method to use the array’s methods for free.&lt;br /&gt;
&lt;br /&gt;
 class EnumExer&lt;br /&gt;
  include Enumerable&lt;br /&gt;
  def initialize(*arrays)&lt;br /&gt;
    @arrays = arrays&lt;br /&gt;
  end&lt;br /&gt;
  def each&lt;br /&gt;
    @arrays.each { |a| a.each { |x| yield x } }&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 ma = EnumExer.new([4, 1], [3], [2])&lt;br /&gt;
 puts ma.sort&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 1&amp;lt;br&amp;gt;&lt;br /&gt;
 2&amp;lt;br&amp;gt;&lt;br /&gt;
 3&amp;lt;br&amp;gt;&lt;br /&gt;
 4&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can observe that although the arguments passed to the ma instance did not have a similar structure, the sort method was able to identify each entry and sort them. This is because the each method was defined to identify and serve all the entries to the sort method.&lt;br /&gt;
The methods that are defined for Enumerable modules are:&lt;br /&gt;
 [&amp;quot;all?&amp;quot;, &amp;quot;any?&amp;quot;, &amp;quot;collect&amp;quot;, &amp;quot;detect&amp;quot;, &amp;quot;each_with_index&amp;quot;, &amp;quot;entries&amp;quot;,&lt;br /&gt;
 &amp;quot;find&amp;quot;, &amp;quot;find_all&amp;quot;, &amp;quot;grep&amp;quot;, &amp;quot;include?&amp;quot;, &amp;quot;inject&amp;quot;, &amp;quot;map&amp;quot;, &amp;quot;max&amp;quot;,&lt;br /&gt;
 &amp;quot;member?&amp;quot;, &amp;quot;min&amp;quot;, &amp;quot;partition&amp;quot;, &amp;quot;reject&amp;quot;, &amp;quot;select&amp;quot;, &amp;quot;sort&amp;quot;, &amp;quot;sort_by&amp;quot;,&lt;br /&gt;
 &amp;quot;to_a&amp;quot;, &amp;quot;zip&amp;quot;]&lt;br /&gt;
In Java, to use the methods of Enumerable interface, the class should be a subclass of Array.&lt;br /&gt;
&lt;br /&gt;
 public class EnumExer {&lt;br /&gt;
     static String content_string = &amp;quot;zyxwvutsrqponmlkjihgfedcba&amp;quot;;&lt;br /&gt;
     String a=&amp;quot;1&amp;quot;, b=&amp;quot;2&amp;quot;;&lt;br /&gt;
     public static void main(String args[])&lt;br /&gt;
     {&lt;br /&gt;
         char[] content_array = content_string.toCharArray();&lt;br /&gt;
         java.util.Arrays.sort(content_array);&lt;br /&gt;
         content_string=new String(content_array);&lt;br /&gt;
         System.out.println(content_string);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 Abcdefghijklmnopqrstuvwxyz&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, the string object was converted to a character array to be able to use with the sort method.&lt;br /&gt;
&lt;br /&gt;
====Inheritance:====&lt;br /&gt;
Java interfaces can inherit other interfaces but Ruby modules cannot inherit other modules but can contain classes and methods.&lt;br /&gt;
 module Wrapper&lt;br /&gt;
  class Base&lt;br /&gt;
     # Class Body&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Java,&lt;br /&gt;
 public interface Dinasour extends Frog{&lt;br /&gt;
     //variables and method Declarations&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
==== Achieving Multiple Inheritance ====&lt;br /&gt;
Modules and Interfaces help acheive multiple inheritance. Programming languages such as Ruby and Java do not support multiple inheritance. &amp;lt;br&amp;gt;&lt;br /&gt;
Interfaces are used in Java to achieve multiple inheritance. Similarly, modules in Ruby help achieve multiple inheritance.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Ruby,&lt;br /&gt;
&lt;br /&gt;
 module Frog&lt;br /&gt;
   def jump()&lt;br /&gt;
     //Method definition&lt;br /&gt;
    end&lt;br /&gt;
 end &amp;lt;br&amp;gt;&lt;br /&gt;
 module Dinosaur&lt;br /&gt;
  def strength()&lt;br /&gt;
    //Method definition&lt;br /&gt;
  end&lt;br /&gt;
 end &amp;lt;br&amp;gt;&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
      include Frog&lt;br /&gt;
      include Dinosaur&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
In Java,&lt;br /&gt;
 public interface Frog {&lt;br /&gt;
     void jump();&lt;br /&gt;
 } &amp;lt;br&amp;gt;&lt;br /&gt;
 public interface Dinasour {&lt;br /&gt;
     void strength();&lt;br /&gt;
 } &amp;lt;br&amp;gt;&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
  //Method Implementations&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Frogosaur inherits the methods of both frog and dinosaur. &lt;br /&gt;
&lt;br /&gt;
===Tabular Comparison===&lt;br /&gt;
{|border=&amp;quot;2&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Mixins&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Interfaces&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|The number of lines of code is lesser since the &amp;lt;br&amp;gt;&lt;br /&gt;
modules contain the method definition. &lt;br /&gt;
|The number of lines of code is bigger since the &amp;lt;br&amp;gt;&lt;br /&gt;
classes have to define the methods.&lt;br /&gt;
|-&lt;br /&gt;
|More modules in a class makes code less readable.&lt;br /&gt;
|The code is more readable since the class contain the &amp;lt;br&amp;gt;&lt;br /&gt;
method definition.&lt;br /&gt;
|-&lt;br /&gt;
|Modules cannot be inherited and cannot form is-a hierarchy.&lt;br /&gt;
|Interfaces can extend another interface and hence can form &amp;lt;br&amp;gt;&lt;br /&gt;
is-a hierarchy.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Drawbacks===&lt;br /&gt;
The drawbacks of the technique of Mixins are very much debated. Though Mixins provide us with an easy way to write flexible and decoupled code and also help us solve the diamond problem, they pose their own problems. In large programs there could be large number of modules and each module could have loads of methods each performing a certain task. To trace the origin of the methods and to keep in mind the hierarchy is practically impossible. Another issue associated with Mixins pose is silent method overriding. Silent method overriding occurs when different modules are included in a class and each module have a method with the same definition. In such a case, the method which gets executed depends upon the way it is included in the class and this is done without any sort of message to the user (silently). In large scale applications this could certainly be an issue considering the number of modules that would be mixed in and it would be difficult to keep track of the hierarchy in which the modules were included.&lt;br /&gt;
Although Java provides polymorphic behavior with the use of interfaces, they sometimes tend to be very slow. The implementation of interfaces is also limited to public methods and constants with no implementation.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Modules and Interfaces offer many advantages. However, they have their own disadvantages. Modules add more flexibility to object-oriented programming by the use of DRY principle and are easy to maintain.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;/div&gt;</summary>
		<author><name>Rgovind2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w16_br&amp;diff=63630</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w16 br</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w16_br&amp;diff=63630"/>
		<updated>2012-09-12T01:10:12Z</updated>

		<summary type="html">&lt;p&gt;Rgovind2: /* Mixins */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Overview==&lt;br /&gt;
&lt;br /&gt;
Mixins are Ruby modules. A module is a grouping of objects under a single name. The objects may be constants, methods, classes, or other modules. Modules are defined in Ruby using the keyword module. They can be incorporated into a class with Ruby’s include statement. This is called a mixin.&lt;br /&gt;
Modules are for sharing behavior (methods), while classes are for modeling relationship between objects. Ruby classes can Mixin a module and receive all its methods for free.&lt;br /&gt;
An Interface in Java programming language is an abstract type that classes must implement. Interfaces are declared using the interface keyword, and may only contain method signature and constant declarations.&lt;br /&gt;
A class that implements an interface must implement all of the methods described in the interface, or be an abstract class.&lt;br /&gt;
&lt;br /&gt;
==Definitions==&lt;br /&gt;
===Mixins===&lt;br /&gt;
	Modules provide a structure to collect classes, methods, and constants into a single, separately defined unit. They provide the simplest and an elegant way to adhere to the [http://en.wikipedia.org/wiki/Don't_repeat_yourself Don't repeat yourself] principle. This is useful so as to avoid clashes with existing classes, methods, and constants.&lt;br /&gt;
Modules are defined in Ruby using the module keyword.&lt;br /&gt;
A Mixin is a class that is mixed with a module or a set of modules i.e. the implementation of the modules and the class are intertwined and combined together [3].&lt;br /&gt;
This gives us a neat and controlled way of adding new functionality to classes. Modules are similar to classes in that they hold a collection of methods, variables constants and other modules and class definitions. However, the real usage of a Mixin is exploited when the code in the Mixin starts to interact with code in the class that uses it.&lt;br /&gt;
Below is an example for modules and Mixins:&lt;br /&gt;
The module ParkingSlot consists of the methods park and unpark&lt;br /&gt;
 &lt;br /&gt;
 module ParkingSlot&lt;br /&gt;
       def park&lt;br /&gt;
          #code to park the car&lt;br /&gt;
       end&lt;br /&gt;
       def unpark&lt;br /&gt;
          #code to unpark the car&lt;br /&gt;
       end&lt;br /&gt;
 end&lt;br /&gt;
The module ParkingMeter consists of the methods calculate and printreceipt&lt;br /&gt;
&lt;br /&gt;
 module ParkingMeter&lt;br /&gt;
       def calculate&lt;br /&gt;
          #code to calculate the parking fee&lt;br /&gt;
       end&lt;br /&gt;
       def printreceipt&lt;br /&gt;
          #code to print the receipt&lt;br /&gt;
       end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
When a class includes a module via include Module Name, all the methods on that module become instance methods on the class.&lt;br /&gt;
&lt;br /&gt;
 class Parking&lt;br /&gt;
      include ParkingSlot&lt;br /&gt;
      include ParkingMeter&lt;br /&gt;
      def printcardetails&lt;br /&gt;
          #code to print the details and type of car&lt;br /&gt;
      end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
The modules ParkingSlot and ParkingMeter are “included” in the Parking class. All the variables and the instance methods of the modules ParkingSlot and ParkingMeter are now said to be “mixed in” into the class. It is now possible for class instances to use these methods defined in the modules as and when required.&lt;br /&gt;
 p=Parking.new   &lt;br /&gt;
 p.park            # -&amp;gt; Method park from module ParkingSlot&lt;br /&gt;
 p.unpark          # -&amp;gt; Method unpark from module ParkingSlot&lt;br /&gt;
 p.calculate       # -&amp;gt; Method calculate from module ParkingMeter&lt;br /&gt;
 p.printreceipt    # -&amp;gt; Method printreceipt from module ParkingMeter&lt;br /&gt;
 p.printcardetails # -&amp;gt; Calls printCarDetails from class&lt;br /&gt;
The class Parking inherits from both the modules and the module methods are now available in Parking. Hence, it is now possible to use the methods with an instance of the Parking class p.&lt;br /&gt;
Hence, Mixins can be thought of taking different methods and variables defined in different modules making them available as instance methods in the class as well, thereby extending the class’ functionality. Effectively mixed in modules behave as superclass. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:ParkingLot.PNG| center]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Interfaces===&lt;br /&gt;
In an object-oriented programming languages such as Java, C#, an interface[2] is a reference type, similar to a class that can contain only constants, method signatures, and nested types. There are no method bodies. Interfaces cannot be instantiated and they can only be implemented by classes or extended by other interfaces. All methods are public by default and any fields declared in an interface are by default static and final.&lt;br /&gt;
Example:&lt;br /&gt;
 public interface ParkingLot {&lt;br /&gt;
       boolean add(Car car, int slot);&lt;br /&gt;
       boolean remove(Car car);&lt;br /&gt;
       boolean findCar(Int number);&lt;br /&gt;
       int freeSlotsCount();&lt;br /&gt;
       int occupiedSlots();&lt;br /&gt;
       int parkingType();&lt;br /&gt;
       int calculateParkingFee(int hours, int charge, int duration);&lt;br /&gt;
       int findParkingSlot();&lt;br /&gt;
 }&lt;br /&gt;
 public class Mall implements ParkingLot {&lt;br /&gt;
       boolean add(Car car, int slot) { //... Code to add a car into the parking lot... }&lt;br /&gt;
       boolean remove(Car car){ // do something }&lt;br /&gt;
       boolean findCar(Int number){ // do something }&lt;br /&gt;
       int freeSlotsCount(){ // do something }&lt;br /&gt;
       int occupiedSlots(){ // do something }&lt;br /&gt;
       int parkingType(){ // do something }&lt;br /&gt;
       int calculateParkingFee(int hours, int charge, int duration){ // do something }&lt;br /&gt;
       int findParkingSlot(){ // do something }&lt;br /&gt;
 }&lt;br /&gt;
Interfaces play another important role in the object-oriented programming language. Interfaces are not part of the class hierarchy, although they work in combination with classes. Interfaces provide an alternative for languages like Java that does not allow multiple inheritance .&lt;br /&gt;
&lt;br /&gt;
==Mixins vs. Interfaces==&lt;br /&gt;
===Similarities and Differences===&lt;br /&gt;
	Mixins and Interfaces can be considered as a block of code having some variables and methods that are used in a class. The key difference between them is that a Mixin can contain method definitions whereas an Interface cannot.&lt;br /&gt;
&lt;br /&gt;
====Multiple method definitions:====&lt;br /&gt;
	In a class, when more than one module is Mixed in where some modules have overloaded methods, the method from the module that was last Mixed in into the class will be executed.&lt;br /&gt;
 module Frog&lt;br /&gt;
    def sound()&lt;br /&gt;
      puts &amp;quot;croak...&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Dinosaur&lt;br /&gt;
   def sound()&lt;br /&gt;
     puts &amp;quot;roar...&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
       include Frog&lt;br /&gt;
       include Dinosaur&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 Frogosaur.new.sound &amp;lt;br&amp;gt;	&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 roar...&amp;lt;br&amp;gt;&lt;br /&gt;
This is because the class Frogosaur will have only one definition for each method. The users have the flexibility to make use of methods from different modules by rearranging the order of include.&lt;br /&gt;
Interfaces, on the other hand, do not offer the flexibility to have different implementation for a same method. This is because the class implementing the interface can provide only one implementation for each method. Hence there exists only one method definition.&lt;br /&gt;
 public interface Frog {&lt;br /&gt;
     void sound();&lt;br /&gt;
 }&lt;br /&gt;
 public interface Dinasour {&lt;br /&gt;
     void sound();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
 public void sound()&lt;br /&gt;
 {&lt;br /&gt;
     System.out.println(&amp;quot;barks......&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public static void main(String args[])&lt;br /&gt;
 {&lt;br /&gt;
     new Frogosaur().sound();;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 barks...&amp;lt;br&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
====Multiple object behavior:====&lt;br /&gt;
In Ruby, a class’s instance can extend a module and exhibit different behavior than the other instances of the same class.&lt;br /&gt;
&lt;br /&gt;
 module Frog&lt;br /&gt;
  def sound()&lt;br /&gt;
     puts &amp;quot;croak...&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Dinasour&lt;br /&gt;
  def sound()&lt;br /&gt;
    puts &amp;quot;roar...&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
  def sound()&lt;br /&gt;
    puts &amp;quot;barks...&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 beast1= Frogosaur.new&lt;br /&gt;
 beast2= Frogosaur.new&lt;br /&gt;
 beast1.extend(Frog)&lt;br /&gt;
 beast1.sound&lt;br /&gt;
 beast2.sound&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 croak...&amp;lt;br&amp;gt;&lt;br /&gt;
 barks...&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Interfaces are implemented by a class and all instances of the class behave in the same way. They can use only the methods of the class from which they were instantiated.&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
  public void sound()&lt;br /&gt;
  {&lt;br /&gt;
      System.out.println(&amp;quot;Sounds...&amp;quot;);&lt;br /&gt;
  }  &lt;br /&gt;
  public static void main(String args[])&lt;br /&gt;
  {&lt;br /&gt;
      Frogosaur beast1 = new Frogosaur();&lt;br /&gt;
      Frogosaur beast2 = new Frogosaur();&lt;br /&gt;
      object1.sound();&lt;br /&gt;
      object2.sound();&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 Sounds...&amp;lt;br&amp;gt;&lt;br /&gt;
 Sounds...&amp;lt;br&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
====Enumerable – module and interface====&lt;br /&gt;
&lt;br /&gt;
Ruby’s Arrays have several methods defined in them. To use them, the classes need not be a subclass of Array. The classes can include Enumerable and define the each method to use the array’s methods for free.&lt;br /&gt;
&lt;br /&gt;
 class EnumExer&lt;br /&gt;
  include Enumerable&lt;br /&gt;
  def initialize(*arrays)&lt;br /&gt;
    @arrays = arrays&lt;br /&gt;
  end&lt;br /&gt;
  def each&lt;br /&gt;
    @arrays.each { |a| a.each { |x| yield x } }&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 ma = EnumExer.new([4, 1], [3], [2])&lt;br /&gt;
 puts ma.sort&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 1&amp;lt;br&amp;gt;&lt;br /&gt;
 2&amp;lt;br&amp;gt;&lt;br /&gt;
 3&amp;lt;br&amp;gt;&lt;br /&gt;
 4&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can observe that although the arguments passed to the ma instance did not have a similar structure, the sort method was able to identify each entry and sort them. This is because the each method was defined to identify and serve all the entries to the sort method.&lt;br /&gt;
The methods that are defined for Enumerable modules are:&lt;br /&gt;
 [&amp;quot;all?&amp;quot;, &amp;quot;any?&amp;quot;, &amp;quot;collect&amp;quot;, &amp;quot;detect&amp;quot;, &amp;quot;each_with_index&amp;quot;, &amp;quot;entries&amp;quot;,&lt;br /&gt;
 &amp;quot;find&amp;quot;, &amp;quot;find_all&amp;quot;, &amp;quot;grep&amp;quot;, &amp;quot;include?&amp;quot;, &amp;quot;inject&amp;quot;, &amp;quot;map&amp;quot;, &amp;quot;max&amp;quot;,&lt;br /&gt;
 &amp;quot;member?&amp;quot;, &amp;quot;min&amp;quot;, &amp;quot;partition&amp;quot;, &amp;quot;reject&amp;quot;, &amp;quot;select&amp;quot;, &amp;quot;sort&amp;quot;, &amp;quot;sort_by&amp;quot;,&lt;br /&gt;
 &amp;quot;to_a&amp;quot;, &amp;quot;zip&amp;quot;]&lt;br /&gt;
In Java, to use the methods of Enumerable interface, the class should be a subclass of Array.&lt;br /&gt;
&lt;br /&gt;
 public class EnumExer {&lt;br /&gt;
     static String content_string = &amp;quot;zyxwvutsrqponmlkjihgfedcba&amp;quot;;&lt;br /&gt;
     String a=&amp;quot;1&amp;quot;, b=&amp;quot;2&amp;quot;;&lt;br /&gt;
     public static void main(String args[])&lt;br /&gt;
     {&lt;br /&gt;
         char[] content_array = content_string.toCharArray();&lt;br /&gt;
         java.util.Arrays.sort(content_array);&lt;br /&gt;
         content_string=new String(content_array);&lt;br /&gt;
         System.out.println(content_string);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 Abcdefghijklmnopqrstuvwxyz&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, the string object was converted to a character array to be able to use with the sort method.&lt;br /&gt;
&lt;br /&gt;
====Inheritance:====&lt;br /&gt;
Java interfaces can inherit other interfaces but Ruby modules cannot inherit other modules but can contain classes and methods.&lt;br /&gt;
 module Wrapper&lt;br /&gt;
  class Base&lt;br /&gt;
     # Class Body&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Java,&lt;br /&gt;
 public interface Dinasour extends Frog{&lt;br /&gt;
     //variables and method Declarations&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
==== Achieving Multiple Inheritance ====&lt;br /&gt;
Modules and Interfaces help acheive multiple inheritance. Programming languages such as Ruby and Java do not support multiple inheritance. &amp;lt;br&amp;gt;&lt;br /&gt;
Interfaces are used in Java to achieve multiple inheritance. Similarly, modules in Ruby help achieve multiple inheritance.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Ruby,&lt;br /&gt;
&lt;br /&gt;
 module Frog&lt;br /&gt;
   def jump()&lt;br /&gt;
     //Method definition&lt;br /&gt;
    end&lt;br /&gt;
 end &amp;lt;br&amp;gt;&lt;br /&gt;
 module Dinosaur&lt;br /&gt;
  def strength()&lt;br /&gt;
    //Method definition&lt;br /&gt;
  end&lt;br /&gt;
 end &amp;lt;br&amp;gt;&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
      include Frog&lt;br /&gt;
      include Dinosaur&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
In Java,&lt;br /&gt;
 public interface Frog {&lt;br /&gt;
     void jump();&lt;br /&gt;
 } &amp;lt;br&amp;gt;&lt;br /&gt;
 public interface Dinasour {&lt;br /&gt;
     void strength();&lt;br /&gt;
 } &amp;lt;br&amp;gt;&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
  //Method Implementations&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Frogosaur inherits the methods of both frog and dinosaur. &lt;br /&gt;
&lt;br /&gt;
===Tabular Comparison===&lt;br /&gt;
{|border=&amp;quot;2&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Mixins&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Interfaces&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|The number of lines of code is lesser since the &amp;lt;br&amp;gt;&lt;br /&gt;
modules contain the method definition. &lt;br /&gt;
|The number of lines of code is bigger since the &amp;lt;br&amp;gt;&lt;br /&gt;
classes have to define the methods.&lt;br /&gt;
|-&lt;br /&gt;
|More modules in a class makes code less readable.&lt;br /&gt;
|The code is more readable since the class contain the &amp;lt;br&amp;gt;&lt;br /&gt;
method definition.&lt;br /&gt;
|-&lt;br /&gt;
|Modules cannot be inherited and cannot form is-a hierarchy.&lt;br /&gt;
|Interfaces can extend another interface and hence can form &amp;lt;br&amp;gt;&lt;br /&gt;
is-a hierarchy.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Drawbacks===&lt;br /&gt;
The drawbacks of the technique of Mixins are very much debated. Though Mixins provide us with an easy way to write flexible and decoupled code and also help us solve the diamond problem, they pose their own problems. In large programs there could be large number of modules and each module could have loads of methods each performing a certain task. To trace the origin of the methods and to keep in mind the hierarchy is practically impossible. Another issue associated with Mixins pose is silent method overriding. Silent method overriding occurs when different modules are included in a class and each module have a method with the same definition. In such a case, the method which gets executed depends upon the way it is included in the class and this is done without any sort of message to the user (silently). In large scale applications this could certainly be an issue considering the number of modules that would be mixed in and it would be difficult to keep track of the hierarchy in which the modules were included.&lt;br /&gt;
Although Java provides polymorphic behavior with the use of interfaces, they sometimes tend to be very slow. The implementation of interfaces is also limited to public methods and constants with no implementation.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Modules and Interfaces offer many advantages. However, they have their own disadvantages. Modules add more flexibility to object-oriented programming by the use of DRY principle and are easy to maintain.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;/div&gt;</summary>
		<author><name>Rgovind2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w16_br&amp;diff=63627</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w16 br</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w16_br&amp;diff=63627"/>
		<updated>2012-09-12T00:59:46Z</updated>

		<summary type="html">&lt;p&gt;Rgovind2: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Overview==&lt;br /&gt;
&lt;br /&gt;
Mixins are Ruby modules. A module is a grouping of objects under a single name. The objects may be constants, methods, classes, or other modules. Modules are defined in Ruby using the keyword module. They can be incorporated into a class with Ruby’s include statement. This is called a mixin.&lt;br /&gt;
Modules are for sharing behavior (methods), while classes are for modeling relationship between objects. Ruby classes can Mixin a module and receive all its methods for free.&lt;br /&gt;
An Interface in Java programming language is an abstract type that classes must implement. Interfaces are declared using the interface keyword, and may only contain method signature and constant declarations.&lt;br /&gt;
A class that implements an interface must implement all of the methods described in the interface, or be an abstract class.&lt;br /&gt;
&lt;br /&gt;
==Definitions==&lt;br /&gt;
===Mixins===&lt;br /&gt;
	Modules provide a structure to collect classes, methods, and constants into a single, separately defined unit. They provide the simplest and an elegant way to adhere to the Don't repeat yourself principle. This is useful so as to avoid clashes with existing classes, methods, and constants.&lt;br /&gt;
Modules are defined in Ruby using the module keyword.&lt;br /&gt;
A Mixin is a class that is mixed with a module or a set of modules i.e. the implementation of the modules and the class are intertwined and combined together [3].&lt;br /&gt;
This gives us a neat and controlled way of adding new functionality to classes. Modules are similar to classes in that they hold a collection of methods, variables constants and other modules and class definitions. However, the real usage of a Mixin is exploited when the code in the Mixin starts to interact with code in the class that uses it.&lt;br /&gt;
Below is an example for modules and Mixins:&lt;br /&gt;
The module ParkingSlot consists of the methods park and unpark&lt;br /&gt;
 &lt;br /&gt;
 module ParkingSlot&lt;br /&gt;
       def park&lt;br /&gt;
          #code to park the car&lt;br /&gt;
       end&lt;br /&gt;
       def unpark&lt;br /&gt;
          #code to unpark the car&lt;br /&gt;
       end&lt;br /&gt;
 end&lt;br /&gt;
The module ParkingMeter consists of the methods calculate and printreceipt&lt;br /&gt;
&lt;br /&gt;
 module ParkingMeter&lt;br /&gt;
       def calculate&lt;br /&gt;
          #code to calculate the parking fee&lt;br /&gt;
       end&lt;br /&gt;
       def printreceipt&lt;br /&gt;
          #code to print the receipt&lt;br /&gt;
       end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
When a class includes a module via include Module Name, all the methods on that module become instance methods on the class.&lt;br /&gt;
&lt;br /&gt;
 class Parking&lt;br /&gt;
      include ParkingSlot&lt;br /&gt;
      include ParkingMeter&lt;br /&gt;
      def printcardetails&lt;br /&gt;
          #code to print the details and type of car&lt;br /&gt;
      end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
The modules ParkingSlot and ParkingMeter are “included” in the Parking class. All the variables and the instance methods of the modules ParkingSlot and ParkingMeter are now said to be “mixed in” into the class. It is now possible for class instances to use these methods defined in the modules as and when required.&lt;br /&gt;
 p=Parking.new   &lt;br /&gt;
 p.park            # -&amp;gt; Method park from module ParkingSlot&lt;br /&gt;
 p.unpark          # -&amp;gt; Method unpark from module ParkingSlot&lt;br /&gt;
 p.calculate       # -&amp;gt; Method calculate from module ParkingMeter&lt;br /&gt;
 p.printreceipt    # -&amp;gt; Method printreceipt from module ParkingMeter&lt;br /&gt;
 p.printcardetails # -&amp;gt; Calls printCarDetails from class&lt;br /&gt;
The class Parking inherits from both the modules and the module methods are now available in Parking. Hence, it is now possible to use the methods with an instance of the Parking class p.&lt;br /&gt;
Hence, Mixins can be thought of taking different methods and variables defined in different modules making them available as instance methods in the class as well, thereby extending the class’ functionality. Effectively mixed in modules behave as superclass. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:ParkingLot.PNG| center]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
===Interfaces===&lt;br /&gt;
In an object-oriented programming languages such as Java, C#, an interface[2] is a reference type, similar to a class that can contain only constants, method signatures, and nested types. There are no method bodies. Interfaces cannot be instantiated and they can only be implemented by classes or extended by other interfaces. All methods are public by default and any fields declared in an interface are by default static and final.&lt;br /&gt;
Example:&lt;br /&gt;
 public interface ParkingLot {&lt;br /&gt;
       boolean add(Car car, int slot);&lt;br /&gt;
       boolean remove(Car car);&lt;br /&gt;
       boolean findCar(Int number);&lt;br /&gt;
       int freeSlotsCount();&lt;br /&gt;
       int occupiedSlots();&lt;br /&gt;
       int parkingType();&lt;br /&gt;
       int calculateParkingFee(int hours, int charge, int duration);&lt;br /&gt;
       int findParkingSlot();&lt;br /&gt;
 }&lt;br /&gt;
 public class Mall implements ParkingLot {&lt;br /&gt;
       boolean add(Car car, int slot) { //... Code to add a car into the parking lot... }&lt;br /&gt;
       boolean remove(Car car){ // do something }&lt;br /&gt;
       boolean findCar(Int number){ // do something }&lt;br /&gt;
       int freeSlotsCount(){ // do something }&lt;br /&gt;
       int occupiedSlots(){ // do something }&lt;br /&gt;
       int parkingType(){ // do something }&lt;br /&gt;
       int calculateParkingFee(int hours, int charge, int duration){ // do something }&lt;br /&gt;
       int findParkingSlot(){ // do something }&lt;br /&gt;
 }&lt;br /&gt;
Interfaces play another important role in the object-oriented programming language. Interfaces are not part of the class hierarchy, although they work in combination with classes. Interfaces provide an alternative for languages like Java that does not allow multiple inheritance .&lt;br /&gt;
&lt;br /&gt;
==Mixins vs. Interfaces==&lt;br /&gt;
===Similarities and Differences===&lt;br /&gt;
	Mixins and Interfaces can be considered as a block of code having some variables and methods that are used in a class. The key difference between them is that a Mixin can contain method definitions whereas an Interface cannot.&lt;br /&gt;
&lt;br /&gt;
====Multiple method definitions:====&lt;br /&gt;
	In a class, when more than one module is Mixed in where some modules have overloaded methods, the method from the module that was last Mixed in into the class will be executed.&lt;br /&gt;
 module Frog&lt;br /&gt;
    def sound()&lt;br /&gt;
      puts &amp;quot;croak...&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Dinosaur&lt;br /&gt;
   def sound()&lt;br /&gt;
     puts &amp;quot;roar...&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
       include Frog&lt;br /&gt;
       include Dinosaur&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 Frogosaur.new.sound &amp;lt;br&amp;gt;	&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 roar...&amp;lt;br&amp;gt;&lt;br /&gt;
This is because the class Frogosaur will have only one definition for each method. The users have the flexibility to make use of methods from different modules by rearranging the order of include.&lt;br /&gt;
Interfaces, on the other hand, do not offer the flexibility to have different implementation for a same method. This is because the class implementing the interface can provide only one implementation for each method. Hence there exists only one method definition.&lt;br /&gt;
 public interface Frog {&lt;br /&gt;
     void sound();&lt;br /&gt;
 }&lt;br /&gt;
 public interface Dinasour {&lt;br /&gt;
     void sound();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
 public void sound()&lt;br /&gt;
 {&lt;br /&gt;
     System.out.println(&amp;quot;barks......&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public static void main(String args[])&lt;br /&gt;
 {&lt;br /&gt;
     new Frogosaur().sound();;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 barks...&amp;lt;br&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
====Multiple object behavior:====&lt;br /&gt;
In Ruby, a class’s instance can extend a module and exhibit different behavior than the other instances of the same class.&lt;br /&gt;
&lt;br /&gt;
 module Frog&lt;br /&gt;
  def sound()&lt;br /&gt;
     puts &amp;quot;croak...&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Dinasour&lt;br /&gt;
  def sound()&lt;br /&gt;
    puts &amp;quot;roar...&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
  def sound()&lt;br /&gt;
    puts &amp;quot;barks...&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 beast1= Frogosaur.new&lt;br /&gt;
 beast2= Frogosaur.new&lt;br /&gt;
 beast1.extend(Frog)&lt;br /&gt;
 beast1.sound&lt;br /&gt;
 beast2.sound&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 croak...&amp;lt;br&amp;gt;&lt;br /&gt;
 barks...&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Interfaces are implemented by a class and all instances of the class behave in the same way. They can use only the methods of the class from which they were instantiated.&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
  public void sound()&lt;br /&gt;
  {&lt;br /&gt;
      System.out.println(&amp;quot;Sounds...&amp;quot;);&lt;br /&gt;
  }  &lt;br /&gt;
  public static void main(String args[])&lt;br /&gt;
  {&lt;br /&gt;
      Frogosaur beast1 = new Frogosaur();&lt;br /&gt;
      Frogosaur beast2 = new Frogosaur();&lt;br /&gt;
      object1.sound();&lt;br /&gt;
      object2.sound();&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 Sounds...&amp;lt;br&amp;gt;&lt;br /&gt;
 Sounds...&amp;lt;br&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
====Enumerable – module and interface====&lt;br /&gt;
&lt;br /&gt;
Ruby’s Arrays have several methods defined in them. To use them, the classes need not be a subclass of Array. The classes can include Enumerable and define the each method to use the array’s methods for free.&lt;br /&gt;
&lt;br /&gt;
 class EnumExer&lt;br /&gt;
  include Enumerable&lt;br /&gt;
  def initialize(*arrays)&lt;br /&gt;
    @arrays = arrays&lt;br /&gt;
  end&lt;br /&gt;
  def each&lt;br /&gt;
    @arrays.each { |a| a.each { |x| yield x } }&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 ma = EnumExer.new([4, 1], [3], [2])&lt;br /&gt;
 puts ma.sort&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 1&amp;lt;br&amp;gt;&lt;br /&gt;
 2&amp;lt;br&amp;gt;&lt;br /&gt;
 3&amp;lt;br&amp;gt;&lt;br /&gt;
 4&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can observe that although the arguments passed to the ma instance did not have a similar structure, the sort method was able to identify each entry and sort them. This is because the each method was defined to identify and serve all the entries to the sort method.&lt;br /&gt;
The methods that are defined for Enumerable modules are:&lt;br /&gt;
 [&amp;quot;all?&amp;quot;, &amp;quot;any?&amp;quot;, &amp;quot;collect&amp;quot;, &amp;quot;detect&amp;quot;, &amp;quot;each_with_index&amp;quot;, &amp;quot;entries&amp;quot;,&lt;br /&gt;
 &amp;quot;find&amp;quot;, &amp;quot;find_all&amp;quot;, &amp;quot;grep&amp;quot;, &amp;quot;include?&amp;quot;, &amp;quot;inject&amp;quot;, &amp;quot;map&amp;quot;, &amp;quot;max&amp;quot;,&lt;br /&gt;
 &amp;quot;member?&amp;quot;, &amp;quot;min&amp;quot;, &amp;quot;partition&amp;quot;, &amp;quot;reject&amp;quot;, &amp;quot;select&amp;quot;, &amp;quot;sort&amp;quot;, &amp;quot;sort_by&amp;quot;,&lt;br /&gt;
 &amp;quot;to_a&amp;quot;, &amp;quot;zip&amp;quot;]&lt;br /&gt;
In Java, to use the methods of Enumerable interface, the class should be a subclass of Array.&lt;br /&gt;
&lt;br /&gt;
 public class EnumExer {&lt;br /&gt;
     static String content_string = &amp;quot;zyxwvutsrqponmlkjihgfedcba&amp;quot;;&lt;br /&gt;
     String a=&amp;quot;1&amp;quot;, b=&amp;quot;2&amp;quot;;&lt;br /&gt;
     public static void main(String args[])&lt;br /&gt;
     {&lt;br /&gt;
         char[] content_array = content_string.toCharArray();&lt;br /&gt;
         java.util.Arrays.sort(content_array);&lt;br /&gt;
         content_string=new String(content_array);&lt;br /&gt;
         System.out.println(content_string);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 Abcdefghijklmnopqrstuvwxyz&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, the string object was converted to a character array to be able to use with the sort method.&lt;br /&gt;
&lt;br /&gt;
====Inheritance:====&lt;br /&gt;
Java interfaces can inherit other interfaces but Ruby modules cannot inherit other modules but can contain classes and methods.&lt;br /&gt;
 module Wrapper&lt;br /&gt;
  class Base&lt;br /&gt;
     # Class Body&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Java,&lt;br /&gt;
 public interface Dinasour extends Frog{&lt;br /&gt;
     //variables and method Declarations&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
==== Achieving Multiple Inheritance ====&lt;br /&gt;
Modules and Interfaces help acheive multiple inheritance. Programming languages such as Ruby and Java do not support multiple inheritance. &amp;lt;br&amp;gt;&lt;br /&gt;
Interfaces are used in Java to achieve multiple inheritance. Similarly, modules in Ruby help achieve multiple inheritance.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Ruby,&lt;br /&gt;
&lt;br /&gt;
 module Frog&lt;br /&gt;
   def jump()&lt;br /&gt;
     //Method definition&lt;br /&gt;
    end&lt;br /&gt;
 end &amp;lt;br&amp;gt;&lt;br /&gt;
 module Dinosaur&lt;br /&gt;
  def strength()&lt;br /&gt;
    //Method definition&lt;br /&gt;
  end&lt;br /&gt;
 end &amp;lt;br&amp;gt;&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
      include Frog&lt;br /&gt;
      include Dinosaur&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
In Java,&lt;br /&gt;
 public interface Frog {&lt;br /&gt;
     void jump();&lt;br /&gt;
 } &amp;lt;br&amp;gt;&lt;br /&gt;
 public interface Dinasour {&lt;br /&gt;
     void strength();&lt;br /&gt;
 } &amp;lt;br&amp;gt;&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
  //Method Implementations&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Frogosaur inherits the methods of both frog and dinosaur. &lt;br /&gt;
&lt;br /&gt;
===Tabular Comparison===&lt;br /&gt;
{|border=&amp;quot;2&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Mixins&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Interfaces&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|The number of lines of code is lesser since the &amp;lt;br&amp;gt;&lt;br /&gt;
modules contain the method definition. &lt;br /&gt;
|The number of lines of code is bigger since the &amp;lt;br&amp;gt;&lt;br /&gt;
classes have to define the methods.&lt;br /&gt;
|-&lt;br /&gt;
|More modules in a class makes code less readable.&lt;br /&gt;
|The code is more readable since the class contain the &amp;lt;br&amp;gt;&lt;br /&gt;
method definition.&lt;br /&gt;
|-&lt;br /&gt;
|Modules cannot be inherited and cannot form is-a hierarchy.&lt;br /&gt;
|Interfaces can extend another interface and hence can form &amp;lt;br&amp;gt;&lt;br /&gt;
is-a hierarchy.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Drawbacks===&lt;br /&gt;
The drawbacks of the technique of Mixins are very much debated. Though Mixins provide us with an easy way to write flexible and decoupled code and also help us solve the diamond problem, they pose their own problems. In large programs there could be large number of modules and each module could have loads of methods each performing a certain task. To trace the origin of the methods and to keep in mind the hierarchy is practically impossible. Another issue associated with Mixins pose is silent method overriding. Silent method overriding occurs when different modules are included in a class and each module have a method with the same definition. In such a case, the method which gets executed depends upon the way it is included in the class and this is done without any sort of message to the user (silently). In large scale applications this could certainly be an issue considering the number of modules that would be mixed in and it would be difficult to keep track of the hierarchy in which the modules were included.&lt;br /&gt;
Although Java provides polymorphic behavior with the use of interfaces, they sometimes tend to be very slow. The implementation of interfaces is also limited to public methods and constants with no implementation.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Modules and Interfaces offer many advantages. However, they have their own disadvantages. Modules add more flexibility to object-oriented programming by the use of DRY principle and are easy to maintain.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;/div&gt;</summary>
		<author><name>Rgovind2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w16_br&amp;diff=63625</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w16 br</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w16_br&amp;diff=63625"/>
		<updated>2012-09-12T00:58:18Z</updated>

		<summary type="html">&lt;p&gt;Rgovind2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Overview==&lt;br /&gt;
&lt;br /&gt;
Mixins are Ruby modules. A module is a grouping of objects under a single name. The objects may be constants, methods, classes, or other modules. Modules are defined in Ruby using the keyword module. They can be incorporated into a class with Ruby’s include statement. This is called a mixin.&lt;br /&gt;
Modules are for sharing behavior (methods), while classes are for modeling relationship between objects. Ruby classes can Mixin a module and receive all its methods for free.&lt;br /&gt;
An Interface in Java programming language is an abstract type that classes must implement. Interfaces are declared using the interface keyword, and may only contain method signature and constant declarations.&lt;br /&gt;
A class that implements an interface must implement all of the methods described in the interface, or be an abstract class.&lt;br /&gt;
&lt;br /&gt;
==Definitions==&lt;br /&gt;
===Mixins===&lt;br /&gt;
	Modules provide a structure to collect classes, methods, and constants into a single, separately defined unit. They provide the simplest and an elegant way to adhere to the Don't repeat yourself principle. This is useful so as to avoid clashes with existing classes, methods, and constants.&lt;br /&gt;
Modules are defined in Ruby using the module keyword.&lt;br /&gt;
A Mixin is a class that is mixed with a module or a set of modules i.e. the implementation of the modules and the class are intertwined and combined together [3].&lt;br /&gt;
This gives us a neat and controlled way of adding new functionality to classes. Modules are similar to classes in that they hold a collection of methods, variables constants and other modules and class definitions. However, the real usage of a Mixin is exploited when the code in the Mixin starts to interact with code in the class that uses it.&lt;br /&gt;
Below is an example for modules and Mixins:&lt;br /&gt;
The module ParkingSlot consists of the methods park and unpark&lt;br /&gt;
 &lt;br /&gt;
 module ParkingSlot&lt;br /&gt;
       def park&lt;br /&gt;
          #code to park the car&lt;br /&gt;
       end&lt;br /&gt;
       def unpark&lt;br /&gt;
          #code to unpark the car&lt;br /&gt;
       end&lt;br /&gt;
 end&lt;br /&gt;
The module ParkingMeter consists of the methods calculate and printreceipt&lt;br /&gt;
&lt;br /&gt;
 module ParkingMeter&lt;br /&gt;
       def calculate&lt;br /&gt;
          #code to calculate the parking fee&lt;br /&gt;
       end&lt;br /&gt;
       def printreceipt&lt;br /&gt;
          #code to print the receipt&lt;br /&gt;
       end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
When a class includes a module via include Module Name, all the methods on that module become instance methods on the class.&lt;br /&gt;
&lt;br /&gt;
 class Parking&lt;br /&gt;
      include ParkingSlot&lt;br /&gt;
      include ParkingMeter&lt;br /&gt;
      def printcardetails&lt;br /&gt;
          #code to print the details and type of car&lt;br /&gt;
      end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
The modules ParkingSlot and ParkingMeter are “included” in the Parking class. All the variables and the instance methods of the modules ParkingSlot and ParkingMeter are now said to be “mixed in” into the class. It is now possible for class instances to use these methods defined in the modules as and when required.&lt;br /&gt;
 p=Parking.new   &lt;br /&gt;
 p.park            # -&amp;gt; Method park from module ParkingSlot&lt;br /&gt;
 p.unpark          # -&amp;gt; Method unpark from module ParkingSlot&lt;br /&gt;
 p.calculate       # -&amp;gt; Method calculate from module ParkingMeter&lt;br /&gt;
 p.printreceipt    # -&amp;gt; Method printreceipt from module ParkingMeter&lt;br /&gt;
 p.printcardetails # -&amp;gt; Calls printCarDetails from class&lt;br /&gt;
The class Parking inherits from both the modules and the module methods are now available in Parking. Hence, it is now possible to use the methods with an instance of the Parking class p.&lt;br /&gt;
Hence, Mixins can be thought of taking different methods and variables defined in different modules making them available as instance methods in the class as well, thereby extending the class’ functionality. Effectively mixed in modules behave as superclass. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:ParkingLot.PNG| center]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
===Interfaces===&lt;br /&gt;
In an object-oriented programming languages such as Java, C#, an interface[2] is a reference type, similar to a class that can contain only constants, method signatures, and nested types. There are no method bodies. Interfaces cannot be instantiated and they can only be implemented by classes or extended by other interfaces. All methods are public by default and any fields declared in an interface are by default static and final.&lt;br /&gt;
Example:&lt;br /&gt;
 public interface ParkingLot {&lt;br /&gt;
       boolean add(Car car, int slot);&lt;br /&gt;
       boolean remove(Car car);&lt;br /&gt;
       boolean findCar(Int number);&lt;br /&gt;
       int freeSlotsCount();&lt;br /&gt;
       int occupiedSlots();&lt;br /&gt;
       int parkingType();&lt;br /&gt;
       int calculateParkingFee(int hours, int charge, int duration);&lt;br /&gt;
       int findParkingSlot();&lt;br /&gt;
 }&lt;br /&gt;
 public class Mall implements ParkingLot {&lt;br /&gt;
       boolean add(Car car, int slot) { //... Code to add a car into the parking lot... }&lt;br /&gt;
       boolean remove(Car car){ // do something }&lt;br /&gt;
       boolean findCar(Int number){ // do something }&lt;br /&gt;
       int freeSlotsCount(){ // do something }&lt;br /&gt;
       int occupiedSlots(){ // do something }&lt;br /&gt;
       int parkingType(){ // do something }&lt;br /&gt;
       int calculateParkingFee(int hours, int charge, int duration){ // do something }&lt;br /&gt;
       int findParkingSlot(){ // do something }&lt;br /&gt;
 }&lt;br /&gt;
Interfaces play another important role in the object-oriented programming language. Interfaces are not part of the class hierarchy, although they work in combination with classes. Interfaces provide an alternative for languages like Java that does not allow multiple inheritance .&lt;br /&gt;
&lt;br /&gt;
==Mixins vs. Interfaces==&lt;br /&gt;
===Similarities and Differences===&lt;br /&gt;
	Mixins and Interfaces can be considered as a block of code having some variables and methods that are used in a class. The key difference between them is that a Mixin can contain method definitions whereas an Interface cannot.&lt;br /&gt;
&lt;br /&gt;
====Multiple method definitions:====&lt;br /&gt;
	In a class, when more than one module is Mixed in where some modules have overloaded methods, the method from the module that was last Mixed in into the class will be executed.&lt;br /&gt;
 module Frog&lt;br /&gt;
    def sound()&lt;br /&gt;
      puts &amp;quot;croak...&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Dinosaur&lt;br /&gt;
   def sound()&lt;br /&gt;
     puts &amp;quot;roar...&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
       include Frog&lt;br /&gt;
       include Dinosaur&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 Frogosaur.new.sound &amp;lt;br&amp;gt;	&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 roar...&amp;lt;br&amp;gt;&lt;br /&gt;
This is because the class Frogosaur will have only one definition for each method. The users have the flexibility to make use of methods from different modules by rearranging the order of include.&lt;br /&gt;
Interfaces, on the other hand, do not offer the flexibility to have different implementation for a same method. This is because the class implementing the interface can provide only one implementation for each method. Hence there exists only one method definition.&lt;br /&gt;
 public interface Frog {&lt;br /&gt;
     void sound();&lt;br /&gt;
 }&lt;br /&gt;
 public interface Dinasour {&lt;br /&gt;
     void sound();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
 public void sound()&lt;br /&gt;
 {&lt;br /&gt;
     System.out.println(&amp;quot;barks......&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public static void main(String args[])&lt;br /&gt;
 {&lt;br /&gt;
     new Frogosaur().sound();;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 barks...&amp;lt;br&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
====Multiple object behavior:====&lt;br /&gt;
In Ruby, a class’s instance can extend a module and exhibit different behavior than the other instances of the same class.&lt;br /&gt;
&lt;br /&gt;
 module Frog&lt;br /&gt;
  def sound()&lt;br /&gt;
     puts &amp;quot;croak...&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Dinasour&lt;br /&gt;
  def sound()&lt;br /&gt;
    puts &amp;quot;roar...&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
  def sound()&lt;br /&gt;
    puts &amp;quot;barks...&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 beast1= Frogosaur.new&lt;br /&gt;
 beast2= Frogosaur.new&lt;br /&gt;
 beast1.extend(Frog)&lt;br /&gt;
 beast1.sound&lt;br /&gt;
 beast2.sound&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 croak...&amp;lt;br&amp;gt;&lt;br /&gt;
 barks...&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Interfaces are implemented by a class and all instances of the class behave in the same way. They can use only the methods of the class from which they were instantiated.&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
  public void sound()&lt;br /&gt;
  {&lt;br /&gt;
      System.out.println(&amp;quot;Sounds...&amp;quot;);&lt;br /&gt;
  }  &lt;br /&gt;
  public static void main(String args[])&lt;br /&gt;
  {&lt;br /&gt;
      Frogosaur beast1 = new Frogosaur();&lt;br /&gt;
      Frogosaur beast2 = new Frogosaur();&lt;br /&gt;
      object1.sound();&lt;br /&gt;
      object2.sound();&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 Sounds...&amp;lt;br&amp;gt;&lt;br /&gt;
 Sounds...&amp;lt;br&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
====Enumerable – module and interface====&lt;br /&gt;
&lt;br /&gt;
Ruby’s Arrays have several methods defined in them. To use them, the classes need not be a subclass of Array. The classes can include Enumerable and define the each method to use the array’s methods for free.&lt;br /&gt;
&lt;br /&gt;
 class EnumExer&lt;br /&gt;
  include Enumerable&lt;br /&gt;
  def initialize(*arrays)&lt;br /&gt;
    @arrays = arrays&lt;br /&gt;
  end&lt;br /&gt;
  def each&lt;br /&gt;
    @arrays.each { |a| a.each { |x| yield x } }&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 ma = EnumExer.new([4, 1], [3], [2])&lt;br /&gt;
 puts ma.sort&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 1&amp;lt;br&amp;gt;&lt;br /&gt;
 2&amp;lt;br&amp;gt;&lt;br /&gt;
 3&amp;lt;br&amp;gt;&lt;br /&gt;
 4&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can observe that although the arguments passed to the ma instance did not have a similar structure, the sort method was able to identify each entry and sort them. This is because the each method was defined to identify and serve all the entries to the sort method.&lt;br /&gt;
The methods that are defined for Enumerable modules are:&lt;br /&gt;
 [&amp;quot;all?&amp;quot;, &amp;quot;any?&amp;quot;, &amp;quot;collect&amp;quot;, &amp;quot;detect&amp;quot;, &amp;quot;each_with_index&amp;quot;, &amp;quot;entries&amp;quot;,&lt;br /&gt;
 &amp;quot;find&amp;quot;, &amp;quot;find_all&amp;quot;, &amp;quot;grep&amp;quot;, &amp;quot;include?&amp;quot;, &amp;quot;inject&amp;quot;, &amp;quot;map&amp;quot;, &amp;quot;max&amp;quot;,&lt;br /&gt;
 &amp;quot;member?&amp;quot;, &amp;quot;min&amp;quot;, &amp;quot;partition&amp;quot;, &amp;quot;reject&amp;quot;, &amp;quot;select&amp;quot;, &amp;quot;sort&amp;quot;, &amp;quot;sort_by&amp;quot;,&lt;br /&gt;
 &amp;quot;to_a&amp;quot;, &amp;quot;zip&amp;quot;]&lt;br /&gt;
In Java, to use the methods of Enumerable interface, the class should be a subclass of Array.&lt;br /&gt;
&lt;br /&gt;
 public class EnumExer {&lt;br /&gt;
     static String content_string = &amp;quot;zyxwvutsrqponmlkjihgfedcba&amp;quot;;&lt;br /&gt;
     String a=&amp;quot;1&amp;quot;, b=&amp;quot;2&amp;quot;;&lt;br /&gt;
     public static void main(String args[])&lt;br /&gt;
     {&lt;br /&gt;
         char[] content_array = content_string.toCharArray();&lt;br /&gt;
         java.util.Arrays.sort(content_array);&lt;br /&gt;
         content_string=new String(content_array);&lt;br /&gt;
         System.out.println(content_string);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 Abcdefghijklmnopqrstuvwxyz&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, the string object was converted to a character array to be able to use with the sort method.&lt;br /&gt;
&lt;br /&gt;
====Inheritance:====&lt;br /&gt;
Java interfaces can inherit other interfaces but Ruby modules cannot inherit other modules but can contain classes and methods.&lt;br /&gt;
 module Wrapper&lt;br /&gt;
  class Base&lt;br /&gt;
     # Class Body&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Java,&lt;br /&gt;
 public interface Dinasour extends Frog{&lt;br /&gt;
     //variables and method Declarations&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
==== Achieving Multiple Inheritance ====&lt;br /&gt;
Modules and Interfaces help acheive multiple inheritance. Programming languages such as Ruby and Java do not support multiple inheritance. &amp;lt;br&amp;gt;&lt;br /&gt;
Interfaces are used in Java to achieve multiple inheritance. Similarly, modules in Ruby help achieve multiple inheritance.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Ruby,&lt;br /&gt;
&lt;br /&gt;
 module Frog&lt;br /&gt;
   def jump()&lt;br /&gt;
     //Method definition&lt;br /&gt;
    end&lt;br /&gt;
 end &amp;lt;br&amp;gt;&lt;br /&gt;
 module Dinosaur&lt;br /&gt;
  def strength()&lt;br /&gt;
    //Method definition&lt;br /&gt;
  end&lt;br /&gt;
 end &amp;lt;br&amp;gt;&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
      include Frog&lt;br /&gt;
      include Dinosaur&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
In Java,&lt;br /&gt;
 public interface Frog {&lt;br /&gt;
     void jump();&lt;br /&gt;
 } &amp;lt;br&amp;gt;&lt;br /&gt;
 public interface Dinasour {&lt;br /&gt;
     void strength();&lt;br /&gt;
 } &amp;lt;br&amp;gt;&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
  //Method Implementations&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Frogosaur inherits the methods of both frog and dinosaur. &lt;br /&gt;
&lt;br /&gt;
===Tabular Comparison===&lt;br /&gt;
{|border=&amp;quot;2&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Mixins&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Interfaces&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|The number of lines of code is lesser since the &amp;lt;br&amp;gt;&lt;br /&gt;
modules contain the method definition. &lt;br /&gt;
|The number of lines of code is bigger since the &amp;lt;br&amp;gt;&lt;br /&gt;
classes have to define the methods.&lt;br /&gt;
|-&lt;br /&gt;
|More modules in a class makes code less readable.&lt;br /&gt;
|The code is more readable since the class contain the &amp;lt;br&amp;gt;&lt;br /&gt;
method definition.&lt;br /&gt;
|-&lt;br /&gt;
|Modules cannot be inherited and cannot form is-a hierarchy.&lt;br /&gt;
|Interfaces can extend another interface and hence can form &amp;lt;br&amp;gt;&lt;br /&gt;
is-a hierarchy.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Drawbacks===&lt;br /&gt;
The drawbacks of the technique of Mixins are very much debated. Though Mixins provide us with an easy way to write flexible and decoupled code and also help us solve the diamond problem, they pose their own problems. In large programs there could be large number of modules and each module could have loads of methods each performing a certain task. To trace the origin of the methods and to keep in mind the hierarchy is practically impossible. Another issue associated with Mixins pose is silent method overriding. Silent method overriding occurs when different modules are included in a class and each module have a method with the same definition. In such a case, the method which gets executed depends upon the way it is included in the class and this is done without any sort of message to the user (silently). In large scale applications this could certainly be an issue considering the number of modules that would be mixed in and it would be difficult to keep track of the hierarchy in which the modules were included.&lt;br /&gt;
Although Java provides polymorphic behavior with the use of interfaces, they sometimes tend to be very slow. The implementation of interfaces is also limited to public methods and constants with no implementation.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Modules and Interfaces offer many advantages. However, they have their own disadvantages. Modules add more flexibility to object-oriented programming by the use of DRY principle and are easy to maintain.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
==See Also==&lt;/div&gt;</summary>
		<author><name>Rgovind2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w16_br&amp;diff=63622</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w16 br</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w16_br&amp;diff=63622"/>
		<updated>2012-09-12T00:52:12Z</updated>

		<summary type="html">&lt;p&gt;Rgovind2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==1. Overview==&lt;br /&gt;
&lt;br /&gt;
Mixins are Ruby modules. A module is a grouping of objects under a single name. The objects may be constants, methods, classes, or other modules. Modules are defined in Ruby using the keyword module. They can be incorporated into a class with Ruby’s include statement. This is called a mixin.&lt;br /&gt;
Modules are for sharing behavior (methods), while classes are for modeling relationship between objects. Ruby classes can Mixin a module and receive all its methods for free.&lt;br /&gt;
An Interface in Java programming language is an abstract type that classes must implement. Interfaces are declared using the interface keyword, and may only contain method signature and constant declarations.&lt;br /&gt;
A class that implements an interface must implement all of the methods described in the interface, or be an abstract class.&lt;br /&gt;
&lt;br /&gt;
==2. Definitions==&lt;br /&gt;
===2.1 Mixins===&lt;br /&gt;
	Modules provide a structure to collect classes, methods, and constants into a single, separately defined unit. They provide the simplest and an elegant way to adhere to the Don't repeat yourself principle. This is useful so as to avoid clashes with existing classes, methods, and constants.&lt;br /&gt;
Modules are defined in Ruby using the module keyword.&lt;br /&gt;
A Mixin is a class that is mixed with a module or a set of modules i.e. the implementation of the modules and the class are intertwined and combined together [3].&lt;br /&gt;
This gives us a neat and controlled way of adding new functionality to classes. Modules are similar to classes in that they hold a collection of methods, variables constants and other modules and class definitions. However, the real usage of a Mixin is exploited when the code in the Mixin starts to interact with code in the class that uses it.&lt;br /&gt;
Below is an example for modules and Mixins:&lt;br /&gt;
The module ParkingSlot consists of the methods park and unpark&lt;br /&gt;
 &lt;br /&gt;
 module ParkingSlot&lt;br /&gt;
       def park&lt;br /&gt;
          #code to park the car&lt;br /&gt;
       end&lt;br /&gt;
       def unpark&lt;br /&gt;
          #code to unpark the car&lt;br /&gt;
       end&lt;br /&gt;
 end&lt;br /&gt;
The module ParkingMeter consists of the methods calculate and printreceipt&lt;br /&gt;
&lt;br /&gt;
 module ParkingMeter&lt;br /&gt;
       def calculate&lt;br /&gt;
          #code to calculate the parking fee&lt;br /&gt;
       end&lt;br /&gt;
       def printreceipt&lt;br /&gt;
          #code to print the receipt&lt;br /&gt;
       end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
When a class includes a module via include Module Name, all the methods on that module become instance methods on the class.&lt;br /&gt;
&lt;br /&gt;
 class Parking&lt;br /&gt;
      include ParkingSlot&lt;br /&gt;
      include ParkingMeter&lt;br /&gt;
      def printcardetails&lt;br /&gt;
          #code to print the details and type of car&lt;br /&gt;
      end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
The modules ParkingSlot and ParkingMeter are “included” in the Parking class. All the variables and the instance methods of the modules ParkingSlot and ParkingMeter are now said to be “mixed in” into the class. It is now possible for class instances to use these methods defined in the modules as and when required.&lt;br /&gt;
 p=Parking.new   &lt;br /&gt;
 p.park            # -&amp;gt; Method park from module ParkingSlot&lt;br /&gt;
 p.unpark          # -&amp;gt; Method unpark from module ParkingSlot&lt;br /&gt;
 p.calculate       # -&amp;gt; Method calculate from module ParkingMeter&lt;br /&gt;
 p.printreceipt    # -&amp;gt; Method printreceipt from module ParkingMeter&lt;br /&gt;
 p.printcardetails # -&amp;gt; Calls printCarDetails from class&lt;br /&gt;
The class Parking inherits from both the modules and the module methods are now available in Parking. Hence, it is now possible to use the methods with an instance of the Parking class p.&lt;br /&gt;
Hence, Mixins can be thought of taking different methods and variables defined in different modules making them available as instance methods in the class as well, thereby extending the class’ functionality. Effectively mixed in modules behave as superclass. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:ParkingLot.PNG| center]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
===2.2 Interfaces===&lt;br /&gt;
In an object-oriented programming languages such as Java, C#, an interface[2] is a reference type, similar to a class that can contain only constants, method signatures, and nested types. There are no method bodies. Interfaces cannot be instantiated and they can only be implemented by classes or extended by other interfaces. All methods are public by default and any fields declared in an interface are by default static and final.&lt;br /&gt;
Example:&lt;br /&gt;
 public interface ParkingLot {&lt;br /&gt;
       boolean add(Car car, int slot);&lt;br /&gt;
       boolean remove(Car car);&lt;br /&gt;
       boolean findCar(Int number);&lt;br /&gt;
       int freeSlotsCount();&lt;br /&gt;
       int occupiedSlots();&lt;br /&gt;
       int parkingType();&lt;br /&gt;
       int calculateParkingFee(int hours, int charge, int duration);&lt;br /&gt;
       int findParkingSlot();&lt;br /&gt;
 }&lt;br /&gt;
 public class Mall implements ParkingLot {&lt;br /&gt;
       boolean add(Car car, int slot) { //... Code to add a car into the parking lot... }&lt;br /&gt;
       boolean remove(Car car){ // do something }&lt;br /&gt;
       boolean findCar(Int number){ // do something }&lt;br /&gt;
       int freeSlotsCount(){ // do something }&lt;br /&gt;
       int occupiedSlots(){ // do something }&lt;br /&gt;
       int parkingType(){ // do something }&lt;br /&gt;
       int calculateParkingFee(int hours, int charge, int duration){ // do something }&lt;br /&gt;
       int findParkingSlot(){ // do something }&lt;br /&gt;
 }&lt;br /&gt;
Interfaces play another important role in the object-oriented programming language. Interfaces are not part of the class hierarchy, although they work in combination with classes. Interfaces provide an alternative for languages like Java that does not allow multiple inheritance .&lt;br /&gt;
&lt;br /&gt;
==3. Mixins vs. Interfaces==&lt;br /&gt;
===3.1 Similarities and Differences===&lt;br /&gt;
	Mixins and Interfaces can be considered as a block of code having some variables and methods that are used in a class. The key difference between them is that a Mixin can contain method definitions whereas an Interface cannot.&lt;br /&gt;
&lt;br /&gt;
====Multiple method definitions:====&lt;br /&gt;
	In a class, when more than one module is Mixed in where some modules have overloaded methods, the method from the module that was last Mixed in into the class will be executed.&lt;br /&gt;
 module Frog&lt;br /&gt;
    def sound()&lt;br /&gt;
      puts &amp;quot;croak...&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Dinosaur&lt;br /&gt;
   def sound()&lt;br /&gt;
     puts &amp;quot;roar...&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
       include Frog&lt;br /&gt;
       include Dinosaur&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 Frogosaur.new.sound &amp;lt;br&amp;gt;	&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 roar...&amp;lt;br&amp;gt;&lt;br /&gt;
This is because the class Frogosaur will have only one definition for each method. The users have the flexibility to make use of methods from different modules by rearranging the order of include.&lt;br /&gt;
Interfaces, on the other hand, do not offer the flexibility to have different implementation for a same method. This is because the class implementing the interface can provide only one implementation for each method. Hence there exists only one method definition.&lt;br /&gt;
 public interface Frog {&lt;br /&gt;
     void sound();&lt;br /&gt;
 }&lt;br /&gt;
 public interface Dinasour {&lt;br /&gt;
     void sound();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
 public void sound()&lt;br /&gt;
 {&lt;br /&gt;
     System.out.println(&amp;quot;barks......&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public static void main(String args[])&lt;br /&gt;
 {&lt;br /&gt;
     new Frogosaur().sound();;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 barks...&amp;lt;br&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
====Multiple object behavior:====&lt;br /&gt;
In Ruby, a class’s instance can extend a module and exhibit different behavior than the other instances of the same class.&lt;br /&gt;
&lt;br /&gt;
 module Frog&lt;br /&gt;
  def sound()&lt;br /&gt;
     puts &amp;quot;croak...&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Dinasour&lt;br /&gt;
  def sound()&lt;br /&gt;
    puts &amp;quot;roar...&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
  def sound()&lt;br /&gt;
    puts &amp;quot;barks...&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 beast1= Frogosaur.new&lt;br /&gt;
 beast2= Frogosaur.new&lt;br /&gt;
 beast1.extend(Frog)&lt;br /&gt;
 beast1.sound&lt;br /&gt;
 beast2.sound&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 croak...&amp;lt;br&amp;gt;&lt;br /&gt;
 barks...&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Interfaces are implemented by a class and all instances of the class behave in the same way. They can use only the methods of the class from which they were instantiated.&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
  public void sound()&lt;br /&gt;
  {&lt;br /&gt;
      System.out.println(&amp;quot;Sounds...&amp;quot;);&lt;br /&gt;
  }  &lt;br /&gt;
  public static void main(String args[])&lt;br /&gt;
  {&lt;br /&gt;
      Frogosaur beast1 = new Frogosaur();&lt;br /&gt;
      Frogosaur beast2 = new Frogosaur();&lt;br /&gt;
      object1.sound();&lt;br /&gt;
      object2.sound();&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 Sounds...&amp;lt;br&amp;gt;&lt;br /&gt;
 Sounds...&amp;lt;br&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
====Enumerable – module and interface====&lt;br /&gt;
&lt;br /&gt;
Ruby’s Arrays have several methods defined in them. To use them, the classes need not be a subclass of Array. The classes can include Enumerable and define the each method to use the array’s methods for free.&lt;br /&gt;
&lt;br /&gt;
 class EnumExer&lt;br /&gt;
  include Enumerable&lt;br /&gt;
  def initialize(*arrays)&lt;br /&gt;
    @arrays = arrays&lt;br /&gt;
  end&lt;br /&gt;
  def each&lt;br /&gt;
    @arrays.each { |a| a.each { |x| yield x } }&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 ma = EnumExer.new([4, 1], [3], [2])&lt;br /&gt;
 puts ma.sort&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 1&amp;lt;br&amp;gt;&lt;br /&gt;
 2&amp;lt;br&amp;gt;&lt;br /&gt;
 3&amp;lt;br&amp;gt;&lt;br /&gt;
 4&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can observe that although the arguments passed to the ma instance did not have a similar structure, the sort method was able to identify each entry and sort them. This is because the each method was defined to identify and serve all the entries to the sort method.&lt;br /&gt;
The methods that are defined for Enumerable modules are:&lt;br /&gt;
 [&amp;quot;all?&amp;quot;, &amp;quot;any?&amp;quot;, &amp;quot;collect&amp;quot;, &amp;quot;detect&amp;quot;, &amp;quot;each_with_index&amp;quot;, &amp;quot;entries&amp;quot;,&lt;br /&gt;
 &amp;quot;find&amp;quot;, &amp;quot;find_all&amp;quot;, &amp;quot;grep&amp;quot;, &amp;quot;include?&amp;quot;, &amp;quot;inject&amp;quot;, &amp;quot;map&amp;quot;, &amp;quot;max&amp;quot;,&lt;br /&gt;
 &amp;quot;member?&amp;quot;, &amp;quot;min&amp;quot;, &amp;quot;partition&amp;quot;, &amp;quot;reject&amp;quot;, &amp;quot;select&amp;quot;, &amp;quot;sort&amp;quot;, &amp;quot;sort_by&amp;quot;,&lt;br /&gt;
 &amp;quot;to_a&amp;quot;, &amp;quot;zip&amp;quot;]&lt;br /&gt;
In Java, to use the methods of Enumerable interface, the class should be a subclass of Array.&lt;br /&gt;
&lt;br /&gt;
 public class EnumExer {&lt;br /&gt;
     static String content_string = &amp;quot;zyxwvutsrqponmlkjihgfedcba&amp;quot;;&lt;br /&gt;
     String a=&amp;quot;1&amp;quot;, b=&amp;quot;2&amp;quot;;&lt;br /&gt;
     public static void main(String args[])&lt;br /&gt;
     {&lt;br /&gt;
         char[] content_array = content_string.toCharArray();&lt;br /&gt;
         java.util.Arrays.sort(content_array);&lt;br /&gt;
         content_string=new String(content_array);&lt;br /&gt;
         System.out.println(content_string);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 Abcdefghijklmnopqrstuvwxyz&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, the string object was converted to a character array to be able to use with the sort method.&lt;br /&gt;
&lt;br /&gt;
====Inheritance:====&lt;br /&gt;
Java interfaces can inherit other interfaces but Ruby modules cannot inherit other modules but can contain classes and methods.&lt;br /&gt;
 module Wrapper&lt;br /&gt;
  class Base&lt;br /&gt;
     # Class Body&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Java,&lt;br /&gt;
 public interface Dinasour extends Frog{&lt;br /&gt;
     //variables and method Declarations&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
==== Achieving Multiple Inheritance ====&lt;br /&gt;
Modules and Interfaces help acheive multiple inheritance. Programming languages such as Ruby and Java do not support multiple inheritance. &amp;lt;br&amp;gt;&lt;br /&gt;
Interfaces are used in Java to achieve multiple inheritance. Similarly, modules in Ruby help achieve multiple inheritance.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Ruby,&lt;br /&gt;
&lt;br /&gt;
 module Frog&lt;br /&gt;
   def jump()&lt;br /&gt;
     //Method definition&lt;br /&gt;
    end&lt;br /&gt;
 end &amp;lt;br&amp;gt;&lt;br /&gt;
 module Dinosaur&lt;br /&gt;
  def strength()&lt;br /&gt;
    //Method definition&lt;br /&gt;
  end&lt;br /&gt;
 end &amp;lt;br&amp;gt;&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
      include Frog&lt;br /&gt;
      include Dinosaur&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
In Java,&lt;br /&gt;
 public interface Frog {&lt;br /&gt;
     void jump();&lt;br /&gt;
 } &amp;lt;br&amp;gt;&lt;br /&gt;
 public interface Dinasour {&lt;br /&gt;
     void strength();&lt;br /&gt;
 } &amp;lt;br&amp;gt;&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
  //Method Implementations&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Frogosaur inherits the methods of both frog and dinosaur. &lt;br /&gt;
&lt;br /&gt;
===3.2 Tabular Comparison===&lt;br /&gt;
{|border=&amp;quot;2&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Mixins&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Interfaces&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|The number of lines of code is lesser since the &amp;lt;br&amp;gt;&lt;br /&gt;
modules contain the method definition. &lt;br /&gt;
|The number of lines of code is bigger since the &amp;lt;br&amp;gt;&lt;br /&gt;
classes have to define the methods.&lt;br /&gt;
|-&lt;br /&gt;
|More modules in a class makes code less readable.&lt;br /&gt;
|The code is more readable since the class contain the &amp;lt;br&amp;gt;&lt;br /&gt;
method definition.&lt;br /&gt;
|-&lt;br /&gt;
|Modules cannot be inherited and cannot form is-a hierarchy.&lt;br /&gt;
|Interfaces can extend another interface and hence can form &amp;lt;br&amp;gt;&lt;br /&gt;
is-a hierarchy.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===3.3 Drawbacks===&lt;br /&gt;
The drawbacks of the technique of Mixins are very much debated. Though Mixins provide us with an easy way to write flexible and decoupled code and also help us solve the diamond problem, they pose their own problems. In large programs there could be large number of modules and each module could have loads of methods each performing a certain task. To trace the origin of the methods and to keep in mind the hierarchy is practically impossible. Another issue associated with Mixins pose is silent method overriding. Silent method overriding occurs when different modules are included in a class and each module have a method with the same definition. In such a case, the method which gets executed depends upon the way it is included in the class and this is done without any sort of message to the user (silently). In large scale applications this could certainly be an issue considering the number of modules that would be mixed in and it would be difficult to keep track of the hierarchy in which the modules were included.&lt;br /&gt;
Although Java provides polymorphic behavior with the use of interfaces, they sometimes tend to be very slow. The implementation of interfaces is also limited to public methods and constants with no implementation.&lt;br /&gt;
&lt;br /&gt;
==4. Conclusion==&lt;br /&gt;
Modules and Interfaces offer many advantages. However, they have their own disadvantages. Modules add more flexibility to object-oriented programming by the use of DRY principle and are easy to maintain.&lt;br /&gt;
&lt;br /&gt;
==5. References==&lt;br /&gt;
==6. See Also==&lt;/div&gt;</summary>
		<author><name>Rgovind2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w16_br&amp;diff=63620</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w16 br</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w16_br&amp;diff=63620"/>
		<updated>2012-09-12T00:37:30Z</updated>

		<summary type="html">&lt;p&gt;Rgovind2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==1. Overview==&lt;br /&gt;
&lt;br /&gt;
Mixins are Ruby modules. A module is a grouping of objects under a single name. The objects may be constants, methods, classes, or other modules. Modules are defined in Ruby using the keyword module. They can be incorporated into a class with Ruby’s include statement. This is called a mixin.&lt;br /&gt;
Modules are for sharing behavior (methods), while classes are for modeling relationship between objects. Ruby classes can Mixin a module and receive all its methods for free.&lt;br /&gt;
An Interface in Java programming language is an abstract type that classes must implement. Interfaces are declared using the interface keyword, and may only contain method signature and constant declarations.&lt;br /&gt;
A class that implements an interface must implement all of the methods described in the interface, or be an abstract class.&lt;br /&gt;
&lt;br /&gt;
==2. Definitions==&lt;br /&gt;
===2.1 Mixins===&lt;br /&gt;
	Modules provide a structure to collect classes, methods, and constants into a single, separately defined unit. They provide the simplest and an elegant way to adhere to the Don't repeat yourself principle. This is useful so as to avoid clashes with existing classes, methods, and constants.&lt;br /&gt;
Modules are defined in Ruby using the module keyword.&lt;br /&gt;
A Mixin is a class that is mixed with a module or a set of modules i.e. the implementation of the modules and the class are intertwined and combined together [3].&lt;br /&gt;
This gives us a neat and controlled way of adding new functionality to classes. Modules are similar to classes in that they hold a collection of methods, variables constants and other modules and class definitions. However, the real usage of a Mixin is exploited when the code in the Mixin starts to interact with code in the class that uses it.&lt;br /&gt;
Below is an example for modules and Mixins:&lt;br /&gt;
The module ParkingSlot consists of the methods park and unpark&lt;br /&gt;
 &lt;br /&gt;
 module ParkingSlot&lt;br /&gt;
       def park&lt;br /&gt;
          #code to park the car&lt;br /&gt;
       end&lt;br /&gt;
       def unpark&lt;br /&gt;
          #code to unpark the car&lt;br /&gt;
       end&lt;br /&gt;
 end&lt;br /&gt;
The module ParkingMeter consists of the methods calculate and printreceipt&lt;br /&gt;
&lt;br /&gt;
 module ParkingMeter&lt;br /&gt;
       def calculate&lt;br /&gt;
          #code to calculate the parking fee&lt;br /&gt;
       end&lt;br /&gt;
       def printreceipt&lt;br /&gt;
          #code to print the receipt&lt;br /&gt;
       end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
When a class includes a module via include Module Name, all the methods on that module become instance methods on the class.&lt;br /&gt;
&lt;br /&gt;
 class Parking&lt;br /&gt;
      include ParkingSlot&lt;br /&gt;
      include ParkingMeter&lt;br /&gt;
      def printcardetails&lt;br /&gt;
          #code to print the details and type of car&lt;br /&gt;
      end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
The modules ParkingSlot and ParkingMeter are “included” in the Parking class. All the variables and the instance methods of the modules ParkingSlot and ParkingMeter are now said to be “mixed in” into the class. It is now possible for class instances to use these methods defined in the modules as and when required.&lt;br /&gt;
 p=Parking.new   &lt;br /&gt;
 p.park            # -&amp;gt; Method park from module ParkingSlot&lt;br /&gt;
 p.unpark          # -&amp;gt; Method unpark from module ParkingSlot&lt;br /&gt;
 p.calculate       # -&amp;gt; Method calculate from module ParkingMeter&lt;br /&gt;
 p.printreceipt    # -&amp;gt; Method printreceipt from module ParkingMeter&lt;br /&gt;
 p.printcardetails # -&amp;gt; Calls printCarDetails from class&lt;br /&gt;
The class Parking inherits from both the modules and the module methods are now available in Parking. Hence, it is now possible to use the methods with an instance of the Parking class p.&lt;br /&gt;
Hence, Mixins can be thought of taking different methods and variables defined in different modules making them available as instance methods in the class as well, thereby extending the class’ functionality. Effectively mixed in modules behave as superclass. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:ParkingLot.PNG| center]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
===2.2 Interfaces===&lt;br /&gt;
In an object-oriented programming languages such as Java, C#, an interface[2] is a reference type, similar to a class that can contain only constants, method signatures, and nested types. There are no method bodies. Interfaces cannot be instantiated and they can only be implemented by classes or extended by other interfaces. All methods are public by default and any fields declared in an interface are by default static and final.&lt;br /&gt;
Example:&lt;br /&gt;
 public interface ParkingLot {&lt;br /&gt;
       boolean add(Car car, int slot);&lt;br /&gt;
       boolean remove(Car car);&lt;br /&gt;
       boolean findCar(Int number);&lt;br /&gt;
       int freeSlotsCount();&lt;br /&gt;
       int occupiedSlots();&lt;br /&gt;
       int parkingType();&lt;br /&gt;
       int calculateParkingFee(int hours, int charge, int duration);&lt;br /&gt;
       int findParkingSlot();&lt;br /&gt;
 }&lt;br /&gt;
 public class Mall implements ParkingLot {&lt;br /&gt;
       boolean add(Car car, int slot) { //... Code to add a car into the parking lot... }&lt;br /&gt;
       boolean remove(Car car){ // do something }&lt;br /&gt;
       boolean findCar(Int number){ // do something }&lt;br /&gt;
       int freeSlotsCount(){ // do something }&lt;br /&gt;
       int occupiedSlots(){ // do something }&lt;br /&gt;
       int parkingType(){ // do something }&lt;br /&gt;
       int calculateParkingFee(int hours, int charge, int duration){ // do something }&lt;br /&gt;
       int findParkingSlot(){ // do something }&lt;br /&gt;
 }&lt;br /&gt;
Interfaces play another important role in the object-oriented programming language. Interfaces are not part of the class hierarchy, although they work in combination with classes. Interfaces provide an alternative for languages like Java that does not allow multiple inheritance .&lt;br /&gt;
&lt;br /&gt;
==3. Mixins vs. Interfaces==&lt;br /&gt;
===3.1 Similarities and Differences===&lt;br /&gt;
	Mixins and Interfaces can be considered as a block of code having some variables and methods that are used in a class. The key difference between them is that a Mixin can contain method definitions whereas an Interface cannot.&lt;br /&gt;
&lt;br /&gt;
====Multiple method definitions:====&lt;br /&gt;
	In a class, when more than one module is Mixed in where some modules have overloaded methods, the method from the module that was last Mixed in into the class will be executed.&lt;br /&gt;
 module Frog&lt;br /&gt;
    def sound()&lt;br /&gt;
      puts &amp;quot;croak...&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Dinosaur&lt;br /&gt;
   def sound()&lt;br /&gt;
     puts &amp;quot;roar...&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
       include Frog&lt;br /&gt;
       include Dinosaur&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 Frogosaur.new.sound &amp;lt;br&amp;gt;	&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 roar...&amp;lt;br&amp;gt;&lt;br /&gt;
This is because the class Frogosaur will have only one definition for each method. The users have the flexibility to make use of methods from different modules by rearranging the order of include.&lt;br /&gt;
Interfaces, on the other hand, do not offer the flexibility to have different implementation for a same method. This is because the class implementing the interface can provide only one implementation for each method. Hence there exists only one method definition.&lt;br /&gt;
 public interface Frog {&lt;br /&gt;
     void sound();&lt;br /&gt;
 }&lt;br /&gt;
 public interface Dinasour {&lt;br /&gt;
     void sound();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
 public void sound()&lt;br /&gt;
 {&lt;br /&gt;
     System.out.println(&amp;quot;barks......&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public static void main(String args[])&lt;br /&gt;
 {&lt;br /&gt;
     new Frogosaur().sound();;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 barks...&amp;lt;br&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
====Multiple object behavior:====&lt;br /&gt;
In Ruby, a class’s instance can extend a module and exhibit different behavior than the other instances of the same class.&lt;br /&gt;
&lt;br /&gt;
 module Frog&lt;br /&gt;
  def sound()&lt;br /&gt;
     puts &amp;quot;croak...&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Dinasour&lt;br /&gt;
  def sound()&lt;br /&gt;
    puts &amp;quot;roar...&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
  def sound()&lt;br /&gt;
    puts &amp;quot;barks...&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 beast1= Frogosaur.new&lt;br /&gt;
 beast2= Frogosaur.new&lt;br /&gt;
 beast1.extend(Frog)&lt;br /&gt;
 beast1.sound&lt;br /&gt;
 beast2.sound&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 croak...&amp;lt;br&amp;gt;&lt;br /&gt;
 barks...&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Interfaces are implemented by a class and all instances of the class behave in the same way. They can use only the methods of the class from which they were instantiated.&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
  public void sound()&lt;br /&gt;
  {&lt;br /&gt;
      System.out.println(&amp;quot;Sounds...&amp;quot;);&lt;br /&gt;
  }  &lt;br /&gt;
  public static void main(String args[])&lt;br /&gt;
  {&lt;br /&gt;
      Frogosaur beast1 = new Frogosaur();&lt;br /&gt;
      Frogosaur beast2 = new Frogosaur();&lt;br /&gt;
      object1.sound();&lt;br /&gt;
      object2.sound();&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 Sounds...&amp;lt;br&amp;gt;&lt;br /&gt;
 Sounds...&amp;lt;br&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
====Enumerable – module and interface====&lt;br /&gt;
&lt;br /&gt;
Ruby’s Arrays have several methods defined in them. To use them, the classes need not be a subclass of Array. The classes can include Enumerable and define the each method to use the array’s methods for free.&lt;br /&gt;
&lt;br /&gt;
 class EnumExer&lt;br /&gt;
  include Enumerable&lt;br /&gt;
  def initialize(*arrays)&lt;br /&gt;
    @arrays = arrays&lt;br /&gt;
  end&lt;br /&gt;
  def each&lt;br /&gt;
    @arrays.each { |a| a.each { |x| yield x } }&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 ma = EnumExer.new([4, 1], [3], [2])&lt;br /&gt;
 puts ma.sort&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 1&amp;lt;br&amp;gt;&lt;br /&gt;
 2&amp;lt;br&amp;gt;&lt;br /&gt;
 3&amp;lt;br&amp;gt;&lt;br /&gt;
 4&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can observe that although the arguments passed to the ma instance did not have a similar structure, the sort method was able to identify each entry and sort them. This is because the each method was defined to identify and serve all the entries to the sort method.&lt;br /&gt;
The methods that are defined for Enumerable modules are:&lt;br /&gt;
 [&amp;quot;all?&amp;quot;, &amp;quot;any?&amp;quot;, &amp;quot;collect&amp;quot;, &amp;quot;detect&amp;quot;, &amp;quot;each_with_index&amp;quot;, &amp;quot;entries&amp;quot;,&lt;br /&gt;
 &amp;quot;find&amp;quot;, &amp;quot;find_all&amp;quot;, &amp;quot;grep&amp;quot;, &amp;quot;include?&amp;quot;, &amp;quot;inject&amp;quot;, &amp;quot;map&amp;quot;, &amp;quot;max&amp;quot;,&lt;br /&gt;
 &amp;quot;member?&amp;quot;, &amp;quot;min&amp;quot;, &amp;quot;partition&amp;quot;, &amp;quot;reject&amp;quot;, &amp;quot;select&amp;quot;, &amp;quot;sort&amp;quot;, &amp;quot;sort_by&amp;quot;,&lt;br /&gt;
 &amp;quot;to_a&amp;quot;, &amp;quot;zip&amp;quot;]&lt;br /&gt;
In Java, to use the methods of Enumerable interface, the class should be a subclass of Array.&lt;br /&gt;
&lt;br /&gt;
 public class EnumExer {&lt;br /&gt;
     static String content_string = &amp;quot;zyxwvutsrqponmlkjihgfedcba&amp;quot;;&lt;br /&gt;
     String a=&amp;quot;1&amp;quot;, b=&amp;quot;2&amp;quot;;&lt;br /&gt;
     public static void main(String args[])&lt;br /&gt;
     {&lt;br /&gt;
         char[] content_array = content_string.toCharArray();&lt;br /&gt;
         java.util.Arrays.sort(content_array);&lt;br /&gt;
         content_string=new String(content_array);&lt;br /&gt;
         System.out.println(content_string);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 Abcdefghijklmnopqrstuvwxyz&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, the string object was converted to a character array to be able to use with the sort method.&lt;br /&gt;
&lt;br /&gt;
====Inheritance:====&lt;br /&gt;
Java interfaces can inherit other interfaces but Ruby modules cannot inherit other modules but can contain classes and methods.&lt;br /&gt;
 module Wrapper&lt;br /&gt;
  class Base&lt;br /&gt;
     # Class Body&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Java,&lt;br /&gt;
 public interface Dinasour extends Frog{&lt;br /&gt;
     //variables and method Declarations&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
==== Achieving Multiple Inheritance ====&lt;br /&gt;
Modules and Interfaces help acheive multiple inheritance. Programming languages such as Ruby and Java do not support multiple inheritance. &amp;lt;br&amp;gt;&lt;br /&gt;
Interfaces are used in Java to achieve multiple inheritance. Similarly, modules in Ruby help achieve multiple inheritance.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===3.2 Tabular Comparison===&lt;br /&gt;
{|border=&amp;quot;2&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Mixins&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Interfaces&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|The number of lines of code is lesser since the &amp;lt;br&amp;gt;&lt;br /&gt;
modules contain the method definition. &lt;br /&gt;
|The number of lines of code is bigger since the &amp;lt;br&amp;gt;&lt;br /&gt;
classes have to define the methods.&lt;br /&gt;
|-&lt;br /&gt;
|More modules in a class makes code less readable.&lt;br /&gt;
|The code is more readable since the class contain the &amp;lt;br&amp;gt;&lt;br /&gt;
method definition.&lt;br /&gt;
|-&lt;br /&gt;
|Modules cannot be inherited and cannot form is-a hierarchy.&lt;br /&gt;
|Interfaces can extend another interface and hence can form &amp;lt;br&amp;gt;&lt;br /&gt;
is-a hierarchy.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===3.3 Drawbacks===&lt;br /&gt;
The drawbacks of the technique of Mixins are very much debated. Though Mixins provide us with an easy way to write flexible and decoupled code and also help us solve the diamond problem, they pose their own problems. In large programs there could be large number of modules and each module could have loads of methods each performing a certain task. To trace the origin of the methods and to keep in mind the hierarchy is practically impossible. Another issue associated with Mixins pose is silent method overriding. Silent method overriding occurs when different modules are included in a class and each module have a method with the same definition. In such a case, the method which gets executed depends upon the way it is included in the class and this is done without any sort of message to the user (silently). In large scale applications this could certainly be an issue considering the number of modules that would be mixed in and it would be difficult to keep track of the hierarchy in which the modules were included.&lt;br /&gt;
Although Java provides polymorphic behavior with the use of interfaces, they sometimes tend to be very slow. The implementation of interfaces is also limited to public methods and constants with no implementation.&lt;br /&gt;
&lt;br /&gt;
==4. Conclusion==&lt;br /&gt;
Modules and Interfaces offer many advantages. However, they have their own disadvantages. Modules add more flexibility to object-oriented programming by the use of DRY principle and are easy to maintain.&lt;br /&gt;
&lt;br /&gt;
==5. References==&lt;br /&gt;
==6. See Also==&lt;/div&gt;</summary>
		<author><name>Rgovind2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w16_br&amp;diff=63619</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w16 br</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w16_br&amp;diff=63619"/>
		<updated>2012-09-12T00:36:15Z</updated>

		<summary type="html">&lt;p&gt;Rgovind2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==1. Overview==&lt;br /&gt;
&lt;br /&gt;
Mixins are Ruby modules. A module is a grouping of objects under a single name. The objects may be constants, methods, classes, or other modules. Modules are defined in Ruby using the keyword module. They can be incorporated into a class with Ruby’s include statement. This is called a mixin.&lt;br /&gt;
Modules are for sharing behavior (methods), while classes are for modeling relationship between objects. Ruby classes can Mixin a module and receive all its methods for free.&lt;br /&gt;
An Interface in Java programming language is an abstract type that classes must implement. Interfaces are declared using the interface keyword, and may only contain method signature and constant declarations.&lt;br /&gt;
A class that implements an interface must implement all of the methods described in the interface, or be an abstract class.&lt;br /&gt;
&lt;br /&gt;
==2. Definitions==&lt;br /&gt;
===2.1 Mixins===&lt;br /&gt;
	Modules provide a structure to collect classes, methods, and constants into a single, separately defined unit. They provide the simplest and an elegant way to adhere to the Don't repeat yourself principle. This is useful so as to avoid clashes with existing classes, methods, and constants.&lt;br /&gt;
Modules are defined in Ruby using the module keyword.&lt;br /&gt;
A Mixin is a class that is mixed with a module or a set of modules i.e. the implementation of the modules and the class are intertwined and combined together [3].&lt;br /&gt;
This gives us a neat and controlled way of adding new functionality to classes. Modules are similar to classes in that they hold a collection of methods, variables constants and other modules and class definitions. However, the real usage of a Mixin is exploited when the code in the Mixin starts to interact with code in the class that uses it.&lt;br /&gt;
Below is an example for modules and Mixins:&lt;br /&gt;
The module ParkingSlot consists of the methods park and unpark&lt;br /&gt;
 &lt;br /&gt;
 module ParkingSlot&lt;br /&gt;
       def park&lt;br /&gt;
          #code to park the car&lt;br /&gt;
       end&lt;br /&gt;
       def unpark&lt;br /&gt;
          #code to unpark the car&lt;br /&gt;
       end&lt;br /&gt;
 end&lt;br /&gt;
The module ParkingMeter consists of the methods calculate and printreceipt&lt;br /&gt;
&lt;br /&gt;
 module ParkingMeter&lt;br /&gt;
       def calculate&lt;br /&gt;
          #code to calculate the parking fee&lt;br /&gt;
       end&lt;br /&gt;
       def printreceipt&lt;br /&gt;
          #code to print the receipt&lt;br /&gt;
       end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
When a class includes a module via include Module Name, all the methods on that module become instance methods on the class.&lt;br /&gt;
&lt;br /&gt;
 class Parking&lt;br /&gt;
      include ParkingSlot&lt;br /&gt;
      include ParkingMeter&lt;br /&gt;
      def printcardetails&lt;br /&gt;
          #code to print the details and type of car&lt;br /&gt;
      end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
The modules ParkingSlot and ParkingMeter are “included” in the Parking class. All the variables and the instance methods of the modules ParkingSlot and ParkingMeter are now said to be “mixed in” into the class. It is now possible for class instances to use these methods defined in the modules as and when required.&lt;br /&gt;
 p=Parking.new   &lt;br /&gt;
 p.park            # -&amp;gt; Method park from module ParkingSlot&lt;br /&gt;
 p.unpark          # -&amp;gt; Method unpark from module ParkingSlot&lt;br /&gt;
 p.calculate       # -&amp;gt; Method calculate from module ParkingMeter&lt;br /&gt;
 p.printreceipt    # -&amp;gt; Method printreceipt from module ParkingMeter&lt;br /&gt;
 p.printcardetails # -&amp;gt; Calls printCarDetails from class&lt;br /&gt;
The class Parking inherits from both the modules and the module methods are now available in Parking. Hence, it is now possible to use the methods with an instance of the Parking class p.&lt;br /&gt;
Hence, Mixins can be thought of taking different methods and variables defined in different modules making them available as instance methods in the class as well, thereby extending the class’ functionality. Effectively mixed in modules behave as superclass. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:ParkingLot.PNG| center]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
===2.2 Interfaces===&lt;br /&gt;
In an object-oriented programming languages such as Java, C#, an interface[2] is a reference type, similar to a class that can contain only constants, method signatures, and nested types. There are no method bodies. Interfaces cannot be instantiated and they can only be implemented by classes or extended by other interfaces. All methods are public by default and any fields declared in an interface are by default static and final.&lt;br /&gt;
Example:&lt;br /&gt;
 public interface ParkingLot {&lt;br /&gt;
       boolean add(Car car, int slot);&lt;br /&gt;
       boolean remove(Car car);&lt;br /&gt;
       boolean findCar(Int number);&lt;br /&gt;
       int freeSlotsCount();&lt;br /&gt;
       int occupiedSlots();&lt;br /&gt;
       int parkingType();&lt;br /&gt;
       int calculateParkingFee(int hours, int charge, int duration);&lt;br /&gt;
       int findParkingSlot();&lt;br /&gt;
 }&lt;br /&gt;
 public class Mall implements ParkingLot {&lt;br /&gt;
       boolean add(Car car, int slot) { //... Code to add a car into the parking lot... }&lt;br /&gt;
       boolean remove(Car car){ // do something }&lt;br /&gt;
       boolean findCar(Int number){ // do something }&lt;br /&gt;
       int freeSlotsCount(){ // do something }&lt;br /&gt;
       int occupiedSlots(){ // do something }&lt;br /&gt;
       int parkingType(){ // do something }&lt;br /&gt;
       int calculateParkingFee(int hours, int charge, int duration){ // do something }&lt;br /&gt;
       int findParkingSlot(){ // do something }&lt;br /&gt;
 }&lt;br /&gt;
Interfaces play another important role in the object-oriented programming language. Interfaces are not part of the class hierarchy, although they work in combination with classes. Interfaces provide an alternative for languages like Java that does not allow multiple inheritance .&lt;br /&gt;
&lt;br /&gt;
==3. Mixins vs. Interfaces==&lt;br /&gt;
===3.1 Similarities and Differences===&lt;br /&gt;
	Mixins and Interfaces can be considered as a block of code having some variables and methods that are used in a class. The key difference between them is that a Mixin can contain method definitions whereas an Interface cannot.&lt;br /&gt;
&lt;br /&gt;
====Multiple method definitions:====&lt;br /&gt;
	In a class, when more than one module is Mixed in where some modules have overloaded methods, the method from the module that was last Mixed in into the class will be executed.&lt;br /&gt;
 module Frog&lt;br /&gt;
    def sound()&lt;br /&gt;
      puts &amp;quot;croak...&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Dinosaur&lt;br /&gt;
   def sound()&lt;br /&gt;
     puts &amp;quot;roar...&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
       include Frog&lt;br /&gt;
       include Dinosaur&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 Frogosaur.new.sound &amp;lt;br&amp;gt;	&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 roar...&amp;lt;br&amp;gt;&lt;br /&gt;
This is because the class Frogosaur will have only one definition for each method. The users have the flexibility to make use of methods from different modules by rearranging the order of include.&lt;br /&gt;
Interfaces, on the other hand, do not offer the flexibility to have different implementation for a same method. This is because the class implementing the interface can provide only one implementation for each method. Hence there exists only one method definition.&lt;br /&gt;
 public interface Frog {&lt;br /&gt;
     void sound();&lt;br /&gt;
 }&lt;br /&gt;
 public interface Dinasour {&lt;br /&gt;
     void sound();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
 public void sound()&lt;br /&gt;
 {&lt;br /&gt;
     System.out.println(&amp;quot;barks......&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public static void main(String args[])&lt;br /&gt;
 {&lt;br /&gt;
     new Frogosaur().sound();;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 barks...&amp;lt;br&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
====Multiple object behavior:====&lt;br /&gt;
In Ruby, a class’s instance can extend a module and exhibit different behavior than the other instances of the same class.&lt;br /&gt;
&lt;br /&gt;
 module Frog&lt;br /&gt;
  def sound()&lt;br /&gt;
     puts &amp;quot;croak...&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Dinasour&lt;br /&gt;
  def sound()&lt;br /&gt;
    puts &amp;quot;roar...&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
  def sound()&lt;br /&gt;
    puts &amp;quot;barks...&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 beast1= Frogosaur.new&lt;br /&gt;
 beast2= Frogosaur.new&lt;br /&gt;
 beast1.extend(Frog)&lt;br /&gt;
 beast1.sound&lt;br /&gt;
 beast2.sound&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 croak...&amp;lt;br&amp;gt;&lt;br /&gt;
 barks...&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Interfaces are implemented by a class and all instances of the class behave in the same way. They can use only the methods of the class from which they were instantiated.&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
  public void sound()&lt;br /&gt;
  {&lt;br /&gt;
      System.out.println(&amp;quot;Sounds...&amp;quot;);&lt;br /&gt;
  }  &lt;br /&gt;
  public static void main(String args[])&lt;br /&gt;
  {&lt;br /&gt;
      Frogosaur beast1 = new Frogosaur();&lt;br /&gt;
      Frogosaur beast2 = new Frogosaur();&lt;br /&gt;
      object1.sound();&lt;br /&gt;
      object2.sound();&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 Sounds...&amp;lt;br&amp;gt;&lt;br /&gt;
 Sounds...&amp;lt;br&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
====Enumerable – module and interface====&lt;br /&gt;
&lt;br /&gt;
Ruby’s Arrays have several methods defined in them. To use them, the classes need not be a subclass of Array. The classes can include Enumerable and define the each method to use the array’s methods for free.&lt;br /&gt;
&lt;br /&gt;
 class EnumExer&lt;br /&gt;
  include Enumerable&lt;br /&gt;
  def initialize(*arrays)&lt;br /&gt;
    @arrays = arrays&lt;br /&gt;
  end&lt;br /&gt;
  def each&lt;br /&gt;
    @arrays.each { |a| a.each { |x| yield x } }&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 ma = EnumExer.new([4, 1], [3], [2])&lt;br /&gt;
 puts ma.sort&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 1&amp;lt;br&amp;gt;&lt;br /&gt;
 2&amp;lt;br&amp;gt;&lt;br /&gt;
 3&amp;lt;br&amp;gt;&lt;br /&gt;
 4&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can observe that although the arguments passed to the ma instance did not have a similar structure, the sort method was able to identify each entry and sort them. This is because the each method was defined to identify and serve all the entries to the sort method.&lt;br /&gt;
The methods that are defined for Enumerable modules are:&lt;br /&gt;
 [&amp;quot;all?&amp;quot;, &amp;quot;any?&amp;quot;, &amp;quot;collect&amp;quot;, &amp;quot;detect&amp;quot;, &amp;quot;each_with_index&amp;quot;, &amp;quot;entries&amp;quot;,&lt;br /&gt;
 &amp;quot;find&amp;quot;, &amp;quot;find_all&amp;quot;, &amp;quot;grep&amp;quot;, &amp;quot;include?&amp;quot;, &amp;quot;inject&amp;quot;, &amp;quot;map&amp;quot;, &amp;quot;max&amp;quot;,&lt;br /&gt;
 &amp;quot;member?&amp;quot;, &amp;quot;min&amp;quot;, &amp;quot;partition&amp;quot;, &amp;quot;reject&amp;quot;, &amp;quot;select&amp;quot;, &amp;quot;sort&amp;quot;, &amp;quot;sort_by&amp;quot;,&lt;br /&gt;
 &amp;quot;to_a&amp;quot;, &amp;quot;zip&amp;quot;]&lt;br /&gt;
In Java, to use the methods of Enumerable interface, the class should be a subclass of Array.&lt;br /&gt;
&lt;br /&gt;
 public class EnumExer {&lt;br /&gt;
     static String content_string = &amp;quot;zyxwvutsrqponmlkjihgfedcba&amp;quot;;&lt;br /&gt;
     String a=&amp;quot;1&amp;quot;, b=&amp;quot;2&amp;quot;;&lt;br /&gt;
     public static void main(String args[])&lt;br /&gt;
     {&lt;br /&gt;
         char[] content_array = content_string.toCharArray();&lt;br /&gt;
         java.util.Arrays.sort(content_array);&lt;br /&gt;
         content_string=new String(content_array);&lt;br /&gt;
         System.out.println(content_string);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 '''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
 Abcdefghijklmnopqrstuvwxyz&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, the string object was converted to a character array to be able to use with the sort method.&lt;br /&gt;
&lt;br /&gt;
====Inheritance:====&lt;br /&gt;
Java interfaces can inherit other interfaces but Ruby modules cannot inherit other modules but can contain classes and methods.&lt;br /&gt;
 module Wrapper&lt;br /&gt;
  class Base&lt;br /&gt;
     # Class Body&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Java,&lt;br /&gt;
 public interface Dinasour extends Frog{&lt;br /&gt;
     //variables and method Declarations&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Achieving Multiple Inheritance ====&lt;br /&gt;
Modules and Interfaces help acheive multiple inheritance. Programming languages such as Ruby and Java do not support multiple inheritance. &amp;lt;br&amp;gt;&lt;br /&gt;
Interfaces are used in Java to achieve multiple inheritance. Similarly, modules in Ruby help achieve multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
===3.2 Tabular Comparison===&lt;br /&gt;
{|border=&amp;quot;2&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Mixins&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Interfaces&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|The number of lines of code is lesser since the &amp;lt;br&amp;gt;&lt;br /&gt;
modules contain the method definition. &lt;br /&gt;
|The number of lines of code is bigger since the &amp;lt;br&amp;gt;&lt;br /&gt;
classes have to define the methods.&lt;br /&gt;
|-&lt;br /&gt;
|More modules in a class makes code less readable.&lt;br /&gt;
|The code is more readable since the class contain the &amp;lt;br&amp;gt;&lt;br /&gt;
method definition.&lt;br /&gt;
|-&lt;br /&gt;
|Modules cannot be inherited and cannot form is-a hierarchy.&lt;br /&gt;
|Interfaces can extend another interface and hence can form &amp;lt;br&amp;gt;&lt;br /&gt;
is-a hierarchy.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===3.3 Drawbacks===&lt;br /&gt;
The drawbacks of the technique of Mixins are very much debated. Though Mixins provide us with an easy way to write flexible and decoupled code and also help us solve the diamond problem, they pose their own problems. In large programs there could be large number of modules and each module could have loads of methods each performing a certain task. To trace the origin of the methods and to keep in mind the hierarchy is practically impossible. Another issue associated with Mixins pose is silent method overriding. Silent method overriding occurs when different modules are included in a class and each module have a method with the same definition. In such a case, the method which gets executed depends upon the way it is included in the class and this is done without any sort of message to the user (silently). In large scale applications this could certainly be an issue considering the number of modules that would be mixed in and it would be difficult to keep track of the hierarchy in which the modules were included.&lt;br /&gt;
Although Java provides polymorphic behavior with the use of interfaces, they sometimes tend to be very slow. The implementation of interfaces is also limited to public methods and constants with no implementation.&lt;br /&gt;
&lt;br /&gt;
==4. Conclusion==&lt;br /&gt;
Modules and Interfaces offer many advantages. However, they have their own disadvantages. Modules add more flexibility to object-oriented programming by the use of DRY principle and are easy to maintain.&lt;br /&gt;
&lt;br /&gt;
==5. References==&lt;br /&gt;
==6. See Also==&lt;/div&gt;</summary>
		<author><name>Rgovind2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w16_br&amp;diff=63618</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w16 br</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w16_br&amp;diff=63618"/>
		<updated>2012-09-12T00:17:16Z</updated>

		<summary type="html">&lt;p&gt;Rgovind2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==1. Overview==&lt;br /&gt;
&lt;br /&gt;
Mixins are Ruby modules. A module is a grouping of objects under a single name. The objects may be constants, methods, classes, or other modules. Modules are defined in Ruby using the keyword module. They can be incorporated into a class with Ruby’s include statement. This is called a mixin.&lt;br /&gt;
Modules are for sharing behavior (methods), while classes are for modeling relationship between objects. Ruby classes can Mixin a module and receive all its methods for free.&lt;br /&gt;
An Interface in Java programming language is an abstract type that classes must implement. Interfaces are declared using the interface keyword, and may only contain method signature and constant declarations.&lt;br /&gt;
A class that implements an interface must implement all of the methods described in the interface, or be an abstract class.&lt;br /&gt;
&lt;br /&gt;
==2. Definitions==&lt;br /&gt;
===2.1 Mixins===&lt;br /&gt;
	Modules provide a structure to collect classes, methods, and constants into a single, separately defined unit. They provide the simplest and an elegant way to adhere to the Don't repeat yourself principle. This is useful so as to avoid clashes with existing classes, methods, and constants.&lt;br /&gt;
Modules are defined in Ruby using the module keyword.&lt;br /&gt;
A Mixin is a class that is mixed with a module or a set of modules i.e. the implementation of the modules and the class are intertwined and combined together [3].&lt;br /&gt;
This gives us a neat and controlled way of adding new functionality to classes. Modules are similar to classes in that they hold a collection of methods, variables constants and other modules and class definitions. However, the real usage of a Mixin is exploited when the code in the Mixin starts to interact with code in the class that uses it.&lt;br /&gt;
Below is an example for modules and Mixins:&lt;br /&gt;
The module ParkingSlot consists of the methods park and unpark&lt;br /&gt;
 &lt;br /&gt;
 module ParkingSlot&lt;br /&gt;
       def park&lt;br /&gt;
          #code to park the car&lt;br /&gt;
       end&lt;br /&gt;
       def unpark&lt;br /&gt;
          #code to unpark the car&lt;br /&gt;
       end&lt;br /&gt;
 end&lt;br /&gt;
The module ParkingMeter consists of the methods calculate and printreceipt&lt;br /&gt;
&lt;br /&gt;
 module ParkingMeter&lt;br /&gt;
       def calculate&lt;br /&gt;
          #code to calculate the parking fee&lt;br /&gt;
       end&lt;br /&gt;
       def printreceipt&lt;br /&gt;
          #code to print the receipt&lt;br /&gt;
       end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
When a class includes a module via include Module Name, all the methods on that module become instance methods on the class.&lt;br /&gt;
&lt;br /&gt;
 class Parking&lt;br /&gt;
      include ParkingSlot&lt;br /&gt;
      include ParkingMeter&lt;br /&gt;
      def printcardetails&lt;br /&gt;
          #code to print the details and type of car&lt;br /&gt;
      end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
The modules ParkingSlot and ParkingMeter are “included” in the Parking class. All the variables and the instance methods of the modules ParkingSlot and ParkingMeter are now said to be “mixed in” into the class. It is now possible for class instances to use these methods defined in the modules as and when required.&lt;br /&gt;
 p=Parking.new   &lt;br /&gt;
 p.park            # -&amp;gt; Method park from module ParkingSlot&lt;br /&gt;
 p.unpark          # -&amp;gt; Method unpark from module ParkingSlot&lt;br /&gt;
 p.calculate       # -&amp;gt; Method calculate from module ParkingMeter&lt;br /&gt;
 p.printreceipt    # -&amp;gt; Method printreceipt from module ParkingMeter&lt;br /&gt;
 p.printcardetails # -&amp;gt; Calls printCarDetails from class&lt;br /&gt;
The class Parking inherits from both the modules and the module methods are now available in Parking. Hence, it is now possible to use the methods with an instance of the Parking class p.&lt;br /&gt;
Hence, Mixins can be thought of taking different methods and variables defined in different modules making them available as instance methods in the class as well, thereby extending the class’ functionality. Effectively mixed in modules behave as superclass. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:ParkingLot.PNG| center]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
===2.2 Interfaces===&lt;br /&gt;
In an object-oriented programming languages such as Java, C#, an interface[2] is a reference type, similar to a class that can contain only constants, method signatures, and nested types. There are no method bodies. Interfaces cannot be instantiated and they can only be implemented by classes or extended by other interfaces. All methods are public by default and any fields declared in an interface are by default static and final.&lt;br /&gt;
Example:&lt;br /&gt;
 public interface ParkingLot {&lt;br /&gt;
       boolean add(Car car, int slot);&lt;br /&gt;
       boolean remove(Car car);&lt;br /&gt;
       boolean findCar(Int number);&lt;br /&gt;
       int freeSlotsCount();&lt;br /&gt;
       int occupiedSlots();&lt;br /&gt;
       int parkingType();&lt;br /&gt;
       int calculateParkingFee(int hours, int charge, int duration);&lt;br /&gt;
       int findParkingSlot();&lt;br /&gt;
 }&lt;br /&gt;
 public class Mall implements ParkingLot {&lt;br /&gt;
       boolean add(Car car, int slot) { //... Code to add a car into the parking lot... }&lt;br /&gt;
       boolean remove(Car car){ // do something }&lt;br /&gt;
       boolean findCar(Int number){ // do something }&lt;br /&gt;
       int freeSlotsCount(){ // do something }&lt;br /&gt;
       int occupiedSlots(){ // do something }&lt;br /&gt;
       int parkingType(){ // do something }&lt;br /&gt;
       int calculateParkingFee(int hours, int charge, int duration){ // do something }&lt;br /&gt;
       int findParkingSlot(){ // do something }&lt;br /&gt;
 }&lt;br /&gt;
Interfaces play another important role in the object-oriented programming language. Interfaces are not part of the class hierarchy, although they work in combination with classes. Interfaces provide an alternative for languages like Java that does not allow multiple inheritance .&lt;br /&gt;
&lt;br /&gt;
==3. Mixins vs. Interfaces==&lt;br /&gt;
===3.1 Similarities and Differences===&lt;br /&gt;
	Mixins and Interfaces can be considered as a block of code having some variables and methods that are used in a class. The key difference between them is that a Mixin can contain method definitions whereas an Interface cannot.&lt;br /&gt;
&lt;br /&gt;
====Multiple method definitions:====&lt;br /&gt;
	In a class, when more than one module is Mixed in where some modules have overloaded methods, the method from the module that was last Mixed in into the class will be executed.&lt;br /&gt;
 module Frog&lt;br /&gt;
    def sound()&lt;br /&gt;
      puts &amp;quot;croak...&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Dinosaur&lt;br /&gt;
   def sound()&lt;br /&gt;
     puts &amp;quot;roar...&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
       include Frog&lt;br /&gt;
       include Dinosaur&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Frogosaur.new.sound &amp;lt;br&amp;gt;	&lt;br /&gt;
'''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
roar...&amp;lt;br&amp;gt;&lt;br /&gt;
This is because the class Frogosaur will have only one definition for each method. The users have the flexibility to make use of methods from different modules by rearranging the order of include.&lt;br /&gt;
Interfaces, on the other hand, do not offer the flexibility to have different implementation for a same method. This is because the class implementing the interface can provide only one implementation for each method. Hence there exists only one method definition.&lt;br /&gt;
 public interface Frog {&lt;br /&gt;
     void sound();&lt;br /&gt;
 }&lt;br /&gt;
 public interface Dinasour {&lt;br /&gt;
     void sound();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
 public void sound()&lt;br /&gt;
 {&lt;br /&gt;
     System.out.println(&amp;quot;barks......&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public static void main(String args[])&lt;br /&gt;
 {&lt;br /&gt;
     new Frogosaur().sound();;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
barks...&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
====Multiple object behavior:====&lt;br /&gt;
In Ruby, a class’s instance can extend a module and exhibit different behavior than the other instances of the same class.&lt;br /&gt;
&lt;br /&gt;
 module Frog&lt;br /&gt;
  def sound()&lt;br /&gt;
     puts &amp;quot;croak...&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Dinasour&lt;br /&gt;
  def sound()&lt;br /&gt;
    puts &amp;quot;roar...&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
  def sound()&lt;br /&gt;
    puts &amp;quot;barks...&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 beast1= Frogosaur.new&lt;br /&gt;
 beast2= Frogosaur.new&lt;br /&gt;
 beast1.extend(Frog)&lt;br /&gt;
 beast1.sound&lt;br /&gt;
 beast2.sound&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
croak...&amp;lt;br&amp;gt;&lt;br /&gt;
barks...&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Interfaces are implemented by a class and all instances of the class behave in the same way. They can use only the methods of the class from which they were instantiated.&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
  public void sound()&lt;br /&gt;
  {&lt;br /&gt;
      System.out.println(&amp;quot;Sounds...&amp;quot;);&lt;br /&gt;
  }  &lt;br /&gt;
  public static void main(String args[])&lt;br /&gt;
  {&lt;br /&gt;
      Frogosaur beast1 = new Frogosaur();&lt;br /&gt;
      Frogosaur beast2 = new Frogosaur();&lt;br /&gt;
      object1.sound();&lt;br /&gt;
      object2.sound();&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
Sounds...&amp;lt;br&amp;gt;&lt;br /&gt;
Sounds...&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;	&lt;br /&gt;
====Enumerable – module and interface====&lt;br /&gt;
&lt;br /&gt;
Ruby’s Arrays have several methods defined in them. To use them, the classes need not be a subclass of Array. The classes can include Enumerable and define the each method to use the array’s methods for free.&lt;br /&gt;
&lt;br /&gt;
 class EnumExer&lt;br /&gt;
  include Enumerable&lt;br /&gt;
  def initialize(*arrays)&lt;br /&gt;
    @arrays = arrays&lt;br /&gt;
  end&lt;br /&gt;
  def each&lt;br /&gt;
    @arrays.each { |a| a.each { |x| yield x } }&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 ma = EnumExer.new([4, 1], [3], [2])&lt;br /&gt;
 puts ma.sort&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
1&amp;lt;br&amp;gt;&lt;br /&gt;
2&amp;lt;br&amp;gt;&lt;br /&gt;
3&amp;lt;br&amp;gt;&lt;br /&gt;
4&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
You can observe that although the arguments passed to the ma instance did not have a similar structure, the sort method was able to identify each entry and sort them. This is because the each method was defined to identify and serve all the entries to the sort method.&lt;br /&gt;
The methods that are defined for Enumerable modules are:&lt;br /&gt;
 [&amp;quot;all?&amp;quot;, &amp;quot;any?&amp;quot;, &amp;quot;collect&amp;quot;, &amp;quot;detect&amp;quot;, &amp;quot;each_with_index&amp;quot;, &amp;quot;entries&amp;quot;,&lt;br /&gt;
 &amp;quot;find&amp;quot;, &amp;quot;find_all&amp;quot;, &amp;quot;grep&amp;quot;, &amp;quot;include?&amp;quot;, &amp;quot;inject&amp;quot;, &amp;quot;map&amp;quot;, &amp;quot;max&amp;quot;,&lt;br /&gt;
 &amp;quot;member?&amp;quot;, &amp;quot;min&amp;quot;, &amp;quot;partition&amp;quot;, &amp;quot;reject&amp;quot;, &amp;quot;select&amp;quot;, &amp;quot;sort&amp;quot;, &amp;quot;sort_by&amp;quot;,&lt;br /&gt;
 &amp;quot;to_a&amp;quot;, &amp;quot;zip&amp;quot;]&lt;br /&gt;
In Java, to use the methods of Enumerable interface, the class should be a subclass of Array.&lt;br /&gt;
&lt;br /&gt;
 public class EnumExer {&lt;br /&gt;
     static String content_string = &amp;quot;zyxwvutsrqponmlkjihgfedcba&amp;quot;;&lt;br /&gt;
     String a=&amp;quot;1&amp;quot;, b=&amp;quot;2&amp;quot;;&lt;br /&gt;
     public static void main(String args[])&lt;br /&gt;
     {&lt;br /&gt;
         char[] content_array = content_string.toCharArray();&lt;br /&gt;
         java.util.Arrays.sort(content_array);&lt;br /&gt;
         content_string=new String(content_array);&lt;br /&gt;
         System.out.println(content_string);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
Abcdefghijklmnopqrstuvwxyz&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, the string object was converted to a character array to be able to use with the sort method.&lt;br /&gt;
&lt;br /&gt;
====Inheritance:====&lt;br /&gt;
Java interfaces can inherit other interfaces but Ruby modules cannot inherit other modules but can contain classes and methods.&lt;br /&gt;
 module Wrapper&lt;br /&gt;
  class Base&lt;br /&gt;
     # Class Body&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Java,&lt;br /&gt;
 public interface Dinasour extends Frog{&lt;br /&gt;
     //variables and method Declarations&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Space for a similarity&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===3.2 Tabular Comparison===&lt;br /&gt;
{|border=&amp;quot;2&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Mixins&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Interfaces&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|The number of lines of code is lesser since the &amp;lt;br&amp;gt;&lt;br /&gt;
modules contain the method definition. &lt;br /&gt;
|The number of lines of code is bigger since the &amp;lt;br&amp;gt;&lt;br /&gt;
classes have to define the methods.&lt;br /&gt;
|-&lt;br /&gt;
|More modules in a class makes code less readable.&lt;br /&gt;
|The code is more readable since the class contain the &amp;lt;br&amp;gt;&lt;br /&gt;
method definition.&lt;br /&gt;
|-&lt;br /&gt;
|Modules cannot be inherited and cannot form is-a hierarchy.&lt;br /&gt;
|Interfaces can extend another interface and hence can form &amp;lt;br&amp;gt;&lt;br /&gt;
is-a hierarchy.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===3.3 Drawbacks===&lt;br /&gt;
The drawbacks of the technique of Mixins are very much debated. Though Mixins provide us with an easy way to write flexible and decoupled code and also help us solve the diamond problem, they pose their own problems. In large programs there could be large number of modules and each module could have loads of methods each performing a certain task. To trace the origin of the methods and to keep in mind the hierarchy is practically impossible. Another issue associated with Mixins pose is silent method overriding. Silent method overriding occurs when different modules are included in a class and each module have a method with the same definition. In such a case, the method which gets executed depends upon the way it is included in the class and this is done without any sort of message to the user (silently). In large scale applications this could certainly be an issue considering the number of modules that would be mixed in and it would be difficult to keep track of the hierarchy in which the modules were included.&lt;br /&gt;
Although Java provides polymorphic behavior with the use of interfaces, they sometimes tend to be very slow. The implementation of interfaces is also limited to public methods and constants with no implementation.&lt;br /&gt;
&lt;br /&gt;
==4. Conclusion==&lt;br /&gt;
&lt;br /&gt;
==5. References==&lt;br /&gt;
==6. See Also==&lt;/div&gt;</summary>
		<author><name>Rgovind2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w16_br&amp;diff=63614</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w16 br</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w16_br&amp;diff=63614"/>
		<updated>2012-09-11T23:55:32Z</updated>

		<summary type="html">&lt;p&gt;Rgovind2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==1. Overview==&lt;br /&gt;
&lt;br /&gt;
Mixins are Ruby modules. A module is a grouping of objects under a single name. The objects may be constants, methods, classes, or other modules. Modules are defined in Ruby using the keyword module. They can be incorporated into a class with Ruby’s include statement. This is called a mixin.&lt;br /&gt;
Modules are for sharing behavior (methods), while classes are for modeling relationship between objects. Ruby classes can Mixin a module and receive all its methods for free.&lt;br /&gt;
An Interface in Java programming language is an abstract type that classes must implement. Interfaces are declared using the interface keyword, and may only contain method signature and constant declarations.&lt;br /&gt;
A class that implements an interface must implement all of the methods described in the interface, or be an abstract class.&lt;br /&gt;
&lt;br /&gt;
==2. Definitions==&lt;br /&gt;
===2.1 Mixins===&lt;br /&gt;
	Modules provide a structure to collect classes, methods, and constants into a single, separately defined unit. They provide the simplest and an elegant way to adhere to the Don't repeat yourself principle. This is useful so as to avoid clashes with existing classes, methods, and constants.&lt;br /&gt;
Modules are defined in Ruby using the module keyword.&lt;br /&gt;
A Mixin is a class that is mixed with a module or a set of modules i.e. the implementation of the modules and the class are intertwined and combined together [3].&lt;br /&gt;
This gives us a neat and controlled way of adding new functionality to classes. Modules are similar to classes in that they hold a collection of methods, variables constants and other modules and class definitions. However, the real usage of a Mixin is exploited when the code in the Mixin starts to interact with code in the class that uses it.&lt;br /&gt;
Below is an example for modules and Mixins:&lt;br /&gt;
The module ParkingSlot consists of the methods park and unpark&lt;br /&gt;
 &lt;br /&gt;
 module ParkingSlot&lt;br /&gt;
       def park&lt;br /&gt;
          #code to park the car&lt;br /&gt;
       end&lt;br /&gt;
       def unpark&lt;br /&gt;
          #code to unpark the car&lt;br /&gt;
       end&lt;br /&gt;
 end&lt;br /&gt;
The module ParkingMeter consists of the methods calculate and printreceipt&lt;br /&gt;
&lt;br /&gt;
 module ParkingMeter&lt;br /&gt;
       def calculate&lt;br /&gt;
          #code to calculate the parking fee&lt;br /&gt;
       end&lt;br /&gt;
       def printreceipt&lt;br /&gt;
          #code to print the receipt&lt;br /&gt;
       end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
When a class includes a module via include Module Name, all the methods on that module become instance methods on the class.&lt;br /&gt;
&lt;br /&gt;
 class Parking&lt;br /&gt;
      include ParkingSlot&lt;br /&gt;
      include ParkingMeter&lt;br /&gt;
      def printcardetails&lt;br /&gt;
          #code to print the details and type of car&lt;br /&gt;
      end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
The modules ParkingSlot and ParkingMeter are “included” in the Parking class. All the variables and the instance methods of the modules ParkingSlot and ParkingMeter are now said to be “mixed in” into the class. It is now possible for class instances to use these methods defined in the modules as and when required.&lt;br /&gt;
 p=Parking.new   &lt;br /&gt;
 p.park            # -&amp;gt; Method park from module ParkingSlot&lt;br /&gt;
 p.unpark          # -&amp;gt; Method unpark from module ParkingSlot&lt;br /&gt;
 p.calculate       # -&amp;gt; Method calculate from module ParkingMeter&lt;br /&gt;
 p.printreceipt    # -&amp;gt; Method printreceipt from module ParkingMeter&lt;br /&gt;
 p.printcardetails # -&amp;gt; Calls printCarDetails from class&lt;br /&gt;
The class Parking inherits from both the modules and the module methods are now available in Parking. Hence, it is now possible to use the methods with an instance of the Parking class p.&lt;br /&gt;
Hence, Mixins can be thought of taking different methods and variables defined in different modules making them available as instance methods in the class as well, thereby extending the class’ functionality. Effectively mixed in modules behave as superclass.&lt;br /&gt;
===2.2 Interfaces===&lt;br /&gt;
In an object-oriented programming languages such as Java, C#, an interface[2] is a reference type, similar to a class that can contain only constants, method signatures, and nested types. There are no method bodies. Interfaces cannot be instantiated and they can only be implemented by classes or extended by other interfaces. All methods are public by default and any fields declared in an interface are by default static and final.&lt;br /&gt;
Example:&lt;br /&gt;
 public interface ParkingLot {&lt;br /&gt;
       boolean add(Car car, int slot);&lt;br /&gt;
       boolean remove(Car car);&lt;br /&gt;
       boolean findCar(Int number);&lt;br /&gt;
       int freeSlotsCount();&lt;br /&gt;
       int occupiedSlots();&lt;br /&gt;
       int parkingType();&lt;br /&gt;
       int calculateParkingFee(int hours, int charge, int duration);&lt;br /&gt;
       int findParkingSlot();&lt;br /&gt;
 }&lt;br /&gt;
 public class Mall implements ParkingLot {&lt;br /&gt;
       boolean add(Car car, int slot) { //... Code to add a car into the parking lot... }&lt;br /&gt;
       boolean remove(Car car){ // do something }&lt;br /&gt;
       boolean findCar(Int number){ // do something }&lt;br /&gt;
       int freeSlotsCount(){ // do something }&lt;br /&gt;
       int occupiedSlots(){ // do something }&lt;br /&gt;
       int parkingType(){ // do something }&lt;br /&gt;
       int calculateParkingFee(int hours, int charge, int duration){ // do something }&lt;br /&gt;
       int findParkingSlot(){ // do something }&lt;br /&gt;
 }&lt;br /&gt;
Interfaces play another important role in the object-oriented programming language. Interfaces are not part of the class hierarchy, although they work in combination with classes. Interfaces provide an alternative for languages like Java that does not allow multiple inheritance .&lt;br /&gt;
&lt;br /&gt;
==3. Mixins vs. Interfaces==&lt;br /&gt;
===3.1 Similarities and Differences===&lt;br /&gt;
	Mixins and Interfaces can be considered as a block of code having some variables and methods that are used in a class. The key difference between them is that a Mixin can contain method definitions whereas an Interface cannot.&lt;br /&gt;
&lt;br /&gt;
====Multiple method definitions:====&lt;br /&gt;
	In a class, when more than one module is Mixed in where some modules have overloaded methods, the method from the module that was last Mixed in into the class will be executed.&lt;br /&gt;
 module Frog&lt;br /&gt;
    def sound()&lt;br /&gt;
      puts &amp;quot;croak...&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Dinosaur&lt;br /&gt;
   def sound()&lt;br /&gt;
     puts &amp;quot;roar...&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
       include Frog&lt;br /&gt;
       include Dinosaur&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Frogosaur.new.sound &amp;lt;br&amp;gt;	&lt;br /&gt;
'''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
roar...&amp;lt;br&amp;gt;&lt;br /&gt;
This is because the class Frogosaur will have only one definition for each method. The users have the flexibility to make use of methods from different modules by rearranging the order of include.&lt;br /&gt;
Interfaces, on the other hand, do not offer the flexibility to have different implementation for a same method. This is because the class implementing the interface can provide only one implementation for each method. Hence there exists only one method definition.&lt;br /&gt;
 public interface Frog {&lt;br /&gt;
     void sound();&lt;br /&gt;
 }&lt;br /&gt;
 public interface Dinasour {&lt;br /&gt;
     void sound();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
 public void sound()&lt;br /&gt;
 {&lt;br /&gt;
     System.out.println(&amp;quot;barks......&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public static void main(String args[])&lt;br /&gt;
 {&lt;br /&gt;
     new Frogosaur().sound();;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
barks...&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Multiple object behavior:====&lt;br /&gt;
In Ruby, a class’s instance can extend a module and exhibit different behavior than the other instances of the same class.&lt;br /&gt;
&lt;br /&gt;
 module Frog&lt;br /&gt;
  def sound()&lt;br /&gt;
     puts &amp;quot;croak...&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Dinasour&lt;br /&gt;
  def sound()&lt;br /&gt;
    puts &amp;quot;roar...&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
  def sound()&lt;br /&gt;
    puts &amp;quot;barks...&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 beast1= Frogosaur.new&lt;br /&gt;
 beast2= Frogosaur.new&lt;br /&gt;
 beast1.extend(Frog)&lt;br /&gt;
 beast1.sound&lt;br /&gt;
 beast2.sound&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
croak...&amp;lt;br&amp;gt;&lt;br /&gt;
barks...&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Interfaces are implemented by a class and all instances of the class behave in the same way. They can use only the methods of the class from which they were instantiated.&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
  public void sound()&lt;br /&gt;
  {&lt;br /&gt;
      System.out.println(&amp;quot;Sounds...&amp;quot;);&lt;br /&gt;
  }  &lt;br /&gt;
  public static void main(String args[])&lt;br /&gt;
  {&lt;br /&gt;
      Frogosaur beast1 = new Frogosaur();&lt;br /&gt;
      Frogosaur beast2 = new Frogosaur();&lt;br /&gt;
      object1.sound();&lt;br /&gt;
      object2.sound();&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
Sounds...&amp;lt;br&amp;gt;&lt;br /&gt;
Sounds...&amp;lt;br&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
====Enumerable – module and interface====&lt;br /&gt;
&lt;br /&gt;
Ruby’s Arrays have several methods defined in them. To use them, the classes need not be a subclass of Array. The classes can include Enumerable and define the each method to use the array’s methods for free.&lt;br /&gt;
&lt;br /&gt;
 class EnumExer&lt;br /&gt;
  include Enumerable&lt;br /&gt;
  def initialize(*arrays)&lt;br /&gt;
    @arrays = arrays&lt;br /&gt;
  end&lt;br /&gt;
  def each&lt;br /&gt;
    @arrays.each { |a| a.each { |x| yield x } }&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 ma = EnumExer.new([4, 1], [3], [2])&lt;br /&gt;
 puts ma.sort&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
1&amp;lt;br&amp;gt;&lt;br /&gt;
2&amp;lt;br&amp;gt;&lt;br /&gt;
3&amp;lt;br&amp;gt;&lt;br /&gt;
4&amp;lt;br&amp;gt;&lt;br /&gt;
You can observe that although the arguments passed to the ma instance did not have a similar structure, the sort method was able to identify each entry and sort them. This is because the each method was defined to identify and serve all the entries to the sort method.&lt;br /&gt;
The methods that are defined for Enumerable modules are:&lt;br /&gt;
 [&amp;quot;all?&amp;quot;, &amp;quot;any?&amp;quot;, &amp;quot;collect&amp;quot;, &amp;quot;detect&amp;quot;, &amp;quot;each_with_index&amp;quot;, &amp;quot;entries&amp;quot;,&lt;br /&gt;
 &amp;quot;find&amp;quot;, &amp;quot;find_all&amp;quot;, &amp;quot;grep&amp;quot;, &amp;quot;include?&amp;quot;, &amp;quot;inject&amp;quot;, &amp;quot;map&amp;quot;, &amp;quot;max&amp;quot;,&lt;br /&gt;
 &amp;quot;member?&amp;quot;, &amp;quot;min&amp;quot;, &amp;quot;partition&amp;quot;, &amp;quot;reject&amp;quot;, &amp;quot;select&amp;quot;, &amp;quot;sort&amp;quot;, &amp;quot;sort_by&amp;quot;,&lt;br /&gt;
 &amp;quot;to_a&amp;quot;, &amp;quot;zip&amp;quot;]&lt;br /&gt;
In Java, to use the methods of Enumerable interface, the class should be a subclass of Array.&lt;br /&gt;
&lt;br /&gt;
 public class EnumExer {&lt;br /&gt;
     static String content_string = &amp;quot;zyxwvutsrqponmlkjihgfedcba&amp;quot;;&lt;br /&gt;
     String a=&amp;quot;1&amp;quot;, b=&amp;quot;2&amp;quot;;&lt;br /&gt;
     public static void main(String args[])&lt;br /&gt;
     {&lt;br /&gt;
         char[] content_array = content_string.toCharArray();&lt;br /&gt;
         java.util.Arrays.sort(content_array);&lt;br /&gt;
         content_string=new String(content_array);&lt;br /&gt;
         System.out.println(content_string);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
Abcdefghijklmnopqrstuvwxyz&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, the string object was converted to a character array to be able to use with the sort method.&lt;br /&gt;
&lt;br /&gt;
====Inheritance:====&lt;br /&gt;
Java interfaces can inherit other interfaces but Ruby modules cannot inherit other modules but can contain classes and methods.&lt;br /&gt;
 module Wrapper&lt;br /&gt;
  class Base&lt;br /&gt;
     # Class Body&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Java,&lt;br /&gt;
 public interface Dinasour extends Frog{&lt;br /&gt;
     //variables and method Declarations&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Space for a similarity&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===3.2 Tabular Comparison===&lt;br /&gt;
{|border=&amp;quot;1&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Mixins&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Interfaces&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|The number of lines of code is lesser since the modules contain the method definition. &lt;br /&gt;
|The number of lines of code is bigger since the classes have to define the methods.&lt;br /&gt;
|-&lt;br /&gt;
|More modules in a class makes code less readable.&lt;br /&gt;
|The code is more readable since the class contain the method definition.&lt;br /&gt;
|-&lt;br /&gt;
|Modules cannot be inherited and cannot form is-a hierarchy.&lt;br /&gt;
|Interfaces can extend another interface and hence can form is-a hierarchy.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===3.3 Drawbacks===&lt;br /&gt;
The drawbacks of the technique of Mixins are very much debated. Though Mixins provide us with an easy way to write flexible and decoupled code and also help us solve the diamond problem, they pose their own problems. In large programs there could be large number of modules and each module could have loads of methods each performing a certain task. To trace the origin of the methods and to keep in mind the hierarchy is practically impossible. Another issue associated with Mixins pose is silent method overriding. Silent method overriding occurs when different modules are included in a class and each module have a method with the same definition. In such a case, the method which gets executed depends upon the way it is included in the class and this is done without any sort of message to the user (silently). In large scale applications this could certainly be an issue considering the number of modules that would be mixed in and it would be difficult to keep track of the hierarchy in which the modules were included.&lt;br /&gt;
Although Java provides polymorphic behavior with the use of interfaces, they sometimes tend to be very slow. The implementation of interfaces is also limited to public methods and constants with no implementation.&lt;br /&gt;
&lt;br /&gt;
==4. Conclusion==&lt;br /&gt;
&lt;br /&gt;
==5. References==&lt;br /&gt;
==6. See Also==&lt;/div&gt;</summary>
		<author><name>Rgovind2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w16_br&amp;diff=63613</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w16 br</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w16_br&amp;diff=63613"/>
		<updated>2012-09-11T23:50:45Z</updated>

		<summary type="html">&lt;p&gt;Rgovind2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==1. Overview==&lt;br /&gt;
&lt;br /&gt;
Mixins are Ruby modules. A module is a grouping of objects under a single name. The objects may be constants, methods, classes, or other modules. Modules are defined in Ruby using the keyword module. They can be incorporated into a class with Ruby’s include statement. This is called a mixin.&lt;br /&gt;
Modules are for sharing behavior (methods), while classes are for modeling relationship between objects. Ruby classes can Mixin a module and receive all its methods for free.&lt;br /&gt;
An Interface in Java programming language is an abstract type that classes must implement. Interfaces are declared using the interface keyword, and may only contain method signature and constant declarations.&lt;br /&gt;
A class that implements an interface must implement all of the methods described in the interface, or be an abstract class.&lt;br /&gt;
&lt;br /&gt;
==2. Definitions==&lt;br /&gt;
===2.1 Mixins===&lt;br /&gt;
	Modules provide a structure to collect classes, methods, and constants into a single, separately defined unit. They provide the simplest and an elegant way to adhere to the Don't repeat yourself principle. This is useful so as to avoid clashes with existing classes, methods, and constants.&lt;br /&gt;
Modules are defined in Ruby using the module keyword.&lt;br /&gt;
A Mixin is a class that is mixed with a module or a set of modules i.e. the implementation of the modules and the class are intertwined and combined together [3].&lt;br /&gt;
This gives us a neat and controlled way of adding new functionality to classes. Modules are similar to classes in that they hold a collection of methods, variables constants and other modules and class definitions. However, the real usage of a Mixin is exploited when the code in the Mixin starts to interact with code in the class that uses it.&lt;br /&gt;
Below is an example for modules and Mixins:&lt;br /&gt;
The module ParkingSlot consists of the methods park and unpark&lt;br /&gt;
 &lt;br /&gt;
 module ParkingSlot&lt;br /&gt;
       def park&lt;br /&gt;
          #code to park the car&lt;br /&gt;
       end&lt;br /&gt;
       def unpark&lt;br /&gt;
          #code to unpark the car&lt;br /&gt;
       end&lt;br /&gt;
 end&lt;br /&gt;
The module ParkingMeter consists of the methods calculate and printreceipt&lt;br /&gt;
&lt;br /&gt;
 module ParkingMeter&lt;br /&gt;
       def calculate&lt;br /&gt;
          #code to calculate the parking fee&lt;br /&gt;
       end&lt;br /&gt;
       def printreceipt&lt;br /&gt;
          #code to print the receipt&lt;br /&gt;
       end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
When a class includes a module via include Module Name, all the methods on that module become instance methods on the class.&lt;br /&gt;
&lt;br /&gt;
 class Parking&lt;br /&gt;
      include ParkingSlot&lt;br /&gt;
      include ParkingMeter&lt;br /&gt;
      def printcardetails&lt;br /&gt;
          #code to print the details and type of car&lt;br /&gt;
      end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
The modules ParkingSlot and ParkingMeter are “included” in the Parking class. All the variables and the instance methods of the modules ParkingSlot and ParkingMeter are now said to be “mixed in” into the class. It is now possible for class instances to use these methods defined in the modules as and when required.&lt;br /&gt;
 p=Parking.new   &lt;br /&gt;
 p.park            # -&amp;gt; Method park from module ParkingSlot&lt;br /&gt;
 p.unpark          # -&amp;gt; Method unpark from module ParkingSlot&lt;br /&gt;
 p.calculate       # -&amp;gt; Method calculate from module ParkingMeter&lt;br /&gt;
 p.printreceipt    # -&amp;gt; Method printreceipt from module ParkingMeter&lt;br /&gt;
 p.printcardetails # -&amp;gt; Calls printCarDetails from class&lt;br /&gt;
The class Parking inherits from both the modules and the module methods are now available in Parking. Hence, it is now possible to use the methods with an instance of the Parking class p.&lt;br /&gt;
Hence, Mixins can be thought of taking different methods and variables defined in different modules making them available as instance methods in the class as well, thereby extending the class’ functionality. Effectively mixed in modules behave as superclass.&lt;br /&gt;
===2.2 Interfaces===&lt;br /&gt;
In an object-oriented programming languages such as Java, C#, an interface[2] is a reference type, similar to a class that can contain only constants, method signatures, and nested types. There are no method bodies. Interfaces cannot be instantiated and they can only be implemented by classes or extended by other interfaces. All methods are public by default and any fields declared in an interface are by default static and final.&lt;br /&gt;
Example:&lt;br /&gt;
 public interface ParkingLot {&lt;br /&gt;
       boolean add(Car car, int slot);&lt;br /&gt;
       boolean remove(Car car);&lt;br /&gt;
       boolean findCar(Int number);&lt;br /&gt;
       int freeSlotsCount();&lt;br /&gt;
       int occupiedSlots();&lt;br /&gt;
       int parkingType();&lt;br /&gt;
       int calculateParkingFee(int hours, int charge, int duration);&lt;br /&gt;
       int findParkingSlot();&lt;br /&gt;
 }&lt;br /&gt;
 public class Mall implements ParkingLot {&lt;br /&gt;
       boolean add(Car car, int slot) { //... Code to add a car into the parking lot... }&lt;br /&gt;
       boolean remove(Car car){ // do something }&lt;br /&gt;
       boolean findCar(Int number){ // do something }&lt;br /&gt;
       int freeSlotsCount(){ // do something }&lt;br /&gt;
       int occupiedSlots(){ // do something }&lt;br /&gt;
       int parkingType(){ // do something }&lt;br /&gt;
       int calculateParkingFee(int hours, int charge, int duration){ // do something }&lt;br /&gt;
       int findParkingSlot(){ // do something }&lt;br /&gt;
 }&lt;br /&gt;
Interfaces play another important role in the object-oriented programming language. Interfaces are not part of the class hierarchy, although they work in combination with classes. Interfaces provide an alternative for languages like Java that does not allow multiple inheritance .&lt;br /&gt;
&lt;br /&gt;
==3. Mixins vs. Interfaces==&lt;br /&gt;
===3.1 Similarities and Differences===&lt;br /&gt;
	Mixins and Interfaces can be considered as a block of code having some variables and methods that are used in a class. The key difference between them is that a Mixin can contain method definitions whereas an Interface cannot.&lt;br /&gt;
&lt;br /&gt;
====Multiple method definitions:====&lt;br /&gt;
	In a class, when more than one module is Mixed in where some modules have overloaded methods, the method from the module that was last Mixed in into the class will be executed.&lt;br /&gt;
 module Frog&lt;br /&gt;
    def sound()&lt;br /&gt;
      puts &amp;quot;croak...&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Dinosaur&lt;br /&gt;
   def sound()&lt;br /&gt;
     puts &amp;quot;roar...&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
       include Frog&lt;br /&gt;
       include Dinosaur&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Frogosaur.new.sound &amp;lt;br&amp;gt;	&lt;br /&gt;
'''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
roar...&amp;lt;br&amp;gt;&lt;br /&gt;
This is because the class Frogosaur will have only one definition for each method. The users have the flexibility to make use of methods from different modules by rearranging the order of include.&lt;br /&gt;
Interfaces, on the other hand, do not offer the flexibility to have different implementation for a same method. This is because the class implementing the interface can provide only one implementation for each method. Hence there exists only one method definition.&lt;br /&gt;
 public interface Frog {&lt;br /&gt;
     void sound();&lt;br /&gt;
 }&lt;br /&gt;
 public interface Dinasour {&lt;br /&gt;
     void sound();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
 public void sound()&lt;br /&gt;
 {&lt;br /&gt;
     System.out.println(&amp;quot;barks......&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public static void main(String args[])&lt;br /&gt;
 {&lt;br /&gt;
     new Frogosaur().sound();;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
barks...&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Multiple object behavior:====&lt;br /&gt;
In Ruby, a class’s instance can extend a module and exhibit different behavior than the other instances of the same class.&lt;br /&gt;
&lt;br /&gt;
 module Frog&lt;br /&gt;
  def sound()&lt;br /&gt;
     puts &amp;quot;croak...&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Dinasour&lt;br /&gt;
  def sound()&lt;br /&gt;
    puts &amp;quot;roar...&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
  def sound()&lt;br /&gt;
    puts &amp;quot;barks...&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 beast1= Frogosaur.new&lt;br /&gt;
 beast2= Frogosaur.new&lt;br /&gt;
 beast1.extend(Frog)&lt;br /&gt;
 beast1.sound&lt;br /&gt;
 beast2.sound&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
croak...&amp;lt;br&amp;gt;&lt;br /&gt;
barks...&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Interfaces are implemented by a class and all instances of the class behave in the same way. They can use only the methods of the class from which they were instantiated.&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
  public void sound()&lt;br /&gt;
  {&lt;br /&gt;
      System.out.println(&amp;quot;Sounds...&amp;quot;);&lt;br /&gt;
  }  &lt;br /&gt;
  public static void main(String args[])&lt;br /&gt;
  {&lt;br /&gt;
      Frogosaur beast1 = new Frogosaur();&lt;br /&gt;
      Frogosaur beast2 = new Frogosaur();&lt;br /&gt;
      object1.sound();&lt;br /&gt;
      object2.sound();&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
Sounds...&amp;lt;br&amp;gt;&lt;br /&gt;
Sounds...&amp;lt;br&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
====Enumerable – module and interface====&lt;br /&gt;
&lt;br /&gt;
Ruby’s Arrays have several methods defined in them. To use them, the classes need not be a subclass of Array. The classes can include Enumerable and define the each method to use the array’s methods for free.&lt;br /&gt;
&lt;br /&gt;
 class EnumExer&lt;br /&gt;
  include Enumerable&lt;br /&gt;
  def initialize(*arrays)&lt;br /&gt;
    @arrays = arrays&lt;br /&gt;
  end&lt;br /&gt;
  def each&lt;br /&gt;
    @arrays.each { |a| a.each { |x| yield x } }&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 ma = EnumExer.new([4, 1], [3], [2])&lt;br /&gt;
 puts ma.sort&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
1&amp;lt;br&amp;gt;&lt;br /&gt;
2&amp;lt;br&amp;gt;&lt;br /&gt;
3&amp;lt;br&amp;gt;&lt;br /&gt;
4&amp;lt;br&amp;gt;&lt;br /&gt;
You can observe that although the arguments passed to the ma instance did not have a similar structure, the sort method was able to identify each entry and sort them. This is because the each method was defined to identify and serve all the entries to the sort method.&lt;br /&gt;
The methods that are defined for Enumerable modules are:&lt;br /&gt;
 [&amp;quot;all?&amp;quot;, &amp;quot;any?&amp;quot;, &amp;quot;collect&amp;quot;, &amp;quot;detect&amp;quot;, &amp;quot;each_with_index&amp;quot;, &amp;quot;entries&amp;quot;,&lt;br /&gt;
 &amp;quot;find&amp;quot;, &amp;quot;find_all&amp;quot;, &amp;quot;grep&amp;quot;, &amp;quot;include?&amp;quot;, &amp;quot;inject&amp;quot;, &amp;quot;map&amp;quot;, &amp;quot;max&amp;quot;,&lt;br /&gt;
 &amp;quot;member?&amp;quot;, &amp;quot;min&amp;quot;, &amp;quot;partition&amp;quot;, &amp;quot;reject&amp;quot;, &amp;quot;select&amp;quot;, &amp;quot;sort&amp;quot;, &amp;quot;sort_by&amp;quot;,&lt;br /&gt;
 &amp;quot;to_a&amp;quot;, &amp;quot;zip&amp;quot;]&lt;br /&gt;
In Java, to use the methods of Enumerable interface, the class should be a subclass of Array.&lt;br /&gt;
&lt;br /&gt;
 public class EnumExer {&lt;br /&gt;
     static String content_string = &amp;quot;zyxwvutsrqponmlkjihgfedcba&amp;quot;;&lt;br /&gt;
     String a=&amp;quot;1&amp;quot;, b=&amp;quot;2&amp;quot;;&lt;br /&gt;
     public static void main(String args[])&lt;br /&gt;
     {&lt;br /&gt;
         char[] content_array = content_string.toCharArray();&lt;br /&gt;
         java.util.Arrays.sort(content_array);&lt;br /&gt;
         content_string=new String(content_array);&lt;br /&gt;
         System.out.println(content_string);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
Abcdefghijklmnopqrstuvwxyz&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, the string object was converted to a character array to be able to use with the sort method.&lt;br /&gt;
&lt;br /&gt;
====Inheritance:====&lt;br /&gt;
Java interfaces can inherit other interfaces but Ruby modules cannot inherit other modules but can contain classes and methods.&lt;br /&gt;
 module Wrapper&lt;br /&gt;
  class Base&lt;br /&gt;
     # Class Body&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Java,&lt;br /&gt;
 public interface Dinasour extends Frog{&lt;br /&gt;
     //variables and method Declarations&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Space for a similarity&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===3.2 Tabular Comparison===&lt;br /&gt;
{|border=&amp;quot;1&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Mixins&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Interfaces&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|text for row 1, column 1&lt;br /&gt;
|text for row 1, column 2&lt;br /&gt;
|-&lt;br /&gt;
|text for row 2, column 1&lt;br /&gt;
|text for row 2, column 2&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===3.3 Drawbacks===&lt;br /&gt;
The drawbacks of the technique of Mixins are very much debated. Though Mixins provide us with an easy way to write flexible and decoupled code and also help us solve the diamond problem, they pose their own problems. In large programs there could be large number of modules and each module could have loads of methods each performing a certain task. To trace the origin of the methods and to keep in mind the hierarchy is practically impossible. Another issue associated with Mixins pose is silent method overriding. Silent method overriding occurs when different modules are included in a class and each module have a method with the same definition. In such a case, the method which gets executed depends upon the way it is included in the class and this is done without any sort of message to the user (silently). In large scale applications this could certainly be an issue considering the number of modules that would be mixed in and it would be difficult to keep track of the hierarchy in which the modules were included.&lt;br /&gt;
Although Java provides polymorphic behavior with the use of interfaces, they sometimes tend to be very slow. The implementation of interfaces is also limited to public methods and constants with no implementation.&lt;br /&gt;
&lt;br /&gt;
==4. Conclusion==&lt;br /&gt;
&lt;br /&gt;
==5. References==&lt;br /&gt;
==6. See Also==&lt;/div&gt;</summary>
		<author><name>Rgovind2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w16_br&amp;diff=63608</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w16 br</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w16_br&amp;diff=63608"/>
		<updated>2012-09-11T23:35:35Z</updated>

		<summary type="html">&lt;p&gt;Rgovind2: Created page with &amp;quot;==1. Overview==  Mixins are Ruby modules. A module is a grouping of objects under a single name. The objects may be constants, methods, classes, or other modules. Modules are def...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==1. Overview==&lt;br /&gt;
&lt;br /&gt;
Mixins are Ruby modules. A module is a grouping of objects under a single name. The objects may be constants, methods, classes, or other modules. Modules are defined in Ruby using the keyword module. They can be incorporated into a class with Ruby’s include statement. This is called a mixin.&lt;br /&gt;
Modules are for sharing behavior (methods), while classes are for modeling relationship between objects. Ruby classes can Mixin a module and receive all its methods for free.&lt;br /&gt;
An Interface in Java programming language is an abstract type that classes must implement. Interfaces are declared using the interface keyword, and may only contain method signature and constant declarations.&lt;br /&gt;
A class that implements an interface must implement all of the methods described in the interface, or be an abstract class.&lt;br /&gt;
&lt;br /&gt;
==2. Definitions==&lt;br /&gt;
===2.1 Mixins===&lt;br /&gt;
	Modules provide a structure to collect classes, methods, and constants into a single, separately defined unit. They provide the simplest and an elegant way to adhere to the Don't repeat yourself principle. This is useful so as to avoid clashes with existing classes, methods, and constants.&lt;br /&gt;
Modules are defined in Ruby using the module keyword.&lt;br /&gt;
A Mixin is a class that is mixed with a module or a set of modules i.e. the implementation of the modules and the class are intertwined and combined together [3].&lt;br /&gt;
This gives us a neat and controlled way of adding new functionality to classes. Modules are similar to classes in that they hold a collection of methods, variables constants and other modules and class definitions. However, the real usage of a Mixin is exploited when the code in the Mixin starts to interact with code in the class that uses it.&lt;br /&gt;
Below is an example for modules and Mixins:&lt;br /&gt;
The module ParkingSlot consists of the methods park and unpark&lt;br /&gt;
 &lt;br /&gt;
 module ParkingSlot&lt;br /&gt;
       def park&lt;br /&gt;
          #code to park the car&lt;br /&gt;
       end&lt;br /&gt;
       def unpark&lt;br /&gt;
          #code to unpark the car&lt;br /&gt;
       end&lt;br /&gt;
 end&lt;br /&gt;
The module ParkingMeter consists of the methods calculate and printreceipt&lt;br /&gt;
&lt;br /&gt;
 module ParkingMeter&lt;br /&gt;
       def calculate&lt;br /&gt;
          #code to calculate the parking fee&lt;br /&gt;
       end&lt;br /&gt;
       def printreceipt&lt;br /&gt;
          #code to print the receipt&lt;br /&gt;
       end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
When a class includes a module via include Module Name, all the methods on that module become instance methods on the class.&lt;br /&gt;
&lt;br /&gt;
 class Parking&lt;br /&gt;
      include ParkingSlot&lt;br /&gt;
      include ParkingMeter&lt;br /&gt;
      def printcardetails&lt;br /&gt;
          #code to print the details and type of car&lt;br /&gt;
      end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
The modules ParkingSlot and ParkingMeter are “included” in the Parking class. All the variables and the instance methods of the modules ParkingSlot and ParkingMeter are now said to be “mixed in” into the class. It is now possible for class instances to use these methods defined in the modules as and when required.&lt;br /&gt;
 p=Parking.new   &lt;br /&gt;
 p.park            # -&amp;gt; Method park from module ParkingSlot&lt;br /&gt;
 p.unpark          # -&amp;gt; Method unpark from module ParkingSlot&lt;br /&gt;
 p.calculate       # -&amp;gt; Method calculate from module ParkingMeter&lt;br /&gt;
 p.printreceipt    # -&amp;gt; Method printreceipt from module ParkingMeter&lt;br /&gt;
 p.printcardetails # -&amp;gt; Calls printCarDetails from class&lt;br /&gt;
The class Parking inherits from both the modules and the module methods are now available in Parking. Hence, it is now possible to use the methods with an instance of the Parking class p.&lt;br /&gt;
Hence, Mixins can be thought of taking different methods and variables defined in different modules making them available as instance methods in the class as well, thereby extending the class’ functionality. Effectively mixed in modules behave as superclass.&lt;br /&gt;
===2.2 Interfaces===&lt;br /&gt;
In an object-oriented programming languages such as Java, C#, an interface[2] is a reference type, similar to a class that can contain only constants, method signatures, and nested types. There are no method bodies. Interfaces cannot be instantiated and they can only be implemented by classes or extended by other interfaces. All methods are public by default and any fields declared in an interface are by default static and final.&lt;br /&gt;
Example:&lt;br /&gt;
 public interface ParkingLot {&lt;br /&gt;
       boolean add(Car car, int slot);&lt;br /&gt;
       boolean remove(Car car);&lt;br /&gt;
       boolean findCar(Int number);&lt;br /&gt;
       int freeSlotsCount();&lt;br /&gt;
       int occupiedSlots();&lt;br /&gt;
       int parkingType();&lt;br /&gt;
       int calculateParkingFee(int hours, int charge, int duration);&lt;br /&gt;
       int findParkingSlot();&lt;br /&gt;
 }&lt;br /&gt;
 public class Mall implements ParkingLot {&lt;br /&gt;
       boolean add(Car car, int slot) { //... Code to add a car into the parking lot... }&lt;br /&gt;
       boolean remove(Car car){ // do something }&lt;br /&gt;
       boolean findCar(Int number){ // do something }&lt;br /&gt;
       int freeSlotsCount(){ // do something }&lt;br /&gt;
       int occupiedSlots(){ // do something }&lt;br /&gt;
       int parkingType(){ // do something }&lt;br /&gt;
       int calculateParkingFee(int hours, int charge, int duration){ // do something }&lt;br /&gt;
       int findParkingSlot(){ // do something }&lt;br /&gt;
 }&lt;br /&gt;
Interfaces play another important role in the object-oriented programming language. Interfaces are not part of the class hierarchy, although they work in combination with classes. Interfaces provide an alternative for languages like Java that does not allow multiple inheritance .&lt;br /&gt;
&lt;br /&gt;
==3. Mixins vs. Interfaces==&lt;br /&gt;
===3.1 Similarities and Differences===&lt;br /&gt;
	Mixins and Interfaces can be considered as a block of code having some variables and methods that are used in a class. The key difference between them is that a Mixin can contain method definitions whereas an Interface cannot.&lt;br /&gt;
&lt;br /&gt;
====Multiple method definitions:====&lt;br /&gt;
	In a class, when more than one module is Mixed in where some modules have overloaded methods, the method from the module that was last Mixed in into the class will be executed.&lt;br /&gt;
 module Frog&lt;br /&gt;
    def sound()&lt;br /&gt;
      puts &amp;quot;croak...&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Dinosaur&lt;br /&gt;
   def sound()&lt;br /&gt;
     puts &amp;quot;roar...&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
       include Frog&lt;br /&gt;
       include Dinosaur&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Frogosaur.new.sound &amp;lt;br&amp;gt;	&lt;br /&gt;
'''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
roar...&amp;lt;br&amp;gt;&lt;br /&gt;
This is because the class Frogosaur will have only one definition for each method. The users have the flexibility to make use of methods from different modules by rearranging the order of include.&lt;br /&gt;
Interfaces, on the other hand, do not offer the flexibility to have different implementation for a same method. This is because the class implementing the interface can provide only one implementation for each method. Hence there exists only one method definition.&lt;br /&gt;
 public interface Frog {&lt;br /&gt;
     void sound();&lt;br /&gt;
 }&lt;br /&gt;
 public interface Dinasour {&lt;br /&gt;
     void sound();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
 public void sound()&lt;br /&gt;
 {&lt;br /&gt;
     System.out.println(&amp;quot;barks......&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public static void main(String args[])&lt;br /&gt;
 {&lt;br /&gt;
     new Frogosaur().sound();;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
barks...&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Multiple object behavior:====&lt;br /&gt;
In Ruby, a class’s instance can extend a module and exhibit different behavior than the other instances of the same class.&lt;br /&gt;
&lt;br /&gt;
 module Frog&lt;br /&gt;
  def sound()&lt;br /&gt;
     puts &amp;quot;croak...&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 module Dinasour&lt;br /&gt;
  def sound()&lt;br /&gt;
    puts &amp;quot;roar...&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Frogosaur&lt;br /&gt;
  def sound()&lt;br /&gt;
    puts &amp;quot;barks...&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 beast1= Frogosaur.new&lt;br /&gt;
 beast2= Frogosaur.new&lt;br /&gt;
 beast1.extend(Frog)&lt;br /&gt;
 beast1.sound&lt;br /&gt;
 beast2.sound&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
croak...&amp;lt;br&amp;gt;&lt;br /&gt;
barks...&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Interfaces are implemented by a class and all instances of the class behave in the same way. They can use only the methods of the class from which they were instantiated.&lt;br /&gt;
 public class Frogosaur implements Frog,Dinasour{&lt;br /&gt;
  public void sound()&lt;br /&gt;
  {&lt;br /&gt;
      System.out.println(&amp;quot;Sounds...&amp;quot;);&lt;br /&gt;
  }  &lt;br /&gt;
  public static void main(String args[])&lt;br /&gt;
  {&lt;br /&gt;
      Frogosaur beast1 = new Frogosaur();&lt;br /&gt;
      Frogosaur beast2 = new Frogosaur();&lt;br /&gt;
      object1.sound();&lt;br /&gt;
      object2.sound();&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
Sounds...&amp;lt;br&amp;gt;&lt;br /&gt;
Sounds...&amp;lt;br&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
====Enumerable – module and interface====&lt;br /&gt;
&lt;br /&gt;
Ruby’s Arrays have several methods defined in them. To use them, the classes need not be a subclass of Array. The classes can include Enumerable and define the each method to use the array’s methods for free.&lt;br /&gt;
&lt;br /&gt;
 class EnumExer&lt;br /&gt;
  include Enumerable&lt;br /&gt;
  def initialize(*arrays)&lt;br /&gt;
    @arrays = arrays&lt;br /&gt;
  end&lt;br /&gt;
  def each&lt;br /&gt;
    @arrays.each { |a| a.each { |x| yield x } }&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 ma = EnumExer.new([4, 1], [3], [2])&lt;br /&gt;
 puts ma.sort&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
1&amp;lt;br&amp;gt;&lt;br /&gt;
2&amp;lt;br&amp;gt;&lt;br /&gt;
3&amp;lt;br&amp;gt;&lt;br /&gt;
4&amp;lt;br&amp;gt;&lt;br /&gt;
You can observe that although the arguments passed to the ma instance did not have a similar structure, the sort method was able to identify each entry and sort them. This is because the each method was defined to identify and serve all the entries to the sort method.&lt;br /&gt;
The methods that are defined for Enumerable modules are:&lt;br /&gt;
 [&amp;quot;all?&amp;quot;, &amp;quot;any?&amp;quot;, &amp;quot;collect&amp;quot;, &amp;quot;detect&amp;quot;, &amp;quot;each_with_index&amp;quot;, &amp;quot;entries&amp;quot;,&lt;br /&gt;
 &amp;quot;find&amp;quot;, &amp;quot;find_all&amp;quot;, &amp;quot;grep&amp;quot;, &amp;quot;include?&amp;quot;, &amp;quot;inject&amp;quot;, &amp;quot;map&amp;quot;, &amp;quot;max&amp;quot;,&lt;br /&gt;
 &amp;quot;member?&amp;quot;, &amp;quot;min&amp;quot;, &amp;quot;partition&amp;quot;, &amp;quot;reject&amp;quot;, &amp;quot;select&amp;quot;, &amp;quot;sort&amp;quot;, &amp;quot;sort_by&amp;quot;,&lt;br /&gt;
 &amp;quot;to_a&amp;quot;, &amp;quot;zip&amp;quot;]&lt;br /&gt;
In Java, to use the methods of Enumerable interface, the class should be a subclass of Array.&lt;br /&gt;
&lt;br /&gt;
 public class EnumExer {&lt;br /&gt;
     static String content_string = &amp;quot;zyxwvutsrqponmlkjihgfedcba&amp;quot;;&lt;br /&gt;
     String a=&amp;quot;1&amp;quot;, b=&amp;quot;2&amp;quot;;&lt;br /&gt;
     public static void main(String args[])&lt;br /&gt;
     {&lt;br /&gt;
         char[] content_array = content_string.toCharArray();&lt;br /&gt;
         java.util.Arrays.sort(content_array);&lt;br /&gt;
         content_string=new String(content_array);&lt;br /&gt;
         System.out.println(content_string);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''Output:'''&amp;lt;br&amp;gt;&lt;br /&gt;
Abcdefghijklmnopqrstuvwxyz&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, the string object was converted to a character array to be able to use with the sort method.&lt;br /&gt;
&lt;br /&gt;
====Inheritance:====&lt;br /&gt;
Java interfaces can inherit other interfaces but Ruby modules cannot inherit other modules but can contain classes and methods.&lt;br /&gt;
 module Wrapper&lt;br /&gt;
  class Base&lt;br /&gt;
     # Class Body&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Java,&lt;br /&gt;
 public interface Dinasour extends Frog{&lt;br /&gt;
     //variables and method Declarations&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Space for a similarity&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===3.2 Tabular Comparison===&lt;br /&gt;
This is a general comparison:&lt;br /&gt;
====Mixins====&lt;br /&gt;
====Interfaces====&lt;br /&gt;
In Ruby, modules have the implementation of the methods. So all the classes which include the modules gets the same Implementation because classes create a reference to these modules.	In Java, all classes which implement the interfaces should provide implementation for each of the methods declared in the interface. This might duplicate the code if the two or more classes have similar functionality.&lt;br /&gt;
The size of the code is relatively smaller because the classes automatically get access to methods defined in the modules. Modules server as a central repository	The size is relatively larger because a class implementing more than one interfaces has to provide implementation.&lt;br /&gt;
Having more modules in the classes can make code less readable. Ideally mixins are suitable for small teams having few modules.	The code is more readable here as the implementation is provided to each method and can be found in place&lt;br /&gt;
In Ruby, a method inside a module can have Module name as qualifier. Hence it’s possible for a class to have inherit two or more modules which have same names.	In Java, the class itself has to provide the implementation of the methods. Inheriting methods of same names from different interfaces will signal an error message.&lt;br /&gt;
Modules cannot be inherited and cannot form is-a hierarchy	Interfaces can extend another interface and hence can form is-a hierarchy&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===3.3 Drawbacks===&lt;br /&gt;
The drawbacks of the technique of Mixins are very much debated. Though Mixins provide us with an easy way to write flexible and decoupled code and also help us solve the diamond problem, they pose their own problems. In large programs there could be large number of modules and each module could have loads of methods each performing a certain task. To trace the origin of the methods and to keep in mind the hierarchy is practically impossible. Another issue associated with Mixins pose is silent method overriding. Silent method overriding occurs when different modules are included in a class and each module have a method with the same definition. In such a case, the method which gets executed depends upon the way it is included in the class and this is done without any sort of message to the user (silently). In large scale applications this could certainly be an issue considering the number of modules that would be mixed in and it would be difficult to keep track of the hierarchy in which the modules were included.&lt;br /&gt;
Although Java provides polymorphic behavior with the use of interfaces, they sometimes tend to be very slow. The implementation of interfaces is also limited to public methods and constants with no implementation.&lt;br /&gt;
&lt;br /&gt;
==4. Conclusion==&lt;br /&gt;
&lt;br /&gt;
==5. References==&lt;br /&gt;
==6. See Also==&lt;/div&gt;</summary>
		<author><name>Rgovind2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012&amp;diff=63607</id>
		<title>CSC/ECE 517 Fall 2012</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012&amp;diff=63607"/>
		<updated>2012-09-11T23:35:08Z</updated>

		<summary type="html">&lt;p&gt;Rgovind2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;*[[CSC/ECE 517 Fall 2012/ch1 n xx]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w1 rk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w20 pp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w5 su]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w6 pp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w7 am]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w8 aa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w10 pk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w14 gv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w17 ir]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w18 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w22 an]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w21 aa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w21 wi]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w31 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w16 br]]&lt;/div&gt;</summary>
		<author><name>Rgovind2</name></author>
	</entry>
</feed>