<?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=Sthirum</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=Sthirum"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Sthirum"/>
	<updated>2026-05-11T01:51:50Z</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_2011/ch3_4b_ms&amp;diff=53194</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b ms</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53194"/>
		<updated>2011-10-20T19:52:01Z</updated>

		<summary type="html">&lt;p&gt;Sthirum: /* Duck Typing in Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Lecture 5: Object Oriented Programming in Ruby =&lt;br /&gt;
&lt;br /&gt;
This wiki tries to cover the Object Oriented concepts in ruby.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Lecture 5 &amp;lt;ref name=&amp;quot;CSC517&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517//common/lectures/notes/lec5.pdf OOP in Ruby], CSC 517 Lecture5&amp;lt;/ref&amp;gt; of CSC 517 covered topics of [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented programming] in [http://www.ruby-lang.org/en/ Ruby]. Object oriented programming is a paradigm in which real world entities are represented as objects, where each object has its own attributes and set of functions that it can perform. This gives a data structure called an &amp;quot;object&amp;quot; that consists of data fields and operations. This paradigm presents features like [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) encapsulation], [http://en.wikipedia.org/wiki/Data_abstraction data abstraction], [http://en.wikipedia.org/wiki/Inheritance_(computer_science) inheritance] and [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This wiki page tries to cover the other OOP languages and their features in a succinct manner and talks about the features of Ruby in detail with examples. We have also tried to compare the various languages in terms of their features to provide a better understanding.&lt;br /&gt;
&lt;br /&gt;
The lecture also covered the concepts of [http://en.wikipedia.org/wiki/Duck_typing duck typing] in Ruby, closures and currying. The latter two have not been included as per the instructions by the instructor.&lt;br /&gt;
&lt;br /&gt;
== Object oriented languages: An Overview ==&lt;br /&gt;
&lt;br /&gt;
==== C++: ====&lt;br /&gt;
[http://en.wikipedia.org/wiki/C%2B%2B C++] is a [http://en.wikipedia.org/wiki/Type_system#Static_typing statically typed], multi-paradigm programming language. It supports some features of object-oriented languages like encapsulation, polymorphism and inheritance. It provides access control by specifiers public, private and protected and also allows “friend” classes. C++  allows [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance] which is eliminated in most other programming languages as it allows a class to inherit from more than one base class which results in an ambiguity effect called [http://en.wikipedia.org/wiki/Diamond_problem Diamond problem].&lt;br /&gt;
&lt;br /&gt;
==== Java: ====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Java_(programming_language) Java] is classified as a statically typed, hybrid object oriented language. Although it provides information hiding, inheritance and polymorphism, it has a few basic types incorporated that are not objects and some of the built-in operators are provided by basic arithmetic and not as messages to objects.[http://www.jvoegele.com/software/langcomp.html] For these reasons, it is not considered to be a “pure” object-oriented language.&lt;br /&gt;
&lt;br /&gt;
Java provides access control and supports private, public and protected access. It also allows to create packages to create and maintain namespaces. Java is designed not to provide multiple inheritance to avoid problems faced in C++. However, it presents a single-rooted hierarchy where “Object” is the ancestor of all classes (seen in Ruby too) and provides interfaces to extend/inherit functionality.&lt;br /&gt;
&lt;br /&gt;
==== Python: ====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Python_(programming_language) Python] is a [http://en.wikipedia.org/wiki/Dynamic_programming_language dynamically typed], interpreted, object oriented language. It is argued that Python is not completely object oriented as it does not provide access control in the form of public, private or protected variables. It has different security mechanisms. However, it allows to emulate protected member by using the prefix '_' before the name and private member by the use of the prefix '__' (double underscore) before the name.&lt;br /&gt;
&lt;br /&gt;
==== Ruby: ====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_language Ruby] is dynamically typed and highly object-oriented, much more than Java and Python. In Ruby, it is said that “everything is an object”.  Every value happens to be an object in Ruby. You may call methods on a string, an integer just like you call on any object in Java. For ex,&lt;br /&gt;
&lt;br /&gt;
     (2.1143).size.times{puts “Hello!”}&lt;br /&gt;
&lt;br /&gt;
This example will display “Hello!” six times on your IRB as the size of the parameter &amp;quot;2.1143&amp;quot; is 6.&lt;br /&gt;
&lt;br /&gt;
== Comparison of object Oriented Languages ==&lt;br /&gt;
The following table compares the Properties of some of the well-known Object Oriented Languages.&amp;lt;ref name=&amp;quot;comparisonOOP&amp;quot;&amp;gt;[http://www.jvoegele.com/software/langcomp.html Programming Language Comparison]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 9pad case0%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Language&lt;br /&gt;
!Ruby&lt;br /&gt;
!Java&lt;br /&gt;
!C#&lt;br /&gt;
!C++&lt;br /&gt;
!Python&lt;br /&gt;
!SmallTalk&lt;br /&gt;
!Perl&lt;br /&gt;
|-&lt;br /&gt;
!Object Orientation&lt;br /&gt;
|Pure&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid/Multi-Paradigm&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Pure&lt;br /&gt;
|Add-on/Hybrid&lt;br /&gt;
|-&lt;br /&gt;
!Static/Dynamic Typing&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|-&lt;br /&gt;
!Generic Classes&lt;br /&gt;
|N/A&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|-&lt;br /&gt;
!Inheritance&lt;br /&gt;
|Single Class/Multiple Mixins&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Multiple&lt;br /&gt;
|Multiple&lt;br /&gt;
|Single&lt;br /&gt;
|Multiple&lt;br /&gt;
|- &lt;br /&gt;
!Feature Renaming&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|NO&lt;br /&gt;
|-&lt;br /&gt;
!Method Overloading&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Operator Overloading&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|-&lt;br /&gt;
!Class Variables or methods&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Access Control&lt;br /&gt;
|Public, Protected, Private&lt;br /&gt;
|Public, Protected, Package, Private&lt;br /&gt;
|Public, Protected, Private, Internal, Protected Internal&lt;br /&gt;
|Public, Protected, Private, Friends&lt;br /&gt;
|Name Mangling&lt;br /&gt;
|Protected Data, Public Methods&lt;br /&gt;
|None&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Ruby : Purely Object Oriented ==&lt;br /&gt;
&lt;br /&gt;
Ruby is considered to be a purely Object oriented Language, as everything in it is considered to be an object. Unlike Java, even primitives such as characters, punctuation, numbers and strings are treated as objects in Ruby. Ruby was designed to facilitate the enforcement of Object Oriented Methods. Ruby is a Dynamically Typed Language.&lt;br /&gt;
&lt;br /&gt;
=== Object oriented Concepts in ruby: ===&lt;br /&gt;
&lt;br /&gt;
Ruby is a singly rooted Object Oriented Programming language which has all its classes having the same superclass which is the class Object.&lt;br /&gt;
&lt;br /&gt;
==== Classes:====&lt;br /&gt;
[[Image:RubyClass.png|thumb|right|280px|'''''Classes in Ruby''''']]&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Class_(computer_programming) class] is used to define a blueprint of a data type. It does not contain any data, but it specifies what kind of variables and methods an object of the class will have.&lt;br /&gt;
A class in Ruby is defined using the keyword class followed by the classname. The variables and methods are defined within the class definition and the class definition terminates with the end keyword.&lt;br /&gt;
&lt;br /&gt;
For ex:&lt;br /&gt;
  class Rectangle&lt;br /&gt;
     def initialize(w,l)&lt;br /&gt;
        @width, @length = w, l&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
Here, the initialize method is used to set the values of the instance variables (@width and @length. The instance variables are the properties of the objects created of this class type. These instance variables can be accessed within the class using an @. These variables can be accessed from outside the class using the accessor methods which are defined inside the class.&lt;br /&gt;
&lt;br /&gt;
In the following case printLength and printWidth are public methods used to access the instance variables from outside the class. These are called accessor methods.&lt;br /&gt;
Similarly, to&lt;br /&gt;
&lt;br /&gt;
   class Rectangle&lt;br /&gt;
  # constructor method&lt;br /&gt;
  def initialize(w,l)&lt;br /&gt;
     @width, @length = w, l&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # accessor methods&lt;br /&gt;
  def printWidth&lt;br /&gt;
     @width&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def printLength&lt;br /&gt;
     @length&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # setter methods&lt;br /&gt;
  def setWidth=(value)&lt;br /&gt;
     @width = value&lt;br /&gt;
  end&lt;br /&gt;
  def setHeight=(value)&lt;br /&gt;
     @height = value&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   # create an object&lt;br /&gt;
     rectagle = Rectangle.new(10, 20)&lt;br /&gt;
&lt;br /&gt;
    # use accessor methods&lt;br /&gt;
      x = rectangle.printWidth()&lt;br /&gt;
      y = rectangle.printLength()&lt;br /&gt;
&lt;br /&gt;
      puts &amp;quot;Width of the Rectangle is : #{x}&amp;quot;&lt;br /&gt;
      puts &amp;quot;Length of the Rectangle is : #{y}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==== Class Methods and Class Variables:====&lt;br /&gt;
A class variable is shared between all instances of the class. The value of the class variable remains the same for all the objects of the class. A class variable is accessed within the class using two @ signs at the beginning of the variable name. The class variables must be initialized within the class.&lt;br /&gt;
&lt;br /&gt;
The class methods are defined using def self.methodname() and ends with an end delimiter. These methods are called using the class name itself, for example: classname.methodname&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Attributes and accessor methods: ===&lt;br /&gt;
&lt;br /&gt;
Every class in Ruby defines a set of attributes for its objects. Ruby provides accessor methods (famously known as getter-setter methods in Java) to access these variables. Ruby provides three kinds of accessor methods:&lt;br /&gt;
&lt;br /&gt;
    Class Car&lt;br /&gt;
       attr_accessor : name, company&lt;br /&gt;
       attr_reader: price&lt;br /&gt;
       attr_writer: color&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
attr_accessor creates a setter and getter method. The attr_reader method creates only the getter method. This is required in case of attributes that are not to be modified by other programs but their value may be viewed. The attr_writer creates the setter method. In case of Java, the setter and getter methods need to be written in 3 lines of code each whereas in Ruby, the methods are written just by specifying the type of accessor, which have already been coded using [http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Access Control:====&lt;br /&gt;
The only easy way to change an object's state in Ruby is by calling one of its methods. Control access to the methods, and you have controlled access to the object. A good rule of the thumb is never to expose methods that could leave an object in an invalid state.&lt;br /&gt;
&lt;br /&gt;
Ruby gives you three levels of protection:&lt;br /&gt;
&lt;br /&gt;
''Public methods'' can be called by everyone - no access control is enforced. A class's instance methods (these do not belong only to one object; instead, every instance of the class can call them) are public by default; anyone can call them. The initialize method is always private.&lt;br /&gt;
&lt;br /&gt;
''Protected methods'' can be invoked only by objects of the defining class and its subclasses. Access is kept within the family. However, usage of protected is limited.&lt;br /&gt;
&lt;br /&gt;
''Private methods'' cannot be called with an explicit receiver - the receiver is always self. This means that private methods can be called only in the context of the current object; you cannot invoke another object's private methods.&lt;br /&gt;
&lt;br /&gt;
Access control is determined dynamically, as the program runs, not statically. You will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
Ex:&lt;br /&gt;
&lt;br /&gt;
    class Employee&lt;br /&gt;
       def initialize(name, age, salary)&lt;br /&gt;
           @name = name&lt;br /&gt;
           @age = age&lt;br /&gt;
           @salary = salary&lt;br /&gt;
       end&lt;br /&gt;
  &lt;br /&gt;
       def name&lt;br /&gt;
           @name&lt;br /&gt;
       end &lt;br /&gt;
       def age&lt;br /&gt;
          @age&lt;br /&gt;
       end&lt;br /&gt;
       def salary&lt;br /&gt;
           @salary&lt;br /&gt;
       end&lt;br /&gt;
&lt;br /&gt;
        public :name&lt;br /&gt;
        protected :age&lt;br /&gt;
        private :salary&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      emp = Employee.new('Jack', 25, 2000);&lt;br /&gt;
      puts emp.name&lt;br /&gt;
      #puts emp.age    #age protected. gives access violation error&lt;br /&gt;
      #puts emp.salary #salary private&lt;br /&gt;
&lt;br /&gt;
====Abstract methods====&lt;br /&gt;
&lt;br /&gt;
Ruby is a dynamically typed language and does not provide abstract classes as in Java or C++. It instead provides modules that can be included within any class. The reason why Ruby does not provide abstract classes is because of the idea that it only matters if two objects can perform a function, they don’t necessarily have to be inherited from the same class. This paradigm is provided by modules, where the method of a module can be redefined as required by a class that includes it.&lt;br /&gt;
&lt;br /&gt;
Also, if required the technique of abstract class can be simulated by something like this:&lt;br /&gt;
&lt;br /&gt;
  Class Vehicle&lt;br /&gt;
   def drive&lt;br /&gt;
   raise NotImplementedError.new(&amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The subclasses that inherit from the above class will have to redefine the method “drive” as required.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Ruby ===&lt;br /&gt;
&lt;br /&gt;
Ruby provides unbounded polymorphism as a dynamically typed language through the concept of [http://en.wikipedia.org/wiki/Duck_typing#In_Ruby “Duck-typing”]. “Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface” &amp;lt;ref name=&amp;quot;Duck typing&amp;quot;&amp;gt;[http://en.wikipedia.org/wiki/Duck_typing DuckTyping]&amp;lt;/ref&amp;gt;. This concept is explained by the famous definition as “When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.” This essentially means that in languages like Ruby, it is only checked if an object can implement a function and the type of the object is not taken into consideration. It can be explained better with the help of the following example&lt;br /&gt;
&lt;br /&gt;
  class Lion&lt;br /&gt;
    def roar&lt;br /&gt;
        puts “The Lion roars when hungry”&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
     def roar&lt;br /&gt;
        puts “The man roared in fury and anger”&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   def quality lion&lt;br /&gt;
    lion.roar&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def start&lt;br /&gt;
    leo = Lion.new&lt;br /&gt;
    jack = Person.new&lt;br /&gt;
    quality leo&lt;br /&gt;
    quality jack&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   start&lt;br /&gt;
&lt;br /&gt;
In the above example, leo is an object of class “Lion” and the “roar” method defined in its class is invoked. jack is an object of class “Person” and the corresponding method defined in the Person class is called for him. Although in the real world, both objects are not of the same type, Ruby allows unbounded polymorphism as both of them implement a particular aspect.&lt;br /&gt;
&lt;br /&gt;
Thus, the check for an invoked method is done at run-time in dynamically typed languages and the method defined on that object type is called. If the method is undefined, a “NoMethodError” exception is raised.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Python===&lt;br /&gt;
&lt;br /&gt;
Python also heavily uses duck typing to allow flexibility to programmers. It emphasizes on the interface rather than on the specifc type which allows for polymorphic substitution&amp;lt;ref&amp;gt;[http://docs.python.org/glossary.html#term-duck-typing Python Duck-typing]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
In Python, functions like [http://docs.python.org/library/functions.html#type type()] or [http://docs.python.org/library/functions.html#isinstance isinstance()] is used to check for usual methods to match the type of find out if an object is an instance of a particular class. However, in case of duck typing, the focus is on the attribute of that object, so it checks for the presence of a particular attribute in that object using [http://docs.python.org/library/functions.html#hasattr hasattr()].&lt;br /&gt;
&lt;br /&gt;
Example for duck typing in Python is where Python uses a [http://docs.python.org/library/pickle.html “pickle”] module to serialize and de-serialize an object. So, we can introduce a new class that creates a “Tape” object. Now, an object of this type can be passed to the pickle module. Python will not differentiate between a file object or a tape object as long as they both have read() and write() methods.&amp;lt;ref name=&amp;quot;example&amp;quot;&amp;gt;[http://www.remyroy.com/2008/06/01/duck-typing-and-python/ Ducktyping example in Python]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   class Tapefile():&lt;br /&gt;
      “A tape file”&lt;br /&gt;
       def __init__(self,params):&lt;br /&gt;
          #various initialization for tape access&lt;br /&gt;
       pass&lt;br /&gt;
       def read(len):&lt;br /&gt;
          #use special methods to access tape&lt;br /&gt;
       pass&lt;br /&gt;
       def readline():&lt;br /&gt;
          #use special methods to read a whole line on tape&lt;br /&gt;
       pass&lt;br /&gt;
       def write(value):&lt;br /&gt;
          #use special methods to write on tape&lt;br /&gt;
       pass&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in other languages===&lt;br /&gt;
&lt;br /&gt;
Duck typing is considered to be a feature provided by dynamic languages. C++ as a static language checks for methods by parameter and signature types. Java does not support duck typing either but it is argued that it can be simulated by the use of interfaces. The concept of duck-typing is criticized as it requires the programmer to have a very wide understanding of the source code and that two objects may implement a method in completely different contexts and so it may result in strange errors. &lt;br /&gt;
&lt;br /&gt;
Although duck typing is considered to be a feature of dynamic languages, some static languages like C# and [http://boo.codehaus.org/Duck+Typing Boo] support it by allowing type checking of classes by the compiler to occur at run time.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sthirum</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53193</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b ms</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53193"/>
		<updated>2011-10-20T19:51:09Z</updated>

		<summary type="html">&lt;p&gt;Sthirum: /* Duck Typing in Python */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Lecture 5: Object Oriented Programming in Ruby =&lt;br /&gt;
&lt;br /&gt;
This wiki tries to cover the Object Oriented concepts in ruby.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Lecture 5 &amp;lt;ref name=&amp;quot;CSC517&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517//common/lectures/notes/lec5.pdf OOP in Ruby], CSC 517 Lecture5&amp;lt;/ref&amp;gt; of CSC 517 covered topics of [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented programming] in [http://www.ruby-lang.org/en/ Ruby]. Object oriented programming is a paradigm in which real world entities are represented as objects, where each object has its own attributes and set of functions that it can perform. This gives a data structure called an &amp;quot;object&amp;quot; that consists of data fields and operations. This paradigm presents features like [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) encapsulation], [http://en.wikipedia.org/wiki/Data_abstraction data abstraction], [http://en.wikipedia.org/wiki/Inheritance_(computer_science) inheritance] and [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This wiki page tries to cover the other OOP languages and their features in a succinct manner and talks about the features of Ruby in detail with examples. We have also tried to compare the various languages in terms of their features to provide a better understanding.&lt;br /&gt;
&lt;br /&gt;
The lecture also covered the concepts of [http://en.wikipedia.org/wiki/Duck_typing duck typing] in Ruby, closures and currying. The latter two have not been included as per the instructions by the instructor.&lt;br /&gt;
&lt;br /&gt;
== Object oriented languages: An Overview ==&lt;br /&gt;
&lt;br /&gt;
==== C++: ====&lt;br /&gt;
[http://en.wikipedia.org/wiki/C%2B%2B C++] is a [http://en.wikipedia.org/wiki/Type_system#Static_typing statically typed], multi-paradigm programming language. It supports some features of object-oriented languages like encapsulation, polymorphism and inheritance. It provides access control by specifiers public, private and protected and also allows “friend” classes. C++  allows [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance] which is eliminated in most other programming languages as it allows a class to inherit from more than one base class which results in an ambiguity effect called [http://en.wikipedia.org/wiki/Diamond_problem Diamond problem].&lt;br /&gt;
&lt;br /&gt;
==== Java: ====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Java_(programming_language) Java] is classified as a statically typed, hybrid object oriented language. Although it provides information hiding, inheritance and polymorphism, it has a few basic types incorporated that are not objects and some of the built-in operators are provided by basic arithmetic and not as messages to objects.[http://www.jvoegele.com/software/langcomp.html] For these reasons, it is not considered to be a “pure” object-oriented language.&lt;br /&gt;
&lt;br /&gt;
Java provides access control and supports private, public and protected access. It also allows to create packages to create and maintain namespaces. Java is designed not to provide multiple inheritance to avoid problems faced in C++. However, it presents a single-rooted hierarchy where “Object” is the ancestor of all classes (seen in Ruby too) and provides interfaces to extend/inherit functionality.&lt;br /&gt;
&lt;br /&gt;
==== Python: ====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Python_(programming_language) Python] is a [http://en.wikipedia.org/wiki/Dynamic_programming_language dynamically typed], interpreted, object oriented language. It is argued that Python is not completely object oriented as it does not provide access control in the form of public, private or protected variables. It has different security mechanisms. However, it allows to emulate protected member by using the prefix '_' before the name and private member by the use of the prefix '__' (double underscore) before the name.&lt;br /&gt;
&lt;br /&gt;
==== Ruby: ====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_language Ruby] is dynamically typed and highly object-oriented, much more than Java and Python. In Ruby, it is said that “everything is an object”.  Every value happens to be an object in Ruby. You may call methods on a string, an integer just like you call on any object in Java. For ex,&lt;br /&gt;
&lt;br /&gt;
     (2.1143).size.times{puts “Hello!”}&lt;br /&gt;
&lt;br /&gt;
This example will display “Hello!” six times on your IRB as the size of the parameter &amp;quot;2.1143&amp;quot; is 6.&lt;br /&gt;
&lt;br /&gt;
== Comparison of object Oriented Languages ==&lt;br /&gt;
The following table compares the Properties of some of the well-known Object Oriented Languages.&amp;lt;ref name=&amp;quot;comparisonOOP&amp;quot;&amp;gt;[http://www.jvoegele.com/software/langcomp.html Programming Language Comparison]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 9pad case0%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Language&lt;br /&gt;
!Ruby&lt;br /&gt;
!Java&lt;br /&gt;
!C#&lt;br /&gt;
!C++&lt;br /&gt;
!Python&lt;br /&gt;
!SmallTalk&lt;br /&gt;
!Perl&lt;br /&gt;
|-&lt;br /&gt;
!Object Orientation&lt;br /&gt;
|Pure&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid/Multi-Paradigm&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Pure&lt;br /&gt;
|Add-on/Hybrid&lt;br /&gt;
|-&lt;br /&gt;
!Static/Dynamic Typing&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|-&lt;br /&gt;
!Generic Classes&lt;br /&gt;
|N/A&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|-&lt;br /&gt;
!Inheritance&lt;br /&gt;
|Single Class/Multiple Mixins&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Multiple&lt;br /&gt;
|Multiple&lt;br /&gt;
|Single&lt;br /&gt;
|Multiple&lt;br /&gt;
|- &lt;br /&gt;
!Feature Renaming&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|NO&lt;br /&gt;
|-&lt;br /&gt;
!Method Overloading&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Operator Overloading&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|-&lt;br /&gt;
!Class Variables or methods&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Access Control&lt;br /&gt;
|Public, Protected, Private&lt;br /&gt;
|Public, Protected, Package, Private&lt;br /&gt;
|Public, Protected, Private, Internal, Protected Internal&lt;br /&gt;
|Public, Protected, Private, Friends&lt;br /&gt;
|Name Mangling&lt;br /&gt;
|Protected Data, Public Methods&lt;br /&gt;
|None&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Ruby : Purely Object Oriented ==&lt;br /&gt;
&lt;br /&gt;
Ruby is considered to be a purely Object oriented Language, as everything in it is considered to be an object. Unlike Java, even primitives such as characters, punctuation, numbers and strings are treated as objects in Ruby. Ruby was designed to facilitate the enforcement of Object Oriented Methods. Ruby is a Dynamically Typed Language.&lt;br /&gt;
&lt;br /&gt;
=== Object oriented Concepts in ruby: ===&lt;br /&gt;
&lt;br /&gt;
Ruby is a singly rooted Object Oriented Programming language which has all its classes having the same superclass which is the class Object.&lt;br /&gt;
&lt;br /&gt;
==== Classes:====&lt;br /&gt;
[[Image:RubyClass.png|thumb|right|280px|'''''Classes in Ruby''''']]&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Class_(computer_programming) class] is used to define a blueprint of a data type. It does not contain any data, but it specifies what kind of variables and methods an object of the class will have.&lt;br /&gt;
A class in Ruby is defined using the keyword class followed by the classname. The variables and methods are defined within the class definition and the class definition terminates with the end keyword.&lt;br /&gt;
&lt;br /&gt;
For ex:&lt;br /&gt;
  class Rectangle&lt;br /&gt;
     def initialize(w,l)&lt;br /&gt;
        @width, @length = w, l&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
Here, the initialize method is used to set the values of the instance variables (@width and @length. The instance variables are the properties of the objects created of this class type. These instance variables can be accessed within the class using an @. These variables can be accessed from outside the class using the accessor methods which are defined inside the class.&lt;br /&gt;
&lt;br /&gt;
In the following case printLength and printWidth are public methods used to access the instance variables from outside the class. These are called accessor methods.&lt;br /&gt;
Similarly, to&lt;br /&gt;
&lt;br /&gt;
   class Rectangle&lt;br /&gt;
  # constructor method&lt;br /&gt;
  def initialize(w,l)&lt;br /&gt;
     @width, @length = w, l&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # accessor methods&lt;br /&gt;
  def printWidth&lt;br /&gt;
     @width&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def printLength&lt;br /&gt;
     @length&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # setter methods&lt;br /&gt;
  def setWidth=(value)&lt;br /&gt;
     @width = value&lt;br /&gt;
  end&lt;br /&gt;
  def setHeight=(value)&lt;br /&gt;
     @height = value&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   # create an object&lt;br /&gt;
     rectagle = Rectangle.new(10, 20)&lt;br /&gt;
&lt;br /&gt;
    # use accessor methods&lt;br /&gt;
      x = rectangle.printWidth()&lt;br /&gt;
      y = rectangle.printLength()&lt;br /&gt;
&lt;br /&gt;
      puts &amp;quot;Width of the Rectangle is : #{x}&amp;quot;&lt;br /&gt;
      puts &amp;quot;Length of the Rectangle is : #{y}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==== Class Methods and Class Variables:====&lt;br /&gt;
A class variable is shared between all instances of the class. The value of the class variable remains the same for all the objects of the class. A class variable is accessed within the class using two @ signs at the beginning of the variable name. The class variables must be initialized within the class.&lt;br /&gt;
&lt;br /&gt;
The class methods are defined using def self.methodname() and ends with an end delimiter. These methods are called using the class name itself, for example: classname.methodname&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Attributes and accessor methods: ===&lt;br /&gt;
&lt;br /&gt;
Every class in Ruby defines a set of attributes for its objects. Ruby provides accessor methods (famously known as getter-setter methods in Java) to access these variables. Ruby provides three kinds of accessor methods:&lt;br /&gt;
&lt;br /&gt;
    Class Car&lt;br /&gt;
       attr_accessor : name, company&lt;br /&gt;
       attr_reader: price&lt;br /&gt;
       attr_writer: color&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
attr_accessor creates a setter and getter method. The attr_reader method creates only the getter method. This is required in case of attributes that are not to be modified by other programs but their value may be viewed. The attr_writer creates the setter method. In case of Java, the setter and getter methods need to be written in 3 lines of code each whereas in Ruby, the methods are written just by specifying the type of accessor, which have already been coded using [http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Access Control:====&lt;br /&gt;
The only easy way to change an object's state in Ruby is by calling one of its methods. Control access to the methods, and you have controlled access to the object. A good rule of the thumb is never to expose methods that could leave an object in an invalid state.&lt;br /&gt;
&lt;br /&gt;
Ruby gives you three levels of protection:&lt;br /&gt;
&lt;br /&gt;
''Public methods'' can be called by everyone - no access control is enforced. A class's instance methods (these do not belong only to one object; instead, every instance of the class can call them) are public by default; anyone can call them. The initialize method is always private.&lt;br /&gt;
&lt;br /&gt;
''Protected methods'' can be invoked only by objects of the defining class and its subclasses. Access is kept within the family. However, usage of protected is limited.&lt;br /&gt;
&lt;br /&gt;
''Private methods'' cannot be called with an explicit receiver - the receiver is always self. This means that private methods can be called only in the context of the current object; you cannot invoke another object's private methods.&lt;br /&gt;
&lt;br /&gt;
Access control is determined dynamically, as the program runs, not statically. You will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
Ex:&lt;br /&gt;
&lt;br /&gt;
    class Employee&lt;br /&gt;
       def initialize(name, age, salary)&lt;br /&gt;
           @name = name&lt;br /&gt;
           @age = age&lt;br /&gt;
           @salary = salary&lt;br /&gt;
       end&lt;br /&gt;
  &lt;br /&gt;
       def name&lt;br /&gt;
           @name&lt;br /&gt;
       end &lt;br /&gt;
       def age&lt;br /&gt;
          @age&lt;br /&gt;
       end&lt;br /&gt;
       def salary&lt;br /&gt;
           @salary&lt;br /&gt;
       end&lt;br /&gt;
&lt;br /&gt;
        public :name&lt;br /&gt;
        protected :age&lt;br /&gt;
        private :salary&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      emp = Employee.new('Jack', 25, 2000);&lt;br /&gt;
      puts emp.name&lt;br /&gt;
      #puts emp.age    #age protected. gives access violation error&lt;br /&gt;
      #puts emp.salary #salary private&lt;br /&gt;
&lt;br /&gt;
====Abstract methods====&lt;br /&gt;
&lt;br /&gt;
Ruby is a dynamically typed language and does not provide abstract classes as in Java or C++. It instead provides modules that can be included within any class. The reason why Ruby does not provide abstract classes is because of the idea that it only matters if two objects can perform a function, they don’t necessarily have to be inherited from the same class. This paradigm is provided by modules, where the method of a module can be redefined as required by a class that includes it.&lt;br /&gt;
&lt;br /&gt;
Also, if required the technique of abstract class can be simulated by something like this:&lt;br /&gt;
&lt;br /&gt;
  Class Vehicle&lt;br /&gt;
   def drive&lt;br /&gt;
   raise NotImplementedError.new(&amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The subclasses that inherit from the above class will have to redefine the method “drive” as required.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Ruby ===&lt;br /&gt;
&lt;br /&gt;
Ruby provides unbounded polymorphism as a dynamically typed language through the concept of “Duck-typing”. “Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface” &amp;lt;ref name=&amp;quot;Duck typing&amp;quot;&amp;gt;[http://en.wikipedia.org/wiki/Duck_typing DuckTyping]&amp;lt;/ref&amp;gt;. This concept is explained by the famous definition as “When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.” This essentially means that in languages like Ruby, it is only checked if an object can implement a function and the type of the object is not taken into consideration. It can be explained better with the help of the following example&lt;br /&gt;
&lt;br /&gt;
  class Lion&lt;br /&gt;
    def roar&lt;br /&gt;
        puts “The Lion roars when hungry”&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
     def roar&lt;br /&gt;
        puts “The man roared in fury and anger”&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   def quality lion&lt;br /&gt;
    lion.roar&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def start&lt;br /&gt;
    leo = Lion.new&lt;br /&gt;
    jack = Person.new&lt;br /&gt;
    quality leo&lt;br /&gt;
    quality jack&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   start&lt;br /&gt;
&lt;br /&gt;
In the above example, leo is an object of class “Lion” and the “roar” method defined in its class is invoked. jack is an object of class “Person” and the corresponding method defined in the Person class is called for him. Although in the real world, both objects are not of the same type, Ruby allows unbounded polymorphism as both of them implement a particular aspect.&lt;br /&gt;
&lt;br /&gt;
Thus, the check for an invoked method is done at run-time in dynamically typed languages and the method defined on that object type is called. If the method is undefined, a “NoMethodError” exception is raised.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Python===&lt;br /&gt;
&lt;br /&gt;
Python also heavily uses duck typing to allow flexibility to programmers. It emphasizes on the interface rather than on the specifc type which allows for polymorphic substitution&amp;lt;ref&amp;gt;[http://docs.python.org/glossary.html#term-duck-typing Python Duck-typing]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
In Python, functions like [http://docs.python.org/library/functions.html#type type()] or [http://docs.python.org/library/functions.html#isinstance isinstance()] is used to check for usual methods to match the type of find out if an object is an instance of a particular class. However, in case of duck typing, the focus is on the attribute of that object, so it checks for the presence of a particular attribute in that object using [http://docs.python.org/library/functions.html#hasattr hasattr()].&lt;br /&gt;
&lt;br /&gt;
Example for duck typing in Python is where Python uses a [http://docs.python.org/library/pickle.html “pickle”] module to serialize and de-serialize an object. So, we can introduce a new class that creates a “Tape” object. Now, an object of this type can be passed to the pickle module. Python will not differentiate between a file object or a tape object as long as they both have read() and write() methods.&amp;lt;ref name=&amp;quot;example&amp;quot;&amp;gt;[http://www.remyroy.com/2008/06/01/duck-typing-and-python/ Ducktyping example in Python]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   class Tapefile():&lt;br /&gt;
      “A tape file”&lt;br /&gt;
       def __init__(self,params):&lt;br /&gt;
          #various initialization for tape access&lt;br /&gt;
       pass&lt;br /&gt;
       def read(len):&lt;br /&gt;
          #use special methods to access tape&lt;br /&gt;
       pass&lt;br /&gt;
       def readline():&lt;br /&gt;
          #use special methods to read a whole line on tape&lt;br /&gt;
       pass&lt;br /&gt;
       def write(value):&lt;br /&gt;
          #use special methods to write on tape&lt;br /&gt;
       pass&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in other languages===&lt;br /&gt;
&lt;br /&gt;
Duck typing is considered to be a feature provided by dynamic languages. C++ as a static language checks for methods by parameter and signature types. Java does not support duck typing either but it is argued that it can be simulated by the use of interfaces. The concept of duck-typing is criticized as it requires the programmer to have a very wide understanding of the source code and that two objects may implement a method in completely different contexts and so it may result in strange errors. &lt;br /&gt;
&lt;br /&gt;
Although duck typing is considered to be a feature of dynamic languages, some static languages like C# and [http://boo.codehaus.org/Duck+Typing Boo] support it by allowing type checking of classes by the compiler to occur at run time.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sthirum</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53192</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b ms</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53192"/>
		<updated>2011-10-20T19:49:59Z</updated>

		<summary type="html">&lt;p&gt;Sthirum: /* Duck Typing in other languages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Lecture 5: Object Oriented Programming in Ruby =&lt;br /&gt;
&lt;br /&gt;
This wiki tries to cover the Object Oriented concepts in ruby.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Lecture 5 &amp;lt;ref name=&amp;quot;CSC517&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517//common/lectures/notes/lec5.pdf OOP in Ruby], CSC 517 Lecture5&amp;lt;/ref&amp;gt; of CSC 517 covered topics of [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented programming] in [http://www.ruby-lang.org/en/ Ruby]. Object oriented programming is a paradigm in which real world entities are represented as objects, where each object has its own attributes and set of functions that it can perform. This gives a data structure called an &amp;quot;object&amp;quot; that consists of data fields and operations. This paradigm presents features like [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) encapsulation], [http://en.wikipedia.org/wiki/Data_abstraction data abstraction], [http://en.wikipedia.org/wiki/Inheritance_(computer_science) inheritance] and [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This wiki page tries to cover the other OOP languages and their features in a succinct manner and talks about the features of Ruby in detail with examples. We have also tried to compare the various languages in terms of their features to provide a better understanding.&lt;br /&gt;
&lt;br /&gt;
The lecture also covered the concepts of [http://en.wikipedia.org/wiki/Duck_typing duck typing] in Ruby, closures and currying. The latter two have not been included as per the instructions by the instructor.&lt;br /&gt;
&lt;br /&gt;
== Object oriented languages: An Overview ==&lt;br /&gt;
&lt;br /&gt;
==== C++: ====&lt;br /&gt;
[http://en.wikipedia.org/wiki/C%2B%2B C++] is a [http://en.wikipedia.org/wiki/Type_system#Static_typing statically typed], multi-paradigm programming language. It supports some features of object-oriented languages like encapsulation, polymorphism and inheritance. It provides access control by specifiers public, private and protected and also allows “friend” classes. C++  allows [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance] which is eliminated in most other programming languages as it allows a class to inherit from more than one base class which results in an ambiguity effect called [http://en.wikipedia.org/wiki/Diamond_problem Diamond problem].&lt;br /&gt;
&lt;br /&gt;
==== Java: ====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Java_(programming_language) Java] is classified as a statically typed, hybrid object oriented language. Although it provides information hiding, inheritance and polymorphism, it has a few basic types incorporated that are not objects and some of the built-in operators are provided by basic arithmetic and not as messages to objects.[http://www.jvoegele.com/software/langcomp.html] For these reasons, it is not considered to be a “pure” object-oriented language.&lt;br /&gt;
&lt;br /&gt;
Java provides access control and supports private, public and protected access. It also allows to create packages to create and maintain namespaces. Java is designed not to provide multiple inheritance to avoid problems faced in C++. However, it presents a single-rooted hierarchy where “Object” is the ancestor of all classes (seen in Ruby too) and provides interfaces to extend/inherit functionality.&lt;br /&gt;
&lt;br /&gt;
==== Python: ====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Python_(programming_language) Python] is a [http://en.wikipedia.org/wiki/Dynamic_programming_language dynamically typed], interpreted, object oriented language. It is argued that Python is not completely object oriented as it does not provide access control in the form of public, private or protected variables. It has different security mechanisms. However, it allows to emulate protected member by using the prefix '_' before the name and private member by the use of the prefix '__' (double underscore) before the name.&lt;br /&gt;
&lt;br /&gt;
==== Ruby: ====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_language Ruby] is dynamically typed and highly object-oriented, much more than Java and Python. In Ruby, it is said that “everything is an object”.  Every value happens to be an object in Ruby. You may call methods on a string, an integer just like you call on any object in Java. For ex,&lt;br /&gt;
&lt;br /&gt;
     (2.1143).size.times{puts “Hello!”}&lt;br /&gt;
&lt;br /&gt;
This example will display “Hello!” six times on your IRB as the size of the parameter &amp;quot;2.1143&amp;quot; is 6.&lt;br /&gt;
&lt;br /&gt;
== Comparison of object Oriented Languages ==&lt;br /&gt;
The following table compares the Properties of some of the well-known Object Oriented Languages.&amp;lt;ref name=&amp;quot;comparisonOOP&amp;quot;&amp;gt;[http://www.jvoegele.com/software/langcomp.html Programming Language Comparison]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 9pad case0%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Language&lt;br /&gt;
!Ruby&lt;br /&gt;
!Java&lt;br /&gt;
!C#&lt;br /&gt;
!C++&lt;br /&gt;
!Python&lt;br /&gt;
!SmallTalk&lt;br /&gt;
!Perl&lt;br /&gt;
|-&lt;br /&gt;
!Object Orientation&lt;br /&gt;
|Pure&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid/Multi-Paradigm&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Pure&lt;br /&gt;
|Add-on/Hybrid&lt;br /&gt;
|-&lt;br /&gt;
!Static/Dynamic Typing&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|-&lt;br /&gt;
!Generic Classes&lt;br /&gt;
|N/A&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|-&lt;br /&gt;
!Inheritance&lt;br /&gt;
|Single Class/Multiple Mixins&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Multiple&lt;br /&gt;
|Multiple&lt;br /&gt;
|Single&lt;br /&gt;
|Multiple&lt;br /&gt;
|- &lt;br /&gt;
!Feature Renaming&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|NO&lt;br /&gt;
|-&lt;br /&gt;
!Method Overloading&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Operator Overloading&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|-&lt;br /&gt;
!Class Variables or methods&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Access Control&lt;br /&gt;
|Public, Protected, Private&lt;br /&gt;
|Public, Protected, Package, Private&lt;br /&gt;
|Public, Protected, Private, Internal, Protected Internal&lt;br /&gt;
|Public, Protected, Private, Friends&lt;br /&gt;
|Name Mangling&lt;br /&gt;
|Protected Data, Public Methods&lt;br /&gt;
|None&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Ruby : Purely Object Oriented ==&lt;br /&gt;
&lt;br /&gt;
Ruby is considered to be a purely Object oriented Language, as everything in it is considered to be an object. Unlike Java, even primitives such as characters, punctuation, numbers and strings are treated as objects in Ruby. Ruby was designed to facilitate the enforcement of Object Oriented Methods. Ruby is a Dynamically Typed Language.&lt;br /&gt;
&lt;br /&gt;
=== Object oriented Concepts in ruby: ===&lt;br /&gt;
&lt;br /&gt;
Ruby is a singly rooted Object Oriented Programming language which has all its classes having the same superclass which is the class Object.&lt;br /&gt;
&lt;br /&gt;
==== Classes:====&lt;br /&gt;
[[Image:RubyClass.png|thumb|right|280px|'''''Classes in Ruby''''']]&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Class_(computer_programming) class] is used to define a blueprint of a data type. It does not contain any data, but it specifies what kind of variables and methods an object of the class will have.&lt;br /&gt;
A class in Ruby is defined using the keyword class followed by the classname. The variables and methods are defined within the class definition and the class definition terminates with the end keyword.&lt;br /&gt;
&lt;br /&gt;
For ex:&lt;br /&gt;
  class Rectangle&lt;br /&gt;
     def initialize(w,l)&lt;br /&gt;
        @width, @length = w, l&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
Here, the initialize method is used to set the values of the instance variables (@width and @length. The instance variables are the properties of the objects created of this class type. These instance variables can be accessed within the class using an @. These variables can be accessed from outside the class using the accessor methods which are defined inside the class.&lt;br /&gt;
&lt;br /&gt;
In the following case printLength and printWidth are public methods used to access the instance variables from outside the class. These are called accessor methods.&lt;br /&gt;
Similarly, to&lt;br /&gt;
&lt;br /&gt;
   class Rectangle&lt;br /&gt;
  # constructor method&lt;br /&gt;
  def initialize(w,l)&lt;br /&gt;
     @width, @length = w, l&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # accessor methods&lt;br /&gt;
  def printWidth&lt;br /&gt;
     @width&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def printLength&lt;br /&gt;
     @length&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # setter methods&lt;br /&gt;
  def setWidth=(value)&lt;br /&gt;
     @width = value&lt;br /&gt;
  end&lt;br /&gt;
  def setHeight=(value)&lt;br /&gt;
     @height = value&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   # create an object&lt;br /&gt;
     rectagle = Rectangle.new(10, 20)&lt;br /&gt;
&lt;br /&gt;
    # use accessor methods&lt;br /&gt;
      x = rectangle.printWidth()&lt;br /&gt;
      y = rectangle.printLength()&lt;br /&gt;
&lt;br /&gt;
      puts &amp;quot;Width of the Rectangle is : #{x}&amp;quot;&lt;br /&gt;
      puts &amp;quot;Length of the Rectangle is : #{y}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==== Class Methods and Class Variables:====&lt;br /&gt;
A class variable is shared between all instances of the class. The value of the class variable remains the same for all the objects of the class. A class variable is accessed within the class using two @ signs at the beginning of the variable name. The class variables must be initialized within the class.&lt;br /&gt;
&lt;br /&gt;
The class methods are defined using def self.methodname() and ends with an end delimiter. These methods are called using the class name itself, for example: classname.methodname&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Attributes and accessor methods: ===&lt;br /&gt;
&lt;br /&gt;
Every class in Ruby defines a set of attributes for its objects. Ruby provides accessor methods (famously known as getter-setter methods in Java) to access these variables. Ruby provides three kinds of accessor methods:&lt;br /&gt;
&lt;br /&gt;
    Class Car&lt;br /&gt;
       attr_accessor : name, company&lt;br /&gt;
       attr_reader: price&lt;br /&gt;
       attr_writer: color&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
attr_accessor creates a setter and getter method. The attr_reader method creates only the getter method. This is required in case of attributes that are not to be modified by other programs but their value may be viewed. The attr_writer creates the setter method. In case of Java, the setter and getter methods need to be written in 3 lines of code each whereas in Ruby, the methods are written just by specifying the type of accessor, which have already been coded using [http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Access Control:====&lt;br /&gt;
The only easy way to change an object's state in Ruby is by calling one of its methods. Control access to the methods, and you have controlled access to the object. A good rule of the thumb is never to expose methods that could leave an object in an invalid state.&lt;br /&gt;
&lt;br /&gt;
Ruby gives you three levels of protection:&lt;br /&gt;
&lt;br /&gt;
''Public methods'' can be called by everyone - no access control is enforced. A class's instance methods (these do not belong only to one object; instead, every instance of the class can call them) are public by default; anyone can call them. The initialize method is always private.&lt;br /&gt;
&lt;br /&gt;
''Protected methods'' can be invoked only by objects of the defining class and its subclasses. Access is kept within the family. However, usage of protected is limited.&lt;br /&gt;
&lt;br /&gt;
''Private methods'' cannot be called with an explicit receiver - the receiver is always self. This means that private methods can be called only in the context of the current object; you cannot invoke another object's private methods.&lt;br /&gt;
&lt;br /&gt;
Access control is determined dynamically, as the program runs, not statically. You will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
Ex:&lt;br /&gt;
&lt;br /&gt;
    class Employee&lt;br /&gt;
       def initialize(name, age, salary)&lt;br /&gt;
           @name = name&lt;br /&gt;
           @age = age&lt;br /&gt;
           @salary = salary&lt;br /&gt;
       end&lt;br /&gt;
  &lt;br /&gt;
       def name&lt;br /&gt;
           @name&lt;br /&gt;
       end &lt;br /&gt;
       def age&lt;br /&gt;
          @age&lt;br /&gt;
       end&lt;br /&gt;
       def salary&lt;br /&gt;
           @salary&lt;br /&gt;
       end&lt;br /&gt;
&lt;br /&gt;
        public :name&lt;br /&gt;
        protected :age&lt;br /&gt;
        private :salary&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      emp = Employee.new('Jack', 25, 2000);&lt;br /&gt;
      puts emp.name&lt;br /&gt;
      #puts emp.age    #age protected. gives access violation error&lt;br /&gt;
      #puts emp.salary #salary private&lt;br /&gt;
&lt;br /&gt;
====Abstract methods====&lt;br /&gt;
&lt;br /&gt;
Ruby is a dynamically typed language and does not provide abstract classes as in Java or C++. It instead provides modules that can be included within any class. The reason why Ruby does not provide abstract classes is because of the idea that it only matters if two objects can perform a function, they don’t necessarily have to be inherited from the same class. This paradigm is provided by modules, where the method of a module can be redefined as required by a class that includes it.&lt;br /&gt;
&lt;br /&gt;
Also, if required the technique of abstract class can be simulated by something like this:&lt;br /&gt;
&lt;br /&gt;
  Class Vehicle&lt;br /&gt;
   def drive&lt;br /&gt;
   raise NotImplementedError.new(&amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The subclasses that inherit from the above class will have to redefine the method “drive” as required.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Ruby ===&lt;br /&gt;
&lt;br /&gt;
Ruby provides unbounded polymorphism as a dynamically typed language through the concept of “Duck-typing”. “Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface” &amp;lt;ref name=&amp;quot;Duck typing&amp;quot;&amp;gt;[http://en.wikipedia.org/wiki/Duck_typing DuckTyping]&amp;lt;/ref&amp;gt;. This concept is explained by the famous definition as “When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.” This essentially means that in languages like Ruby, it is only checked if an object can implement a function and the type of the object is not taken into consideration. It can be explained better with the help of the following example&lt;br /&gt;
&lt;br /&gt;
  class Lion&lt;br /&gt;
    def roar&lt;br /&gt;
        puts “The Lion roars when hungry”&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
     def roar&lt;br /&gt;
        puts “The man roared in fury and anger”&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   def quality lion&lt;br /&gt;
    lion.roar&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def start&lt;br /&gt;
    leo = Lion.new&lt;br /&gt;
    jack = Person.new&lt;br /&gt;
    quality leo&lt;br /&gt;
    quality jack&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   start&lt;br /&gt;
&lt;br /&gt;
In the above example, leo is an object of class “Lion” and the “roar” method defined in its class is invoked. jack is an object of class “Person” and the corresponding method defined in the Person class is called for him. Although in the real world, both objects are not of the same type, Ruby allows unbounded polymorphism as both of them implement a particular aspect.&lt;br /&gt;
&lt;br /&gt;
Thus, the check for an invoked method is done at run-time in dynamically typed languages and the method defined on that object type is called. If the method is undefined, a “NoMethodError” exception is raised.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Python===&lt;br /&gt;
&lt;br /&gt;
Python also heavily uses duck typing to allow flexibility to programmers. It emphasizes on the interface rather than on the specifc type which allows for polymorphic substitution&amp;lt;ref&amp;gt;[http://docs.python.org/glossary.html#term-duck-typing Python Duck-typing]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
In Python, functions like [http://docs.python.org/library/functions.html#type type()] or [http://docs.python.org/library/functions.html#isinstance isinstance()] is used to check for usual methods to match the type of find out if an object is an instance of a particular class. However, in case of duck typing, the focus is on the attribute of that object, so it checks for the presence of a particular attribute in that object using [http://docs.python.org/library/functions.html#hasattr hasattr()].&lt;br /&gt;
&lt;br /&gt;
Example for duck typing in Python is where Python uses a [http://docs.python.org/library/pickle.html “pickle”] module to serialize and de-serialize an object. So, we can introduce a new class that creates a “Tape” object. Now, an object of this type can be passed to the pickle module. Python will not differentiate between a file object or a tape object as long as they both have read() and write() methods.&lt;br /&gt;
&lt;br /&gt;
   class Tapefile():&lt;br /&gt;
      “A tape file”&lt;br /&gt;
       def __init__(self,params):&lt;br /&gt;
          #various initialization for tape access&lt;br /&gt;
       pass&lt;br /&gt;
       def read(len):&lt;br /&gt;
          #use special methods to access tape&lt;br /&gt;
       pass&lt;br /&gt;
       def readline():&lt;br /&gt;
          #use special methods to read a whole line on tape&lt;br /&gt;
       pass&lt;br /&gt;
       def write(value):&lt;br /&gt;
          #use special methods to write on tape&lt;br /&gt;
       pass&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in other languages===&lt;br /&gt;
&lt;br /&gt;
Duck typing is considered to be a feature provided by dynamic languages. C++ as a static language checks for methods by parameter and signature types. Java does not support duck typing either but it is argued that it can be simulated by the use of interfaces. The concept of duck-typing is criticized as it requires the programmer to have a very wide understanding of the source code and that two objects may implement a method in completely different contexts and so it may result in strange errors. &lt;br /&gt;
&lt;br /&gt;
Although duck typing is considered to be a feature of dynamic languages, some static languages like C# and [http://boo.codehaus.org/Duck+Typing Boo] support it by allowing type checking of classes by the compiler to occur at run time.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sthirum</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53191</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b ms</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53191"/>
		<updated>2011-10-20T19:46:22Z</updated>

		<summary type="html">&lt;p&gt;Sthirum: /* Object oriented languages: An Overview */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Lecture 5: Object Oriented Programming in Ruby =&lt;br /&gt;
&lt;br /&gt;
This wiki tries to cover the Object Oriented concepts in ruby.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Lecture 5 &amp;lt;ref name=&amp;quot;CSC517&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517//common/lectures/notes/lec5.pdf OOP in Ruby], CSC 517 Lecture5&amp;lt;/ref&amp;gt; of CSC 517 covered topics of [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented programming] in [http://www.ruby-lang.org/en/ Ruby]. Object oriented programming is a paradigm in which real world entities are represented as objects, where each object has its own attributes and set of functions that it can perform. This gives a data structure called an &amp;quot;object&amp;quot; that consists of data fields and operations. This paradigm presents features like [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) encapsulation], [http://en.wikipedia.org/wiki/Data_abstraction data abstraction], [http://en.wikipedia.org/wiki/Inheritance_(computer_science) inheritance] and [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This wiki page tries to cover the other OOP languages and their features in a succinct manner and talks about the features of Ruby in detail with examples. We have also tried to compare the various languages in terms of their features to provide a better understanding.&lt;br /&gt;
&lt;br /&gt;
The lecture also covered the concepts of [http://en.wikipedia.org/wiki/Duck_typing duck typing] in Ruby, closures and currying. The latter two have not been included as per the instructions by the instructor.&lt;br /&gt;
&lt;br /&gt;
== Object oriented languages: An Overview ==&lt;br /&gt;
&lt;br /&gt;
==== C++: ====&lt;br /&gt;
[http://en.wikipedia.org/wiki/C%2B%2B C++] is a [http://en.wikipedia.org/wiki/Type_system#Static_typing statically typed], multi-paradigm programming language. It supports some features of object-oriented languages like encapsulation, polymorphism and inheritance. It provides access control by specifiers public, private and protected and also allows “friend” classes. C++  allows [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance] which is eliminated in most other programming languages as it allows a class to inherit from more than one base class which results in an ambiguity effect called [http://en.wikipedia.org/wiki/Diamond_problem Diamond problem].&lt;br /&gt;
&lt;br /&gt;
==== Java: ====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Java_(programming_language) Java] is classified as a statically typed, hybrid object oriented language. Although it provides information hiding, inheritance and polymorphism, it has a few basic types incorporated that are not objects and some of the built-in operators are provided by basic arithmetic and not as messages to objects.[http://www.jvoegele.com/software/langcomp.html] For these reasons, it is not considered to be a “pure” object-oriented language.&lt;br /&gt;
&lt;br /&gt;
Java provides access control and supports private, public and protected access. It also allows to create packages to create and maintain namespaces. Java is designed not to provide multiple inheritance to avoid problems faced in C++. However, it presents a single-rooted hierarchy where “Object” is the ancestor of all classes (seen in Ruby too) and provides interfaces to extend/inherit functionality.&lt;br /&gt;
&lt;br /&gt;
==== Python: ====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Python_(programming_language) Python] is a [http://en.wikipedia.org/wiki/Dynamic_programming_language dynamically typed], interpreted, object oriented language. It is argued that Python is not completely object oriented as it does not provide access control in the form of public, private or protected variables. It has different security mechanisms. However, it allows to emulate protected member by using the prefix '_' before the name and private member by the use of the prefix '__' (double underscore) before the name.&lt;br /&gt;
&lt;br /&gt;
==== Ruby: ====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_language Ruby] is dynamically typed and highly object-oriented, much more than Java and Python. In Ruby, it is said that “everything is an object”.  Every value happens to be an object in Ruby. You may call methods on a string, an integer just like you call on any object in Java. For ex,&lt;br /&gt;
&lt;br /&gt;
     (2.1143).size.times{puts “Hello!”}&lt;br /&gt;
&lt;br /&gt;
This example will display “Hello!” six times on your IRB as the size of the parameter &amp;quot;2.1143&amp;quot; is 6.&lt;br /&gt;
&lt;br /&gt;
== Comparison of object Oriented Languages ==&lt;br /&gt;
The following table compares the Properties of some of the well-known Object Oriented Languages.&amp;lt;ref name=&amp;quot;comparisonOOP&amp;quot;&amp;gt;[http://www.jvoegele.com/software/langcomp.html Programming Language Comparison]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 9pad case0%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Language&lt;br /&gt;
!Ruby&lt;br /&gt;
!Java&lt;br /&gt;
!C#&lt;br /&gt;
!C++&lt;br /&gt;
!Python&lt;br /&gt;
!SmallTalk&lt;br /&gt;
!Perl&lt;br /&gt;
|-&lt;br /&gt;
!Object Orientation&lt;br /&gt;
|Pure&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid/Multi-Paradigm&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Pure&lt;br /&gt;
|Add-on/Hybrid&lt;br /&gt;
|-&lt;br /&gt;
!Static/Dynamic Typing&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|-&lt;br /&gt;
!Generic Classes&lt;br /&gt;
|N/A&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|-&lt;br /&gt;
!Inheritance&lt;br /&gt;
|Single Class/Multiple Mixins&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Multiple&lt;br /&gt;
|Multiple&lt;br /&gt;
|Single&lt;br /&gt;
|Multiple&lt;br /&gt;
|- &lt;br /&gt;
!Feature Renaming&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|NO&lt;br /&gt;
|-&lt;br /&gt;
!Method Overloading&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Operator Overloading&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|-&lt;br /&gt;
!Class Variables or methods&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Access Control&lt;br /&gt;
|Public, Protected, Private&lt;br /&gt;
|Public, Protected, Package, Private&lt;br /&gt;
|Public, Protected, Private, Internal, Protected Internal&lt;br /&gt;
|Public, Protected, Private, Friends&lt;br /&gt;
|Name Mangling&lt;br /&gt;
|Protected Data, Public Methods&lt;br /&gt;
|None&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Ruby : Purely Object Oriented ==&lt;br /&gt;
&lt;br /&gt;
Ruby is considered to be a purely Object oriented Language, as everything in it is considered to be an object. Unlike Java, even primitives such as characters, punctuation, numbers and strings are treated as objects in Ruby. Ruby was designed to facilitate the enforcement of Object Oriented Methods. Ruby is a Dynamically Typed Language.&lt;br /&gt;
&lt;br /&gt;
=== Object oriented Concepts in ruby: ===&lt;br /&gt;
&lt;br /&gt;
Ruby is a singly rooted Object Oriented Programming language which has all its classes having the same superclass which is the class Object.&lt;br /&gt;
&lt;br /&gt;
==== Classes:====&lt;br /&gt;
[[Image:RubyClass.png|thumb|right|280px|'''''Classes in Ruby''''']]&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Class_(computer_programming) class] is used to define a blueprint of a data type. It does not contain any data, but it specifies what kind of variables and methods an object of the class will have.&lt;br /&gt;
A class in Ruby is defined using the keyword class followed by the classname. The variables and methods are defined within the class definition and the class definition terminates with the end keyword.&lt;br /&gt;
&lt;br /&gt;
For ex:&lt;br /&gt;
  class Rectangle&lt;br /&gt;
     def initialize(w,l)&lt;br /&gt;
        @width, @length = w, l&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
Here, the initialize method is used to set the values of the instance variables (@width and @length. The instance variables are the properties of the objects created of this class type. These instance variables can be accessed within the class using an @. These variables can be accessed from outside the class using the accessor methods which are defined inside the class.&lt;br /&gt;
&lt;br /&gt;
In the following case printLength and printWidth are public methods used to access the instance variables from outside the class. These are called accessor methods.&lt;br /&gt;
Similarly, to&lt;br /&gt;
&lt;br /&gt;
   class Rectangle&lt;br /&gt;
  # constructor method&lt;br /&gt;
  def initialize(w,l)&lt;br /&gt;
     @width, @length = w, l&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # accessor methods&lt;br /&gt;
  def printWidth&lt;br /&gt;
     @width&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def printLength&lt;br /&gt;
     @length&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # setter methods&lt;br /&gt;
  def setWidth=(value)&lt;br /&gt;
     @width = value&lt;br /&gt;
  end&lt;br /&gt;
  def setHeight=(value)&lt;br /&gt;
     @height = value&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   # create an object&lt;br /&gt;
     rectagle = Rectangle.new(10, 20)&lt;br /&gt;
&lt;br /&gt;
    # use accessor methods&lt;br /&gt;
      x = rectangle.printWidth()&lt;br /&gt;
      y = rectangle.printLength()&lt;br /&gt;
&lt;br /&gt;
      puts &amp;quot;Width of the Rectangle is : #{x}&amp;quot;&lt;br /&gt;
      puts &amp;quot;Length of the Rectangle is : #{y}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==== Class Methods and Class Variables:====&lt;br /&gt;
A class variable is shared between all instances of the class. The value of the class variable remains the same for all the objects of the class. A class variable is accessed within the class using two @ signs at the beginning of the variable name. The class variables must be initialized within the class.&lt;br /&gt;
&lt;br /&gt;
The class methods are defined using def self.methodname() and ends with an end delimiter. These methods are called using the class name itself, for example: classname.methodname&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Attributes and accessor methods: ===&lt;br /&gt;
&lt;br /&gt;
Every class in Ruby defines a set of attributes for its objects. Ruby provides accessor methods (famously known as getter-setter methods in Java) to access these variables. Ruby provides three kinds of accessor methods:&lt;br /&gt;
&lt;br /&gt;
    Class Car&lt;br /&gt;
       attr_accessor : name, company&lt;br /&gt;
       attr_reader: price&lt;br /&gt;
       attr_writer: color&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
attr_accessor creates a setter and getter method. The attr_reader method creates only the getter method. This is required in case of attributes that are not to be modified by other programs but their value may be viewed. The attr_writer creates the setter method. In case of Java, the setter and getter methods need to be written in 3 lines of code each whereas in Ruby, the methods are written just by specifying the type of accessor, which have already been coded using [http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Access Control:====&lt;br /&gt;
The only easy way to change an object's state in Ruby is by calling one of its methods. Control access to the methods, and you have controlled access to the object. A good rule of the thumb is never to expose methods that could leave an object in an invalid state.&lt;br /&gt;
&lt;br /&gt;
Ruby gives you three levels of protection:&lt;br /&gt;
&lt;br /&gt;
''Public methods'' can be called by everyone - no access control is enforced. A class's instance methods (these do not belong only to one object; instead, every instance of the class can call them) are public by default; anyone can call them. The initialize method is always private.&lt;br /&gt;
&lt;br /&gt;
''Protected methods'' can be invoked only by objects of the defining class and its subclasses. Access is kept within the family. However, usage of protected is limited.&lt;br /&gt;
&lt;br /&gt;
''Private methods'' cannot be called with an explicit receiver - the receiver is always self. This means that private methods can be called only in the context of the current object; you cannot invoke another object's private methods.&lt;br /&gt;
&lt;br /&gt;
Access control is determined dynamically, as the program runs, not statically. You will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
Ex:&lt;br /&gt;
&lt;br /&gt;
    class Employee&lt;br /&gt;
       def initialize(name, age, salary)&lt;br /&gt;
           @name = name&lt;br /&gt;
           @age = age&lt;br /&gt;
           @salary = salary&lt;br /&gt;
       end&lt;br /&gt;
  &lt;br /&gt;
       def name&lt;br /&gt;
           @name&lt;br /&gt;
       end &lt;br /&gt;
       def age&lt;br /&gt;
          @age&lt;br /&gt;
       end&lt;br /&gt;
       def salary&lt;br /&gt;
           @salary&lt;br /&gt;
       end&lt;br /&gt;
&lt;br /&gt;
        public :name&lt;br /&gt;
        protected :age&lt;br /&gt;
        private :salary&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      emp = Employee.new('Jack', 25, 2000);&lt;br /&gt;
      puts emp.name&lt;br /&gt;
      #puts emp.age    #age protected. gives access violation error&lt;br /&gt;
      #puts emp.salary #salary private&lt;br /&gt;
&lt;br /&gt;
====Abstract methods====&lt;br /&gt;
&lt;br /&gt;
Ruby is a dynamically typed language and does not provide abstract classes as in Java or C++. It instead provides modules that can be included within any class. The reason why Ruby does not provide abstract classes is because of the idea that it only matters if two objects can perform a function, they don’t necessarily have to be inherited from the same class. This paradigm is provided by modules, where the method of a module can be redefined as required by a class that includes it.&lt;br /&gt;
&lt;br /&gt;
Also, if required the technique of abstract class can be simulated by something like this:&lt;br /&gt;
&lt;br /&gt;
  Class Vehicle&lt;br /&gt;
   def drive&lt;br /&gt;
   raise NotImplementedError.new(&amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The subclasses that inherit from the above class will have to redefine the method “drive” as required.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Ruby ===&lt;br /&gt;
&lt;br /&gt;
Ruby provides unbounded polymorphism as a dynamically typed language through the concept of “Duck-typing”. “Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface” &amp;lt;ref name=&amp;quot;Duck typing&amp;quot;&amp;gt;[http://en.wikipedia.org/wiki/Duck_typing DuckTyping]&amp;lt;/ref&amp;gt;. This concept is explained by the famous definition as “When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.” This essentially means that in languages like Ruby, it is only checked if an object can implement a function and the type of the object is not taken into consideration. It can be explained better with the help of the following example&lt;br /&gt;
&lt;br /&gt;
  class Lion&lt;br /&gt;
    def roar&lt;br /&gt;
        puts “The Lion roars when hungry”&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
     def roar&lt;br /&gt;
        puts “The man roared in fury and anger”&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   def quality lion&lt;br /&gt;
    lion.roar&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def start&lt;br /&gt;
    leo = Lion.new&lt;br /&gt;
    jack = Person.new&lt;br /&gt;
    quality leo&lt;br /&gt;
    quality jack&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   start&lt;br /&gt;
&lt;br /&gt;
In the above example, leo is an object of class “Lion” and the “roar” method defined in its class is invoked. jack is an object of class “Person” and the corresponding method defined in the Person class is called for him. Although in the real world, both objects are not of the same type, Ruby allows unbounded polymorphism as both of them implement a particular aspect.&lt;br /&gt;
&lt;br /&gt;
Thus, the check for an invoked method is done at run-time in dynamically typed languages and the method defined on that object type is called. If the method is undefined, a “NoMethodError” exception is raised.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Python===&lt;br /&gt;
&lt;br /&gt;
Python also heavily uses duck typing to allow flexibility to programmers. It emphasizes on the interface rather than on the specifc type which allows for polymorphic substitution&amp;lt;ref&amp;gt;[http://docs.python.org/glossary.html#term-duck-typing Python Duck-typing]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
In Python, functions like [http://docs.python.org/library/functions.html#type type()] or [http://docs.python.org/library/functions.html#isinstance isinstance()] is used to check for usual methods to match the type of find out if an object is an instance of a particular class. However, in case of duck typing, the focus is on the attribute of that object, so it checks for the presence of a particular attribute in that object using [http://docs.python.org/library/functions.html#hasattr hasattr()].&lt;br /&gt;
&lt;br /&gt;
Example for duck typing in Python is where Python uses a [http://docs.python.org/library/pickle.html “pickle”] module to serialize and de-serialize an object. So, we can introduce a new class that creates a “Tape” object. Now, an object of this type can be passed to the pickle module. Python will not differentiate between a file object or a tape object as long as they both have read() and write() methods.&lt;br /&gt;
&lt;br /&gt;
   class Tapefile():&lt;br /&gt;
      “A tape file”&lt;br /&gt;
       def __init__(self,params):&lt;br /&gt;
          #various initialization for tape access&lt;br /&gt;
       pass&lt;br /&gt;
       def read(len):&lt;br /&gt;
          #use special methods to access tape&lt;br /&gt;
       pass&lt;br /&gt;
       def readline():&lt;br /&gt;
          #use special methods to read a whole line on tape&lt;br /&gt;
       pass&lt;br /&gt;
       def write(value):&lt;br /&gt;
          #use special methods to write on tape&lt;br /&gt;
       pass&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in other languages===&lt;br /&gt;
&lt;br /&gt;
Duck typing is considered to be a feature provided by dynamic languages. C++ as a static language checks for methods by parameter and signature types. Java does not support duck typing either but it is argued that it can be simulated by the use of interfaces. The concept of duck-typing is criticized as it requires the programmer to have a very wide understanding of the source code and that two objects may implement a method in completely different contexts and so it may result in strange errors. &lt;br /&gt;
&lt;br /&gt;
Although duck typing is considered to be a feature of dynamic languages, some static languages like C# and Boo support it by allowing type checking of classes by the compiler to occur at run time.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sthirum</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53190</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b ms</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53190"/>
		<updated>2011-10-20T19:44:42Z</updated>

		<summary type="html">&lt;p&gt;Sthirum: /* Classes: */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Lecture 5: Object Oriented Programming in Ruby =&lt;br /&gt;
&lt;br /&gt;
This wiki tries to cover the Object Oriented concepts in ruby.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Lecture 5 &amp;lt;ref name=&amp;quot;CSC517&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517//common/lectures/notes/lec5.pdf OOP in Ruby], CSC 517 Lecture5&amp;lt;/ref&amp;gt; of CSC 517 covered topics of [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented programming] in [http://www.ruby-lang.org/en/ Ruby]. Object oriented programming is a paradigm in which real world entities are represented as objects, where each object has its own attributes and set of functions that it can perform. This gives a data structure called an &amp;quot;object&amp;quot; that consists of data fields and operations. This paradigm presents features like [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) encapsulation], [http://en.wikipedia.org/wiki/Data_abstraction data abstraction], [http://en.wikipedia.org/wiki/Inheritance_(computer_science) inheritance] and [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This wiki page tries to cover the other OOP languages and their features in a succinct manner and talks about the features of Ruby in detail with examples. We have also tried to compare the various languages in terms of their features to provide a better understanding.&lt;br /&gt;
&lt;br /&gt;
The lecture also covered the concepts of [http://en.wikipedia.org/wiki/Duck_typing duck typing] in Ruby, closures and currying. The latter two have not been included as per the instructions by the instructor.&lt;br /&gt;
&lt;br /&gt;
== Object oriented languages: An Overview ==&lt;br /&gt;
&lt;br /&gt;
==== C++: ====&lt;br /&gt;
[http://en.wikipedia.org/wiki/C%2B%2B C++] is a statically typed, multi-paradigm programming language. It supports some features of object-oriented languages like encapsulation, polymorphism and inheritance. It provides access control by specifiers public, private and protected and also allows “friend” classes. C++  allows [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance] which is eliminated in most other programming languages as it allows a class to inherit from more than one base class which results in an ambiguity effect called [http://en.wikipedia.org/wiki/Diamond_problem Diamond problem].&lt;br /&gt;
&lt;br /&gt;
==== Java: ====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Java_(programming_language) Java] is classified as a statically typed, hybrid object oriented language. Although it provides information hiding, inheritance and polymorphism, it has a few basic types incorporated that are not objects and some of the built-in operators are provided by basic arithmetic and not as messages to objects.[http://www.jvoegele.com/software/langcomp.html] For these reasons, it is not considered to be a “pure” object-oriented language.&lt;br /&gt;
&lt;br /&gt;
Java provides access control and supports private, public and protected access. It also allows to create packages to create and maintain namespaces. Java is designed not to provide multiple inheritance to avoid problems faced in C++. However, it presents a single-rooted hierarchy where “Object” is the ancestor of all classes (seen in Ruby too) and provides interfaces to extend/inherit functionality.&lt;br /&gt;
&lt;br /&gt;
==== Python: ====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Python_(programming_language) Python] is a dynamically typed, interpreted, object oriented language. It is argued that Python is not completely object oriented as it does not provide access control in the form of public, private or protected variables. It has different security mechanisms. However, it allows to emulate protected member by using the prefix '_' before the name and private member by the use of the prefix '__' (double underscore) before the name.&lt;br /&gt;
&lt;br /&gt;
==== Ruby: ====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_language Ruby] is dynamically typed and highly object-oriented, much more than Java and Python. In Ruby, it is said that “everything is an object”.  Every value happens to be an object in Ruby. You may call methods on a string, an integer just like you call on any object in Java. For ex,&lt;br /&gt;
&lt;br /&gt;
     (2.1143).size.times{puts “Hello!”}&lt;br /&gt;
&lt;br /&gt;
This example will display “Hello!” six times on your IRB as the size of the parameter &amp;quot;2.1143&amp;quot; is 6.&lt;br /&gt;
&lt;br /&gt;
== Comparison of object Oriented Languages ==&lt;br /&gt;
The following table compares the Properties of some of the well-known Object Oriented Languages.&amp;lt;ref name=&amp;quot;comparisonOOP&amp;quot;&amp;gt;[http://www.jvoegele.com/software/langcomp.html Programming Language Comparison]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 9pad case0%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Language&lt;br /&gt;
!Ruby&lt;br /&gt;
!Java&lt;br /&gt;
!C#&lt;br /&gt;
!C++&lt;br /&gt;
!Python&lt;br /&gt;
!SmallTalk&lt;br /&gt;
!Perl&lt;br /&gt;
|-&lt;br /&gt;
!Object Orientation&lt;br /&gt;
|Pure&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid/Multi-Paradigm&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Pure&lt;br /&gt;
|Add-on/Hybrid&lt;br /&gt;
|-&lt;br /&gt;
!Static/Dynamic Typing&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|-&lt;br /&gt;
!Generic Classes&lt;br /&gt;
|N/A&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|-&lt;br /&gt;
!Inheritance&lt;br /&gt;
|Single Class/Multiple Mixins&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Multiple&lt;br /&gt;
|Multiple&lt;br /&gt;
|Single&lt;br /&gt;
|Multiple&lt;br /&gt;
|- &lt;br /&gt;
!Feature Renaming&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|NO&lt;br /&gt;
|-&lt;br /&gt;
!Method Overloading&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Operator Overloading&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|-&lt;br /&gt;
!Class Variables or methods&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Access Control&lt;br /&gt;
|Public, Protected, Private&lt;br /&gt;
|Public, Protected, Package, Private&lt;br /&gt;
|Public, Protected, Private, Internal, Protected Internal&lt;br /&gt;
|Public, Protected, Private, Friends&lt;br /&gt;
|Name Mangling&lt;br /&gt;
|Protected Data, Public Methods&lt;br /&gt;
|None&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Ruby : Purely Object Oriented ==&lt;br /&gt;
&lt;br /&gt;
Ruby is considered to be a purely Object oriented Language, as everything in it is considered to be an object. Unlike Java, even primitives such as characters, punctuation, numbers and strings are treated as objects in Ruby. Ruby was designed to facilitate the enforcement of Object Oriented Methods. Ruby is a Dynamically Typed Language.&lt;br /&gt;
&lt;br /&gt;
=== Object oriented Concepts in ruby: ===&lt;br /&gt;
&lt;br /&gt;
Ruby is a singly rooted Object Oriented Programming language which has all its classes having the same superclass which is the class Object.&lt;br /&gt;
&lt;br /&gt;
==== Classes:====&lt;br /&gt;
[[Image:RubyClass.png|thumb|right|280px|'''''Classes in Ruby''''']]&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Class_(computer_programming) class] is used to define a blueprint of a data type. It does not contain any data, but it specifies what kind of variables and methods an object of the class will have.&lt;br /&gt;
A class in Ruby is defined using the keyword class followed by the classname. The variables and methods are defined within the class definition and the class definition terminates with the end keyword.&lt;br /&gt;
&lt;br /&gt;
For ex:&lt;br /&gt;
  class Rectangle&lt;br /&gt;
     def initialize(w,l)&lt;br /&gt;
        @width, @length = w, l&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
Here, the initialize method is used to set the values of the instance variables (@width and @length. The instance variables are the properties of the objects created of this class type. These instance variables can be accessed within the class using an @. These variables can be accessed from outside the class using the accessor methods which are defined inside the class.&lt;br /&gt;
&lt;br /&gt;
In the following case printLength and printWidth are public methods used to access the instance variables from outside the class. These are called accessor methods.&lt;br /&gt;
Similarly, to&lt;br /&gt;
&lt;br /&gt;
   class Rectangle&lt;br /&gt;
  # constructor method&lt;br /&gt;
  def initialize(w,l)&lt;br /&gt;
     @width, @length = w, l&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # accessor methods&lt;br /&gt;
  def printWidth&lt;br /&gt;
     @width&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def printLength&lt;br /&gt;
     @length&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # setter methods&lt;br /&gt;
  def setWidth=(value)&lt;br /&gt;
     @width = value&lt;br /&gt;
  end&lt;br /&gt;
  def setHeight=(value)&lt;br /&gt;
     @height = value&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   # create an object&lt;br /&gt;
     rectagle = Rectangle.new(10, 20)&lt;br /&gt;
&lt;br /&gt;
    # use accessor methods&lt;br /&gt;
      x = rectangle.printWidth()&lt;br /&gt;
      y = rectangle.printLength()&lt;br /&gt;
&lt;br /&gt;
      puts &amp;quot;Width of the Rectangle is : #{x}&amp;quot;&lt;br /&gt;
      puts &amp;quot;Length of the Rectangle is : #{y}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==== Class Methods and Class Variables:====&lt;br /&gt;
A class variable is shared between all instances of the class. The value of the class variable remains the same for all the objects of the class. A class variable is accessed within the class using two @ signs at the beginning of the variable name. The class variables must be initialized within the class.&lt;br /&gt;
&lt;br /&gt;
The class methods are defined using def self.methodname() and ends with an end delimiter. These methods are called using the class name itself, for example: classname.methodname&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Attributes and accessor methods: ===&lt;br /&gt;
&lt;br /&gt;
Every class in Ruby defines a set of attributes for its objects. Ruby provides accessor methods (famously known as getter-setter methods in Java) to access these variables. Ruby provides three kinds of accessor methods:&lt;br /&gt;
&lt;br /&gt;
    Class Car&lt;br /&gt;
       attr_accessor : name, company&lt;br /&gt;
       attr_reader: price&lt;br /&gt;
       attr_writer: color&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
attr_accessor creates a setter and getter method. The attr_reader method creates only the getter method. This is required in case of attributes that are not to be modified by other programs but their value may be viewed. The attr_writer creates the setter method. In case of Java, the setter and getter methods need to be written in 3 lines of code each whereas in Ruby, the methods are written just by specifying the type of accessor, which have already been coded using [http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Access Control:====&lt;br /&gt;
The only easy way to change an object's state in Ruby is by calling one of its methods. Control access to the methods, and you have controlled access to the object. A good rule of the thumb is never to expose methods that could leave an object in an invalid state.&lt;br /&gt;
&lt;br /&gt;
Ruby gives you three levels of protection:&lt;br /&gt;
&lt;br /&gt;
''Public methods'' can be called by everyone - no access control is enforced. A class's instance methods (these do not belong only to one object; instead, every instance of the class can call them) are public by default; anyone can call them. The initialize method is always private.&lt;br /&gt;
&lt;br /&gt;
''Protected methods'' can be invoked only by objects of the defining class and its subclasses. Access is kept within the family. However, usage of protected is limited.&lt;br /&gt;
&lt;br /&gt;
''Private methods'' cannot be called with an explicit receiver - the receiver is always self. This means that private methods can be called only in the context of the current object; you cannot invoke another object's private methods.&lt;br /&gt;
&lt;br /&gt;
Access control is determined dynamically, as the program runs, not statically. You will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
Ex:&lt;br /&gt;
&lt;br /&gt;
    class Employee&lt;br /&gt;
       def initialize(name, age, salary)&lt;br /&gt;
           @name = name&lt;br /&gt;
           @age = age&lt;br /&gt;
           @salary = salary&lt;br /&gt;
       end&lt;br /&gt;
  &lt;br /&gt;
       def name&lt;br /&gt;
           @name&lt;br /&gt;
       end &lt;br /&gt;
       def age&lt;br /&gt;
          @age&lt;br /&gt;
       end&lt;br /&gt;
       def salary&lt;br /&gt;
           @salary&lt;br /&gt;
       end&lt;br /&gt;
&lt;br /&gt;
        public :name&lt;br /&gt;
        protected :age&lt;br /&gt;
        private :salary&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      emp = Employee.new('Jack', 25, 2000);&lt;br /&gt;
      puts emp.name&lt;br /&gt;
      #puts emp.age    #age protected. gives access violation error&lt;br /&gt;
      #puts emp.salary #salary private&lt;br /&gt;
&lt;br /&gt;
====Abstract methods====&lt;br /&gt;
&lt;br /&gt;
Ruby is a dynamically typed language and does not provide abstract classes as in Java or C++. It instead provides modules that can be included within any class. The reason why Ruby does not provide abstract classes is because of the idea that it only matters if two objects can perform a function, they don’t necessarily have to be inherited from the same class. This paradigm is provided by modules, where the method of a module can be redefined as required by a class that includes it.&lt;br /&gt;
&lt;br /&gt;
Also, if required the technique of abstract class can be simulated by something like this:&lt;br /&gt;
&lt;br /&gt;
  Class Vehicle&lt;br /&gt;
   def drive&lt;br /&gt;
   raise NotImplementedError.new(&amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The subclasses that inherit from the above class will have to redefine the method “drive” as required.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Ruby ===&lt;br /&gt;
&lt;br /&gt;
Ruby provides unbounded polymorphism as a dynamically typed language through the concept of “Duck-typing”. “Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface” &amp;lt;ref name=&amp;quot;Duck typing&amp;quot;&amp;gt;[http://en.wikipedia.org/wiki/Duck_typing DuckTyping]&amp;lt;/ref&amp;gt;. This concept is explained by the famous definition as “When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.” This essentially means that in languages like Ruby, it is only checked if an object can implement a function and the type of the object is not taken into consideration. It can be explained better with the help of the following example&lt;br /&gt;
&lt;br /&gt;
  class Lion&lt;br /&gt;
    def roar&lt;br /&gt;
        puts “The Lion roars when hungry”&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
     def roar&lt;br /&gt;
        puts “The man roared in fury and anger”&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   def quality lion&lt;br /&gt;
    lion.roar&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def start&lt;br /&gt;
    leo = Lion.new&lt;br /&gt;
    jack = Person.new&lt;br /&gt;
    quality leo&lt;br /&gt;
    quality jack&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   start&lt;br /&gt;
&lt;br /&gt;
In the above example, leo is an object of class “Lion” and the “roar” method defined in its class is invoked. jack is an object of class “Person” and the corresponding method defined in the Person class is called for him. Although in the real world, both objects are not of the same type, Ruby allows unbounded polymorphism as both of them implement a particular aspect.&lt;br /&gt;
&lt;br /&gt;
Thus, the check for an invoked method is done at run-time in dynamically typed languages and the method defined on that object type is called. If the method is undefined, a “NoMethodError” exception is raised.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Python===&lt;br /&gt;
&lt;br /&gt;
Python also heavily uses duck typing to allow flexibility to programmers. It emphasizes on the interface rather than on the specifc type which allows for polymorphic substitution&amp;lt;ref&amp;gt;[http://docs.python.org/glossary.html#term-duck-typing Python Duck-typing]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
In Python, functions like [http://docs.python.org/library/functions.html#type type()] or [http://docs.python.org/library/functions.html#isinstance isinstance()] is used to check for usual methods to match the type of find out if an object is an instance of a particular class. However, in case of duck typing, the focus is on the attribute of that object, so it checks for the presence of a particular attribute in that object using [http://docs.python.org/library/functions.html#hasattr hasattr()].&lt;br /&gt;
&lt;br /&gt;
Example for duck typing in Python is where Python uses a [http://docs.python.org/library/pickle.html “pickle”] module to serialize and de-serialize an object. So, we can introduce a new class that creates a “Tape” object. Now, an object of this type can be passed to the pickle module. Python will not differentiate between a file object or a tape object as long as they both have read() and write() methods.&lt;br /&gt;
&lt;br /&gt;
   class Tapefile():&lt;br /&gt;
      “A tape file”&lt;br /&gt;
       def __init__(self,params):&lt;br /&gt;
          #various initialization for tape access&lt;br /&gt;
       pass&lt;br /&gt;
       def read(len):&lt;br /&gt;
          #use special methods to access tape&lt;br /&gt;
       pass&lt;br /&gt;
       def readline():&lt;br /&gt;
          #use special methods to read a whole line on tape&lt;br /&gt;
       pass&lt;br /&gt;
       def write(value):&lt;br /&gt;
          #use special methods to write on tape&lt;br /&gt;
       pass&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in other languages===&lt;br /&gt;
&lt;br /&gt;
Duck typing is considered to be a feature provided by dynamic languages. C++ as a static language checks for methods by parameter and signature types. Java does not support duck typing either but it is argued that it can be simulated by the use of interfaces. The concept of duck-typing is criticized as it requires the programmer to have a very wide understanding of the source code and that two objects may implement a method in completely different contexts and so it may result in strange errors. &lt;br /&gt;
&lt;br /&gt;
Although duck typing is considered to be a feature of dynamic languages, some static languages like C# and Boo support it by allowing type checking of classes by the compiler to occur at run time.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sthirum</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53189</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b ms</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53189"/>
		<updated>2011-10-20T19:43:34Z</updated>

		<summary type="html">&lt;p&gt;Sthirum: /* Duck Typing in Python */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Lecture 5: Object Oriented Programming in Ruby =&lt;br /&gt;
&lt;br /&gt;
This wiki tries to cover the Object Oriented concepts in ruby.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Lecture 5 &amp;lt;ref name=&amp;quot;CSC517&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517//common/lectures/notes/lec5.pdf OOP in Ruby], CSC 517 Lecture5&amp;lt;/ref&amp;gt; of CSC 517 covered topics of [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented programming] in [http://www.ruby-lang.org/en/ Ruby]. Object oriented programming is a paradigm in which real world entities are represented as objects, where each object has its own attributes and set of functions that it can perform. This gives a data structure called an &amp;quot;object&amp;quot; that consists of data fields and operations. This paradigm presents features like [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) encapsulation], [http://en.wikipedia.org/wiki/Data_abstraction data abstraction], [http://en.wikipedia.org/wiki/Inheritance_(computer_science) inheritance] and [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This wiki page tries to cover the other OOP languages and their features in a succinct manner and talks about the features of Ruby in detail with examples. We have also tried to compare the various languages in terms of their features to provide a better understanding.&lt;br /&gt;
&lt;br /&gt;
The lecture also covered the concepts of [http://en.wikipedia.org/wiki/Duck_typing duck typing] in Ruby, closures and currying. The latter two have not been included as per the instructions by the instructor.&lt;br /&gt;
&lt;br /&gt;
== Object oriented languages: An Overview ==&lt;br /&gt;
&lt;br /&gt;
==== C++: ====&lt;br /&gt;
[http://en.wikipedia.org/wiki/C%2B%2B C++] is a statically typed, multi-paradigm programming language. It supports some features of object-oriented languages like encapsulation, polymorphism and inheritance. It provides access control by specifiers public, private and protected and also allows “friend” classes. C++  allows [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance] which is eliminated in most other programming languages as it allows a class to inherit from more than one base class which results in an ambiguity effect called [http://en.wikipedia.org/wiki/Diamond_problem Diamond problem].&lt;br /&gt;
&lt;br /&gt;
==== Java: ====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Java_(programming_language) Java] is classified as a statically typed, hybrid object oriented language. Although it provides information hiding, inheritance and polymorphism, it has a few basic types incorporated that are not objects and some of the built-in operators are provided by basic arithmetic and not as messages to objects.[http://www.jvoegele.com/software/langcomp.html] For these reasons, it is not considered to be a “pure” object-oriented language.&lt;br /&gt;
&lt;br /&gt;
Java provides access control and supports private, public and protected access. It also allows to create packages to create and maintain namespaces. Java is designed not to provide multiple inheritance to avoid problems faced in C++. However, it presents a single-rooted hierarchy where “Object” is the ancestor of all classes (seen in Ruby too) and provides interfaces to extend/inherit functionality.&lt;br /&gt;
&lt;br /&gt;
==== Python: ====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Python_(programming_language) Python] is a dynamically typed, interpreted, object oriented language. It is argued that Python is not completely object oriented as it does not provide access control in the form of public, private or protected variables. It has different security mechanisms. However, it allows to emulate protected member by using the prefix '_' before the name and private member by the use of the prefix '__' (double underscore) before the name.&lt;br /&gt;
&lt;br /&gt;
==== Ruby: ====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_language Ruby] is dynamically typed and highly object-oriented, much more than Java and Python. In Ruby, it is said that “everything is an object”.  Every value happens to be an object in Ruby. You may call methods on a string, an integer just like you call on any object in Java. For ex,&lt;br /&gt;
&lt;br /&gt;
     (2.1143).size.times{puts “Hello!”}&lt;br /&gt;
&lt;br /&gt;
This example will display “Hello!” six times on your IRB as the size of the parameter &amp;quot;2.1143&amp;quot; is 6.&lt;br /&gt;
&lt;br /&gt;
== Comparison of object Oriented Languages ==&lt;br /&gt;
The following table compares the Properties of some of the well-known Object Oriented Languages.&amp;lt;ref name=&amp;quot;comparisonOOP&amp;quot;&amp;gt;[http://www.jvoegele.com/software/langcomp.html Programming Language Comparison]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 9pad case0%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Language&lt;br /&gt;
!Ruby&lt;br /&gt;
!Java&lt;br /&gt;
!C#&lt;br /&gt;
!C++&lt;br /&gt;
!Python&lt;br /&gt;
!SmallTalk&lt;br /&gt;
!Perl&lt;br /&gt;
|-&lt;br /&gt;
!Object Orientation&lt;br /&gt;
|Pure&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid/Multi-Paradigm&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Pure&lt;br /&gt;
|Add-on/Hybrid&lt;br /&gt;
|-&lt;br /&gt;
!Static/Dynamic Typing&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|-&lt;br /&gt;
!Generic Classes&lt;br /&gt;
|N/A&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|-&lt;br /&gt;
!Inheritance&lt;br /&gt;
|Single Class/Multiple Mixins&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Multiple&lt;br /&gt;
|Multiple&lt;br /&gt;
|Single&lt;br /&gt;
|Multiple&lt;br /&gt;
|- &lt;br /&gt;
!Feature Renaming&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|NO&lt;br /&gt;
|-&lt;br /&gt;
!Method Overloading&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Operator Overloading&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|-&lt;br /&gt;
!Class Variables or methods&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Access Control&lt;br /&gt;
|Public, Protected, Private&lt;br /&gt;
|Public, Protected, Package, Private&lt;br /&gt;
|Public, Protected, Private, Internal, Protected Internal&lt;br /&gt;
|Public, Protected, Private, Friends&lt;br /&gt;
|Name Mangling&lt;br /&gt;
|Protected Data, Public Methods&lt;br /&gt;
|None&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Ruby : Purely Object Oriented ==&lt;br /&gt;
&lt;br /&gt;
Ruby is considered to be a purely Object oriented Language, as everything in it is considered to be an object. Unlike Java, even primitives such as characters, punctuation, numbers and strings are treated as objects in Ruby. Ruby was designed to facilitate the enforcement of Object Oriented Methods. Ruby is a Dynamically Typed Language.&lt;br /&gt;
&lt;br /&gt;
=== Object oriented Concepts in ruby: ===&lt;br /&gt;
&lt;br /&gt;
Ruby is a singly rooted Object Oriented Programming language which has all its classes having the same superclass which is the class Object.&lt;br /&gt;
&lt;br /&gt;
==== Classes:====&lt;br /&gt;
[[Image:RubyClass.png|thumb|right|280px|'''''Classes in Ruby''''']]&lt;br /&gt;
&lt;br /&gt;
A class is used to define a blueprint of a data type. It does not contain any data, but it specifies what kind of variables and methods an object of the class will have.&lt;br /&gt;
A class in Ruby is defined using the keyword class followed by the classname. The variables and methods are defined within the class definition and the class definition terminates with the end keyword.&lt;br /&gt;
&lt;br /&gt;
For ex:&lt;br /&gt;
  class Rectangle&lt;br /&gt;
     def initialize(w,l)&lt;br /&gt;
        @width, @length = w, l&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
Here, the initialize method is used to set the values of the instance variables (@width and @length. The instance variables are the properties of the objects created of this class type. These instance variables can be accessed within the class using an @. These variables can be accessed from outside the class using the accessor methods which are defined inside the class.&lt;br /&gt;
&lt;br /&gt;
In the following case printLength and printWidth are public methods used to access the instance variables from outside the class. These are called accessor methods.&lt;br /&gt;
Similarly, to&lt;br /&gt;
&lt;br /&gt;
   class Rectangle&lt;br /&gt;
  # constructor method&lt;br /&gt;
  def initialize(w,l)&lt;br /&gt;
     @width, @length = w, l&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # accessor methods&lt;br /&gt;
  def printWidth&lt;br /&gt;
     @width&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def printLength&lt;br /&gt;
     @length&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # setter methods&lt;br /&gt;
  def setWidth=(value)&lt;br /&gt;
     @width = value&lt;br /&gt;
  end&lt;br /&gt;
  def setHeight=(value)&lt;br /&gt;
     @height = value&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   # create an object&lt;br /&gt;
     rectagle = Rectangle.new(10, 20)&lt;br /&gt;
&lt;br /&gt;
    # use accessor methods&lt;br /&gt;
      x = rectangle.printWidth()&lt;br /&gt;
      y = rectangle.printLength()&lt;br /&gt;
&lt;br /&gt;
      puts &amp;quot;Width of the Rectangle is : #{x}&amp;quot;&lt;br /&gt;
      puts &amp;quot;Length of the Rectangle is : #{y}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==== Class Methods and Class Variables:====&lt;br /&gt;
A class variable is shared between all instances of the class. The value of the class variable remains the same for all the objects of the class. A class variable is accessed within the class using two @ signs at the beginning of the variable name. The class variables must be initialized within the class.&lt;br /&gt;
&lt;br /&gt;
The class methods are defined using def self.methodname() and ends with an end delimiter. These methods are called using the class name itself, for example: classname.methodname&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Attributes and accessor methods: ===&lt;br /&gt;
&lt;br /&gt;
Every class in Ruby defines a set of attributes for its objects. Ruby provides accessor methods (famously known as getter-setter methods in Java) to access these variables. Ruby provides three kinds of accessor methods:&lt;br /&gt;
&lt;br /&gt;
    Class Car&lt;br /&gt;
       attr_accessor : name, company&lt;br /&gt;
       attr_reader: price&lt;br /&gt;
       attr_writer: color&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
attr_accessor creates a setter and getter method. The attr_reader method creates only the getter method. This is required in case of attributes that are not to be modified by other programs but their value may be viewed. The attr_writer creates the setter method. In case of Java, the setter and getter methods need to be written in 3 lines of code each whereas in Ruby, the methods are written just by specifying the type of accessor, which have already been coded using [http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Access Control:====&lt;br /&gt;
The only easy way to change an object's state in Ruby is by calling one of its methods. Control access to the methods, and you have controlled access to the object. A good rule of the thumb is never to expose methods that could leave an object in an invalid state.&lt;br /&gt;
&lt;br /&gt;
Ruby gives you three levels of protection:&lt;br /&gt;
&lt;br /&gt;
''Public methods'' can be called by everyone - no access control is enforced. A class's instance methods (these do not belong only to one object; instead, every instance of the class can call them) are public by default; anyone can call them. The initialize method is always private.&lt;br /&gt;
&lt;br /&gt;
''Protected methods'' can be invoked only by objects of the defining class and its subclasses. Access is kept within the family. However, usage of protected is limited.&lt;br /&gt;
&lt;br /&gt;
''Private methods'' cannot be called with an explicit receiver - the receiver is always self. This means that private methods can be called only in the context of the current object; you cannot invoke another object's private methods.&lt;br /&gt;
&lt;br /&gt;
Access control is determined dynamically, as the program runs, not statically. You will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
Ex:&lt;br /&gt;
&lt;br /&gt;
    class Employee&lt;br /&gt;
       def initialize(name, age, salary)&lt;br /&gt;
           @name = name&lt;br /&gt;
           @age = age&lt;br /&gt;
           @salary = salary&lt;br /&gt;
       end&lt;br /&gt;
  &lt;br /&gt;
       def name&lt;br /&gt;
           @name&lt;br /&gt;
       end &lt;br /&gt;
       def age&lt;br /&gt;
          @age&lt;br /&gt;
       end&lt;br /&gt;
       def salary&lt;br /&gt;
           @salary&lt;br /&gt;
       end&lt;br /&gt;
&lt;br /&gt;
        public :name&lt;br /&gt;
        protected :age&lt;br /&gt;
        private :salary&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      emp = Employee.new('Jack', 25, 2000);&lt;br /&gt;
      puts emp.name&lt;br /&gt;
      #puts emp.age    #age protected. gives access violation error&lt;br /&gt;
      #puts emp.salary #salary private&lt;br /&gt;
&lt;br /&gt;
====Abstract methods====&lt;br /&gt;
&lt;br /&gt;
Ruby is a dynamically typed language and does not provide abstract classes as in Java or C++. It instead provides modules that can be included within any class. The reason why Ruby does not provide abstract classes is because of the idea that it only matters if two objects can perform a function, they don’t necessarily have to be inherited from the same class. This paradigm is provided by modules, where the method of a module can be redefined as required by a class that includes it.&lt;br /&gt;
&lt;br /&gt;
Also, if required the technique of abstract class can be simulated by something like this:&lt;br /&gt;
&lt;br /&gt;
  Class Vehicle&lt;br /&gt;
   def drive&lt;br /&gt;
   raise NotImplementedError.new(&amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The subclasses that inherit from the above class will have to redefine the method “drive” as required.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Ruby ===&lt;br /&gt;
&lt;br /&gt;
Ruby provides unbounded polymorphism as a dynamically typed language through the concept of “Duck-typing”. “Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface” &amp;lt;ref name=&amp;quot;Duck typing&amp;quot;&amp;gt;[http://en.wikipedia.org/wiki/Duck_typing DuckTyping]&amp;lt;/ref&amp;gt;. This concept is explained by the famous definition as “When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.” This essentially means that in languages like Ruby, it is only checked if an object can implement a function and the type of the object is not taken into consideration. It can be explained better with the help of the following example&lt;br /&gt;
&lt;br /&gt;
  class Lion&lt;br /&gt;
    def roar&lt;br /&gt;
        puts “The Lion roars when hungry”&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
     def roar&lt;br /&gt;
        puts “The man roared in fury and anger”&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   def quality lion&lt;br /&gt;
    lion.roar&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def start&lt;br /&gt;
    leo = Lion.new&lt;br /&gt;
    jack = Person.new&lt;br /&gt;
    quality leo&lt;br /&gt;
    quality jack&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   start&lt;br /&gt;
&lt;br /&gt;
In the above example, leo is an object of class “Lion” and the “roar” method defined in its class is invoked. jack is an object of class “Person” and the corresponding method defined in the Person class is called for him. Although in the real world, both objects are not of the same type, Ruby allows unbounded polymorphism as both of them implement a particular aspect.&lt;br /&gt;
&lt;br /&gt;
Thus, the check for an invoked method is done at run-time in dynamically typed languages and the method defined on that object type is called. If the method is undefined, a “NoMethodError” exception is raised.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Python===&lt;br /&gt;
&lt;br /&gt;
Python also heavily uses duck typing to allow flexibility to programmers. It emphasizes on the interface rather than on the specifc type which allows for polymorphic substitution&amp;lt;ref&amp;gt;[http://docs.python.org/glossary.html#term-duck-typing Python Duck-typing]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
In Python, functions like [http://docs.python.org/library/functions.html#type type()] or [http://docs.python.org/library/functions.html#isinstance isinstance()] is used to check for usual methods to match the type of find out if an object is an instance of a particular class. However, in case of duck typing, the focus is on the attribute of that object, so it checks for the presence of a particular attribute in that object using [http://docs.python.org/library/functions.html#hasattr hasattr()].&lt;br /&gt;
&lt;br /&gt;
Example for duck typing in Python is where Python uses a [http://docs.python.org/library/pickle.html “pickle”] module to serialize and de-serialize an object. So, we can introduce a new class that creates a “Tape” object. Now, an object of this type can be passed to the pickle module. Python will not differentiate between a file object or a tape object as long as they both have read() and write() methods.&lt;br /&gt;
&lt;br /&gt;
   class Tapefile():&lt;br /&gt;
      “A tape file”&lt;br /&gt;
       def __init__(self,params):&lt;br /&gt;
          #various initialization for tape access&lt;br /&gt;
       pass&lt;br /&gt;
       def read(len):&lt;br /&gt;
          #use special methods to access tape&lt;br /&gt;
       pass&lt;br /&gt;
       def readline():&lt;br /&gt;
          #use special methods to read a whole line on tape&lt;br /&gt;
       pass&lt;br /&gt;
       def write(value):&lt;br /&gt;
          #use special methods to write on tape&lt;br /&gt;
       pass&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in other languages===&lt;br /&gt;
&lt;br /&gt;
Duck typing is considered to be a feature provided by dynamic languages. C++ as a static language checks for methods by parameter and signature types. Java does not support duck typing either but it is argued that it can be simulated by the use of interfaces. The concept of duck-typing is criticized as it requires the programmer to have a very wide understanding of the source code and that two objects may implement a method in completely different contexts and so it may result in strange errors. &lt;br /&gt;
&lt;br /&gt;
Although duck typing is considered to be a feature of dynamic languages, some static languages like C# and Boo support it by allowing type checking of classes by the compiler to occur at run time.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sthirum</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53188</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b ms</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53188"/>
		<updated>2011-10-20T19:43:05Z</updated>

		<summary type="html">&lt;p&gt;Sthirum: /* Duck Typing in Python */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Lecture 5: Object Oriented Programming in Ruby =&lt;br /&gt;
&lt;br /&gt;
This wiki tries to cover the Object Oriented concepts in ruby.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Lecture 5 &amp;lt;ref name=&amp;quot;CSC517&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517//common/lectures/notes/lec5.pdf OOP in Ruby], CSC 517 Lecture5&amp;lt;/ref&amp;gt; of CSC 517 covered topics of [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented programming] in [http://www.ruby-lang.org/en/ Ruby]. Object oriented programming is a paradigm in which real world entities are represented as objects, where each object has its own attributes and set of functions that it can perform. This gives a data structure called an &amp;quot;object&amp;quot; that consists of data fields and operations. This paradigm presents features like [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) encapsulation], [http://en.wikipedia.org/wiki/Data_abstraction data abstraction], [http://en.wikipedia.org/wiki/Inheritance_(computer_science) inheritance] and [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This wiki page tries to cover the other OOP languages and their features in a succinct manner and talks about the features of Ruby in detail with examples. We have also tried to compare the various languages in terms of their features to provide a better understanding.&lt;br /&gt;
&lt;br /&gt;
The lecture also covered the concepts of [http://en.wikipedia.org/wiki/Duck_typing duck typing] in Ruby, closures and currying. The latter two have not been included as per the instructions by the instructor.&lt;br /&gt;
&lt;br /&gt;
== Object oriented languages: An Overview ==&lt;br /&gt;
&lt;br /&gt;
==== C++: ====&lt;br /&gt;
[http://en.wikipedia.org/wiki/C%2B%2B C++] is a statically typed, multi-paradigm programming language. It supports some features of object-oriented languages like encapsulation, polymorphism and inheritance. It provides access control by specifiers public, private and protected and also allows “friend” classes. C++  allows [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance] which is eliminated in most other programming languages as it allows a class to inherit from more than one base class which results in an ambiguity effect called [http://en.wikipedia.org/wiki/Diamond_problem Diamond problem].&lt;br /&gt;
&lt;br /&gt;
==== Java: ====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Java_(programming_language) Java] is classified as a statically typed, hybrid object oriented language. Although it provides information hiding, inheritance and polymorphism, it has a few basic types incorporated that are not objects and some of the built-in operators are provided by basic arithmetic and not as messages to objects.[http://www.jvoegele.com/software/langcomp.html] For these reasons, it is not considered to be a “pure” object-oriented language.&lt;br /&gt;
&lt;br /&gt;
Java provides access control and supports private, public and protected access. It also allows to create packages to create and maintain namespaces. Java is designed not to provide multiple inheritance to avoid problems faced in C++. However, it presents a single-rooted hierarchy where “Object” is the ancestor of all classes (seen in Ruby too) and provides interfaces to extend/inherit functionality.&lt;br /&gt;
&lt;br /&gt;
==== Python: ====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Python_(programming_language) Python] is a dynamically typed, interpreted, object oriented language. It is argued that Python is not completely object oriented as it does not provide access control in the form of public, private or protected variables. It has different security mechanisms. However, it allows to emulate protected member by using the prefix '_' before the name and private member by the use of the prefix '__' (double underscore) before the name.&lt;br /&gt;
&lt;br /&gt;
==== Ruby: ====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_language Ruby] is dynamically typed and highly object-oriented, much more than Java and Python. In Ruby, it is said that “everything is an object”.  Every value happens to be an object in Ruby. You may call methods on a string, an integer just like you call on any object in Java. For ex,&lt;br /&gt;
&lt;br /&gt;
     (2.1143).size.times{puts “Hello!”}&lt;br /&gt;
&lt;br /&gt;
This example will display “Hello!” six times on your IRB as the size of the parameter &amp;quot;2.1143&amp;quot; is 6.&lt;br /&gt;
&lt;br /&gt;
== Comparison of object Oriented Languages ==&lt;br /&gt;
The following table compares the Properties of some of the well-known Object Oriented Languages.&amp;lt;ref name=&amp;quot;comparisonOOP&amp;quot;&amp;gt;[http://www.jvoegele.com/software/langcomp.html Programming Language Comparison]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 9pad case0%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Language&lt;br /&gt;
!Ruby&lt;br /&gt;
!Java&lt;br /&gt;
!C#&lt;br /&gt;
!C++&lt;br /&gt;
!Python&lt;br /&gt;
!SmallTalk&lt;br /&gt;
!Perl&lt;br /&gt;
|-&lt;br /&gt;
!Object Orientation&lt;br /&gt;
|Pure&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid/Multi-Paradigm&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Pure&lt;br /&gt;
|Add-on/Hybrid&lt;br /&gt;
|-&lt;br /&gt;
!Static/Dynamic Typing&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|-&lt;br /&gt;
!Generic Classes&lt;br /&gt;
|N/A&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|-&lt;br /&gt;
!Inheritance&lt;br /&gt;
|Single Class/Multiple Mixins&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Multiple&lt;br /&gt;
|Multiple&lt;br /&gt;
|Single&lt;br /&gt;
|Multiple&lt;br /&gt;
|- &lt;br /&gt;
!Feature Renaming&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|NO&lt;br /&gt;
|-&lt;br /&gt;
!Method Overloading&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Operator Overloading&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|-&lt;br /&gt;
!Class Variables or methods&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Access Control&lt;br /&gt;
|Public, Protected, Private&lt;br /&gt;
|Public, Protected, Package, Private&lt;br /&gt;
|Public, Protected, Private, Internal, Protected Internal&lt;br /&gt;
|Public, Protected, Private, Friends&lt;br /&gt;
|Name Mangling&lt;br /&gt;
|Protected Data, Public Methods&lt;br /&gt;
|None&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Ruby : Purely Object Oriented ==&lt;br /&gt;
&lt;br /&gt;
Ruby is considered to be a purely Object oriented Language, as everything in it is considered to be an object. Unlike Java, even primitives such as characters, punctuation, numbers and strings are treated as objects in Ruby. Ruby was designed to facilitate the enforcement of Object Oriented Methods. Ruby is a Dynamically Typed Language.&lt;br /&gt;
&lt;br /&gt;
=== Object oriented Concepts in ruby: ===&lt;br /&gt;
&lt;br /&gt;
Ruby is a singly rooted Object Oriented Programming language which has all its classes having the same superclass which is the class Object.&lt;br /&gt;
&lt;br /&gt;
==== Classes:====&lt;br /&gt;
[[Image:RubyClass.png|thumb|right|280px|'''''Classes in Ruby''''']]&lt;br /&gt;
&lt;br /&gt;
A class is used to define a blueprint of a data type. It does not contain any data, but it specifies what kind of variables and methods an object of the class will have.&lt;br /&gt;
A class in Ruby is defined using the keyword class followed by the classname. The variables and methods are defined within the class definition and the class definition terminates with the end keyword.&lt;br /&gt;
&lt;br /&gt;
For ex:&lt;br /&gt;
  class Rectangle&lt;br /&gt;
     def initialize(w,l)&lt;br /&gt;
        @width, @length = w, l&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
Here, the initialize method is used to set the values of the instance variables (@width and @length. The instance variables are the properties of the objects created of this class type. These instance variables can be accessed within the class using an @. These variables can be accessed from outside the class using the accessor methods which are defined inside the class.&lt;br /&gt;
&lt;br /&gt;
In the following case printLength and printWidth are public methods used to access the instance variables from outside the class. These are called accessor methods.&lt;br /&gt;
Similarly, to&lt;br /&gt;
&lt;br /&gt;
   class Rectangle&lt;br /&gt;
  # constructor method&lt;br /&gt;
  def initialize(w,l)&lt;br /&gt;
     @width, @length = w, l&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # accessor methods&lt;br /&gt;
  def printWidth&lt;br /&gt;
     @width&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def printLength&lt;br /&gt;
     @length&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # setter methods&lt;br /&gt;
  def setWidth=(value)&lt;br /&gt;
     @width = value&lt;br /&gt;
  end&lt;br /&gt;
  def setHeight=(value)&lt;br /&gt;
     @height = value&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   # create an object&lt;br /&gt;
     rectagle = Rectangle.new(10, 20)&lt;br /&gt;
&lt;br /&gt;
    # use accessor methods&lt;br /&gt;
      x = rectangle.printWidth()&lt;br /&gt;
      y = rectangle.printLength()&lt;br /&gt;
&lt;br /&gt;
      puts &amp;quot;Width of the Rectangle is : #{x}&amp;quot;&lt;br /&gt;
      puts &amp;quot;Length of the Rectangle is : #{y}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==== Class Methods and Class Variables:====&lt;br /&gt;
A class variable is shared between all instances of the class. The value of the class variable remains the same for all the objects of the class. A class variable is accessed within the class using two @ signs at the beginning of the variable name. The class variables must be initialized within the class.&lt;br /&gt;
&lt;br /&gt;
The class methods are defined using def self.methodname() and ends with an end delimiter. These methods are called using the class name itself, for example: classname.methodname&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Attributes and accessor methods: ===&lt;br /&gt;
&lt;br /&gt;
Every class in Ruby defines a set of attributes for its objects. Ruby provides accessor methods (famously known as getter-setter methods in Java) to access these variables. Ruby provides three kinds of accessor methods:&lt;br /&gt;
&lt;br /&gt;
    Class Car&lt;br /&gt;
       attr_accessor : name, company&lt;br /&gt;
       attr_reader: price&lt;br /&gt;
       attr_writer: color&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
attr_accessor creates a setter and getter method. The attr_reader method creates only the getter method. This is required in case of attributes that are not to be modified by other programs but their value may be viewed. The attr_writer creates the setter method. In case of Java, the setter and getter methods need to be written in 3 lines of code each whereas in Ruby, the methods are written just by specifying the type of accessor, which have already been coded using [http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Access Control:====&lt;br /&gt;
The only easy way to change an object's state in Ruby is by calling one of its methods. Control access to the methods, and you have controlled access to the object. A good rule of the thumb is never to expose methods that could leave an object in an invalid state.&lt;br /&gt;
&lt;br /&gt;
Ruby gives you three levels of protection:&lt;br /&gt;
&lt;br /&gt;
''Public methods'' can be called by everyone - no access control is enforced. A class's instance methods (these do not belong only to one object; instead, every instance of the class can call them) are public by default; anyone can call them. The initialize method is always private.&lt;br /&gt;
&lt;br /&gt;
''Protected methods'' can be invoked only by objects of the defining class and its subclasses. Access is kept within the family. However, usage of protected is limited.&lt;br /&gt;
&lt;br /&gt;
''Private methods'' cannot be called with an explicit receiver - the receiver is always self. This means that private methods can be called only in the context of the current object; you cannot invoke another object's private methods.&lt;br /&gt;
&lt;br /&gt;
Access control is determined dynamically, as the program runs, not statically. You will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
Ex:&lt;br /&gt;
&lt;br /&gt;
    class Employee&lt;br /&gt;
       def initialize(name, age, salary)&lt;br /&gt;
           @name = name&lt;br /&gt;
           @age = age&lt;br /&gt;
           @salary = salary&lt;br /&gt;
       end&lt;br /&gt;
  &lt;br /&gt;
       def name&lt;br /&gt;
           @name&lt;br /&gt;
       end &lt;br /&gt;
       def age&lt;br /&gt;
          @age&lt;br /&gt;
       end&lt;br /&gt;
       def salary&lt;br /&gt;
           @salary&lt;br /&gt;
       end&lt;br /&gt;
&lt;br /&gt;
        public :name&lt;br /&gt;
        protected :age&lt;br /&gt;
        private :salary&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      emp = Employee.new('Jack', 25, 2000);&lt;br /&gt;
      puts emp.name&lt;br /&gt;
      #puts emp.age    #age protected. gives access violation error&lt;br /&gt;
      #puts emp.salary #salary private&lt;br /&gt;
&lt;br /&gt;
====Abstract methods====&lt;br /&gt;
&lt;br /&gt;
Ruby is a dynamically typed language and does not provide abstract classes as in Java or C++. It instead provides modules that can be included within any class. The reason why Ruby does not provide abstract classes is because of the idea that it only matters if two objects can perform a function, they don’t necessarily have to be inherited from the same class. This paradigm is provided by modules, where the method of a module can be redefined as required by a class that includes it.&lt;br /&gt;
&lt;br /&gt;
Also, if required the technique of abstract class can be simulated by something like this:&lt;br /&gt;
&lt;br /&gt;
  Class Vehicle&lt;br /&gt;
   def drive&lt;br /&gt;
   raise NotImplementedError.new(&amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The subclasses that inherit from the above class will have to redefine the method “drive” as required.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Ruby ===&lt;br /&gt;
&lt;br /&gt;
Ruby provides unbounded polymorphism as a dynamically typed language through the concept of “Duck-typing”. “Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface” &amp;lt;ref name=&amp;quot;Duck typing&amp;quot;&amp;gt;[http://en.wikipedia.org/wiki/Duck_typing DuckTyping]&amp;lt;/ref&amp;gt;. This concept is explained by the famous definition as “When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.” This essentially means that in languages like Ruby, it is only checked if an object can implement a function and the type of the object is not taken into consideration. It can be explained better with the help of the following example&lt;br /&gt;
&lt;br /&gt;
  class Lion&lt;br /&gt;
    def roar&lt;br /&gt;
        puts “The Lion roars when hungry”&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
     def roar&lt;br /&gt;
        puts “The man roared in fury and anger”&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   def quality lion&lt;br /&gt;
    lion.roar&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def start&lt;br /&gt;
    leo = Lion.new&lt;br /&gt;
    jack = Person.new&lt;br /&gt;
    quality leo&lt;br /&gt;
    quality jack&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   start&lt;br /&gt;
&lt;br /&gt;
In the above example, leo is an object of class “Lion” and the “roar” method defined in its class is invoked. jack is an object of class “Person” and the corresponding method defined in the Person class is called for him. Although in the real world, both objects are not of the same type, Ruby allows unbounded polymorphism as both of them implement a particular aspect.&lt;br /&gt;
&lt;br /&gt;
Thus, the check for an invoked method is done at run-time in dynamically typed languages and the method defined on that object type is called. If the method is undefined, a “NoMethodError” exception is raised.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Python===&lt;br /&gt;
&lt;br /&gt;
Python also heavily uses duck typing to allow flexibility to programmers. It emphasizes on the interface rather than on the specifc type which allows for polymorphic substitution&amp;lt;ref&amp;gt;[http://docs.python.org/glossary.html#term-duck-typing Python Duck-typing]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
In Python, functions like [http://docs.python.org/library/functions.html#type type()] or [http://docs.python.org/library/functions.html#isinstance isinstance()] is used to check for usual methods to match the type of find out if an object is an instance of a particular class. However, in case of duck typing, the focus is on the attribute of that object, so it checks for the presence of a particular attribute in that object using [http://docs.python.org/library/functions.html#hasattr hasattr()].&lt;br /&gt;
&lt;br /&gt;
Example for duck typing in Python is where Python uses a “pickle” module to serialize and de-serialize an object. So, we can introduce a new class that creates a “Tape” object. Now, an object of this type can be passed to the pickle module. Python will not differentiate between a file object or a tape object as long as they both have read() and write() methods.&lt;br /&gt;
&lt;br /&gt;
   class Tapefile():&lt;br /&gt;
      “A tape file”&lt;br /&gt;
       def __init__(self,params):&lt;br /&gt;
          #various initialization for tape access&lt;br /&gt;
       pass&lt;br /&gt;
       def read(len):&lt;br /&gt;
          #use special methods to access tape&lt;br /&gt;
       pass&lt;br /&gt;
       def readline():&lt;br /&gt;
          #use special methods to read a whole line on tape&lt;br /&gt;
       pass&lt;br /&gt;
       def write(value):&lt;br /&gt;
          #use special methods to write on tape&lt;br /&gt;
       pass&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in other languages===&lt;br /&gt;
&lt;br /&gt;
Duck typing is considered to be a feature provided by dynamic languages. C++ as a static language checks for methods by parameter and signature types. Java does not support duck typing either but it is argued that it can be simulated by the use of interfaces. The concept of duck-typing is criticized as it requires the programmer to have a very wide understanding of the source code and that two objects may implement a method in completely different contexts and so it may result in strange errors. &lt;br /&gt;
&lt;br /&gt;
Although duck typing is considered to be a feature of dynamic languages, some static languages like C# and Boo support it by allowing type checking of classes by the compiler to occur at run time.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sthirum</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53187</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b ms</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53187"/>
		<updated>2011-10-20T19:40:59Z</updated>

		<summary type="html">&lt;p&gt;Sthirum: /* Duck Typing in Python */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Lecture 5: Object Oriented Programming in Ruby =&lt;br /&gt;
&lt;br /&gt;
This wiki tries to cover the Object Oriented concepts in ruby.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Lecture 5 &amp;lt;ref name=&amp;quot;CSC517&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517//common/lectures/notes/lec5.pdf OOP in Ruby], CSC 517 Lecture5&amp;lt;/ref&amp;gt; of CSC 517 covered topics of [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented programming] in [http://www.ruby-lang.org/en/ Ruby]. Object oriented programming is a paradigm in which real world entities are represented as objects, where each object has its own attributes and set of functions that it can perform. This gives a data structure called an &amp;quot;object&amp;quot; that consists of data fields and operations. This paradigm presents features like [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) encapsulation], [http://en.wikipedia.org/wiki/Data_abstraction data abstraction], [http://en.wikipedia.org/wiki/Inheritance_(computer_science) inheritance] and [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This wiki page tries to cover the other OOP languages and their features in a succinct manner and talks about the features of Ruby in detail with examples. We have also tried to compare the various languages in terms of their features to provide a better understanding.&lt;br /&gt;
&lt;br /&gt;
The lecture also covered the concepts of [http://en.wikipedia.org/wiki/Duck_typing duck typing] in Ruby, closures and currying. The latter two have not been included as per the instructions by the instructor.&lt;br /&gt;
&lt;br /&gt;
== Object oriented languages: An Overview ==&lt;br /&gt;
&lt;br /&gt;
==== C++: ====&lt;br /&gt;
[http://en.wikipedia.org/wiki/C%2B%2B C++] is a statically typed, multi-paradigm programming language. It supports some features of object-oriented languages like encapsulation, polymorphism and inheritance. It provides access control by specifiers public, private and protected and also allows “friend” classes. C++  allows [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance] which is eliminated in most other programming languages as it allows a class to inherit from more than one base class which results in an ambiguity effect called [http://en.wikipedia.org/wiki/Diamond_problem Diamond problem].&lt;br /&gt;
&lt;br /&gt;
==== Java: ====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Java_(programming_language) Java] is classified as a statically typed, hybrid object oriented language. Although it provides information hiding, inheritance and polymorphism, it has a few basic types incorporated that are not objects and some of the built-in operators are provided by basic arithmetic and not as messages to objects.[http://www.jvoegele.com/software/langcomp.html] For these reasons, it is not considered to be a “pure” object-oriented language.&lt;br /&gt;
&lt;br /&gt;
Java provides access control and supports private, public and protected access. It also allows to create packages to create and maintain namespaces. Java is designed not to provide multiple inheritance to avoid problems faced in C++. However, it presents a single-rooted hierarchy where “Object” is the ancestor of all classes (seen in Ruby too) and provides interfaces to extend/inherit functionality.&lt;br /&gt;
&lt;br /&gt;
==== Python: ====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Python_(programming_language) Python] is a dynamically typed, interpreted, object oriented language. It is argued that Python is not completely object oriented as it does not provide access control in the form of public, private or protected variables. It has different security mechanisms. However, it allows to emulate protected member by using the prefix '_' before the name and private member by the use of the prefix '__' (double underscore) before the name.&lt;br /&gt;
&lt;br /&gt;
==== Ruby: ====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_language Ruby] is dynamically typed and highly object-oriented, much more than Java and Python. In Ruby, it is said that “everything is an object”.  Every value happens to be an object in Ruby. You may call methods on a string, an integer just like you call on any object in Java. For ex,&lt;br /&gt;
&lt;br /&gt;
     (2.1143).size.times{puts “Hello!”}&lt;br /&gt;
&lt;br /&gt;
This example will display “Hello!” six times on your IRB as the size of the parameter &amp;quot;2.1143&amp;quot; is 6.&lt;br /&gt;
&lt;br /&gt;
== Comparison of object Oriented Languages ==&lt;br /&gt;
The following table compares the Properties of some of the well-known Object Oriented Languages.&amp;lt;ref name=&amp;quot;comparisonOOP&amp;quot;&amp;gt;[http://www.jvoegele.com/software/langcomp.html Programming Language Comparison]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 9pad case0%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Language&lt;br /&gt;
!Ruby&lt;br /&gt;
!Java&lt;br /&gt;
!C#&lt;br /&gt;
!C++&lt;br /&gt;
!Python&lt;br /&gt;
!SmallTalk&lt;br /&gt;
!Perl&lt;br /&gt;
|-&lt;br /&gt;
!Object Orientation&lt;br /&gt;
|Pure&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid/Multi-Paradigm&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Pure&lt;br /&gt;
|Add-on/Hybrid&lt;br /&gt;
|-&lt;br /&gt;
!Static/Dynamic Typing&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|-&lt;br /&gt;
!Generic Classes&lt;br /&gt;
|N/A&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|-&lt;br /&gt;
!Inheritance&lt;br /&gt;
|Single Class/Multiple Mixins&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Multiple&lt;br /&gt;
|Multiple&lt;br /&gt;
|Single&lt;br /&gt;
|Multiple&lt;br /&gt;
|- &lt;br /&gt;
!Feature Renaming&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|NO&lt;br /&gt;
|-&lt;br /&gt;
!Method Overloading&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Operator Overloading&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|-&lt;br /&gt;
!Class Variables or methods&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Access Control&lt;br /&gt;
|Public, Protected, Private&lt;br /&gt;
|Public, Protected, Package, Private&lt;br /&gt;
|Public, Protected, Private, Internal, Protected Internal&lt;br /&gt;
|Public, Protected, Private, Friends&lt;br /&gt;
|Name Mangling&lt;br /&gt;
|Protected Data, Public Methods&lt;br /&gt;
|None&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Ruby : Purely Object Oriented ==&lt;br /&gt;
&lt;br /&gt;
Ruby is considered to be a purely Object oriented Language, as everything in it is considered to be an object. Unlike Java, even primitives such as characters, punctuation, numbers and strings are treated as objects in Ruby. Ruby was designed to facilitate the enforcement of Object Oriented Methods. Ruby is a Dynamically Typed Language.&lt;br /&gt;
&lt;br /&gt;
=== Object oriented Concepts in ruby: ===&lt;br /&gt;
&lt;br /&gt;
Ruby is a singly rooted Object Oriented Programming language which has all its classes having the same superclass which is the class Object.&lt;br /&gt;
&lt;br /&gt;
==== Classes:====&lt;br /&gt;
[[Image:RubyClass.png|thumb|right|280px|'''''Classes in Ruby''''']]&lt;br /&gt;
&lt;br /&gt;
A class is used to define a blueprint of a data type. It does not contain any data, but it specifies what kind of variables and methods an object of the class will have.&lt;br /&gt;
A class in Ruby is defined using the keyword class followed by the classname. The variables and methods are defined within the class definition and the class definition terminates with the end keyword.&lt;br /&gt;
&lt;br /&gt;
For ex:&lt;br /&gt;
  class Rectangle&lt;br /&gt;
     def initialize(w,l)&lt;br /&gt;
        @width, @length = w, l&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
Here, the initialize method is used to set the values of the instance variables (@width and @length. The instance variables are the properties of the objects created of this class type. These instance variables can be accessed within the class using an @. These variables can be accessed from outside the class using the accessor methods which are defined inside the class.&lt;br /&gt;
&lt;br /&gt;
In the following case printLength and printWidth are public methods used to access the instance variables from outside the class. These are called accessor methods.&lt;br /&gt;
Similarly, to&lt;br /&gt;
&lt;br /&gt;
   class Rectangle&lt;br /&gt;
  # constructor method&lt;br /&gt;
  def initialize(w,l)&lt;br /&gt;
     @width, @length = w, l&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # accessor methods&lt;br /&gt;
  def printWidth&lt;br /&gt;
     @width&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def printLength&lt;br /&gt;
     @length&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # setter methods&lt;br /&gt;
  def setWidth=(value)&lt;br /&gt;
     @width = value&lt;br /&gt;
  end&lt;br /&gt;
  def setHeight=(value)&lt;br /&gt;
     @height = value&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   # create an object&lt;br /&gt;
     rectagle = Rectangle.new(10, 20)&lt;br /&gt;
&lt;br /&gt;
    # use accessor methods&lt;br /&gt;
      x = rectangle.printWidth()&lt;br /&gt;
      y = rectangle.printLength()&lt;br /&gt;
&lt;br /&gt;
      puts &amp;quot;Width of the Rectangle is : #{x}&amp;quot;&lt;br /&gt;
      puts &amp;quot;Length of the Rectangle is : #{y}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==== Class Methods and Class Variables:====&lt;br /&gt;
A class variable is shared between all instances of the class. The value of the class variable remains the same for all the objects of the class. A class variable is accessed within the class using two @ signs at the beginning of the variable name. The class variables must be initialized within the class.&lt;br /&gt;
&lt;br /&gt;
The class methods are defined using def self.methodname() and ends with an end delimiter. These methods are called using the class name itself, for example: classname.methodname&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Attributes and accessor methods: ===&lt;br /&gt;
&lt;br /&gt;
Every class in Ruby defines a set of attributes for its objects. Ruby provides accessor methods (famously known as getter-setter methods in Java) to access these variables. Ruby provides three kinds of accessor methods:&lt;br /&gt;
&lt;br /&gt;
    Class Car&lt;br /&gt;
       attr_accessor : name, company&lt;br /&gt;
       attr_reader: price&lt;br /&gt;
       attr_writer: color&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
attr_accessor creates a setter and getter method. The attr_reader method creates only the getter method. This is required in case of attributes that are not to be modified by other programs but their value may be viewed. The attr_writer creates the setter method. In case of Java, the setter and getter methods need to be written in 3 lines of code each whereas in Ruby, the methods are written just by specifying the type of accessor, which have already been coded using [http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Access Control:====&lt;br /&gt;
The only easy way to change an object's state in Ruby is by calling one of its methods. Control access to the methods, and you have controlled access to the object. A good rule of the thumb is never to expose methods that could leave an object in an invalid state.&lt;br /&gt;
&lt;br /&gt;
Ruby gives you three levels of protection:&lt;br /&gt;
&lt;br /&gt;
''Public methods'' can be called by everyone - no access control is enforced. A class's instance methods (these do not belong only to one object; instead, every instance of the class can call them) are public by default; anyone can call them. The initialize method is always private.&lt;br /&gt;
&lt;br /&gt;
''Protected methods'' can be invoked only by objects of the defining class and its subclasses. Access is kept within the family. However, usage of protected is limited.&lt;br /&gt;
&lt;br /&gt;
''Private methods'' cannot be called with an explicit receiver - the receiver is always self. This means that private methods can be called only in the context of the current object; you cannot invoke another object's private methods.&lt;br /&gt;
&lt;br /&gt;
Access control is determined dynamically, as the program runs, not statically. You will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
Ex:&lt;br /&gt;
&lt;br /&gt;
    class Employee&lt;br /&gt;
       def initialize(name, age, salary)&lt;br /&gt;
           @name = name&lt;br /&gt;
           @age = age&lt;br /&gt;
           @salary = salary&lt;br /&gt;
       end&lt;br /&gt;
  &lt;br /&gt;
       def name&lt;br /&gt;
           @name&lt;br /&gt;
       end &lt;br /&gt;
       def age&lt;br /&gt;
          @age&lt;br /&gt;
       end&lt;br /&gt;
       def salary&lt;br /&gt;
           @salary&lt;br /&gt;
       end&lt;br /&gt;
&lt;br /&gt;
        public :name&lt;br /&gt;
        protected :age&lt;br /&gt;
        private :salary&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      emp = Employee.new('Jack', 25, 2000);&lt;br /&gt;
      puts emp.name&lt;br /&gt;
      #puts emp.age    #age protected. gives access violation error&lt;br /&gt;
      #puts emp.salary #salary private&lt;br /&gt;
&lt;br /&gt;
====Abstract methods====&lt;br /&gt;
&lt;br /&gt;
Ruby is a dynamically typed language and does not provide abstract classes as in Java or C++. It instead provides modules that can be included within any class. The reason why Ruby does not provide abstract classes is because of the idea that it only matters if two objects can perform a function, they don’t necessarily have to be inherited from the same class. This paradigm is provided by modules, where the method of a module can be redefined as required by a class that includes it.&lt;br /&gt;
&lt;br /&gt;
Also, if required the technique of abstract class can be simulated by something like this:&lt;br /&gt;
&lt;br /&gt;
  Class Vehicle&lt;br /&gt;
   def drive&lt;br /&gt;
   raise NotImplementedError.new(&amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The subclasses that inherit from the above class will have to redefine the method “drive” as required.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Ruby ===&lt;br /&gt;
&lt;br /&gt;
Ruby provides unbounded polymorphism as a dynamically typed language through the concept of “Duck-typing”. “Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface” &amp;lt;ref name=&amp;quot;Duck typing&amp;quot;&amp;gt;[http://en.wikipedia.org/wiki/Duck_typing DuckTyping]&amp;lt;/ref&amp;gt;. This concept is explained by the famous definition as “When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.” This essentially means that in languages like Ruby, it is only checked if an object can implement a function and the type of the object is not taken into consideration. It can be explained better with the help of the following example&lt;br /&gt;
&lt;br /&gt;
  class Lion&lt;br /&gt;
    def roar&lt;br /&gt;
        puts “The Lion roars when hungry”&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
     def roar&lt;br /&gt;
        puts “The man roared in fury and anger”&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   def quality lion&lt;br /&gt;
    lion.roar&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def start&lt;br /&gt;
    leo = Lion.new&lt;br /&gt;
    jack = Person.new&lt;br /&gt;
    quality leo&lt;br /&gt;
    quality jack&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   start&lt;br /&gt;
&lt;br /&gt;
In the above example, leo is an object of class “Lion” and the “roar” method defined in its class is invoked. jack is an object of class “Person” and the corresponding method defined in the Person class is called for him. Although in the real world, both objects are not of the same type, Ruby allows unbounded polymorphism as both of them implement a particular aspect.&lt;br /&gt;
&lt;br /&gt;
Thus, the check for an invoked method is done at run-time in dynamically typed languages and the method defined on that object type is called. If the method is undefined, a “NoMethodError” exception is raised.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Python===&lt;br /&gt;
&lt;br /&gt;
Python also heavily uses duck typing to allow flexibility to programmers. It emphasizes on the interface rather than on the specifc type which allows for polymorphic substitution&amp;lt;ref&amp;gt;[http://docs.python.org/glossary.html#term-duck-typing Python Duck-typing]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
In Python, functions like type() or isistance() is used to check for usual methods to match the type of find out if an object is an instance of a particular class. However, in case of duck typing, the focus is on the attribute of that object, so it checks for the presence of a particular attribute in that object using hasattr().&lt;br /&gt;
&lt;br /&gt;
Example for duck typing in Python is where Python uses a “pickle” module to serialize and de-serialize an object. So, we can introduce a new class that creates a “Tape” object. Now, an object of this type can be passed to the pickle module. Python will not differentiate between a file object or a tape object as long as they both have read() and write() methods.&lt;br /&gt;
&lt;br /&gt;
   class Tapefile():&lt;br /&gt;
      “A tape file”&lt;br /&gt;
       def __init__(self,params):&lt;br /&gt;
          #various initialization for tape access&lt;br /&gt;
       pass&lt;br /&gt;
       def read(len):&lt;br /&gt;
          #use special methods to access tape&lt;br /&gt;
       pass&lt;br /&gt;
       def readline():&lt;br /&gt;
          #use special methods to read a whole line on tape&lt;br /&gt;
       pass&lt;br /&gt;
       def write(value):&lt;br /&gt;
          #use special methods to write on tape&lt;br /&gt;
       pass&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in other languages===&lt;br /&gt;
&lt;br /&gt;
Duck typing is considered to be a feature provided by dynamic languages. C++ as a static language checks for methods by parameter and signature types. Java does not support duck typing either but it is argued that it can be simulated by the use of interfaces. The concept of duck-typing is criticized as it requires the programmer to have a very wide understanding of the source code and that two objects may implement a method in completely different contexts and so it may result in strange errors. &lt;br /&gt;
&lt;br /&gt;
Although duck typing is considered to be a feature of dynamic languages, some static languages like C# and Boo support it by allowing type checking of classes by the compiler to occur at run time.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sthirum</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53186</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b ms</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53186"/>
		<updated>2011-10-20T19:39:53Z</updated>

		<summary type="html">&lt;p&gt;Sthirum: /* Duck Typing in Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Lecture 5: Object Oriented Programming in Ruby =&lt;br /&gt;
&lt;br /&gt;
This wiki tries to cover the Object Oriented concepts in ruby.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Lecture 5 &amp;lt;ref name=&amp;quot;CSC517&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517//common/lectures/notes/lec5.pdf OOP in Ruby], CSC 517 Lecture5&amp;lt;/ref&amp;gt; of CSC 517 covered topics of [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented programming] in [http://www.ruby-lang.org/en/ Ruby]. Object oriented programming is a paradigm in which real world entities are represented as objects, where each object has its own attributes and set of functions that it can perform. This gives a data structure called an &amp;quot;object&amp;quot; that consists of data fields and operations. This paradigm presents features like [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) encapsulation], [http://en.wikipedia.org/wiki/Data_abstraction data abstraction], [http://en.wikipedia.org/wiki/Inheritance_(computer_science) inheritance] and [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This wiki page tries to cover the other OOP languages and their features in a succinct manner and talks about the features of Ruby in detail with examples. We have also tried to compare the various languages in terms of their features to provide a better understanding.&lt;br /&gt;
&lt;br /&gt;
The lecture also covered the concepts of [http://en.wikipedia.org/wiki/Duck_typing duck typing] in Ruby, closures and currying. The latter two have not been included as per the instructions by the instructor.&lt;br /&gt;
&lt;br /&gt;
== Object oriented languages: An Overview ==&lt;br /&gt;
&lt;br /&gt;
==== C++: ====&lt;br /&gt;
[http://en.wikipedia.org/wiki/C%2B%2B C++] is a statically typed, multi-paradigm programming language. It supports some features of object-oriented languages like encapsulation, polymorphism and inheritance. It provides access control by specifiers public, private and protected and also allows “friend” classes. C++  allows [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance] which is eliminated in most other programming languages as it allows a class to inherit from more than one base class which results in an ambiguity effect called [http://en.wikipedia.org/wiki/Diamond_problem Diamond problem].&lt;br /&gt;
&lt;br /&gt;
==== Java: ====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Java_(programming_language) Java] is classified as a statically typed, hybrid object oriented language. Although it provides information hiding, inheritance and polymorphism, it has a few basic types incorporated that are not objects and some of the built-in operators are provided by basic arithmetic and not as messages to objects.[http://www.jvoegele.com/software/langcomp.html] For these reasons, it is not considered to be a “pure” object-oriented language.&lt;br /&gt;
&lt;br /&gt;
Java provides access control and supports private, public and protected access. It also allows to create packages to create and maintain namespaces. Java is designed not to provide multiple inheritance to avoid problems faced in C++. However, it presents a single-rooted hierarchy where “Object” is the ancestor of all classes (seen in Ruby too) and provides interfaces to extend/inherit functionality.&lt;br /&gt;
&lt;br /&gt;
==== Python: ====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Python_(programming_language) Python] is a dynamically typed, interpreted, object oriented language. It is argued that Python is not completely object oriented as it does not provide access control in the form of public, private or protected variables. It has different security mechanisms. However, it allows to emulate protected member by using the prefix '_' before the name and private member by the use of the prefix '__' (double underscore) before the name.&lt;br /&gt;
&lt;br /&gt;
==== Ruby: ====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_language Ruby] is dynamically typed and highly object-oriented, much more than Java and Python. In Ruby, it is said that “everything is an object”.  Every value happens to be an object in Ruby. You may call methods on a string, an integer just like you call on any object in Java. For ex,&lt;br /&gt;
&lt;br /&gt;
     (2.1143).size.times{puts “Hello!”}&lt;br /&gt;
&lt;br /&gt;
This example will display “Hello!” six times on your IRB as the size of the parameter &amp;quot;2.1143&amp;quot; is 6.&lt;br /&gt;
&lt;br /&gt;
== Comparison of object Oriented Languages ==&lt;br /&gt;
The following table compares the Properties of some of the well-known Object Oriented Languages.&amp;lt;ref name=&amp;quot;comparisonOOP&amp;quot;&amp;gt;[http://www.jvoegele.com/software/langcomp.html Programming Language Comparison]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 9pad case0%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Language&lt;br /&gt;
!Ruby&lt;br /&gt;
!Java&lt;br /&gt;
!C#&lt;br /&gt;
!C++&lt;br /&gt;
!Python&lt;br /&gt;
!SmallTalk&lt;br /&gt;
!Perl&lt;br /&gt;
|-&lt;br /&gt;
!Object Orientation&lt;br /&gt;
|Pure&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid/Multi-Paradigm&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Pure&lt;br /&gt;
|Add-on/Hybrid&lt;br /&gt;
|-&lt;br /&gt;
!Static/Dynamic Typing&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|-&lt;br /&gt;
!Generic Classes&lt;br /&gt;
|N/A&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|-&lt;br /&gt;
!Inheritance&lt;br /&gt;
|Single Class/Multiple Mixins&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Multiple&lt;br /&gt;
|Multiple&lt;br /&gt;
|Single&lt;br /&gt;
|Multiple&lt;br /&gt;
|- &lt;br /&gt;
!Feature Renaming&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|NO&lt;br /&gt;
|-&lt;br /&gt;
!Method Overloading&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Operator Overloading&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|-&lt;br /&gt;
!Class Variables or methods&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Access Control&lt;br /&gt;
|Public, Protected, Private&lt;br /&gt;
|Public, Protected, Package, Private&lt;br /&gt;
|Public, Protected, Private, Internal, Protected Internal&lt;br /&gt;
|Public, Protected, Private, Friends&lt;br /&gt;
|Name Mangling&lt;br /&gt;
|Protected Data, Public Methods&lt;br /&gt;
|None&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Ruby : Purely Object Oriented ==&lt;br /&gt;
&lt;br /&gt;
Ruby is considered to be a purely Object oriented Language, as everything in it is considered to be an object. Unlike Java, even primitives such as characters, punctuation, numbers and strings are treated as objects in Ruby. Ruby was designed to facilitate the enforcement of Object Oriented Methods. Ruby is a Dynamically Typed Language.&lt;br /&gt;
&lt;br /&gt;
=== Object oriented Concepts in ruby: ===&lt;br /&gt;
&lt;br /&gt;
Ruby is a singly rooted Object Oriented Programming language which has all its classes having the same superclass which is the class Object.&lt;br /&gt;
&lt;br /&gt;
==== Classes:====&lt;br /&gt;
[[Image:RubyClass.png|thumb|right|280px|'''''Classes in Ruby''''']]&lt;br /&gt;
&lt;br /&gt;
A class is used to define a blueprint of a data type. It does not contain any data, but it specifies what kind of variables and methods an object of the class will have.&lt;br /&gt;
A class in Ruby is defined using the keyword class followed by the classname. The variables and methods are defined within the class definition and the class definition terminates with the end keyword.&lt;br /&gt;
&lt;br /&gt;
For ex:&lt;br /&gt;
  class Rectangle&lt;br /&gt;
     def initialize(w,l)&lt;br /&gt;
        @width, @length = w, l&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
Here, the initialize method is used to set the values of the instance variables (@width and @length. The instance variables are the properties of the objects created of this class type. These instance variables can be accessed within the class using an @. These variables can be accessed from outside the class using the accessor methods which are defined inside the class.&lt;br /&gt;
&lt;br /&gt;
In the following case printLength and printWidth are public methods used to access the instance variables from outside the class. These are called accessor methods.&lt;br /&gt;
Similarly, to&lt;br /&gt;
&lt;br /&gt;
   class Rectangle&lt;br /&gt;
  # constructor method&lt;br /&gt;
  def initialize(w,l)&lt;br /&gt;
     @width, @length = w, l&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # accessor methods&lt;br /&gt;
  def printWidth&lt;br /&gt;
     @width&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def printLength&lt;br /&gt;
     @length&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # setter methods&lt;br /&gt;
  def setWidth=(value)&lt;br /&gt;
     @width = value&lt;br /&gt;
  end&lt;br /&gt;
  def setHeight=(value)&lt;br /&gt;
     @height = value&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   # create an object&lt;br /&gt;
     rectagle = Rectangle.new(10, 20)&lt;br /&gt;
&lt;br /&gt;
    # use accessor methods&lt;br /&gt;
      x = rectangle.printWidth()&lt;br /&gt;
      y = rectangle.printLength()&lt;br /&gt;
&lt;br /&gt;
      puts &amp;quot;Width of the Rectangle is : #{x}&amp;quot;&lt;br /&gt;
      puts &amp;quot;Length of the Rectangle is : #{y}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==== Class Methods and Class Variables:====&lt;br /&gt;
A class variable is shared between all instances of the class. The value of the class variable remains the same for all the objects of the class. A class variable is accessed within the class using two @ signs at the beginning of the variable name. The class variables must be initialized within the class.&lt;br /&gt;
&lt;br /&gt;
The class methods are defined using def self.methodname() and ends with an end delimiter. These methods are called using the class name itself, for example: classname.methodname&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Attributes and accessor methods: ===&lt;br /&gt;
&lt;br /&gt;
Every class in Ruby defines a set of attributes for its objects. Ruby provides accessor methods (famously known as getter-setter methods in Java) to access these variables. Ruby provides three kinds of accessor methods:&lt;br /&gt;
&lt;br /&gt;
    Class Car&lt;br /&gt;
       attr_accessor : name, company&lt;br /&gt;
       attr_reader: price&lt;br /&gt;
       attr_writer: color&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
attr_accessor creates a setter and getter method. The attr_reader method creates only the getter method. This is required in case of attributes that are not to be modified by other programs but their value may be viewed. The attr_writer creates the setter method. In case of Java, the setter and getter methods need to be written in 3 lines of code each whereas in Ruby, the methods are written just by specifying the type of accessor, which have already been coded using [http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Access Control:====&lt;br /&gt;
The only easy way to change an object's state in Ruby is by calling one of its methods. Control access to the methods, and you have controlled access to the object. A good rule of the thumb is never to expose methods that could leave an object in an invalid state.&lt;br /&gt;
&lt;br /&gt;
Ruby gives you three levels of protection:&lt;br /&gt;
&lt;br /&gt;
''Public methods'' can be called by everyone - no access control is enforced. A class's instance methods (these do not belong only to one object; instead, every instance of the class can call them) are public by default; anyone can call them. The initialize method is always private.&lt;br /&gt;
&lt;br /&gt;
''Protected methods'' can be invoked only by objects of the defining class and its subclasses. Access is kept within the family. However, usage of protected is limited.&lt;br /&gt;
&lt;br /&gt;
''Private methods'' cannot be called with an explicit receiver - the receiver is always self. This means that private methods can be called only in the context of the current object; you cannot invoke another object's private methods.&lt;br /&gt;
&lt;br /&gt;
Access control is determined dynamically, as the program runs, not statically. You will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
Ex:&lt;br /&gt;
&lt;br /&gt;
    class Employee&lt;br /&gt;
       def initialize(name, age, salary)&lt;br /&gt;
           @name = name&lt;br /&gt;
           @age = age&lt;br /&gt;
           @salary = salary&lt;br /&gt;
       end&lt;br /&gt;
  &lt;br /&gt;
       def name&lt;br /&gt;
           @name&lt;br /&gt;
       end &lt;br /&gt;
       def age&lt;br /&gt;
          @age&lt;br /&gt;
       end&lt;br /&gt;
       def salary&lt;br /&gt;
           @salary&lt;br /&gt;
       end&lt;br /&gt;
&lt;br /&gt;
        public :name&lt;br /&gt;
        protected :age&lt;br /&gt;
        private :salary&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      emp = Employee.new('Jack', 25, 2000);&lt;br /&gt;
      puts emp.name&lt;br /&gt;
      #puts emp.age    #age protected. gives access violation error&lt;br /&gt;
      #puts emp.salary #salary private&lt;br /&gt;
&lt;br /&gt;
====Abstract methods====&lt;br /&gt;
&lt;br /&gt;
Ruby is a dynamically typed language and does not provide abstract classes as in Java or C++. It instead provides modules that can be included within any class. The reason why Ruby does not provide abstract classes is because of the idea that it only matters if two objects can perform a function, they don’t necessarily have to be inherited from the same class. This paradigm is provided by modules, where the method of a module can be redefined as required by a class that includes it.&lt;br /&gt;
&lt;br /&gt;
Also, if required the technique of abstract class can be simulated by something like this:&lt;br /&gt;
&lt;br /&gt;
  Class Vehicle&lt;br /&gt;
   def drive&lt;br /&gt;
   raise NotImplementedError.new(&amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The subclasses that inherit from the above class will have to redefine the method “drive” as required.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Ruby ===&lt;br /&gt;
&lt;br /&gt;
Ruby provides unbounded polymorphism as a dynamically typed language through the concept of “Duck-typing”. “Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface” &amp;lt;ref name=&amp;quot;Duck typing&amp;quot;&amp;gt;[http://en.wikipedia.org/wiki/Duck_typing DuckTyping]&amp;lt;/ref&amp;gt;. This concept is explained by the famous definition as “When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.” This essentially means that in languages like Ruby, it is only checked if an object can implement a function and the type of the object is not taken into consideration. It can be explained better with the help of the following example&lt;br /&gt;
&lt;br /&gt;
  class Lion&lt;br /&gt;
    def roar&lt;br /&gt;
        puts “The Lion roars when hungry”&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
     def roar&lt;br /&gt;
        puts “The man roared in fury and anger”&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   def quality lion&lt;br /&gt;
    lion.roar&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def start&lt;br /&gt;
    leo = Lion.new&lt;br /&gt;
    jack = Person.new&lt;br /&gt;
    quality leo&lt;br /&gt;
    quality jack&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   start&lt;br /&gt;
&lt;br /&gt;
In the above example, leo is an object of class “Lion” and the “roar” method defined in its class is invoked. jack is an object of class “Person” and the corresponding method defined in the Person class is called for him. Although in the real world, both objects are not of the same type, Ruby allows unbounded polymorphism as both of them implement a particular aspect.&lt;br /&gt;
&lt;br /&gt;
Thus, the check for an invoked method is done at run-time in dynamically typed languages and the method defined on that object type is called. If the method is undefined, a “NoMethodError” exception is raised.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Python===&lt;br /&gt;
&lt;br /&gt;
Python also heavily uses duck typing to allow flexibility to programmers. It emphasizes on the interface rather than on the specifc type which allows for polymorphic substitution [link].&lt;br /&gt;
In Python, functions like type() or isistance() is used to check for usual methods to match the type of find out if an object is an instance of a particular class. However, in case of duck typing, the focus is on the attribute of that object, so it checks for the presence of a particular attribute in that object using hasattr().&lt;br /&gt;
&lt;br /&gt;
Example for duck typing in Python is where Python uses a “pickle” module to serialize and de-serialize an object. So, we can introduce a new class that creates a “Tape” object. Now, an object of this type can be passed to the pickle module. Python will not differentiate between a file object or a tape object as long as they both have read() and write() methods.&lt;br /&gt;
&lt;br /&gt;
   class Tapefile():&lt;br /&gt;
      “A tape file”&lt;br /&gt;
       def __init__(self,params):&lt;br /&gt;
          #various initialization for tape access&lt;br /&gt;
       pass&lt;br /&gt;
       def read(len):&lt;br /&gt;
          #use special methods to access tape&lt;br /&gt;
       pass&lt;br /&gt;
       def readline():&lt;br /&gt;
          #use special methods to read a whole line on tape&lt;br /&gt;
       pass&lt;br /&gt;
       def write(value):&lt;br /&gt;
          #use special methods to write on tape&lt;br /&gt;
       pass&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in other languages===&lt;br /&gt;
&lt;br /&gt;
Duck typing is considered to be a feature provided by dynamic languages. C++ as a static language checks for methods by parameter and signature types. Java does not support duck typing either but it is argued that it can be simulated by the use of interfaces. The concept of duck-typing is criticized as it requires the programmer to have a very wide understanding of the source code and that two objects may implement a method in completely different contexts and so it may result in strange errors. &lt;br /&gt;
&lt;br /&gt;
Although duck typing is considered to be a feature of dynamic languages, some static languages like C# and Boo support it by allowing type checking of classes by the compiler to occur at run time.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sthirum</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53185</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b ms</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53185"/>
		<updated>2011-10-20T19:38:44Z</updated>

		<summary type="html">&lt;p&gt;Sthirum: /* Attributes and accessor methods: */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Lecture 5: Object Oriented Programming in Ruby =&lt;br /&gt;
&lt;br /&gt;
This wiki tries to cover the Object Oriented concepts in ruby.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Lecture 5 &amp;lt;ref name=&amp;quot;CSC517&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517//common/lectures/notes/lec5.pdf OOP in Ruby], CSC 517 Lecture5&amp;lt;/ref&amp;gt; of CSC 517 covered topics of [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented programming] in [http://www.ruby-lang.org/en/ Ruby]. Object oriented programming is a paradigm in which real world entities are represented as objects, where each object has its own attributes and set of functions that it can perform. This gives a data structure called an &amp;quot;object&amp;quot; that consists of data fields and operations. This paradigm presents features like [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) encapsulation], [http://en.wikipedia.org/wiki/Data_abstraction data abstraction], [http://en.wikipedia.org/wiki/Inheritance_(computer_science) inheritance] and [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This wiki page tries to cover the other OOP languages and their features in a succinct manner and talks about the features of Ruby in detail with examples. We have also tried to compare the various languages in terms of their features to provide a better understanding.&lt;br /&gt;
&lt;br /&gt;
The lecture also covered the concepts of [http://en.wikipedia.org/wiki/Duck_typing duck typing] in Ruby, closures and currying. The latter two have not been included as per the instructions by the instructor.&lt;br /&gt;
&lt;br /&gt;
== Object oriented languages: An Overview ==&lt;br /&gt;
&lt;br /&gt;
==== C++: ====&lt;br /&gt;
[http://en.wikipedia.org/wiki/C%2B%2B C++] is a statically typed, multi-paradigm programming language. It supports some features of object-oriented languages like encapsulation, polymorphism and inheritance. It provides access control by specifiers public, private and protected and also allows “friend” classes. C++  allows [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance] which is eliminated in most other programming languages as it allows a class to inherit from more than one base class which results in an ambiguity effect called [http://en.wikipedia.org/wiki/Diamond_problem Diamond problem].&lt;br /&gt;
&lt;br /&gt;
==== Java: ====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Java_(programming_language) Java] is classified as a statically typed, hybrid object oriented language. Although it provides information hiding, inheritance and polymorphism, it has a few basic types incorporated that are not objects and some of the built-in operators are provided by basic arithmetic and not as messages to objects.[http://www.jvoegele.com/software/langcomp.html] For these reasons, it is not considered to be a “pure” object-oriented language.&lt;br /&gt;
&lt;br /&gt;
Java provides access control and supports private, public and protected access. It also allows to create packages to create and maintain namespaces. Java is designed not to provide multiple inheritance to avoid problems faced in C++. However, it presents a single-rooted hierarchy where “Object” is the ancestor of all classes (seen in Ruby too) and provides interfaces to extend/inherit functionality.&lt;br /&gt;
&lt;br /&gt;
==== Python: ====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Python_(programming_language) Python] is a dynamically typed, interpreted, object oriented language. It is argued that Python is not completely object oriented as it does not provide access control in the form of public, private or protected variables. It has different security mechanisms. However, it allows to emulate protected member by using the prefix '_' before the name and private member by the use of the prefix '__' (double underscore) before the name.&lt;br /&gt;
&lt;br /&gt;
==== Ruby: ====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_language Ruby] is dynamically typed and highly object-oriented, much more than Java and Python. In Ruby, it is said that “everything is an object”.  Every value happens to be an object in Ruby. You may call methods on a string, an integer just like you call on any object in Java. For ex,&lt;br /&gt;
&lt;br /&gt;
     (2.1143).size.times{puts “Hello!”}&lt;br /&gt;
&lt;br /&gt;
This example will display “Hello!” six times on your IRB as the size of the parameter &amp;quot;2.1143&amp;quot; is 6.&lt;br /&gt;
&lt;br /&gt;
== Comparison of object Oriented Languages ==&lt;br /&gt;
The following table compares the Properties of some of the well-known Object Oriented Languages.&amp;lt;ref name=&amp;quot;comparisonOOP&amp;quot;&amp;gt;[http://www.jvoegele.com/software/langcomp.html Programming Language Comparison]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 9pad case0%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Language&lt;br /&gt;
!Ruby&lt;br /&gt;
!Java&lt;br /&gt;
!C#&lt;br /&gt;
!C++&lt;br /&gt;
!Python&lt;br /&gt;
!SmallTalk&lt;br /&gt;
!Perl&lt;br /&gt;
|-&lt;br /&gt;
!Object Orientation&lt;br /&gt;
|Pure&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid/Multi-Paradigm&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Pure&lt;br /&gt;
|Add-on/Hybrid&lt;br /&gt;
|-&lt;br /&gt;
!Static/Dynamic Typing&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|-&lt;br /&gt;
!Generic Classes&lt;br /&gt;
|N/A&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|-&lt;br /&gt;
!Inheritance&lt;br /&gt;
|Single Class/Multiple Mixins&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Multiple&lt;br /&gt;
|Multiple&lt;br /&gt;
|Single&lt;br /&gt;
|Multiple&lt;br /&gt;
|- &lt;br /&gt;
!Feature Renaming&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|NO&lt;br /&gt;
|-&lt;br /&gt;
!Method Overloading&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Operator Overloading&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|-&lt;br /&gt;
!Class Variables or methods&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Access Control&lt;br /&gt;
|Public, Protected, Private&lt;br /&gt;
|Public, Protected, Package, Private&lt;br /&gt;
|Public, Protected, Private, Internal, Protected Internal&lt;br /&gt;
|Public, Protected, Private, Friends&lt;br /&gt;
|Name Mangling&lt;br /&gt;
|Protected Data, Public Methods&lt;br /&gt;
|None&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Ruby : Purely Object Oriented ==&lt;br /&gt;
&lt;br /&gt;
Ruby is considered to be a purely Object oriented Language, as everything in it is considered to be an object. Unlike Java, even primitives such as characters, punctuation, numbers and strings are treated as objects in Ruby. Ruby was designed to facilitate the enforcement of Object Oriented Methods. Ruby is a Dynamically Typed Language.&lt;br /&gt;
&lt;br /&gt;
=== Object oriented Concepts in ruby: ===&lt;br /&gt;
&lt;br /&gt;
Ruby is a singly rooted Object Oriented Programming language which has all its classes having the same superclass which is the class Object.&lt;br /&gt;
&lt;br /&gt;
==== Classes:====&lt;br /&gt;
[[Image:RubyClass.png|thumb|right|280px|'''''Classes in Ruby''''']]&lt;br /&gt;
&lt;br /&gt;
A class is used to define a blueprint of a data type. It does not contain any data, but it specifies what kind of variables and methods an object of the class will have.&lt;br /&gt;
A class in Ruby is defined using the keyword class followed by the classname. The variables and methods are defined within the class definition and the class definition terminates with the end keyword.&lt;br /&gt;
&lt;br /&gt;
For ex:&lt;br /&gt;
  class Rectangle&lt;br /&gt;
     def initialize(w,l)&lt;br /&gt;
        @width, @length = w, l&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
Here, the initialize method is used to set the values of the instance variables (@width and @length. The instance variables are the properties of the objects created of this class type. These instance variables can be accessed within the class using an @. These variables can be accessed from outside the class using the accessor methods which are defined inside the class.&lt;br /&gt;
&lt;br /&gt;
In the following case printLength and printWidth are public methods used to access the instance variables from outside the class. These are called accessor methods.&lt;br /&gt;
Similarly, to&lt;br /&gt;
&lt;br /&gt;
   class Rectangle&lt;br /&gt;
  # constructor method&lt;br /&gt;
  def initialize(w,l)&lt;br /&gt;
     @width, @length = w, l&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # accessor methods&lt;br /&gt;
  def printWidth&lt;br /&gt;
     @width&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def printLength&lt;br /&gt;
     @length&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # setter methods&lt;br /&gt;
  def setWidth=(value)&lt;br /&gt;
     @width = value&lt;br /&gt;
  end&lt;br /&gt;
  def setHeight=(value)&lt;br /&gt;
     @height = value&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   # create an object&lt;br /&gt;
     rectagle = Rectangle.new(10, 20)&lt;br /&gt;
&lt;br /&gt;
    # use accessor methods&lt;br /&gt;
      x = rectangle.printWidth()&lt;br /&gt;
      y = rectangle.printLength()&lt;br /&gt;
&lt;br /&gt;
      puts &amp;quot;Width of the Rectangle is : #{x}&amp;quot;&lt;br /&gt;
      puts &amp;quot;Length of the Rectangle is : #{y}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==== Class Methods and Class Variables:====&lt;br /&gt;
A class variable is shared between all instances of the class. The value of the class variable remains the same for all the objects of the class. A class variable is accessed within the class using two @ signs at the beginning of the variable name. The class variables must be initialized within the class.&lt;br /&gt;
&lt;br /&gt;
The class methods are defined using def self.methodname() and ends with an end delimiter. These methods are called using the class name itself, for example: classname.methodname&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Attributes and accessor methods: ===&lt;br /&gt;
&lt;br /&gt;
Every class in Ruby defines a set of attributes for its objects. Ruby provides accessor methods (famously known as getter-setter methods in Java) to access these variables. Ruby provides three kinds of accessor methods:&lt;br /&gt;
&lt;br /&gt;
    Class Car&lt;br /&gt;
       attr_accessor : name, company&lt;br /&gt;
       attr_reader: price&lt;br /&gt;
       attr_writer: color&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
attr_accessor creates a setter and getter method. The attr_reader method creates only the getter method. This is required in case of attributes that are not to be modified by other programs but their value may be viewed. The attr_writer creates the setter method. In case of Java, the setter and getter methods need to be written in 3 lines of code each whereas in Ruby, the methods are written just by specifying the type of accessor, which have already been coded using [http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Access Control:====&lt;br /&gt;
The only easy way to change an object's state in Ruby is by calling one of its methods. Control access to the methods, and you have controlled access to the object. A good rule of the thumb is never to expose methods that could leave an object in an invalid state.&lt;br /&gt;
&lt;br /&gt;
Ruby gives you three levels of protection:&lt;br /&gt;
&lt;br /&gt;
''Public methods'' can be called by everyone - no access control is enforced. A class's instance methods (these do not belong only to one object; instead, every instance of the class can call them) are public by default; anyone can call them. The initialize method is always private.&lt;br /&gt;
&lt;br /&gt;
''Protected methods'' can be invoked only by objects of the defining class and its subclasses. Access is kept within the family. However, usage of protected is limited.&lt;br /&gt;
&lt;br /&gt;
''Private methods'' cannot be called with an explicit receiver - the receiver is always self. This means that private methods can be called only in the context of the current object; you cannot invoke another object's private methods.&lt;br /&gt;
&lt;br /&gt;
Access control is determined dynamically, as the program runs, not statically. You will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
Ex:&lt;br /&gt;
&lt;br /&gt;
    class Employee&lt;br /&gt;
       def initialize(name, age, salary)&lt;br /&gt;
           @name = name&lt;br /&gt;
           @age = age&lt;br /&gt;
           @salary = salary&lt;br /&gt;
       end&lt;br /&gt;
  &lt;br /&gt;
       def name&lt;br /&gt;
           @name&lt;br /&gt;
       end &lt;br /&gt;
       def age&lt;br /&gt;
          @age&lt;br /&gt;
       end&lt;br /&gt;
       def salary&lt;br /&gt;
           @salary&lt;br /&gt;
       end&lt;br /&gt;
&lt;br /&gt;
        public :name&lt;br /&gt;
        protected :age&lt;br /&gt;
        private :salary&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      emp = Employee.new('Jack', 25, 2000);&lt;br /&gt;
      puts emp.name&lt;br /&gt;
      #puts emp.age    #age protected. gives access violation error&lt;br /&gt;
      #puts emp.salary #salary private&lt;br /&gt;
&lt;br /&gt;
====Abstract methods====&lt;br /&gt;
&lt;br /&gt;
Ruby is a dynamically typed language and does not provide abstract classes as in Java or C++. It instead provides modules that can be included within any class. The reason why Ruby does not provide abstract classes is because of the idea that it only matters if two objects can perform a function, they don’t necessarily have to be inherited from the same class. This paradigm is provided by modules, where the method of a module can be redefined as required by a class that includes it.&lt;br /&gt;
&lt;br /&gt;
Also, if required the technique of abstract class can be simulated by something like this:&lt;br /&gt;
&lt;br /&gt;
  Class Vehicle&lt;br /&gt;
   def drive&lt;br /&gt;
   raise NotImplementedError.new(&amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The subclasses that inherit from the above class will have to redefine the method “drive” as required.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Ruby ===&lt;br /&gt;
&lt;br /&gt;
Ruby provides unbounded polymorphism as a dynamically typed language through the concept of “Duck-typing”. “Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface” [link]. This concept is explained by the famous definition as “When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.” This essentially means that in languages like Ruby, it is only checked if an object can implement a function and the type of the object is not taken into consideration. It can be explained better with the help of the following example&lt;br /&gt;
&lt;br /&gt;
  class Lion&lt;br /&gt;
    def roar&lt;br /&gt;
        puts “The Lion roars when hungry”&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
     def roar&lt;br /&gt;
        puts “The man roared in fury and anger”&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   def quality lion&lt;br /&gt;
    lion.roar&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def start&lt;br /&gt;
    leo = Lion.new&lt;br /&gt;
    jack = Person.new&lt;br /&gt;
    quality leo&lt;br /&gt;
    quality jack&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   start&lt;br /&gt;
&lt;br /&gt;
In the above example, leo is an object of class “Lion” and the “roar” method defined in its class is invoked. jack is an object of class “Person” and the corresponding method defined in the Person class is called for him. Although in the real world, both objects are not of the same type, Ruby allows unbounded polymorphism as both of them implement a particular aspect.&lt;br /&gt;
&lt;br /&gt;
Thus, the check for an invoked method is done at run-time in dynamically typed languages and the method defined on that object type is called. If the method is undefined, a “NoMethodError” exception is raised.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Python===&lt;br /&gt;
&lt;br /&gt;
Python also heavily uses duck typing to allow flexibility to programmers. It emphasizes on the interface rather than on the specifc type which allows for polymorphic substitution [link].&lt;br /&gt;
In Python, functions like type() or isistance() is used to check for usual methods to match the type of find out if an object is an instance of a particular class. However, in case of duck typing, the focus is on the attribute of that object, so it checks for the presence of a particular attribute in that object using hasattr().&lt;br /&gt;
&lt;br /&gt;
Example for duck typing in Python is where Python uses a “pickle” module to serialize and de-serialize an object. So, we can introduce a new class that creates a “Tape” object. Now, an object of this type can be passed to the pickle module. Python will not differentiate between a file object or a tape object as long as they both have read() and write() methods.&lt;br /&gt;
&lt;br /&gt;
   class Tapefile():&lt;br /&gt;
      “A tape file”&lt;br /&gt;
       def __init__(self,params):&lt;br /&gt;
          #various initialization for tape access&lt;br /&gt;
       pass&lt;br /&gt;
       def read(len):&lt;br /&gt;
          #use special methods to access tape&lt;br /&gt;
       pass&lt;br /&gt;
       def readline():&lt;br /&gt;
          #use special methods to read a whole line on tape&lt;br /&gt;
       pass&lt;br /&gt;
       def write(value):&lt;br /&gt;
          #use special methods to write on tape&lt;br /&gt;
       pass&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in other languages===&lt;br /&gt;
&lt;br /&gt;
Duck typing is considered to be a feature provided by dynamic languages. C++ as a static language checks for methods by parameter and signature types. Java does not support duck typing either but it is argued that it can be simulated by the use of interfaces. The concept of duck-typing is criticized as it requires the programmer to have a very wide understanding of the source code and that two objects may implement a method in completely different contexts and so it may result in strange errors. &lt;br /&gt;
&lt;br /&gt;
Although duck typing is considered to be a feature of dynamic languages, some static languages like C# and Boo support it by allowing type checking of classes by the compiler to occur at run time.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sthirum</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53184</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b ms</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53184"/>
		<updated>2011-10-20T19:37:26Z</updated>

		<summary type="html">&lt;p&gt;Sthirum: /* Java: */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Lecture 5: Object Oriented Programming in Ruby =&lt;br /&gt;
&lt;br /&gt;
This wiki tries to cover the Object Oriented concepts in ruby.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Lecture 5 &amp;lt;ref name=&amp;quot;CSC517&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517//common/lectures/notes/lec5.pdf OOP in Ruby], CSC 517 Lecture5&amp;lt;/ref&amp;gt; of CSC 517 covered topics of [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented programming] in [http://www.ruby-lang.org/en/ Ruby]. Object oriented programming is a paradigm in which real world entities are represented as objects, where each object has its own attributes and set of functions that it can perform. This gives a data structure called an &amp;quot;object&amp;quot; that consists of data fields and operations. This paradigm presents features like [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) encapsulation], [http://en.wikipedia.org/wiki/Data_abstraction data abstraction], [http://en.wikipedia.org/wiki/Inheritance_(computer_science) inheritance] and [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This wiki page tries to cover the other OOP languages and their features in a succinct manner and talks about the features of Ruby in detail with examples. We have also tried to compare the various languages in terms of their features to provide a better understanding.&lt;br /&gt;
&lt;br /&gt;
The lecture also covered the concepts of [http://en.wikipedia.org/wiki/Duck_typing duck typing] in Ruby, closures and currying. The latter two have not been included as per the instructions by the instructor.&lt;br /&gt;
&lt;br /&gt;
== Object oriented languages: An Overview ==&lt;br /&gt;
&lt;br /&gt;
==== C++: ====&lt;br /&gt;
[http://en.wikipedia.org/wiki/C%2B%2B C++] is a statically typed, multi-paradigm programming language. It supports some features of object-oriented languages like encapsulation, polymorphism and inheritance. It provides access control by specifiers public, private and protected and also allows “friend” classes. C++  allows [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance] which is eliminated in most other programming languages as it allows a class to inherit from more than one base class which results in an ambiguity effect called [http://en.wikipedia.org/wiki/Diamond_problem Diamond problem].&lt;br /&gt;
&lt;br /&gt;
==== Java: ====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Java_(programming_language) Java] is classified as a statically typed, hybrid object oriented language. Although it provides information hiding, inheritance and polymorphism, it has a few basic types incorporated that are not objects and some of the built-in operators are provided by basic arithmetic and not as messages to objects.[http://www.jvoegele.com/software/langcomp.html] For these reasons, it is not considered to be a “pure” object-oriented language.&lt;br /&gt;
&lt;br /&gt;
Java provides access control and supports private, public and protected access. It also allows to create packages to create and maintain namespaces. Java is designed not to provide multiple inheritance to avoid problems faced in C++. However, it presents a single-rooted hierarchy where “Object” is the ancestor of all classes (seen in Ruby too) and provides interfaces to extend/inherit functionality.&lt;br /&gt;
&lt;br /&gt;
==== Python: ====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Python_(programming_language) Python] is a dynamically typed, interpreted, object oriented language. It is argued that Python is not completely object oriented as it does not provide access control in the form of public, private or protected variables. It has different security mechanisms. However, it allows to emulate protected member by using the prefix '_' before the name and private member by the use of the prefix '__' (double underscore) before the name.&lt;br /&gt;
&lt;br /&gt;
==== Ruby: ====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_language Ruby] is dynamically typed and highly object-oriented, much more than Java and Python. In Ruby, it is said that “everything is an object”.  Every value happens to be an object in Ruby. You may call methods on a string, an integer just like you call on any object in Java. For ex,&lt;br /&gt;
&lt;br /&gt;
     (2.1143).size.times{puts “Hello!”}&lt;br /&gt;
&lt;br /&gt;
This example will display “Hello!” six times on your IRB as the size of the parameter &amp;quot;2.1143&amp;quot; is 6.&lt;br /&gt;
&lt;br /&gt;
== Comparison of object Oriented Languages ==&lt;br /&gt;
The following table compares the Properties of some of the well-known Object Oriented Languages.&amp;lt;ref name=&amp;quot;comparisonOOP&amp;quot;&amp;gt;[http://www.jvoegele.com/software/langcomp.html Programming Language Comparison]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 9pad case0%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Language&lt;br /&gt;
!Ruby&lt;br /&gt;
!Java&lt;br /&gt;
!C#&lt;br /&gt;
!C++&lt;br /&gt;
!Python&lt;br /&gt;
!SmallTalk&lt;br /&gt;
!Perl&lt;br /&gt;
|-&lt;br /&gt;
!Object Orientation&lt;br /&gt;
|Pure&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid/Multi-Paradigm&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Pure&lt;br /&gt;
|Add-on/Hybrid&lt;br /&gt;
|-&lt;br /&gt;
!Static/Dynamic Typing&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|-&lt;br /&gt;
!Generic Classes&lt;br /&gt;
|N/A&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|-&lt;br /&gt;
!Inheritance&lt;br /&gt;
|Single Class/Multiple Mixins&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Multiple&lt;br /&gt;
|Multiple&lt;br /&gt;
|Single&lt;br /&gt;
|Multiple&lt;br /&gt;
|- &lt;br /&gt;
!Feature Renaming&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|NO&lt;br /&gt;
|-&lt;br /&gt;
!Method Overloading&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Operator Overloading&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|-&lt;br /&gt;
!Class Variables or methods&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Access Control&lt;br /&gt;
|Public, Protected, Private&lt;br /&gt;
|Public, Protected, Package, Private&lt;br /&gt;
|Public, Protected, Private, Internal, Protected Internal&lt;br /&gt;
|Public, Protected, Private, Friends&lt;br /&gt;
|Name Mangling&lt;br /&gt;
|Protected Data, Public Methods&lt;br /&gt;
|None&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Ruby : Purely Object Oriented ==&lt;br /&gt;
&lt;br /&gt;
Ruby is considered to be a purely Object oriented Language, as everything in it is considered to be an object. Unlike Java, even primitives such as characters, punctuation, numbers and strings are treated as objects in Ruby. Ruby was designed to facilitate the enforcement of Object Oriented Methods. Ruby is a Dynamically Typed Language.&lt;br /&gt;
&lt;br /&gt;
=== Object oriented Concepts in ruby: ===&lt;br /&gt;
&lt;br /&gt;
Ruby is a singly rooted Object Oriented Programming language which has all its classes having the same superclass which is the class Object.&lt;br /&gt;
&lt;br /&gt;
==== Classes:====&lt;br /&gt;
[[Image:RubyClass.png|thumb|right|280px|'''''Classes in Ruby''''']]&lt;br /&gt;
&lt;br /&gt;
A class is used to define a blueprint of a data type. It does not contain any data, but it specifies what kind of variables and methods an object of the class will have.&lt;br /&gt;
A class in Ruby is defined using the keyword class followed by the classname. The variables and methods are defined within the class definition and the class definition terminates with the end keyword.&lt;br /&gt;
&lt;br /&gt;
For ex:&lt;br /&gt;
  class Rectangle&lt;br /&gt;
     def initialize(w,l)&lt;br /&gt;
        @width, @length = w, l&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
Here, the initialize method is used to set the values of the instance variables (@width and @length. The instance variables are the properties of the objects created of this class type. These instance variables can be accessed within the class using an @. These variables can be accessed from outside the class using the accessor methods which are defined inside the class.&lt;br /&gt;
&lt;br /&gt;
In the following case printLength and printWidth are public methods used to access the instance variables from outside the class. These are called accessor methods.&lt;br /&gt;
Similarly, to&lt;br /&gt;
&lt;br /&gt;
   class Rectangle&lt;br /&gt;
  # constructor method&lt;br /&gt;
  def initialize(w,l)&lt;br /&gt;
     @width, @length = w, l&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # accessor methods&lt;br /&gt;
  def printWidth&lt;br /&gt;
     @width&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def printLength&lt;br /&gt;
     @length&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # setter methods&lt;br /&gt;
  def setWidth=(value)&lt;br /&gt;
     @width = value&lt;br /&gt;
  end&lt;br /&gt;
  def setHeight=(value)&lt;br /&gt;
     @height = value&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   # create an object&lt;br /&gt;
     rectagle = Rectangle.new(10, 20)&lt;br /&gt;
&lt;br /&gt;
    # use accessor methods&lt;br /&gt;
      x = rectangle.printWidth()&lt;br /&gt;
      y = rectangle.printLength()&lt;br /&gt;
&lt;br /&gt;
      puts &amp;quot;Width of the Rectangle is : #{x}&amp;quot;&lt;br /&gt;
      puts &amp;quot;Length of the Rectangle is : #{y}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==== Class Methods and Class Variables:====&lt;br /&gt;
A class variable is shared between all instances of the class. The value of the class variable remains the same for all the objects of the class. A class variable is accessed within the class using two @ signs at the beginning of the variable name. The class variables must be initialized within the class.&lt;br /&gt;
&lt;br /&gt;
The class methods are defined using def self.methodname() and ends with an end delimiter. These methods are called using the class name itself, for example: classname.methodname&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Attributes and accessor methods: ===&lt;br /&gt;
&lt;br /&gt;
Every class in Ruby defines a set of attributes for its objects. Ruby provides accessor methods (famously known as getter-setter methods in Java) to access these variables. Ruby provides three kinds of accessor methods:&lt;br /&gt;
&lt;br /&gt;
    Class Car&lt;br /&gt;
       attr_accessor : name, company&lt;br /&gt;
       attr_reader: price&lt;br /&gt;
       attr_writer: color&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
attr_accessor creates a setter and getter method. The attr_reader method creates only the getter method. This is required in case of attributes that are not to be modified by other programs but their value may be viewed. The attr_writer creates the setter method. In case of Java, the setter and getter methods need to be written in 3 lines of code each whereas in Ruby, the methods are written just by specifying the type of accessor, which have already been coded using Metaprogramming.[link]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Access Control:====&lt;br /&gt;
The only easy way to change an object's state in Ruby is by calling one of its methods. Control access to the methods, and you have controlled access to the object. A good rule of the thumb is never to expose methods that could leave an object in an invalid state.&lt;br /&gt;
&lt;br /&gt;
Ruby gives you three levels of protection:&lt;br /&gt;
&lt;br /&gt;
''Public methods'' can be called by everyone - no access control is enforced. A class's instance methods (these do not belong only to one object; instead, every instance of the class can call them) are public by default; anyone can call them. The initialize method is always private.&lt;br /&gt;
&lt;br /&gt;
''Protected methods'' can be invoked only by objects of the defining class and its subclasses. Access is kept within the family. However, usage of protected is limited.&lt;br /&gt;
&lt;br /&gt;
''Private methods'' cannot be called with an explicit receiver - the receiver is always self. This means that private methods can be called only in the context of the current object; you cannot invoke another object's private methods.&lt;br /&gt;
&lt;br /&gt;
Access control is determined dynamically, as the program runs, not statically. You will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
Ex:&lt;br /&gt;
&lt;br /&gt;
    class Employee&lt;br /&gt;
       def initialize(name, age, salary)&lt;br /&gt;
           @name = name&lt;br /&gt;
           @age = age&lt;br /&gt;
           @salary = salary&lt;br /&gt;
       end&lt;br /&gt;
  &lt;br /&gt;
       def name&lt;br /&gt;
           @name&lt;br /&gt;
       end &lt;br /&gt;
       def age&lt;br /&gt;
          @age&lt;br /&gt;
       end&lt;br /&gt;
       def salary&lt;br /&gt;
           @salary&lt;br /&gt;
       end&lt;br /&gt;
&lt;br /&gt;
        public :name&lt;br /&gt;
        protected :age&lt;br /&gt;
        private :salary&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      emp = Employee.new('Jack', 25, 2000);&lt;br /&gt;
      puts emp.name&lt;br /&gt;
      #puts emp.age    #age protected. gives access violation error&lt;br /&gt;
      #puts emp.salary #salary private&lt;br /&gt;
&lt;br /&gt;
====Abstract methods====&lt;br /&gt;
&lt;br /&gt;
Ruby is a dynamically typed language and does not provide abstract classes as in Java or C++. It instead provides modules that can be included within any class. The reason why Ruby does not provide abstract classes is because of the idea that it only matters if two objects can perform a function, they don’t necessarily have to be inherited from the same class. This paradigm is provided by modules, where the method of a module can be redefined as required by a class that includes it.&lt;br /&gt;
&lt;br /&gt;
Also, if required the technique of abstract class can be simulated by something like this:&lt;br /&gt;
&lt;br /&gt;
  Class Vehicle&lt;br /&gt;
   def drive&lt;br /&gt;
   raise NotImplementedError.new(&amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The subclasses that inherit from the above class will have to redefine the method “drive” as required.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Ruby ===&lt;br /&gt;
&lt;br /&gt;
Ruby provides unbounded polymorphism as a dynamically typed language through the concept of “Duck-typing”. “Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface” [link]. This concept is explained by the famous definition as “When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.” This essentially means that in languages like Ruby, it is only checked if an object can implement a function and the type of the object is not taken into consideration. It can be explained better with the help of the following example&lt;br /&gt;
&lt;br /&gt;
  class Lion&lt;br /&gt;
    def roar&lt;br /&gt;
        puts “The Lion roars when hungry”&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
     def roar&lt;br /&gt;
        puts “The man roared in fury and anger”&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   def quality lion&lt;br /&gt;
    lion.roar&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def start&lt;br /&gt;
    leo = Lion.new&lt;br /&gt;
    jack = Person.new&lt;br /&gt;
    quality leo&lt;br /&gt;
    quality jack&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   start&lt;br /&gt;
&lt;br /&gt;
In the above example, leo is an object of class “Lion” and the “roar” method defined in its class is invoked. jack is an object of class “Person” and the corresponding method defined in the Person class is called for him. Although in the real world, both objects are not of the same type, Ruby allows unbounded polymorphism as both of them implement a particular aspect.&lt;br /&gt;
&lt;br /&gt;
Thus, the check for an invoked method is done at run-time in dynamically typed languages and the method defined on that object type is called. If the method is undefined, a “NoMethodError” exception is raised.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Python===&lt;br /&gt;
&lt;br /&gt;
Python also heavily uses duck typing to allow flexibility to programmers. It emphasizes on the interface rather than on the specifc type which allows for polymorphic substitution [link].&lt;br /&gt;
In Python, functions like type() or isistance() is used to check for usual methods to match the type of find out if an object is an instance of a particular class. However, in case of duck typing, the focus is on the attribute of that object, so it checks for the presence of a particular attribute in that object using hasattr().&lt;br /&gt;
&lt;br /&gt;
Example for duck typing in Python is where Python uses a “pickle” module to serialize and de-serialize an object. So, we can introduce a new class that creates a “Tape” object. Now, an object of this type can be passed to the pickle module. Python will not differentiate between a file object or a tape object as long as they both have read() and write() methods.&lt;br /&gt;
&lt;br /&gt;
   class Tapefile():&lt;br /&gt;
      “A tape file”&lt;br /&gt;
       def __init__(self,params):&lt;br /&gt;
          #various initialization for tape access&lt;br /&gt;
       pass&lt;br /&gt;
       def read(len):&lt;br /&gt;
          #use special methods to access tape&lt;br /&gt;
       pass&lt;br /&gt;
       def readline():&lt;br /&gt;
          #use special methods to read a whole line on tape&lt;br /&gt;
       pass&lt;br /&gt;
       def write(value):&lt;br /&gt;
          #use special methods to write on tape&lt;br /&gt;
       pass&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in other languages===&lt;br /&gt;
&lt;br /&gt;
Duck typing is considered to be a feature provided by dynamic languages. C++ as a static language checks for methods by parameter and signature types. Java does not support duck typing either but it is argued that it can be simulated by the use of interfaces. The concept of duck-typing is criticized as it requires the programmer to have a very wide understanding of the source code and that two objects may implement a method in completely different contexts and so it may result in strange errors. &lt;br /&gt;
&lt;br /&gt;
Although duck typing is considered to be a feature of dynamic languages, some static languages like C# and Boo support it by allowing type checking of classes by the compiler to occur at run time.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sthirum</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53183</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b ms</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53183"/>
		<updated>2011-10-20T19:35:32Z</updated>

		<summary type="html">&lt;p&gt;Sthirum: /* C++: */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Lecture 5: Object Oriented Programming in Ruby =&lt;br /&gt;
&lt;br /&gt;
This wiki tries to cover the Object Oriented concepts in ruby.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Lecture 5 &amp;lt;ref name=&amp;quot;CSC517&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517//common/lectures/notes/lec5.pdf OOP in Ruby], CSC 517 Lecture5&amp;lt;/ref&amp;gt; of CSC 517 covered topics of [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented programming] in [http://www.ruby-lang.org/en/ Ruby]. Object oriented programming is a paradigm in which real world entities are represented as objects, where each object has its own attributes and set of functions that it can perform. This gives a data structure called an &amp;quot;object&amp;quot; that consists of data fields and operations. This paradigm presents features like [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) encapsulation], [http://en.wikipedia.org/wiki/Data_abstraction data abstraction], [http://en.wikipedia.org/wiki/Inheritance_(computer_science) inheritance] and [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This wiki page tries to cover the other OOP languages and their features in a succinct manner and talks about the features of Ruby in detail with examples. We have also tried to compare the various languages in terms of their features to provide a better understanding.&lt;br /&gt;
&lt;br /&gt;
The lecture also covered the concepts of [http://en.wikipedia.org/wiki/Duck_typing duck typing] in Ruby, closures and currying. The latter two have not been included as per the instructions by the instructor.&lt;br /&gt;
&lt;br /&gt;
== Object oriented languages: An Overview ==&lt;br /&gt;
&lt;br /&gt;
==== C++: ====&lt;br /&gt;
[http://en.wikipedia.org/wiki/C%2B%2B C++] is a statically typed, multi-paradigm programming language. It supports some features of object-oriented languages like encapsulation, polymorphism and inheritance. It provides access control by specifiers public, private and protected and also allows “friend” classes. C++  allows [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance] which is eliminated in most other programming languages as it allows a class to inherit from more than one base class which results in an ambiguity effect called [http://en.wikipedia.org/wiki/Diamond_problem Diamond problem].&lt;br /&gt;
&lt;br /&gt;
==== Java: ====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Java_(programming_language) Java] is classified as a statically typed, hybrid object oriented language. Although it provides information hiding, inheritance and polymorphism, it has a few basic types incorporated that are not objects and some of the built-in operators are provided by basic arithmetic and not as messages to objects. [link]. For these reasons, it is not considered to be a “pure” object-oriented language.&lt;br /&gt;
&lt;br /&gt;
Java provides access control and supports private, public and protected access. It also allows to create packages to create and maintain namespaces. Java is designed not to provide multiple inheritance to avoid problems faced in C++. However, it presents a single-rooted hierarchy where “Object” is the ancestor of all classes (seen in Ruby too) and provides interfaces to extend/inherit functionality.&lt;br /&gt;
&lt;br /&gt;
==== Python: ====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Python_(programming_language) Python] is a dynamically typed, interpreted, object oriented language. It is argued that Python is not completely object oriented as it does not provide access control in the form of public, private or protected variables. It has different security mechanisms. However, it allows to emulate protected member by using the prefix '_' before the name and private member by the use of the prefix '__' (double underscore) before the name.&lt;br /&gt;
&lt;br /&gt;
==== Ruby: ====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_language Ruby] is dynamically typed and highly object-oriented, much more than Java and Python. In Ruby, it is said that “everything is an object”.  Every value happens to be an object in Ruby. You may call methods on a string, an integer just like you call on any object in Java. For ex,&lt;br /&gt;
&lt;br /&gt;
     (2.1143).size.times{puts “Hello!”}&lt;br /&gt;
&lt;br /&gt;
This example will display “Hello!” six times on your IRB as the size of the parameter &amp;quot;2.1143&amp;quot; is 6.&lt;br /&gt;
&lt;br /&gt;
== Comparison of object Oriented Languages ==&lt;br /&gt;
The following table compares the Properties of some of the well-known Object Oriented Languages.&amp;lt;ref name=&amp;quot;comparisonOOP&amp;quot;&amp;gt;[http://www.jvoegele.com/software/langcomp.html Programming Language Comparison]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 9pad case0%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Language&lt;br /&gt;
!Ruby&lt;br /&gt;
!Java&lt;br /&gt;
!C#&lt;br /&gt;
!C++&lt;br /&gt;
!Python&lt;br /&gt;
!SmallTalk&lt;br /&gt;
!Perl&lt;br /&gt;
|-&lt;br /&gt;
!Object Orientation&lt;br /&gt;
|Pure&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid/Multi-Paradigm&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Pure&lt;br /&gt;
|Add-on/Hybrid&lt;br /&gt;
|-&lt;br /&gt;
!Static/Dynamic Typing&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|-&lt;br /&gt;
!Generic Classes&lt;br /&gt;
|N/A&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|-&lt;br /&gt;
!Inheritance&lt;br /&gt;
|Single Class/Multiple Mixins&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Multiple&lt;br /&gt;
|Multiple&lt;br /&gt;
|Single&lt;br /&gt;
|Multiple&lt;br /&gt;
|- &lt;br /&gt;
!Feature Renaming&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|NO&lt;br /&gt;
|-&lt;br /&gt;
!Method Overloading&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Operator Overloading&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|-&lt;br /&gt;
!Class Variables or methods&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Access Control&lt;br /&gt;
|Public, Protected, Private&lt;br /&gt;
|Public, Protected, Package, Private&lt;br /&gt;
|Public, Protected, Private, Internal, Protected Internal&lt;br /&gt;
|Public, Protected, Private, Friends&lt;br /&gt;
|Name Mangling&lt;br /&gt;
|Protected Data, Public Methods&lt;br /&gt;
|None&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Ruby : Purely Object Oriented ==&lt;br /&gt;
&lt;br /&gt;
Ruby is considered to be a purely Object oriented Language, as everything in it is considered to be an object. Unlike Java, even primitives such as characters, punctuation, numbers and strings are treated as objects in Ruby. Ruby was designed to facilitate the enforcement of Object Oriented Methods. Ruby is a Dynamically Typed Language.&lt;br /&gt;
&lt;br /&gt;
=== Object oriented Concepts in ruby: ===&lt;br /&gt;
&lt;br /&gt;
Ruby is a singly rooted Object Oriented Programming language which has all its classes having the same superclass which is the class Object.&lt;br /&gt;
&lt;br /&gt;
==== Classes:====&lt;br /&gt;
[[Image:RubyClass.png|thumb|right|280px|'''''Classes in Ruby''''']]&lt;br /&gt;
&lt;br /&gt;
A class is used to define a blueprint of a data type. It does not contain any data, but it specifies what kind of variables and methods an object of the class will have.&lt;br /&gt;
A class in Ruby is defined using the keyword class followed by the classname. The variables and methods are defined within the class definition and the class definition terminates with the end keyword.&lt;br /&gt;
&lt;br /&gt;
For ex:&lt;br /&gt;
  class Rectangle&lt;br /&gt;
     def initialize(w,l)&lt;br /&gt;
        @width, @length = w, l&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
Here, the initialize method is used to set the values of the instance variables (@width and @length. The instance variables are the properties of the objects created of this class type. These instance variables can be accessed within the class using an @. These variables can be accessed from outside the class using the accessor methods which are defined inside the class.&lt;br /&gt;
&lt;br /&gt;
In the following case printLength and printWidth are public methods used to access the instance variables from outside the class. These are called accessor methods.&lt;br /&gt;
Similarly, to&lt;br /&gt;
&lt;br /&gt;
   class Rectangle&lt;br /&gt;
  # constructor method&lt;br /&gt;
  def initialize(w,l)&lt;br /&gt;
     @width, @length = w, l&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # accessor methods&lt;br /&gt;
  def printWidth&lt;br /&gt;
     @width&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def printLength&lt;br /&gt;
     @length&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # setter methods&lt;br /&gt;
  def setWidth=(value)&lt;br /&gt;
     @width = value&lt;br /&gt;
  end&lt;br /&gt;
  def setHeight=(value)&lt;br /&gt;
     @height = value&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   # create an object&lt;br /&gt;
     rectagle = Rectangle.new(10, 20)&lt;br /&gt;
&lt;br /&gt;
    # use accessor methods&lt;br /&gt;
      x = rectangle.printWidth()&lt;br /&gt;
      y = rectangle.printLength()&lt;br /&gt;
&lt;br /&gt;
      puts &amp;quot;Width of the Rectangle is : #{x}&amp;quot;&lt;br /&gt;
      puts &amp;quot;Length of the Rectangle is : #{y}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==== Class Methods and Class Variables:====&lt;br /&gt;
A class variable is shared between all instances of the class. The value of the class variable remains the same for all the objects of the class. A class variable is accessed within the class using two @ signs at the beginning of the variable name. The class variables must be initialized within the class.&lt;br /&gt;
&lt;br /&gt;
The class methods are defined using def self.methodname() and ends with an end delimiter. These methods are called using the class name itself, for example: classname.methodname&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Attributes and accessor methods: ===&lt;br /&gt;
&lt;br /&gt;
Every class in Ruby defines a set of attributes for its objects. Ruby provides accessor methods (famously known as getter-setter methods in Java) to access these variables. Ruby provides three kinds of accessor methods:&lt;br /&gt;
&lt;br /&gt;
    Class Car&lt;br /&gt;
       attr_accessor : name, company&lt;br /&gt;
       attr_reader: price&lt;br /&gt;
       attr_writer: color&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
attr_accessor creates a setter and getter method. The attr_reader method creates only the getter method. This is required in case of attributes that are not to be modified by other programs but their value may be viewed. The attr_writer creates the setter method. In case of Java, the setter and getter methods need to be written in 3 lines of code each whereas in Ruby, the methods are written just by specifying the type of accessor, which have already been coded using Metaprogramming.[link]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Access Control:====&lt;br /&gt;
The only easy way to change an object's state in Ruby is by calling one of its methods. Control access to the methods, and you have controlled access to the object. A good rule of the thumb is never to expose methods that could leave an object in an invalid state.&lt;br /&gt;
&lt;br /&gt;
Ruby gives you three levels of protection:&lt;br /&gt;
&lt;br /&gt;
''Public methods'' can be called by everyone - no access control is enforced. A class's instance methods (these do not belong only to one object; instead, every instance of the class can call them) are public by default; anyone can call them. The initialize method is always private.&lt;br /&gt;
&lt;br /&gt;
''Protected methods'' can be invoked only by objects of the defining class and its subclasses. Access is kept within the family. However, usage of protected is limited.&lt;br /&gt;
&lt;br /&gt;
''Private methods'' cannot be called with an explicit receiver - the receiver is always self. This means that private methods can be called only in the context of the current object; you cannot invoke another object's private methods.&lt;br /&gt;
&lt;br /&gt;
Access control is determined dynamically, as the program runs, not statically. You will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
Ex:&lt;br /&gt;
&lt;br /&gt;
    class Employee&lt;br /&gt;
       def initialize(name, age, salary)&lt;br /&gt;
           @name = name&lt;br /&gt;
           @age = age&lt;br /&gt;
           @salary = salary&lt;br /&gt;
       end&lt;br /&gt;
  &lt;br /&gt;
       def name&lt;br /&gt;
           @name&lt;br /&gt;
       end &lt;br /&gt;
       def age&lt;br /&gt;
          @age&lt;br /&gt;
       end&lt;br /&gt;
       def salary&lt;br /&gt;
           @salary&lt;br /&gt;
       end&lt;br /&gt;
&lt;br /&gt;
        public :name&lt;br /&gt;
        protected :age&lt;br /&gt;
        private :salary&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      emp = Employee.new('Jack', 25, 2000);&lt;br /&gt;
      puts emp.name&lt;br /&gt;
      #puts emp.age    #age protected. gives access violation error&lt;br /&gt;
      #puts emp.salary #salary private&lt;br /&gt;
&lt;br /&gt;
====Abstract methods====&lt;br /&gt;
&lt;br /&gt;
Ruby is a dynamically typed language and does not provide abstract classes as in Java or C++. It instead provides modules that can be included within any class. The reason why Ruby does not provide abstract classes is because of the idea that it only matters if two objects can perform a function, they don’t necessarily have to be inherited from the same class. This paradigm is provided by modules, where the method of a module can be redefined as required by a class that includes it.&lt;br /&gt;
&lt;br /&gt;
Also, if required the technique of abstract class can be simulated by something like this:&lt;br /&gt;
&lt;br /&gt;
  Class Vehicle&lt;br /&gt;
   def drive&lt;br /&gt;
   raise NotImplementedError.new(&amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The subclasses that inherit from the above class will have to redefine the method “drive” as required.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Ruby ===&lt;br /&gt;
&lt;br /&gt;
Ruby provides unbounded polymorphism as a dynamically typed language through the concept of “Duck-typing”. “Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface” [link]. This concept is explained by the famous definition as “When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.” This essentially means that in languages like Ruby, it is only checked if an object can implement a function and the type of the object is not taken into consideration. It can be explained better with the help of the following example&lt;br /&gt;
&lt;br /&gt;
  class Lion&lt;br /&gt;
    def roar&lt;br /&gt;
        puts “The Lion roars when hungry”&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
     def roar&lt;br /&gt;
        puts “The man roared in fury and anger”&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   def quality lion&lt;br /&gt;
    lion.roar&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def start&lt;br /&gt;
    leo = Lion.new&lt;br /&gt;
    jack = Person.new&lt;br /&gt;
    quality leo&lt;br /&gt;
    quality jack&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   start&lt;br /&gt;
&lt;br /&gt;
In the above example, leo is an object of class “Lion” and the “roar” method defined in its class is invoked. jack is an object of class “Person” and the corresponding method defined in the Person class is called for him. Although in the real world, both objects are not of the same type, Ruby allows unbounded polymorphism as both of them implement a particular aspect.&lt;br /&gt;
&lt;br /&gt;
Thus, the check for an invoked method is done at run-time in dynamically typed languages and the method defined on that object type is called. If the method is undefined, a “NoMethodError” exception is raised.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Python===&lt;br /&gt;
&lt;br /&gt;
Python also heavily uses duck typing to allow flexibility to programmers. It emphasizes on the interface rather than on the specifc type which allows for polymorphic substitution [link].&lt;br /&gt;
In Python, functions like type() or isistance() is used to check for usual methods to match the type of find out if an object is an instance of a particular class. However, in case of duck typing, the focus is on the attribute of that object, so it checks for the presence of a particular attribute in that object using hasattr().&lt;br /&gt;
&lt;br /&gt;
Example for duck typing in Python is where Python uses a “pickle” module to serialize and de-serialize an object. So, we can introduce a new class that creates a “Tape” object. Now, an object of this type can be passed to the pickle module. Python will not differentiate between a file object or a tape object as long as they both have read() and write() methods.&lt;br /&gt;
&lt;br /&gt;
   class Tapefile():&lt;br /&gt;
      “A tape file”&lt;br /&gt;
       def __init__(self,params):&lt;br /&gt;
          #various initialization for tape access&lt;br /&gt;
       pass&lt;br /&gt;
       def read(len):&lt;br /&gt;
          #use special methods to access tape&lt;br /&gt;
       pass&lt;br /&gt;
       def readline():&lt;br /&gt;
          #use special methods to read a whole line on tape&lt;br /&gt;
       pass&lt;br /&gt;
       def write(value):&lt;br /&gt;
          #use special methods to write on tape&lt;br /&gt;
       pass&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in other languages===&lt;br /&gt;
&lt;br /&gt;
Duck typing is considered to be a feature provided by dynamic languages. C++ as a static language checks for methods by parameter and signature types. Java does not support duck typing either but it is argued that it can be simulated by the use of interfaces. The concept of duck-typing is criticized as it requires the programmer to have a very wide understanding of the source code and that two objects may implement a method in completely different contexts and so it may result in strange errors. &lt;br /&gt;
&lt;br /&gt;
Although duck typing is considered to be a feature of dynamic languages, some static languages like C# and Boo support it by allowing type checking of classes by the compiler to occur at run time.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sthirum</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53182</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b ms</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53182"/>
		<updated>2011-10-20T19:34:25Z</updated>

		<summary type="html">&lt;p&gt;Sthirum: /* Comparison of object Oriented Languages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Lecture 5: Object Oriented Programming in Ruby =&lt;br /&gt;
&lt;br /&gt;
This wiki tries to cover the Object Oriented concepts in ruby.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Lecture 5 &amp;lt;ref name=&amp;quot;CSC517&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517//common/lectures/notes/lec5.pdf OOP in Ruby], CSC 517 Lecture5&amp;lt;/ref&amp;gt; of CSC 517 covered topics of [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented programming] in [http://www.ruby-lang.org/en/ Ruby]. Object oriented programming is a paradigm in which real world entities are represented as objects, where each object has its own attributes and set of functions that it can perform. This gives a data structure called an &amp;quot;object&amp;quot; that consists of data fields and operations. This paradigm presents features like [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) encapsulation], [http://en.wikipedia.org/wiki/Data_abstraction data abstraction], [http://en.wikipedia.org/wiki/Inheritance_(computer_science) inheritance] and [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This wiki page tries to cover the other OOP languages and their features in a succinct manner and talks about the features of Ruby in detail with examples. We have also tried to compare the various languages in terms of their features to provide a better understanding.&lt;br /&gt;
&lt;br /&gt;
The lecture also covered the concepts of [http://en.wikipedia.org/wiki/Duck_typing duck typing] in Ruby, closures and currying. The latter two have not been included as per the instructions by the instructor.&lt;br /&gt;
&lt;br /&gt;
== Object oriented languages: An Overview ==&lt;br /&gt;
&lt;br /&gt;
==== C++: ====&lt;br /&gt;
[http://en.wikipedia.org/wiki/C%2B%2B C++] is a statically typed, multi-paradigm programming language. It supports some features of object-oriented languages like encapsulation, polymorphism and inheritance. It provides access control by specifiers public, private and protected and also allows “friend” classes. C++  allows multiple inheritance which is eliminated in most other programming languages as it allows a class to inherit from more than one base class which results in an ambiguity effect called [http://en.wikipedia.org/wiki/Diamond_problem Diamond problem].&lt;br /&gt;
&lt;br /&gt;
==== Java: ====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Java_(programming_language) Java] is classified as a statically typed, hybrid object oriented language. Although it provides information hiding, inheritance and polymorphism, it has a few basic types incorporated that are not objects and some of the built-in operators are provided by basic arithmetic and not as messages to objects. [link]. For these reasons, it is not considered to be a “pure” object-oriented language.&lt;br /&gt;
&lt;br /&gt;
Java provides access control and supports private, public and protected access. It also allows to create packages to create and maintain namespaces. Java is designed not to provide multiple inheritance to avoid problems faced in C++. However, it presents a single-rooted hierarchy where “Object” is the ancestor of all classes (seen in Ruby too) and provides interfaces to extend/inherit functionality.&lt;br /&gt;
&lt;br /&gt;
==== Python: ====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Python_(programming_language) Python] is a dynamically typed, interpreted, object oriented language. It is argued that Python is not completely object oriented as it does not provide access control in the form of public, private or protected variables. It has different security mechanisms. However, it allows to emulate protected member by using the prefix '_' before the name and private member by the use of the prefix '__' (double underscore) before the name.&lt;br /&gt;
&lt;br /&gt;
==== Ruby: ====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_language Ruby] is dynamically typed and highly object-oriented, much more than Java and Python. In Ruby, it is said that “everything is an object”.  Every value happens to be an object in Ruby. You may call methods on a string, an integer just like you call on any object in Java. For ex,&lt;br /&gt;
&lt;br /&gt;
     (2.1143).size.times{puts “Hello!”}&lt;br /&gt;
&lt;br /&gt;
This example will display “Hello!” six times on your IRB as the size of the parameter &amp;quot;2.1143&amp;quot; is 6.&lt;br /&gt;
&lt;br /&gt;
== Comparison of object Oriented Languages ==&lt;br /&gt;
The following table compares the Properties of some of the well-known Object Oriented Languages.&amp;lt;ref name=&amp;quot;comparisonOOP&amp;quot;&amp;gt;[http://www.jvoegele.com/software/langcomp.html Programming Language Comparison]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 9pad case0%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Language&lt;br /&gt;
!Ruby&lt;br /&gt;
!Java&lt;br /&gt;
!C#&lt;br /&gt;
!C++&lt;br /&gt;
!Python&lt;br /&gt;
!SmallTalk&lt;br /&gt;
!Perl&lt;br /&gt;
|-&lt;br /&gt;
!Object Orientation&lt;br /&gt;
|Pure&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid/Multi-Paradigm&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Pure&lt;br /&gt;
|Add-on/Hybrid&lt;br /&gt;
|-&lt;br /&gt;
!Static/Dynamic Typing&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|-&lt;br /&gt;
!Generic Classes&lt;br /&gt;
|N/A&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|-&lt;br /&gt;
!Inheritance&lt;br /&gt;
|Single Class/Multiple Mixins&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Multiple&lt;br /&gt;
|Multiple&lt;br /&gt;
|Single&lt;br /&gt;
|Multiple&lt;br /&gt;
|- &lt;br /&gt;
!Feature Renaming&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|NO&lt;br /&gt;
|-&lt;br /&gt;
!Method Overloading&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Operator Overloading&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|-&lt;br /&gt;
!Class Variables or methods&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Access Control&lt;br /&gt;
|Public, Protected, Private&lt;br /&gt;
|Public, Protected, Package, Private&lt;br /&gt;
|Public, Protected, Private, Internal, Protected Internal&lt;br /&gt;
|Public, Protected, Private, Friends&lt;br /&gt;
|Name Mangling&lt;br /&gt;
|Protected Data, Public Methods&lt;br /&gt;
|None&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Ruby : Purely Object Oriented ==&lt;br /&gt;
&lt;br /&gt;
Ruby is considered to be a purely Object oriented Language, as everything in it is considered to be an object. Unlike Java, even primitives such as characters, punctuation, numbers and strings are treated as objects in Ruby. Ruby was designed to facilitate the enforcement of Object Oriented Methods. Ruby is a Dynamically Typed Language.&lt;br /&gt;
&lt;br /&gt;
=== Object oriented Concepts in ruby: ===&lt;br /&gt;
&lt;br /&gt;
Ruby is a singly rooted Object Oriented Programming language which has all its classes having the same superclass which is the class Object.&lt;br /&gt;
&lt;br /&gt;
==== Classes:====&lt;br /&gt;
[[Image:RubyClass.png|thumb|right|280px|'''''Classes in Ruby''''']]&lt;br /&gt;
&lt;br /&gt;
A class is used to define a blueprint of a data type. It does not contain any data, but it specifies what kind of variables and methods an object of the class will have.&lt;br /&gt;
A class in Ruby is defined using the keyword class followed by the classname. The variables and methods are defined within the class definition and the class definition terminates with the end keyword.&lt;br /&gt;
&lt;br /&gt;
For ex:&lt;br /&gt;
  class Rectangle&lt;br /&gt;
     def initialize(w,l)&lt;br /&gt;
        @width, @length = w, l&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
Here, the initialize method is used to set the values of the instance variables (@width and @length. The instance variables are the properties of the objects created of this class type. These instance variables can be accessed within the class using an @. These variables can be accessed from outside the class using the accessor methods which are defined inside the class.&lt;br /&gt;
&lt;br /&gt;
In the following case printLength and printWidth are public methods used to access the instance variables from outside the class. These are called accessor methods.&lt;br /&gt;
Similarly, to&lt;br /&gt;
&lt;br /&gt;
   class Rectangle&lt;br /&gt;
  # constructor method&lt;br /&gt;
  def initialize(w,l)&lt;br /&gt;
     @width, @length = w, l&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # accessor methods&lt;br /&gt;
  def printWidth&lt;br /&gt;
     @width&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def printLength&lt;br /&gt;
     @length&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # setter methods&lt;br /&gt;
  def setWidth=(value)&lt;br /&gt;
     @width = value&lt;br /&gt;
  end&lt;br /&gt;
  def setHeight=(value)&lt;br /&gt;
     @height = value&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   # create an object&lt;br /&gt;
     rectagle = Rectangle.new(10, 20)&lt;br /&gt;
&lt;br /&gt;
    # use accessor methods&lt;br /&gt;
      x = rectangle.printWidth()&lt;br /&gt;
      y = rectangle.printLength()&lt;br /&gt;
&lt;br /&gt;
      puts &amp;quot;Width of the Rectangle is : #{x}&amp;quot;&lt;br /&gt;
      puts &amp;quot;Length of the Rectangle is : #{y}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==== Class Methods and Class Variables:====&lt;br /&gt;
A class variable is shared between all instances of the class. The value of the class variable remains the same for all the objects of the class. A class variable is accessed within the class using two @ signs at the beginning of the variable name. The class variables must be initialized within the class.&lt;br /&gt;
&lt;br /&gt;
The class methods are defined using def self.methodname() and ends with an end delimiter. These methods are called using the class name itself, for example: classname.methodname&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Attributes and accessor methods: ===&lt;br /&gt;
&lt;br /&gt;
Every class in Ruby defines a set of attributes for its objects. Ruby provides accessor methods (famously known as getter-setter methods in Java) to access these variables. Ruby provides three kinds of accessor methods:&lt;br /&gt;
&lt;br /&gt;
    Class Car&lt;br /&gt;
       attr_accessor : name, company&lt;br /&gt;
       attr_reader: price&lt;br /&gt;
       attr_writer: color&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
attr_accessor creates a setter and getter method. The attr_reader method creates only the getter method. This is required in case of attributes that are not to be modified by other programs but their value may be viewed. The attr_writer creates the setter method. In case of Java, the setter and getter methods need to be written in 3 lines of code each whereas in Ruby, the methods are written just by specifying the type of accessor, which have already been coded using Metaprogramming.[link]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Access Control:====&lt;br /&gt;
The only easy way to change an object's state in Ruby is by calling one of its methods. Control access to the methods, and you have controlled access to the object. A good rule of the thumb is never to expose methods that could leave an object in an invalid state.&lt;br /&gt;
&lt;br /&gt;
Ruby gives you three levels of protection:&lt;br /&gt;
&lt;br /&gt;
''Public methods'' can be called by everyone - no access control is enforced. A class's instance methods (these do not belong only to one object; instead, every instance of the class can call them) are public by default; anyone can call them. The initialize method is always private.&lt;br /&gt;
&lt;br /&gt;
''Protected methods'' can be invoked only by objects of the defining class and its subclasses. Access is kept within the family. However, usage of protected is limited.&lt;br /&gt;
&lt;br /&gt;
''Private methods'' cannot be called with an explicit receiver - the receiver is always self. This means that private methods can be called only in the context of the current object; you cannot invoke another object's private methods.&lt;br /&gt;
&lt;br /&gt;
Access control is determined dynamically, as the program runs, not statically. You will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
Ex:&lt;br /&gt;
&lt;br /&gt;
    class Employee&lt;br /&gt;
       def initialize(name, age, salary)&lt;br /&gt;
           @name = name&lt;br /&gt;
           @age = age&lt;br /&gt;
           @salary = salary&lt;br /&gt;
       end&lt;br /&gt;
  &lt;br /&gt;
       def name&lt;br /&gt;
           @name&lt;br /&gt;
       end &lt;br /&gt;
       def age&lt;br /&gt;
          @age&lt;br /&gt;
       end&lt;br /&gt;
       def salary&lt;br /&gt;
           @salary&lt;br /&gt;
       end&lt;br /&gt;
&lt;br /&gt;
        public :name&lt;br /&gt;
        protected :age&lt;br /&gt;
        private :salary&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      emp = Employee.new('Jack', 25, 2000);&lt;br /&gt;
      puts emp.name&lt;br /&gt;
      #puts emp.age    #age protected. gives access violation error&lt;br /&gt;
      #puts emp.salary #salary private&lt;br /&gt;
&lt;br /&gt;
====Abstract methods====&lt;br /&gt;
&lt;br /&gt;
Ruby is a dynamically typed language and does not provide abstract classes as in Java or C++. It instead provides modules that can be included within any class. The reason why Ruby does not provide abstract classes is because of the idea that it only matters if two objects can perform a function, they don’t necessarily have to be inherited from the same class. This paradigm is provided by modules, where the method of a module can be redefined as required by a class that includes it.&lt;br /&gt;
&lt;br /&gt;
Also, if required the technique of abstract class can be simulated by something like this:&lt;br /&gt;
&lt;br /&gt;
  Class Vehicle&lt;br /&gt;
   def drive&lt;br /&gt;
   raise NotImplementedError.new(&amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The subclasses that inherit from the above class will have to redefine the method “drive” as required.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Ruby ===&lt;br /&gt;
&lt;br /&gt;
Ruby provides unbounded polymorphism as a dynamically typed language through the concept of “Duck-typing”. “Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface” [link]. This concept is explained by the famous definition as “When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.” This essentially means that in languages like Ruby, it is only checked if an object can implement a function and the type of the object is not taken into consideration. It can be explained better with the help of the following example&lt;br /&gt;
&lt;br /&gt;
  class Lion&lt;br /&gt;
    def roar&lt;br /&gt;
        puts “The Lion roars when hungry”&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
     def roar&lt;br /&gt;
        puts “The man roared in fury and anger”&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   def quality lion&lt;br /&gt;
    lion.roar&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def start&lt;br /&gt;
    leo = Lion.new&lt;br /&gt;
    jack = Person.new&lt;br /&gt;
    quality leo&lt;br /&gt;
    quality jack&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   start&lt;br /&gt;
&lt;br /&gt;
In the above example, leo is an object of class “Lion” and the “roar” method defined in its class is invoked. jack is an object of class “Person” and the corresponding method defined in the Person class is called for him. Although in the real world, both objects are not of the same type, Ruby allows unbounded polymorphism as both of them implement a particular aspect.&lt;br /&gt;
&lt;br /&gt;
Thus, the check for an invoked method is done at run-time in dynamically typed languages and the method defined on that object type is called. If the method is undefined, a “NoMethodError” exception is raised.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Python===&lt;br /&gt;
&lt;br /&gt;
Python also heavily uses duck typing to allow flexibility to programmers. It emphasizes on the interface rather than on the specifc type which allows for polymorphic substitution [link].&lt;br /&gt;
In Python, functions like type() or isistance() is used to check for usual methods to match the type of find out if an object is an instance of a particular class. However, in case of duck typing, the focus is on the attribute of that object, so it checks for the presence of a particular attribute in that object using hasattr().&lt;br /&gt;
&lt;br /&gt;
Example for duck typing in Python is where Python uses a “pickle” module to serialize and de-serialize an object. So, we can introduce a new class that creates a “Tape” object. Now, an object of this type can be passed to the pickle module. Python will not differentiate between a file object or a tape object as long as they both have read() and write() methods.&lt;br /&gt;
&lt;br /&gt;
   class Tapefile():&lt;br /&gt;
      “A tape file”&lt;br /&gt;
       def __init__(self,params):&lt;br /&gt;
          #various initialization for tape access&lt;br /&gt;
       pass&lt;br /&gt;
       def read(len):&lt;br /&gt;
          #use special methods to access tape&lt;br /&gt;
       pass&lt;br /&gt;
       def readline():&lt;br /&gt;
          #use special methods to read a whole line on tape&lt;br /&gt;
       pass&lt;br /&gt;
       def write(value):&lt;br /&gt;
          #use special methods to write on tape&lt;br /&gt;
       pass&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in other languages===&lt;br /&gt;
&lt;br /&gt;
Duck typing is considered to be a feature provided by dynamic languages. C++ as a static language checks for methods by parameter and signature types. Java does not support duck typing either but it is argued that it can be simulated by the use of interfaces. The concept of duck-typing is criticized as it requires the programmer to have a very wide understanding of the source code and that two objects may implement a method in completely different contexts and so it may result in strange errors. &lt;br /&gt;
&lt;br /&gt;
Although duck typing is considered to be a feature of dynamic languages, some static languages like C# and Boo support it by allowing type checking of classes by the compiler to occur at run time.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sthirum</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53181</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b ms</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53181"/>
		<updated>2011-10-20T19:32:36Z</updated>

		<summary type="html">&lt;p&gt;Sthirum: /* Object oriented languages: An Overview */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Lecture 5: Object Oriented Programming in Ruby =&lt;br /&gt;
&lt;br /&gt;
This wiki tries to cover the Object Oriented concepts in ruby.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Lecture 5 &amp;lt;ref name=&amp;quot;CSC517&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517//common/lectures/notes/lec5.pdf OOP in Ruby], CSC 517 Lecture5&amp;lt;/ref&amp;gt; of CSC 517 covered topics of [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented programming] in [http://www.ruby-lang.org/en/ Ruby]. Object oriented programming is a paradigm in which real world entities are represented as objects, where each object has its own attributes and set of functions that it can perform. This gives a data structure called an &amp;quot;object&amp;quot; that consists of data fields and operations. This paradigm presents features like [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) encapsulation], [http://en.wikipedia.org/wiki/Data_abstraction data abstraction], [http://en.wikipedia.org/wiki/Inheritance_(computer_science) inheritance] and [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This wiki page tries to cover the other OOP languages and their features in a succinct manner and talks about the features of Ruby in detail with examples. We have also tried to compare the various languages in terms of their features to provide a better understanding.&lt;br /&gt;
&lt;br /&gt;
The lecture also covered the concepts of [http://en.wikipedia.org/wiki/Duck_typing duck typing] in Ruby, closures and currying. The latter two have not been included as per the instructions by the instructor.&lt;br /&gt;
&lt;br /&gt;
== Object oriented languages: An Overview ==&lt;br /&gt;
&lt;br /&gt;
==== C++: ====&lt;br /&gt;
[http://en.wikipedia.org/wiki/C%2B%2B C++] is a statically typed, multi-paradigm programming language. It supports some features of object-oriented languages like encapsulation, polymorphism and inheritance. It provides access control by specifiers public, private and protected and also allows “friend” classes. C++  allows multiple inheritance which is eliminated in most other programming languages as it allows a class to inherit from more than one base class which results in an ambiguity effect called [http://en.wikipedia.org/wiki/Diamond_problem Diamond problem].&lt;br /&gt;
&lt;br /&gt;
==== Java: ====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Java_(programming_language) Java] is classified as a statically typed, hybrid object oriented language. Although it provides information hiding, inheritance and polymorphism, it has a few basic types incorporated that are not objects and some of the built-in operators are provided by basic arithmetic and not as messages to objects. [link]. For these reasons, it is not considered to be a “pure” object-oriented language.&lt;br /&gt;
&lt;br /&gt;
Java provides access control and supports private, public and protected access. It also allows to create packages to create and maintain namespaces. Java is designed not to provide multiple inheritance to avoid problems faced in C++. However, it presents a single-rooted hierarchy where “Object” is the ancestor of all classes (seen in Ruby too) and provides interfaces to extend/inherit functionality.&lt;br /&gt;
&lt;br /&gt;
==== Python: ====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Python_(programming_language) Python] is a dynamically typed, interpreted, object oriented language. It is argued that Python is not completely object oriented as it does not provide access control in the form of public, private or protected variables. It has different security mechanisms. However, it allows to emulate protected member by using the prefix '_' before the name and private member by the use of the prefix '__' (double underscore) before the name.&lt;br /&gt;
&lt;br /&gt;
==== Ruby: ====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_language Ruby] is dynamically typed and highly object-oriented, much more than Java and Python. In Ruby, it is said that “everything is an object”.  Every value happens to be an object in Ruby. You may call methods on a string, an integer just like you call on any object in Java. For ex,&lt;br /&gt;
&lt;br /&gt;
     (2.1143).size.times{puts “Hello!”}&lt;br /&gt;
&lt;br /&gt;
This example will display “Hello!” six times on your IRB as the size of the parameter &amp;quot;2.1143&amp;quot; is 6.&lt;br /&gt;
&lt;br /&gt;
== Comparison of object Oriented Languages ==&lt;br /&gt;
The following table compares the Properties of some of the well-known Object Oriented Languages.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 9pad case0%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Language&lt;br /&gt;
!Ruby&lt;br /&gt;
!Java&lt;br /&gt;
!C#&lt;br /&gt;
!C++&lt;br /&gt;
!Python&lt;br /&gt;
!SmallTalk&lt;br /&gt;
!Perl&lt;br /&gt;
|-&lt;br /&gt;
!Object Orientation&lt;br /&gt;
|Pure&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid/Multi-Paradigm&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Pure&lt;br /&gt;
|Add-on/Hybrid&lt;br /&gt;
|-&lt;br /&gt;
!Static/Dynamic Typing&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|-&lt;br /&gt;
!Generic Classes&lt;br /&gt;
|N/A&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|-&lt;br /&gt;
!Inheritance&lt;br /&gt;
|Single Class/Multiple Mixins&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Multiple&lt;br /&gt;
|Multiple&lt;br /&gt;
|Single&lt;br /&gt;
|Multiple&lt;br /&gt;
|- &lt;br /&gt;
!Feature Renaming&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|NO&lt;br /&gt;
|-&lt;br /&gt;
!Method Overloading&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Operator Overloading&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|-&lt;br /&gt;
!Class Variables or methods&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Access Control&lt;br /&gt;
|Public, Protected, Private&lt;br /&gt;
|Public, Protected, Package, Private&lt;br /&gt;
|Public, Protected, Private, Internal, Protected Internal&lt;br /&gt;
|Public, Protected, Private, Friends&lt;br /&gt;
|Name Mangling&lt;br /&gt;
|Protected Data, Public Methods&lt;br /&gt;
|None&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Ruby : Purely Object Oriented ==&lt;br /&gt;
&lt;br /&gt;
Ruby is considered to be a purely Object oriented Language, as everything in it is considered to be an object. Unlike Java, even primitives such as characters, punctuation, numbers and strings are treated as objects in Ruby. Ruby was designed to facilitate the enforcement of Object Oriented Methods. Ruby is a Dynamically Typed Language.&lt;br /&gt;
&lt;br /&gt;
=== Object oriented Concepts in ruby: ===&lt;br /&gt;
&lt;br /&gt;
Ruby is a singly rooted Object Oriented Programming language which has all its classes having the same superclass which is the class Object.&lt;br /&gt;
&lt;br /&gt;
==== Classes:====&lt;br /&gt;
[[Image:RubyClass.png|thumb|right|280px|'''''Classes in Ruby''''']]&lt;br /&gt;
&lt;br /&gt;
A class is used to define a blueprint of a data type. It does not contain any data, but it specifies what kind of variables and methods an object of the class will have.&lt;br /&gt;
A class in Ruby is defined using the keyword class followed by the classname. The variables and methods are defined within the class definition and the class definition terminates with the end keyword.&lt;br /&gt;
&lt;br /&gt;
For ex:&lt;br /&gt;
  class Rectangle&lt;br /&gt;
     def initialize(w,l)&lt;br /&gt;
        @width, @length = w, l&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
Here, the initialize method is used to set the values of the instance variables (@width and @length. The instance variables are the properties of the objects created of this class type. These instance variables can be accessed within the class using an @. These variables can be accessed from outside the class using the accessor methods which are defined inside the class.&lt;br /&gt;
&lt;br /&gt;
In the following case printLength and printWidth are public methods used to access the instance variables from outside the class. These are called accessor methods.&lt;br /&gt;
Similarly, to&lt;br /&gt;
&lt;br /&gt;
   class Rectangle&lt;br /&gt;
  # constructor method&lt;br /&gt;
  def initialize(w,l)&lt;br /&gt;
     @width, @length = w, l&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # accessor methods&lt;br /&gt;
  def printWidth&lt;br /&gt;
     @width&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def printLength&lt;br /&gt;
     @length&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # setter methods&lt;br /&gt;
  def setWidth=(value)&lt;br /&gt;
     @width = value&lt;br /&gt;
  end&lt;br /&gt;
  def setHeight=(value)&lt;br /&gt;
     @height = value&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   # create an object&lt;br /&gt;
     rectagle = Rectangle.new(10, 20)&lt;br /&gt;
&lt;br /&gt;
    # use accessor methods&lt;br /&gt;
      x = rectangle.printWidth()&lt;br /&gt;
      y = rectangle.printLength()&lt;br /&gt;
&lt;br /&gt;
      puts &amp;quot;Width of the Rectangle is : #{x}&amp;quot;&lt;br /&gt;
      puts &amp;quot;Length of the Rectangle is : #{y}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==== Class Methods and Class Variables:====&lt;br /&gt;
A class variable is shared between all instances of the class. The value of the class variable remains the same for all the objects of the class. A class variable is accessed within the class using two @ signs at the beginning of the variable name. The class variables must be initialized within the class.&lt;br /&gt;
&lt;br /&gt;
The class methods are defined using def self.methodname() and ends with an end delimiter. These methods are called using the class name itself, for example: classname.methodname&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Attributes and accessor methods: ===&lt;br /&gt;
&lt;br /&gt;
Every class in Ruby defines a set of attributes for its objects. Ruby provides accessor methods (famously known as getter-setter methods in Java) to access these variables. Ruby provides three kinds of accessor methods:&lt;br /&gt;
&lt;br /&gt;
    Class Car&lt;br /&gt;
       attr_accessor : name, company&lt;br /&gt;
       attr_reader: price&lt;br /&gt;
       attr_writer: color&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
attr_accessor creates a setter and getter method. The attr_reader method creates only the getter method. This is required in case of attributes that are not to be modified by other programs but their value may be viewed. The attr_writer creates the setter method. In case of Java, the setter and getter methods need to be written in 3 lines of code each whereas in Ruby, the methods are written just by specifying the type of accessor, which have already been coded using Metaprogramming.[link]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Access Control:====&lt;br /&gt;
The only easy way to change an object's state in Ruby is by calling one of its methods. Control access to the methods, and you have controlled access to the object. A good rule of the thumb is never to expose methods that could leave an object in an invalid state.&lt;br /&gt;
&lt;br /&gt;
Ruby gives you three levels of protection:&lt;br /&gt;
&lt;br /&gt;
''Public methods'' can be called by everyone - no access control is enforced. A class's instance methods (these do not belong only to one object; instead, every instance of the class can call them) are public by default; anyone can call them. The initialize method is always private.&lt;br /&gt;
&lt;br /&gt;
''Protected methods'' can be invoked only by objects of the defining class and its subclasses. Access is kept within the family. However, usage of protected is limited.&lt;br /&gt;
&lt;br /&gt;
''Private methods'' cannot be called with an explicit receiver - the receiver is always self. This means that private methods can be called only in the context of the current object; you cannot invoke another object's private methods.&lt;br /&gt;
&lt;br /&gt;
Access control is determined dynamically, as the program runs, not statically. You will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
Ex:&lt;br /&gt;
&lt;br /&gt;
    class Employee&lt;br /&gt;
       def initialize(name, age, salary)&lt;br /&gt;
           @name = name&lt;br /&gt;
           @age = age&lt;br /&gt;
           @salary = salary&lt;br /&gt;
       end&lt;br /&gt;
  &lt;br /&gt;
       def name&lt;br /&gt;
           @name&lt;br /&gt;
       end &lt;br /&gt;
       def age&lt;br /&gt;
          @age&lt;br /&gt;
       end&lt;br /&gt;
       def salary&lt;br /&gt;
           @salary&lt;br /&gt;
       end&lt;br /&gt;
&lt;br /&gt;
        public :name&lt;br /&gt;
        protected :age&lt;br /&gt;
        private :salary&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      emp = Employee.new('Jack', 25, 2000);&lt;br /&gt;
      puts emp.name&lt;br /&gt;
      #puts emp.age    #age protected. gives access violation error&lt;br /&gt;
      #puts emp.salary #salary private&lt;br /&gt;
&lt;br /&gt;
====Abstract methods====&lt;br /&gt;
&lt;br /&gt;
Ruby is a dynamically typed language and does not provide abstract classes as in Java or C++. It instead provides modules that can be included within any class. The reason why Ruby does not provide abstract classes is because of the idea that it only matters if two objects can perform a function, they don’t necessarily have to be inherited from the same class. This paradigm is provided by modules, where the method of a module can be redefined as required by a class that includes it.&lt;br /&gt;
&lt;br /&gt;
Also, if required the technique of abstract class can be simulated by something like this:&lt;br /&gt;
&lt;br /&gt;
  Class Vehicle&lt;br /&gt;
   def drive&lt;br /&gt;
   raise NotImplementedError.new(&amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The subclasses that inherit from the above class will have to redefine the method “drive” as required.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Ruby ===&lt;br /&gt;
&lt;br /&gt;
Ruby provides unbounded polymorphism as a dynamically typed language through the concept of “Duck-typing”. “Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface” [link]. This concept is explained by the famous definition as “When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.” This essentially means that in languages like Ruby, it is only checked if an object can implement a function and the type of the object is not taken into consideration. It can be explained better with the help of the following example&lt;br /&gt;
&lt;br /&gt;
  class Lion&lt;br /&gt;
    def roar&lt;br /&gt;
        puts “The Lion roars when hungry”&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
     def roar&lt;br /&gt;
        puts “The man roared in fury and anger”&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   def quality lion&lt;br /&gt;
    lion.roar&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def start&lt;br /&gt;
    leo = Lion.new&lt;br /&gt;
    jack = Person.new&lt;br /&gt;
    quality leo&lt;br /&gt;
    quality jack&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   start&lt;br /&gt;
&lt;br /&gt;
In the above example, leo is an object of class “Lion” and the “roar” method defined in its class is invoked. jack is an object of class “Person” and the corresponding method defined in the Person class is called for him. Although in the real world, both objects are not of the same type, Ruby allows unbounded polymorphism as both of them implement a particular aspect.&lt;br /&gt;
&lt;br /&gt;
Thus, the check for an invoked method is done at run-time in dynamically typed languages and the method defined on that object type is called. If the method is undefined, a “NoMethodError” exception is raised.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Python===&lt;br /&gt;
&lt;br /&gt;
Python also heavily uses duck typing to allow flexibility to programmers. It emphasizes on the interface rather than on the specifc type which allows for polymorphic substitution [link].&lt;br /&gt;
In Python, functions like type() or isistance() is used to check for usual methods to match the type of find out if an object is an instance of a particular class. However, in case of duck typing, the focus is on the attribute of that object, so it checks for the presence of a particular attribute in that object using hasattr().&lt;br /&gt;
&lt;br /&gt;
Example for duck typing in Python is where Python uses a “pickle” module to serialize and de-serialize an object. So, we can introduce a new class that creates a “Tape” object. Now, an object of this type can be passed to the pickle module. Python will not differentiate between a file object or a tape object as long as they both have read() and write() methods.&lt;br /&gt;
&lt;br /&gt;
   class Tapefile():&lt;br /&gt;
      “A tape file”&lt;br /&gt;
       def __init__(self,params):&lt;br /&gt;
          #various initialization for tape access&lt;br /&gt;
       pass&lt;br /&gt;
       def read(len):&lt;br /&gt;
          #use special methods to access tape&lt;br /&gt;
       pass&lt;br /&gt;
       def readline():&lt;br /&gt;
          #use special methods to read a whole line on tape&lt;br /&gt;
       pass&lt;br /&gt;
       def write(value):&lt;br /&gt;
          #use special methods to write on tape&lt;br /&gt;
       pass&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in other languages===&lt;br /&gt;
&lt;br /&gt;
Duck typing is considered to be a feature provided by dynamic languages. C++ as a static language checks for methods by parameter and signature types. Java does not support duck typing either but it is argued that it can be simulated by the use of interfaces. The concept of duck-typing is criticized as it requires the programmer to have a very wide understanding of the source code and that two objects may implement a method in completely different contexts and so it may result in strange errors. &lt;br /&gt;
&lt;br /&gt;
Although duck typing is considered to be a feature of dynamic languages, some static languages like C# and Boo support it by allowing type checking of classes by the compiler to occur at run time.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sthirum</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53179</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b ms</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53179"/>
		<updated>2011-10-20T19:30:23Z</updated>

		<summary type="html">&lt;p&gt;Sthirum: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Lecture 5: Object Oriented Programming in Ruby =&lt;br /&gt;
&lt;br /&gt;
This wiki tries to cover the Object Oriented concepts in ruby.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Lecture 5 &amp;lt;ref name=&amp;quot;CSC517&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517//common/lectures/notes/lec5.pdf OOP in Ruby], CSC 517 Lecture5&amp;lt;/ref&amp;gt; of CSC 517 covered topics of [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented programming] in [http://www.ruby-lang.org/en/ Ruby]. Object oriented programming is a paradigm in which real world entities are represented as objects, where each object has its own attributes and set of functions that it can perform. This gives a data structure called an &amp;quot;object&amp;quot; that consists of data fields and operations. This paradigm presents features like [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) encapsulation], [http://en.wikipedia.org/wiki/Data_abstraction data abstraction], [http://en.wikipedia.org/wiki/Inheritance_(computer_science) inheritance] and [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This wiki page tries to cover the other OOP languages and their features in a succinct manner and talks about the features of Ruby in detail with examples. We have also tried to compare the various languages in terms of their features to provide a better understanding.&lt;br /&gt;
&lt;br /&gt;
The lecture also covered the concepts of [http://en.wikipedia.org/wiki/Duck_typing duck typing] in Ruby, closures and currying. The latter two have not been included as per the instructions by the instructor.&lt;br /&gt;
&lt;br /&gt;
== Object oriented languages: An Overview ==&lt;br /&gt;
&lt;br /&gt;
==== C++: ====&lt;br /&gt;
C++ is a statically typed, multi-paradigm programming language. It supports some features of object-oriented languages like encapsulation, polymorphism and inheritance. It provides access control by specifiers public, private and protected and also allows “friend” classes. C++  allows multiple inheritance which is eliminated in most other programming languages as it allows a class to inherit from more than one base class which results in an ambiguity effect called [http://en.wikipedia.org/wiki/Diamond_problem Diamond problem].&lt;br /&gt;
&lt;br /&gt;
==== Java: ====&lt;br /&gt;
Java is classified as a statically typed, hybrid object oriented language. Although it provides information hiding, inheritance and polymorphism, it has a few basic types incorporated that are not objects and some of the built-in operators are provided by basic arithmetic and not as messages to objects. [link]. For these reasons, it is not considered to be a “pure” object-oriented language.&lt;br /&gt;
&lt;br /&gt;
Java provides access control and supports private, public and protected access. It also allows to create packages to create and maintain namespaces. Java is designed not to provide multiple inheritance to avoid problems faced in C++. However, it presents a single-rooted hierarchy where “Object” is the ancestor of all classes (seen in Ruby too) and provides interfaces to extend/inherit functionality.&lt;br /&gt;
&lt;br /&gt;
==== Python: ====&lt;br /&gt;
Python is a dynamically typed, interpreted, object oriented language. It is argued that Python is not completely object oriented as it does not provide access control in the form of public, private or protected variables. It has different security mechanisms. However, it allows to emulate protected member by using the prefix '_' before the name and private member by the use of the prefix '__' (double underscore) before the name.&lt;br /&gt;
&lt;br /&gt;
==== Ruby: ====&lt;br /&gt;
Ruby is dynamically typed and highly object-oriented, much more than Java and Python. In Ruby, it is said that “everything is an object”.  Every value happens to be an object in Ruby. You may call methods on a string, an integer just like you call on any object in Java. For ex,&lt;br /&gt;
&lt;br /&gt;
     (2.1143).size.times{puts “Hello!”}&lt;br /&gt;
&lt;br /&gt;
This example will display “Hello!” six times on your IRB as the size of the parameter &amp;quot;2.1143&amp;quot; is 6.&lt;br /&gt;
&lt;br /&gt;
== Comparison of object Oriented Languages ==&lt;br /&gt;
The following table compares the Properties of some of the well-known Object Oriented Languages.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 9pad case0%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Language&lt;br /&gt;
!Ruby&lt;br /&gt;
!Java&lt;br /&gt;
!C#&lt;br /&gt;
!C++&lt;br /&gt;
!Python&lt;br /&gt;
!SmallTalk&lt;br /&gt;
!Perl&lt;br /&gt;
|-&lt;br /&gt;
!Object Orientation&lt;br /&gt;
|Pure&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid/Multi-Paradigm&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Pure&lt;br /&gt;
|Add-on/Hybrid&lt;br /&gt;
|-&lt;br /&gt;
!Static/Dynamic Typing&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|-&lt;br /&gt;
!Generic Classes&lt;br /&gt;
|N/A&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|-&lt;br /&gt;
!Inheritance&lt;br /&gt;
|Single Class/Multiple Mixins&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Multiple&lt;br /&gt;
|Multiple&lt;br /&gt;
|Single&lt;br /&gt;
|Multiple&lt;br /&gt;
|- &lt;br /&gt;
!Feature Renaming&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|NO&lt;br /&gt;
|-&lt;br /&gt;
!Method Overloading&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Operator Overloading&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|-&lt;br /&gt;
!Class Variables or methods&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Access Control&lt;br /&gt;
|Public, Protected, Private&lt;br /&gt;
|Public, Protected, Package, Private&lt;br /&gt;
|Public, Protected, Private, Internal, Protected Internal&lt;br /&gt;
|Public, Protected, Private, Friends&lt;br /&gt;
|Name Mangling&lt;br /&gt;
|Protected Data, Public Methods&lt;br /&gt;
|None&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Ruby : Purely Object Oriented ==&lt;br /&gt;
&lt;br /&gt;
Ruby is considered to be a purely Object oriented Language, as everything in it is considered to be an object. Unlike Java, even primitives such as characters, punctuation, numbers and strings are treated as objects in Ruby. Ruby was designed to facilitate the enforcement of Object Oriented Methods. Ruby is a Dynamically Typed Language.&lt;br /&gt;
&lt;br /&gt;
=== Object oriented Concepts in ruby: ===&lt;br /&gt;
&lt;br /&gt;
Ruby is a singly rooted Object Oriented Programming language which has all its classes having the same superclass which is the class Object.&lt;br /&gt;
&lt;br /&gt;
==== Classes:====&lt;br /&gt;
[[Image:RubyClass.png|thumb|right|280px|'''''Classes in Ruby''''']]&lt;br /&gt;
&lt;br /&gt;
A class is used to define a blueprint of a data type. It does not contain any data, but it specifies what kind of variables and methods an object of the class will have.&lt;br /&gt;
A class in Ruby is defined using the keyword class followed by the classname. The variables and methods are defined within the class definition and the class definition terminates with the end keyword.&lt;br /&gt;
&lt;br /&gt;
For ex:&lt;br /&gt;
  class Rectangle&lt;br /&gt;
     def initialize(w,l)&lt;br /&gt;
        @width, @length = w, l&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
Here, the initialize method is used to set the values of the instance variables (@width and @length. The instance variables are the properties of the objects created of this class type. These instance variables can be accessed within the class using an @. These variables can be accessed from outside the class using the accessor methods which are defined inside the class.&lt;br /&gt;
&lt;br /&gt;
In the following case printLength and printWidth are public methods used to access the instance variables from outside the class. These are called accessor methods.&lt;br /&gt;
Similarly, to&lt;br /&gt;
&lt;br /&gt;
   class Rectangle&lt;br /&gt;
  # constructor method&lt;br /&gt;
  def initialize(w,l)&lt;br /&gt;
     @width, @length = w, l&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # accessor methods&lt;br /&gt;
  def printWidth&lt;br /&gt;
     @width&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def printLength&lt;br /&gt;
     @length&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # setter methods&lt;br /&gt;
  def setWidth=(value)&lt;br /&gt;
     @width = value&lt;br /&gt;
  end&lt;br /&gt;
  def setHeight=(value)&lt;br /&gt;
     @height = value&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   # create an object&lt;br /&gt;
     rectagle = Rectangle.new(10, 20)&lt;br /&gt;
&lt;br /&gt;
    # use accessor methods&lt;br /&gt;
      x = rectangle.printWidth()&lt;br /&gt;
      y = rectangle.printLength()&lt;br /&gt;
&lt;br /&gt;
      puts &amp;quot;Width of the Rectangle is : #{x}&amp;quot;&lt;br /&gt;
      puts &amp;quot;Length of the Rectangle is : #{y}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==== Class Methods and Class Variables:====&lt;br /&gt;
A class variable is shared between all instances of the class. The value of the class variable remains the same for all the objects of the class. A class variable is accessed within the class using two @ signs at the beginning of the variable name. The class variables must be initialized within the class.&lt;br /&gt;
&lt;br /&gt;
The class methods are defined using def self.methodname() and ends with an end delimiter. These methods are called using the class name itself, for example: classname.methodname&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Attributes and accessor methods: ===&lt;br /&gt;
&lt;br /&gt;
Every class in Ruby defines a set of attributes for its objects. Ruby provides accessor methods (famously known as getter-setter methods in Java) to access these variables. Ruby provides three kinds of accessor methods:&lt;br /&gt;
&lt;br /&gt;
    Class Car&lt;br /&gt;
       attr_accessor : name, company&lt;br /&gt;
       attr_reader: price&lt;br /&gt;
       attr_writer: color&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
attr_accessor creates a setter and getter method. The attr_reader method creates only the getter method. This is required in case of attributes that are not to be modified by other programs but their value may be viewed. The attr_writer creates the setter method. In case of Java, the setter and getter methods need to be written in 3 lines of code each whereas in Ruby, the methods are written just by specifying the type of accessor, which have already been coded using Metaprogramming.[link]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Access Control:====&lt;br /&gt;
The only easy way to change an object's state in Ruby is by calling one of its methods. Control access to the methods, and you have controlled access to the object. A good rule of the thumb is never to expose methods that could leave an object in an invalid state.&lt;br /&gt;
&lt;br /&gt;
Ruby gives you three levels of protection:&lt;br /&gt;
&lt;br /&gt;
''Public methods'' can be called by everyone - no access control is enforced. A class's instance methods (these do not belong only to one object; instead, every instance of the class can call them) are public by default; anyone can call them. The initialize method is always private.&lt;br /&gt;
&lt;br /&gt;
''Protected methods'' can be invoked only by objects of the defining class and its subclasses. Access is kept within the family. However, usage of protected is limited.&lt;br /&gt;
&lt;br /&gt;
''Private methods'' cannot be called with an explicit receiver - the receiver is always self. This means that private methods can be called only in the context of the current object; you cannot invoke another object's private methods.&lt;br /&gt;
&lt;br /&gt;
Access control is determined dynamically, as the program runs, not statically. You will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
Ex:&lt;br /&gt;
&lt;br /&gt;
    class Employee&lt;br /&gt;
       def initialize(name, age, salary)&lt;br /&gt;
           @name = name&lt;br /&gt;
           @age = age&lt;br /&gt;
           @salary = salary&lt;br /&gt;
       end&lt;br /&gt;
  &lt;br /&gt;
       def name&lt;br /&gt;
           @name&lt;br /&gt;
       end &lt;br /&gt;
       def age&lt;br /&gt;
          @age&lt;br /&gt;
       end&lt;br /&gt;
       def salary&lt;br /&gt;
           @salary&lt;br /&gt;
       end&lt;br /&gt;
&lt;br /&gt;
        public :name&lt;br /&gt;
        protected :age&lt;br /&gt;
        private :salary&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      emp = Employee.new('Jack', 25, 2000);&lt;br /&gt;
      puts emp.name&lt;br /&gt;
      #puts emp.age    #age protected. gives access violation error&lt;br /&gt;
      #puts emp.salary #salary private&lt;br /&gt;
&lt;br /&gt;
====Abstract methods====&lt;br /&gt;
&lt;br /&gt;
Ruby is a dynamically typed language and does not provide abstract classes as in Java or C++. It instead provides modules that can be included within any class. The reason why Ruby does not provide abstract classes is because of the idea that it only matters if two objects can perform a function, they don’t necessarily have to be inherited from the same class. This paradigm is provided by modules, where the method of a module can be redefined as required by a class that includes it.&lt;br /&gt;
&lt;br /&gt;
Also, if required the technique of abstract class can be simulated by something like this:&lt;br /&gt;
&lt;br /&gt;
  Class Vehicle&lt;br /&gt;
   def drive&lt;br /&gt;
   raise NotImplementedError.new(&amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The subclasses that inherit from the above class will have to redefine the method “drive” as required.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Ruby ===&lt;br /&gt;
&lt;br /&gt;
Ruby provides unbounded polymorphism as a dynamically typed language through the concept of “Duck-typing”. “Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface” [link]. This concept is explained by the famous definition as “When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.” This essentially means that in languages like Ruby, it is only checked if an object can implement a function and the type of the object is not taken into consideration. It can be explained better with the help of the following example&lt;br /&gt;
&lt;br /&gt;
  class Lion&lt;br /&gt;
    def roar&lt;br /&gt;
        puts “The Lion roars when hungry”&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
     def roar&lt;br /&gt;
        puts “The man roared in fury and anger”&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   def quality lion&lt;br /&gt;
    lion.roar&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def start&lt;br /&gt;
    leo = Lion.new&lt;br /&gt;
    jack = Person.new&lt;br /&gt;
    quality leo&lt;br /&gt;
    quality jack&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   start&lt;br /&gt;
&lt;br /&gt;
In the above example, leo is an object of class “Lion” and the “roar” method defined in its class is invoked. jack is an object of class “Person” and the corresponding method defined in the Person class is called for him. Although in the real world, both objects are not of the same type, Ruby allows unbounded polymorphism as both of them implement a particular aspect.&lt;br /&gt;
&lt;br /&gt;
Thus, the check for an invoked method is done at run-time in dynamically typed languages and the method defined on that object type is called. If the method is undefined, a “NoMethodError” exception is raised.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Python===&lt;br /&gt;
&lt;br /&gt;
Python also heavily uses duck typing to allow flexibility to programmers. It emphasizes on the interface rather than on the specifc type which allows for polymorphic substitution [link].&lt;br /&gt;
In Python, functions like type() or isistance() is used to check for usual methods to match the type of find out if an object is an instance of a particular class. However, in case of duck typing, the focus is on the attribute of that object, so it checks for the presence of a particular attribute in that object using hasattr().&lt;br /&gt;
&lt;br /&gt;
Example for duck typing in Python is where Python uses a “pickle” module to serialize and de-serialize an object. So, we can introduce a new class that creates a “Tape” object. Now, an object of this type can be passed to the pickle module. Python will not differentiate between a file object or a tape object as long as they both have read() and write() methods.&lt;br /&gt;
&lt;br /&gt;
   class Tapefile():&lt;br /&gt;
      “A tape file”&lt;br /&gt;
       def __init__(self,params):&lt;br /&gt;
          #various initialization for tape access&lt;br /&gt;
       pass&lt;br /&gt;
       def read(len):&lt;br /&gt;
          #use special methods to access tape&lt;br /&gt;
       pass&lt;br /&gt;
       def readline():&lt;br /&gt;
          #use special methods to read a whole line on tape&lt;br /&gt;
       pass&lt;br /&gt;
       def write(value):&lt;br /&gt;
          #use special methods to write on tape&lt;br /&gt;
       pass&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in other languages===&lt;br /&gt;
&lt;br /&gt;
Duck typing is considered to be a feature provided by dynamic languages. C++ as a static language checks for methods by parameter and signature types. Java does not support duck typing either but it is argued that it can be simulated by the use of interfaces. The concept of duck-typing is criticized as it requires the programmer to have a very wide understanding of the source code and that two objects may implement a method in completely different contexts and so it may result in strange errors. &lt;br /&gt;
&lt;br /&gt;
Although duck typing is considered to be a feature of dynamic languages, some static languages like C# and Boo support it by allowing type checking of classes by the compiler to occur at run time.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sthirum</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53178</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b ms</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53178"/>
		<updated>2011-10-20T19:28:55Z</updated>

		<summary type="html">&lt;p&gt;Sthirum: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Lecture 5: Object Oriented Programming in Ruby =&lt;br /&gt;
&lt;br /&gt;
This wiki tries to cover the Object Oriented concepts in ruby.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Lecture 5 &amp;lt;ref name=&amp;quot;CSC517&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517//common/lectures/notes/lec5.pdf OOP in Ruby], CSC 517 Lecture5&amp;lt;/ref&amp;gt; of CSC 517 covered topics of [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented programming] in [http://www.ruby-lang.org/en/ Ruby]. Object oriented programming is a paradigm in which real world entities are represented as objects, where each object has its own attributes and set of functions that it can perform. This gives a data structure called an &amp;quot;object&amp;quot; that consists of data fields and operations. This paradigm presents features like [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) encapsulation], [http://en.wikipedia.org/wiki/Data_abstraction data abstraction], [http://en.wikipedia.org/wiki/Inheritance_(computer_science) inheritance] and [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This wiki page tries to cover the other OOP languages and their features in a succinct manner and talks about the features of Ruby in detail with examples. We have also tried to compare the various languages in terms of their features to provide a better understanding.&lt;br /&gt;
&lt;br /&gt;
The lecture also covered the concepts of duck typing in Ruby, closures and currying. The latter two have not been included as per the instructions by the instructor.&lt;br /&gt;
&lt;br /&gt;
== Object oriented languages: An Overview ==&lt;br /&gt;
&lt;br /&gt;
==== C++: ====&lt;br /&gt;
C++ is a statically typed, multi-paradigm programming language. It supports some features of object-oriented languages like encapsulation, polymorphism and inheritance. It provides access control by specifiers public, private and protected and also allows “friend” classes. C++  allows multiple inheritance which is eliminated in most other programming languages as it allows a class to inherit from more than one base class which results in an ambiguity effect called [http://en.wikipedia.org/wiki/Diamond_problem Diamond problem].&lt;br /&gt;
&lt;br /&gt;
==== Java: ====&lt;br /&gt;
Java is classified as a statically typed, hybrid object oriented language. Although it provides information hiding, inheritance and polymorphism, it has a few basic types incorporated that are not objects and some of the built-in operators are provided by basic arithmetic and not as messages to objects. [link]. For these reasons, it is not considered to be a “pure” object-oriented language.&lt;br /&gt;
&lt;br /&gt;
Java provides access control and supports private, public and protected access. It also allows to create packages to create and maintain namespaces. Java is designed not to provide multiple inheritance to avoid problems faced in C++. However, it presents a single-rooted hierarchy where “Object” is the ancestor of all classes (seen in Ruby too) and provides interfaces to extend/inherit functionality.&lt;br /&gt;
&lt;br /&gt;
==== Python: ====&lt;br /&gt;
Python is a dynamically typed, interpreted, object oriented language. It is argued that Python is not completely object oriented as it does not provide access control in the form of public, private or protected variables. It has different security mechanisms. However, it allows to emulate protected member by using the prefix '_' before the name and private member by the use of the prefix '__' (double underscore) before the name.&lt;br /&gt;
&lt;br /&gt;
==== Ruby: ====&lt;br /&gt;
Ruby is dynamically typed and highly object-oriented, much more than Java and Python. In Ruby, it is said that “everything is an object”.  Every value happens to be an object in Ruby. You may call methods on a string, an integer just like you call on any object in Java. For ex,&lt;br /&gt;
&lt;br /&gt;
     (2.1143).size.times{puts “Hello!”}&lt;br /&gt;
&lt;br /&gt;
This example will display “Hello!” six times on your IRB as the size of the parameter &amp;quot;2.1143&amp;quot; is 6.&lt;br /&gt;
&lt;br /&gt;
== Comparison of object Oriented Languages ==&lt;br /&gt;
The following table compares the Properties of some of the well-known Object Oriented Languages.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 9pad case0%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Language&lt;br /&gt;
!Ruby&lt;br /&gt;
!Java&lt;br /&gt;
!C#&lt;br /&gt;
!C++&lt;br /&gt;
!Python&lt;br /&gt;
!SmallTalk&lt;br /&gt;
!Perl&lt;br /&gt;
|-&lt;br /&gt;
!Object Orientation&lt;br /&gt;
|Pure&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid/Multi-Paradigm&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Pure&lt;br /&gt;
|Add-on/Hybrid&lt;br /&gt;
|-&lt;br /&gt;
!Static/Dynamic Typing&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|-&lt;br /&gt;
!Generic Classes&lt;br /&gt;
|N/A&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|-&lt;br /&gt;
!Inheritance&lt;br /&gt;
|Single Class/Multiple Mixins&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Multiple&lt;br /&gt;
|Multiple&lt;br /&gt;
|Single&lt;br /&gt;
|Multiple&lt;br /&gt;
|- &lt;br /&gt;
!Feature Renaming&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|NO&lt;br /&gt;
|-&lt;br /&gt;
!Method Overloading&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Operator Overloading&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|-&lt;br /&gt;
!Class Variables or methods&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Access Control&lt;br /&gt;
|Public, Protected, Private&lt;br /&gt;
|Public, Protected, Package, Private&lt;br /&gt;
|Public, Protected, Private, Internal, Protected Internal&lt;br /&gt;
|Public, Protected, Private, Friends&lt;br /&gt;
|Name Mangling&lt;br /&gt;
|Protected Data, Public Methods&lt;br /&gt;
|None&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Ruby : Purely Object Oriented ==&lt;br /&gt;
&lt;br /&gt;
Ruby is considered to be a purely Object oriented Language, as everything in it is considered to be an object. Unlike Java, even primitives such as characters, punctuation, numbers and strings are treated as objects in Ruby. Ruby was designed to facilitate the enforcement of Object Oriented Methods. Ruby is a Dynamically Typed Language.&lt;br /&gt;
&lt;br /&gt;
=== Object oriented Concepts in ruby: ===&lt;br /&gt;
&lt;br /&gt;
Ruby is a singly rooted Object Oriented Programming language which has all its classes having the same superclass which is the class Object.&lt;br /&gt;
&lt;br /&gt;
==== Classes:====&lt;br /&gt;
[[Image:RubyClass.png|thumb|right|280px|'''''Classes in Ruby''''']]&lt;br /&gt;
&lt;br /&gt;
A class is used to define a blueprint of a data type. It does not contain any data, but it specifies what kind of variables and methods an object of the class will have.&lt;br /&gt;
A class in Ruby is defined using the keyword class followed by the classname. The variables and methods are defined within the class definition and the class definition terminates with the end keyword.&lt;br /&gt;
&lt;br /&gt;
For ex:&lt;br /&gt;
  class Rectangle&lt;br /&gt;
     def initialize(w,l)&lt;br /&gt;
        @width, @length = w, l&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
Here, the initialize method is used to set the values of the instance variables (@width and @length. The instance variables are the properties of the objects created of this class type. These instance variables can be accessed within the class using an @. These variables can be accessed from outside the class using the accessor methods which are defined inside the class.&lt;br /&gt;
&lt;br /&gt;
In the following case printLength and printWidth are public methods used to access the instance variables from outside the class. These are called accessor methods.&lt;br /&gt;
Similarly, to&lt;br /&gt;
&lt;br /&gt;
   class Rectangle&lt;br /&gt;
  # constructor method&lt;br /&gt;
  def initialize(w,l)&lt;br /&gt;
     @width, @length = w, l&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # accessor methods&lt;br /&gt;
  def printWidth&lt;br /&gt;
     @width&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def printLength&lt;br /&gt;
     @length&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # setter methods&lt;br /&gt;
  def setWidth=(value)&lt;br /&gt;
     @width = value&lt;br /&gt;
  end&lt;br /&gt;
  def setHeight=(value)&lt;br /&gt;
     @height = value&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   # create an object&lt;br /&gt;
     rectagle = Rectangle.new(10, 20)&lt;br /&gt;
&lt;br /&gt;
    # use accessor methods&lt;br /&gt;
      x = rectangle.printWidth()&lt;br /&gt;
      y = rectangle.printLength()&lt;br /&gt;
&lt;br /&gt;
      puts &amp;quot;Width of the Rectangle is : #{x}&amp;quot;&lt;br /&gt;
      puts &amp;quot;Length of the Rectangle is : #{y}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==== Class Methods and Class Variables:====&lt;br /&gt;
A class variable is shared between all instances of the class. The value of the class variable remains the same for all the objects of the class. A class variable is accessed within the class using two @ signs at the beginning of the variable name. The class variables must be initialized within the class.&lt;br /&gt;
&lt;br /&gt;
The class methods are defined using def self.methodname() and ends with an end delimiter. These methods are called using the class name itself, for example: classname.methodname&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Attributes and accessor methods: ===&lt;br /&gt;
&lt;br /&gt;
Every class in Ruby defines a set of attributes for its objects. Ruby provides accessor methods (famously known as getter-setter methods in Java) to access these variables. Ruby provides three kinds of accessor methods:&lt;br /&gt;
&lt;br /&gt;
    Class Car&lt;br /&gt;
       attr_accessor : name, company&lt;br /&gt;
       attr_reader: price&lt;br /&gt;
       attr_writer: color&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
attr_accessor creates a setter and getter method. The attr_reader method creates only the getter method. This is required in case of attributes that are not to be modified by other programs but their value may be viewed. The attr_writer creates the setter method. In case of Java, the setter and getter methods need to be written in 3 lines of code each whereas in Ruby, the methods are written just by specifying the type of accessor, which have already been coded using Metaprogramming.[link]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Access Control:====&lt;br /&gt;
The only easy way to change an object's state in Ruby is by calling one of its methods. Control access to the methods, and you have controlled access to the object. A good rule of the thumb is never to expose methods that could leave an object in an invalid state.&lt;br /&gt;
&lt;br /&gt;
Ruby gives you three levels of protection:&lt;br /&gt;
&lt;br /&gt;
''Public methods'' can be called by everyone - no access control is enforced. A class's instance methods (these do not belong only to one object; instead, every instance of the class can call them) are public by default; anyone can call them. The initialize method is always private.&lt;br /&gt;
&lt;br /&gt;
''Protected methods'' can be invoked only by objects of the defining class and its subclasses. Access is kept within the family. However, usage of protected is limited.&lt;br /&gt;
&lt;br /&gt;
''Private methods'' cannot be called with an explicit receiver - the receiver is always self. This means that private methods can be called only in the context of the current object; you cannot invoke another object's private methods.&lt;br /&gt;
&lt;br /&gt;
Access control is determined dynamically, as the program runs, not statically. You will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
Ex:&lt;br /&gt;
&lt;br /&gt;
    class Employee&lt;br /&gt;
       def initialize(name, age, salary)&lt;br /&gt;
           @name = name&lt;br /&gt;
           @age = age&lt;br /&gt;
           @salary = salary&lt;br /&gt;
       end&lt;br /&gt;
  &lt;br /&gt;
       def name&lt;br /&gt;
           @name&lt;br /&gt;
       end &lt;br /&gt;
       def age&lt;br /&gt;
          @age&lt;br /&gt;
       end&lt;br /&gt;
       def salary&lt;br /&gt;
           @salary&lt;br /&gt;
       end&lt;br /&gt;
&lt;br /&gt;
        public :name&lt;br /&gt;
        protected :age&lt;br /&gt;
        private :salary&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      emp = Employee.new('Jack', 25, 2000);&lt;br /&gt;
      puts emp.name&lt;br /&gt;
      #puts emp.age    #age protected. gives access violation error&lt;br /&gt;
      #puts emp.salary #salary private&lt;br /&gt;
&lt;br /&gt;
====Abstract methods====&lt;br /&gt;
&lt;br /&gt;
Ruby is a dynamically typed language and does not provide abstract classes as in Java or C++. It instead provides modules that can be included within any class. The reason why Ruby does not provide abstract classes is because of the idea that it only matters if two objects can perform a function, they don’t necessarily have to be inherited from the same class. This paradigm is provided by modules, where the method of a module can be redefined as required by a class that includes it.&lt;br /&gt;
&lt;br /&gt;
Also, if required the technique of abstract class can be simulated by something like this:&lt;br /&gt;
&lt;br /&gt;
  Class Vehicle&lt;br /&gt;
   def drive&lt;br /&gt;
   raise NotImplementedError.new(&amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The subclasses that inherit from the above class will have to redefine the method “drive” as required.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Ruby ===&lt;br /&gt;
&lt;br /&gt;
Ruby provides unbounded polymorphism as a dynamically typed language through the concept of “Duck-typing”. “Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface” [link]. This concept is explained by the famous definition as “When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.” This essentially means that in languages like Ruby, it is only checked if an object can implement a function and the type of the object is not taken into consideration. It can be explained better with the help of the following example&lt;br /&gt;
&lt;br /&gt;
  class Lion&lt;br /&gt;
    def roar&lt;br /&gt;
        puts “The Lion roars when hungry”&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
     def roar&lt;br /&gt;
        puts “The man roared in fury and anger”&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   def quality lion&lt;br /&gt;
    lion.roar&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def start&lt;br /&gt;
    leo = Lion.new&lt;br /&gt;
    jack = Person.new&lt;br /&gt;
    quality leo&lt;br /&gt;
    quality jack&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   start&lt;br /&gt;
&lt;br /&gt;
In the above example, leo is an object of class “Lion” and the “roar” method defined in its class is invoked. jack is an object of class “Person” and the corresponding method defined in the Person class is called for him. Although in the real world, both objects are not of the same type, Ruby allows unbounded polymorphism as both of them implement a particular aspect.&lt;br /&gt;
&lt;br /&gt;
Thus, the check for an invoked method is done at run-time in dynamically typed languages and the method defined on that object type is called. If the method is undefined, a “NoMethodError” exception is raised.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Python===&lt;br /&gt;
&lt;br /&gt;
Python also heavily uses duck typing to allow flexibility to programmers. It emphasizes on the interface rather than on the specifc type which allows for polymorphic substitution [link].&lt;br /&gt;
In Python, functions like type() or isistance() is used to check for usual methods to match the type of find out if an object is an instance of a particular class. However, in case of duck typing, the focus is on the attribute of that object, so it checks for the presence of a particular attribute in that object using hasattr().&lt;br /&gt;
&lt;br /&gt;
Example for duck typing in Python is where Python uses a “pickle” module to serialize and de-serialize an object. So, we can introduce a new class that creates a “Tape” object. Now, an object of this type can be passed to the pickle module. Python will not differentiate between a file object or a tape object as long as they both have read() and write() methods.&lt;br /&gt;
&lt;br /&gt;
   class Tapefile():&lt;br /&gt;
      “A tape file”&lt;br /&gt;
       def __init__(self,params):&lt;br /&gt;
          #various initialization for tape access&lt;br /&gt;
       pass&lt;br /&gt;
       def read(len):&lt;br /&gt;
          #use special methods to access tape&lt;br /&gt;
       pass&lt;br /&gt;
       def readline():&lt;br /&gt;
          #use special methods to read a whole line on tape&lt;br /&gt;
       pass&lt;br /&gt;
       def write(value):&lt;br /&gt;
          #use special methods to write on tape&lt;br /&gt;
       pass&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in other languages===&lt;br /&gt;
&lt;br /&gt;
Duck typing is considered to be a feature provided by dynamic languages. C++ as a static language checks for methods by parameter and signature types. Java does not support duck typing either but it is argued that it can be simulated by the use of interfaces. The concept of duck-typing is criticized as it requires the programmer to have a very wide understanding of the source code and that two objects may implement a method in completely different contexts and so it may result in strange errors. &lt;br /&gt;
&lt;br /&gt;
Although duck typing is considered to be a feature of dynamic languages, some static languages like C# and Boo support it by allowing type checking of classes by the compiler to occur at run time.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sthirum</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53177</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b ms</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53177"/>
		<updated>2011-10-20T19:27:39Z</updated>

		<summary type="html">&lt;p&gt;Sthirum: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Lecture 5: Object Oriented Programming in Ruby =&lt;br /&gt;
&lt;br /&gt;
This wiki tries to cover the Object Oriented concepts in ruby.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Lecture 5 &amp;lt;ref name=&amp;quot;CSC517&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517//common/lectures/notes/lec5.pdf], CSC 517 Lecture5 - OOP in Ruby&amp;lt;/ref&amp;gt; of CSC 517 covered topics of [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented programming] in [http://www.ruby-lang.org/en/ Ruby]. Object oriented programming is a paradigm in which real world entities are represented as objects, where each object has its own attributes and set of functions that it can perform. This gives a data structure called an &amp;quot;object&amp;quot; that consists of data fields and operations. This paradigm presents features like [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) encapsulation], [http://en.wikipedia.org/wiki/Data_abstraction data abstraction], [http://en.wikipedia.org/wiki/Inheritance_(computer_science) inheritance] and [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This wiki page tries to cover the other OOP languages and their features in a succinct manner and talks about the features of Ruby in detail with examples. We have also tried to compare the various languages in terms of their features to provide a better understanding.&lt;br /&gt;
&lt;br /&gt;
The lecture also covered the concepts of duck typing in Ruby, closures and currying. The latter two have not been included as per the instructions by the instructor.&lt;br /&gt;
&lt;br /&gt;
== Object oriented languages: An Overview ==&lt;br /&gt;
&lt;br /&gt;
==== C++: ====&lt;br /&gt;
C++ is a statically typed, multi-paradigm programming language. It supports some features of object-oriented languages like encapsulation, polymorphism and inheritance. It provides access control by specifiers public, private and protected and also allows “friend” classes. C++  allows multiple inheritance which is eliminated in most other programming languages as it allows a class to inherit from more than one base class which results in an ambiguity effect called [http://en.wikipedia.org/wiki/Diamond_problem Diamond problem].&lt;br /&gt;
&lt;br /&gt;
==== Java: ====&lt;br /&gt;
Java is classified as a statically typed, hybrid object oriented language. Although it provides information hiding, inheritance and polymorphism, it has a few basic types incorporated that are not objects and some of the built-in operators are provided by basic arithmetic and not as messages to objects. [link]. For these reasons, it is not considered to be a “pure” object-oriented language.&lt;br /&gt;
&lt;br /&gt;
Java provides access control and supports private, public and protected access. It also allows to create packages to create and maintain namespaces. Java is designed not to provide multiple inheritance to avoid problems faced in C++. However, it presents a single-rooted hierarchy where “Object” is the ancestor of all classes (seen in Ruby too) and provides interfaces to extend/inherit functionality.&lt;br /&gt;
&lt;br /&gt;
==== Python: ====&lt;br /&gt;
Python is a dynamically typed, interpreted, object oriented language. It is argued that Python is not completely object oriented as it does not provide access control in the form of public, private or protected variables. It has different security mechanisms. However, it allows to emulate protected member by using the prefix '_' before the name and private member by the use of the prefix '__' (double underscore) before the name.&lt;br /&gt;
&lt;br /&gt;
==== Ruby: ====&lt;br /&gt;
Ruby is dynamically typed and highly object-oriented, much more than Java and Python. In Ruby, it is said that “everything is an object”.  Every value happens to be an object in Ruby. You may call methods on a string, an integer just like you call on any object in Java. For ex,&lt;br /&gt;
&lt;br /&gt;
     (2.1143).size.times{puts “Hello!”}&lt;br /&gt;
&lt;br /&gt;
This example will display “Hello!” six times on your IRB as the size of the parameter &amp;quot;2.1143&amp;quot; is 6.&lt;br /&gt;
&lt;br /&gt;
== Comparison of object Oriented Languages ==&lt;br /&gt;
The following table compares the Properties of some of the well-known Object Oriented Languages.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 9pad case0%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Language&lt;br /&gt;
!Ruby&lt;br /&gt;
!Java&lt;br /&gt;
!C#&lt;br /&gt;
!C++&lt;br /&gt;
!Python&lt;br /&gt;
!SmallTalk&lt;br /&gt;
!Perl&lt;br /&gt;
|-&lt;br /&gt;
!Object Orientation&lt;br /&gt;
|Pure&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid/Multi-Paradigm&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Pure&lt;br /&gt;
|Add-on/Hybrid&lt;br /&gt;
|-&lt;br /&gt;
!Static/Dynamic Typing&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|-&lt;br /&gt;
!Generic Classes&lt;br /&gt;
|N/A&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|-&lt;br /&gt;
!Inheritance&lt;br /&gt;
|Single Class/Multiple Mixins&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Multiple&lt;br /&gt;
|Multiple&lt;br /&gt;
|Single&lt;br /&gt;
|Multiple&lt;br /&gt;
|- &lt;br /&gt;
!Feature Renaming&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|NO&lt;br /&gt;
|-&lt;br /&gt;
!Method Overloading&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Operator Overloading&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|-&lt;br /&gt;
!Class Variables or methods&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Access Control&lt;br /&gt;
|Public, Protected, Private&lt;br /&gt;
|Public, Protected, Package, Private&lt;br /&gt;
|Public, Protected, Private, Internal, Protected Internal&lt;br /&gt;
|Public, Protected, Private, Friends&lt;br /&gt;
|Name Mangling&lt;br /&gt;
|Protected Data, Public Methods&lt;br /&gt;
|None&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Ruby : Purely Object Oriented ==&lt;br /&gt;
&lt;br /&gt;
Ruby is considered to be a purely Object oriented Language, as everything in it is considered to be an object. Unlike Java, even primitives such as characters, punctuation, numbers and strings are treated as objects in Ruby. Ruby was designed to facilitate the enforcement of Object Oriented Methods. Ruby is a Dynamically Typed Language.&lt;br /&gt;
&lt;br /&gt;
=== Object oriented Concepts in ruby: ===&lt;br /&gt;
&lt;br /&gt;
Ruby is a singly rooted Object Oriented Programming language which has all its classes having the same superclass which is the class Object.&lt;br /&gt;
&lt;br /&gt;
==== Classes:====&lt;br /&gt;
[[Image:RubyClass.png|thumb|right|280px|'''''Classes in Ruby''''']]&lt;br /&gt;
&lt;br /&gt;
A class is used to define a blueprint of a data type. It does not contain any data, but it specifies what kind of variables and methods an object of the class will have.&lt;br /&gt;
A class in Ruby is defined using the keyword class followed by the classname. The variables and methods are defined within the class definition and the class definition terminates with the end keyword.&lt;br /&gt;
&lt;br /&gt;
For ex:&lt;br /&gt;
  class Rectangle&lt;br /&gt;
     def initialize(w,l)&lt;br /&gt;
        @width, @length = w, l&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
Here, the initialize method is used to set the values of the instance variables (@width and @length. The instance variables are the properties of the objects created of this class type. These instance variables can be accessed within the class using an @. These variables can be accessed from outside the class using the accessor methods which are defined inside the class.&lt;br /&gt;
&lt;br /&gt;
In the following case printLength and printWidth are public methods used to access the instance variables from outside the class. These are called accessor methods.&lt;br /&gt;
Similarly, to&lt;br /&gt;
&lt;br /&gt;
   class Rectangle&lt;br /&gt;
  # constructor method&lt;br /&gt;
  def initialize(w,l)&lt;br /&gt;
     @width, @length = w, l&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # accessor methods&lt;br /&gt;
  def printWidth&lt;br /&gt;
     @width&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def printLength&lt;br /&gt;
     @length&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # setter methods&lt;br /&gt;
  def setWidth=(value)&lt;br /&gt;
     @width = value&lt;br /&gt;
  end&lt;br /&gt;
  def setHeight=(value)&lt;br /&gt;
     @height = value&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   # create an object&lt;br /&gt;
     rectagle = Rectangle.new(10, 20)&lt;br /&gt;
&lt;br /&gt;
    # use accessor methods&lt;br /&gt;
      x = rectangle.printWidth()&lt;br /&gt;
      y = rectangle.printLength()&lt;br /&gt;
&lt;br /&gt;
      puts &amp;quot;Width of the Rectangle is : #{x}&amp;quot;&lt;br /&gt;
      puts &amp;quot;Length of the Rectangle is : #{y}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==== Class Methods and Class Variables:====&lt;br /&gt;
A class variable is shared between all instances of the class. The value of the class variable remains the same for all the objects of the class. A class variable is accessed within the class using two @ signs at the beginning of the variable name. The class variables must be initialized within the class.&lt;br /&gt;
&lt;br /&gt;
The class methods are defined using def self.methodname() and ends with an end delimiter. These methods are called using the class name itself, for example: classname.methodname&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Attributes and accessor methods: ===&lt;br /&gt;
&lt;br /&gt;
Every class in Ruby defines a set of attributes for its objects. Ruby provides accessor methods (famously known as getter-setter methods in Java) to access these variables. Ruby provides three kinds of accessor methods:&lt;br /&gt;
&lt;br /&gt;
    Class Car&lt;br /&gt;
       attr_accessor : name, company&lt;br /&gt;
       attr_reader: price&lt;br /&gt;
       attr_writer: color&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
attr_accessor creates a setter and getter method. The attr_reader method creates only the getter method. This is required in case of attributes that are not to be modified by other programs but their value may be viewed. The attr_writer creates the setter method. In case of Java, the setter and getter methods need to be written in 3 lines of code each whereas in Ruby, the methods are written just by specifying the type of accessor, which have already been coded using Metaprogramming.[link]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Access Control:====&lt;br /&gt;
The only easy way to change an object's state in Ruby is by calling one of its methods. Control access to the methods, and you have controlled access to the object. A good rule of the thumb is never to expose methods that could leave an object in an invalid state.&lt;br /&gt;
&lt;br /&gt;
Ruby gives you three levels of protection:&lt;br /&gt;
&lt;br /&gt;
''Public methods'' can be called by everyone - no access control is enforced. A class's instance methods (these do not belong only to one object; instead, every instance of the class can call them) are public by default; anyone can call them. The initialize method is always private.&lt;br /&gt;
&lt;br /&gt;
''Protected methods'' can be invoked only by objects of the defining class and its subclasses. Access is kept within the family. However, usage of protected is limited.&lt;br /&gt;
&lt;br /&gt;
''Private methods'' cannot be called with an explicit receiver - the receiver is always self. This means that private methods can be called only in the context of the current object; you cannot invoke another object's private methods.&lt;br /&gt;
&lt;br /&gt;
Access control is determined dynamically, as the program runs, not statically. You will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
Ex:&lt;br /&gt;
&lt;br /&gt;
    class Employee&lt;br /&gt;
       def initialize(name, age, salary)&lt;br /&gt;
           @name = name&lt;br /&gt;
           @age = age&lt;br /&gt;
           @salary = salary&lt;br /&gt;
       end&lt;br /&gt;
  &lt;br /&gt;
       def name&lt;br /&gt;
           @name&lt;br /&gt;
       end &lt;br /&gt;
       def age&lt;br /&gt;
          @age&lt;br /&gt;
       end&lt;br /&gt;
       def salary&lt;br /&gt;
           @salary&lt;br /&gt;
       end&lt;br /&gt;
&lt;br /&gt;
        public :name&lt;br /&gt;
        protected :age&lt;br /&gt;
        private :salary&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      emp = Employee.new('Jack', 25, 2000);&lt;br /&gt;
      puts emp.name&lt;br /&gt;
      #puts emp.age    #age protected. gives access violation error&lt;br /&gt;
      #puts emp.salary #salary private&lt;br /&gt;
&lt;br /&gt;
====Abstract methods====&lt;br /&gt;
&lt;br /&gt;
Ruby is a dynamically typed language and does not provide abstract classes as in Java or C++. It instead provides modules that can be included within any class. The reason why Ruby does not provide abstract classes is because of the idea that it only matters if two objects can perform a function, they don’t necessarily have to be inherited from the same class. This paradigm is provided by modules, where the method of a module can be redefined as required by a class that includes it.&lt;br /&gt;
&lt;br /&gt;
Also, if required the technique of abstract class can be simulated by something like this:&lt;br /&gt;
&lt;br /&gt;
  Class Vehicle&lt;br /&gt;
   def drive&lt;br /&gt;
   raise NotImplementedError.new(&amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The subclasses that inherit from the above class will have to redefine the method “drive” as required.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Ruby ===&lt;br /&gt;
&lt;br /&gt;
Ruby provides unbounded polymorphism as a dynamically typed language through the concept of “Duck-typing”. “Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface” [link]. This concept is explained by the famous definition as “When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.” This essentially means that in languages like Ruby, it is only checked if an object can implement a function and the type of the object is not taken into consideration. It can be explained better with the help of the following example&lt;br /&gt;
&lt;br /&gt;
  class Lion&lt;br /&gt;
    def roar&lt;br /&gt;
        puts “The Lion roars when hungry”&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
     def roar&lt;br /&gt;
        puts “The man roared in fury and anger”&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   def quality lion&lt;br /&gt;
    lion.roar&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def start&lt;br /&gt;
    leo = Lion.new&lt;br /&gt;
    jack = Person.new&lt;br /&gt;
    quality leo&lt;br /&gt;
    quality jack&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   start&lt;br /&gt;
&lt;br /&gt;
In the above example, leo is an object of class “Lion” and the “roar” method defined in its class is invoked. jack is an object of class “Person” and the corresponding method defined in the Person class is called for him. Although in the real world, both objects are not of the same type, Ruby allows unbounded polymorphism as both of them implement a particular aspect.&lt;br /&gt;
&lt;br /&gt;
Thus, the check for an invoked method is done at run-time in dynamically typed languages and the method defined on that object type is called. If the method is undefined, a “NoMethodError” exception is raised.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Python===&lt;br /&gt;
&lt;br /&gt;
Python also heavily uses duck typing to allow flexibility to programmers. It emphasizes on the interface rather than on the specifc type which allows for polymorphic substitution [link].&lt;br /&gt;
In Python, functions like type() or isistance() is used to check for usual methods to match the type of find out if an object is an instance of a particular class. However, in case of duck typing, the focus is on the attribute of that object, so it checks for the presence of a particular attribute in that object using hasattr().&lt;br /&gt;
&lt;br /&gt;
Example for duck typing in Python is where Python uses a “pickle” module to serialize and de-serialize an object. So, we can introduce a new class that creates a “Tape” object. Now, an object of this type can be passed to the pickle module. Python will not differentiate between a file object or a tape object as long as they both have read() and write() methods.&lt;br /&gt;
&lt;br /&gt;
   class Tapefile():&lt;br /&gt;
      “A tape file”&lt;br /&gt;
       def __init__(self,params):&lt;br /&gt;
          #various initialization for tape access&lt;br /&gt;
       pass&lt;br /&gt;
       def read(len):&lt;br /&gt;
          #use special methods to access tape&lt;br /&gt;
       pass&lt;br /&gt;
       def readline():&lt;br /&gt;
          #use special methods to read a whole line on tape&lt;br /&gt;
       pass&lt;br /&gt;
       def write(value):&lt;br /&gt;
          #use special methods to write on tape&lt;br /&gt;
       pass&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in other languages===&lt;br /&gt;
&lt;br /&gt;
Duck typing is considered to be a feature provided by dynamic languages. C++ as a static language checks for methods by parameter and signature types. Java does not support duck typing either but it is argued that it can be simulated by the use of interfaces. The concept of duck-typing is criticized as it requires the programmer to have a very wide understanding of the source code and that two objects may implement a method in completely different contexts and so it may result in strange errors. &lt;br /&gt;
&lt;br /&gt;
Although duck typing is considered to be a feature of dynamic languages, some static languages like C# and Boo support it by allowing type checking of classes by the compiler to occur at run time.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sthirum</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53174</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b ms</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53174"/>
		<updated>2011-10-20T19:26:10Z</updated>

		<summary type="html">&lt;p&gt;Sthirum: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Lecture 5: Object Oriented Programming in Ruby =&lt;br /&gt;
&lt;br /&gt;
This wiki tries to cover the Object Oriented concepts in ruby.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Lecture 5 &amp;lt;ref name=&amp;quot;CSC517&amp;quot;&amp;gt;CSC 517 Lecture5 - OOP in Ruby&amp;lt;/ref&amp;gt; [http://courses.ncsu.edu/csc517//common/lectures/notes/lec5.pdf] of CSC 517 covered topics of [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented programming] in [http://www.ruby-lang.org/en/ Ruby]. Object oriented programming is a paradigm in which real world entities are represented as objects, where each object has its own attributes and set of functions that it can perform. This gives a data structure called an &amp;quot;object&amp;quot; that consists of data fields and operations. This paradigm presents features like [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) encapsulation], [http://en.wikipedia.org/wiki/Data_abstraction data abstraction], [http://en.wikipedia.org/wiki/Inheritance_(computer_science) inheritance] and [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This wiki page tries to cover the other OOP languages and their features in a succinct manner and talks about the features of Ruby in detail with examples. We have also tried to compare the various languages in terms of their features to provide a better understanding.&lt;br /&gt;
&lt;br /&gt;
The lecture also covered the concepts of duck typing in Ruby, closures and currying. The latter two have not been included as per the instructions by the instructor.&lt;br /&gt;
&lt;br /&gt;
== Object oriented languages: An Overview ==&lt;br /&gt;
&lt;br /&gt;
==== C++: ====&lt;br /&gt;
C++ is a statically typed, multi-paradigm programming language. It supports some features of object-oriented languages like encapsulation, polymorphism and inheritance. It provides access control by specifiers public, private and protected and also allows “friend” classes. C++  allows multiple inheritance which is eliminated in most other programming languages as it allows a class to inherit from more than one base class which results in an ambiguity effect called [http://en.wikipedia.org/wiki/Diamond_problem Diamond problem].&lt;br /&gt;
&lt;br /&gt;
==== Java: ====&lt;br /&gt;
Java is classified as a statically typed, hybrid object oriented language. Although it provides information hiding, inheritance and polymorphism, it has a few basic types incorporated that are not objects and some of the built-in operators are provided by basic arithmetic and not as messages to objects. [link]. For these reasons, it is not considered to be a “pure” object-oriented language.&lt;br /&gt;
&lt;br /&gt;
Java provides access control and supports private, public and protected access. It also allows to create packages to create and maintain namespaces. Java is designed not to provide multiple inheritance to avoid problems faced in C++. However, it presents a single-rooted hierarchy where “Object” is the ancestor of all classes (seen in Ruby too) and provides interfaces to extend/inherit functionality.&lt;br /&gt;
&lt;br /&gt;
==== Python: ====&lt;br /&gt;
Python is a dynamically typed, interpreted, object oriented language. It is argued that Python is not completely object oriented as it does not provide access control in the form of public, private or protected variables. It has different security mechanisms. However, it allows to emulate protected member by using the prefix '_' before the name and private member by the use of the prefix '__' (double underscore) before the name.&lt;br /&gt;
&lt;br /&gt;
==== Ruby: ====&lt;br /&gt;
Ruby is dynamically typed and highly object-oriented, much more than Java and Python. In Ruby, it is said that “everything is an object”.  Every value happens to be an object in Ruby. You may call methods on a string, an integer just like you call on any object in Java. For ex,&lt;br /&gt;
&lt;br /&gt;
     (2.1143).size.times{puts “Hello!”}&lt;br /&gt;
&lt;br /&gt;
This example will display “Hello!” six times on your IRB as the size of the parameter &amp;quot;2.1143&amp;quot; is 6.&lt;br /&gt;
&lt;br /&gt;
== Comparison of object Oriented Languages ==&lt;br /&gt;
The following table compares the Properties of some of the well-known Object Oriented Languages.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 9pad case0%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Language&lt;br /&gt;
!Ruby&lt;br /&gt;
!Java&lt;br /&gt;
!C#&lt;br /&gt;
!C++&lt;br /&gt;
!Python&lt;br /&gt;
!SmallTalk&lt;br /&gt;
!Perl&lt;br /&gt;
|-&lt;br /&gt;
!Object Orientation&lt;br /&gt;
|Pure&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid/Multi-Paradigm&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Pure&lt;br /&gt;
|Add-on/Hybrid&lt;br /&gt;
|-&lt;br /&gt;
!Static/Dynamic Typing&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|-&lt;br /&gt;
!Generic Classes&lt;br /&gt;
|N/A&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|-&lt;br /&gt;
!Inheritance&lt;br /&gt;
|Single Class/Multiple Mixins&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Multiple&lt;br /&gt;
|Multiple&lt;br /&gt;
|Single&lt;br /&gt;
|Multiple&lt;br /&gt;
|- &lt;br /&gt;
!Feature Renaming&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|NO&lt;br /&gt;
|-&lt;br /&gt;
!Method Overloading&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Operator Overloading&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|-&lt;br /&gt;
!Class Variables or methods&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Access Control&lt;br /&gt;
|Public, Protected, Private&lt;br /&gt;
|Public, Protected, Package, Private&lt;br /&gt;
|Public, Protected, Private, Internal, Protected Internal&lt;br /&gt;
|Public, Protected, Private, Friends&lt;br /&gt;
|Name Mangling&lt;br /&gt;
|Protected Data, Public Methods&lt;br /&gt;
|None&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Ruby : Purely Object Oriented ==&lt;br /&gt;
&lt;br /&gt;
Ruby is considered to be a purely Object oriented Language, as everything in it is considered to be an object. Unlike Java, even primitives such as characters, punctuation, numbers and strings are treated as objects in Ruby. Ruby was designed to facilitate the enforcement of Object Oriented Methods. Ruby is a Dynamically Typed Language.&lt;br /&gt;
&lt;br /&gt;
=== Object oriented Concepts in ruby: ===&lt;br /&gt;
&lt;br /&gt;
Ruby is a singly rooted Object Oriented Programming language which has all its classes having the same superclass which is the class Object.&lt;br /&gt;
&lt;br /&gt;
==== Classes:====&lt;br /&gt;
[[Image:RubyClass.png|thumb|right|280px|'''''Classes in Ruby''''']]&lt;br /&gt;
&lt;br /&gt;
A class is used to define a blueprint of a data type. It does not contain any data, but it specifies what kind of variables and methods an object of the class will have.&lt;br /&gt;
A class in Ruby is defined using the keyword class followed by the classname. The variables and methods are defined within the class definition and the class definition terminates with the end keyword.&lt;br /&gt;
&lt;br /&gt;
For ex:&lt;br /&gt;
  class Rectangle&lt;br /&gt;
     def initialize(w,l)&lt;br /&gt;
        @width, @length = w, l&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
Here, the initialize method is used to set the values of the instance variables (@width and @length. The instance variables are the properties of the objects created of this class type. These instance variables can be accessed within the class using an @. These variables can be accessed from outside the class using the accessor methods which are defined inside the class.&lt;br /&gt;
&lt;br /&gt;
In the following case printLength and printWidth are public methods used to access the instance variables from outside the class. These are called accessor methods.&lt;br /&gt;
Similarly, to&lt;br /&gt;
&lt;br /&gt;
   class Rectangle&lt;br /&gt;
  # constructor method&lt;br /&gt;
  def initialize(w,l)&lt;br /&gt;
     @width, @length = w, l&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # accessor methods&lt;br /&gt;
  def printWidth&lt;br /&gt;
     @width&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def printLength&lt;br /&gt;
     @length&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # setter methods&lt;br /&gt;
  def setWidth=(value)&lt;br /&gt;
     @width = value&lt;br /&gt;
  end&lt;br /&gt;
  def setHeight=(value)&lt;br /&gt;
     @height = value&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   # create an object&lt;br /&gt;
     rectagle = Rectangle.new(10, 20)&lt;br /&gt;
&lt;br /&gt;
    # use accessor methods&lt;br /&gt;
      x = rectangle.printWidth()&lt;br /&gt;
      y = rectangle.printLength()&lt;br /&gt;
&lt;br /&gt;
      puts &amp;quot;Width of the Rectangle is : #{x}&amp;quot;&lt;br /&gt;
      puts &amp;quot;Length of the Rectangle is : #{y}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==== Class Methods and Class Variables:====&lt;br /&gt;
A class variable is shared between all instances of the class. The value of the class variable remains the same for all the objects of the class. A class variable is accessed within the class using two @ signs at the beginning of the variable name. The class variables must be initialized within the class.&lt;br /&gt;
&lt;br /&gt;
The class methods are defined using def self.methodname() and ends with an end delimiter. These methods are called using the class name itself, for example: classname.methodname&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Attributes and accessor methods: ===&lt;br /&gt;
&lt;br /&gt;
Every class in Ruby defines a set of attributes for its objects. Ruby provides accessor methods (famously known as getter-setter methods in Java) to access these variables. Ruby provides three kinds of accessor methods:&lt;br /&gt;
&lt;br /&gt;
    Class Car&lt;br /&gt;
       attr_accessor : name, company&lt;br /&gt;
       attr_reader: price&lt;br /&gt;
       attr_writer: color&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
attr_accessor creates a setter and getter method. The attr_reader method creates only the getter method. This is required in case of attributes that are not to be modified by other programs but their value may be viewed. The attr_writer creates the setter method. In case of Java, the setter and getter methods need to be written in 3 lines of code each whereas in Ruby, the methods are written just by specifying the type of accessor, which have already been coded using Metaprogramming.[link]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Access Control:====&lt;br /&gt;
The only easy way to change an object's state in Ruby is by calling one of its methods. Control access to the methods, and you have controlled access to the object. A good rule of the thumb is never to expose methods that could leave an object in an invalid state.&lt;br /&gt;
&lt;br /&gt;
Ruby gives you three levels of protection:&lt;br /&gt;
&lt;br /&gt;
''Public methods'' can be called by everyone - no access control is enforced. A class's instance methods (these do not belong only to one object; instead, every instance of the class can call them) are public by default; anyone can call them. The initialize method is always private.&lt;br /&gt;
&lt;br /&gt;
''Protected methods'' can be invoked only by objects of the defining class and its subclasses. Access is kept within the family. However, usage of protected is limited.&lt;br /&gt;
&lt;br /&gt;
''Private methods'' cannot be called with an explicit receiver - the receiver is always self. This means that private methods can be called only in the context of the current object; you cannot invoke another object's private methods.&lt;br /&gt;
&lt;br /&gt;
Access control is determined dynamically, as the program runs, not statically. You will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
Ex:&lt;br /&gt;
&lt;br /&gt;
    class Employee&lt;br /&gt;
       def initialize(name, age, salary)&lt;br /&gt;
           @name = name&lt;br /&gt;
           @age = age&lt;br /&gt;
           @salary = salary&lt;br /&gt;
       end&lt;br /&gt;
  &lt;br /&gt;
       def name&lt;br /&gt;
           @name&lt;br /&gt;
       end &lt;br /&gt;
       def age&lt;br /&gt;
          @age&lt;br /&gt;
       end&lt;br /&gt;
       def salary&lt;br /&gt;
           @salary&lt;br /&gt;
       end&lt;br /&gt;
&lt;br /&gt;
        public :name&lt;br /&gt;
        protected :age&lt;br /&gt;
        private :salary&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      emp = Employee.new('Jack', 25, 2000);&lt;br /&gt;
      puts emp.name&lt;br /&gt;
      #puts emp.age    #age protected. gives access violation error&lt;br /&gt;
      #puts emp.salary #salary private&lt;br /&gt;
&lt;br /&gt;
====Abstract methods====&lt;br /&gt;
&lt;br /&gt;
Ruby is a dynamically typed language and does not provide abstract classes as in Java or C++. It instead provides modules that can be included within any class. The reason why Ruby does not provide abstract classes is because of the idea that it only matters if two objects can perform a function, they don’t necessarily have to be inherited from the same class. This paradigm is provided by modules, where the method of a module can be redefined as required by a class that includes it.&lt;br /&gt;
&lt;br /&gt;
Also, if required the technique of abstract class can be simulated by something like this:&lt;br /&gt;
&lt;br /&gt;
  Class Vehicle&lt;br /&gt;
   def drive&lt;br /&gt;
   raise NotImplementedError.new(&amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The subclasses that inherit from the above class will have to redefine the method “drive” as required.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Ruby ===&lt;br /&gt;
&lt;br /&gt;
Ruby provides unbounded polymorphism as a dynamically typed language through the concept of “Duck-typing”. “Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface” [link]. This concept is explained by the famous definition as “When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.” This essentially means that in languages like Ruby, it is only checked if an object can implement a function and the type of the object is not taken into consideration. It can be explained better with the help of the following example&lt;br /&gt;
&lt;br /&gt;
  class Lion&lt;br /&gt;
    def roar&lt;br /&gt;
        puts “The Lion roars when hungry”&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
     def roar&lt;br /&gt;
        puts “The man roared in fury and anger”&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   def quality lion&lt;br /&gt;
    lion.roar&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def start&lt;br /&gt;
    leo = Lion.new&lt;br /&gt;
    jack = Person.new&lt;br /&gt;
    quality leo&lt;br /&gt;
    quality jack&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   start&lt;br /&gt;
&lt;br /&gt;
In the above example, leo is an object of class “Lion” and the “roar” method defined in its class is invoked. jack is an object of class “Person” and the corresponding method defined in the Person class is called for him. Although in the real world, both objects are not of the same type, Ruby allows unbounded polymorphism as both of them implement a particular aspect.&lt;br /&gt;
&lt;br /&gt;
Thus, the check for an invoked method is done at run-time in dynamically typed languages and the method defined on that object type is called. If the method is undefined, a “NoMethodError” exception is raised.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Python===&lt;br /&gt;
&lt;br /&gt;
Python also heavily uses duck typing to allow flexibility to programmers. It emphasizes on the interface rather than on the specifc type which allows for polymorphic substitution [link].&lt;br /&gt;
In Python, functions like type() or isistance() is used to check for usual methods to match the type of find out if an object is an instance of a particular class. However, in case of duck typing, the focus is on the attribute of that object, so it checks for the presence of a particular attribute in that object using hasattr().&lt;br /&gt;
&lt;br /&gt;
Example for duck typing in Python is where Python uses a “pickle” module to serialize and de-serialize an object. So, we can introduce a new class that creates a “Tape” object. Now, an object of this type can be passed to the pickle module. Python will not differentiate between a file object or a tape object as long as they both have read() and write() methods.&lt;br /&gt;
&lt;br /&gt;
   class Tapefile():&lt;br /&gt;
      “A tape file”&lt;br /&gt;
       def __init__(self,params):&lt;br /&gt;
          #various initialization for tape access&lt;br /&gt;
       pass&lt;br /&gt;
       def read(len):&lt;br /&gt;
          #use special methods to access tape&lt;br /&gt;
       pass&lt;br /&gt;
       def readline():&lt;br /&gt;
          #use special methods to read a whole line on tape&lt;br /&gt;
       pass&lt;br /&gt;
       def write(value):&lt;br /&gt;
          #use special methods to write on tape&lt;br /&gt;
       pass&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in other languages===&lt;br /&gt;
&lt;br /&gt;
Duck typing is considered to be a feature provided by dynamic languages. C++ as a static language checks for methods by parameter and signature types. Java does not support duck typing either but it is argued that it can be simulated by the use of interfaces. The concept of duck-typing is criticized as it requires the programmer to have a very wide understanding of the source code and that two objects may implement a method in completely different contexts and so it may result in strange errors. &lt;br /&gt;
&lt;br /&gt;
Although duck typing is considered to be a feature of dynamic languages, some static languages like C# and Boo support it by allowing type checking of classes by the compiler to occur at run time.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sthirum</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53173</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b ms</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53173"/>
		<updated>2011-10-20T19:25:21Z</updated>

		<summary type="html">&lt;p&gt;Sthirum: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Lecture 5: Object Oriented Programming in Ruby =&lt;br /&gt;
&lt;br /&gt;
This wiki tries to cover the Object Oriented concepts in ruby.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Lecture 5 &amp;lt;ref name=&amp;quot;CSC517&amp;quot;&amp;gt;CSC 517 Lecture5 - OOP in Ruby&amp;lt;/ref&amp;gt; of CSC 517 covered topics of [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented programming] in [http://www.ruby-lang.org/en/ Ruby]. Object oriented programming is a paradigm in which real world entities are represented as objects, where each object has its own attributes and set of functions that it can perform. This gives a data structure called an &amp;quot;object&amp;quot; that consists of data fields and operations. This paradigm presents features like [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) encapsulation], [http://en.wikipedia.org/wiki/Data_abstraction data abstraction], [http://en.wikipedia.org/wiki/Inheritance_(computer_science) inheritance] and [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This wiki page tries to cover the other OOP languages and their features in a succinct manner and talks about the features of Ruby in detail with examples. We have also tried to compare the various languages in terms of their features to provide a better understanding.&lt;br /&gt;
&lt;br /&gt;
The lecture also covered the concepts of duck typing in Ruby, closures and currying. The latter two have not been included as per the instructions by the instructor.&lt;br /&gt;
&lt;br /&gt;
== Object oriented languages: An Overview ==&lt;br /&gt;
&lt;br /&gt;
==== C++: ====&lt;br /&gt;
C++ is a statically typed, multi-paradigm programming language. It supports some features of object-oriented languages like encapsulation, polymorphism and inheritance. It provides access control by specifiers public, private and protected and also allows “friend” classes. C++  allows multiple inheritance which is eliminated in most other programming languages as it allows a class to inherit from more than one base class which results in an ambiguity effect called [http://en.wikipedia.org/wiki/Diamond_problem Diamond problem].&lt;br /&gt;
&lt;br /&gt;
==== Java: ====&lt;br /&gt;
Java is classified as a statically typed, hybrid object oriented language. Although it provides information hiding, inheritance and polymorphism, it has a few basic types incorporated that are not objects and some of the built-in operators are provided by basic arithmetic and not as messages to objects. [link]. For these reasons, it is not considered to be a “pure” object-oriented language.&lt;br /&gt;
&lt;br /&gt;
Java provides access control and supports private, public and protected access. It also allows to create packages to create and maintain namespaces. Java is designed not to provide multiple inheritance to avoid problems faced in C++. However, it presents a single-rooted hierarchy where “Object” is the ancestor of all classes (seen in Ruby too) and provides interfaces to extend/inherit functionality.&lt;br /&gt;
&lt;br /&gt;
==== Python: ====&lt;br /&gt;
Python is a dynamically typed, interpreted, object oriented language. It is argued that Python is not completely object oriented as it does not provide access control in the form of public, private or protected variables. It has different security mechanisms. However, it allows to emulate protected member by using the prefix '_' before the name and private member by the use of the prefix '__' (double underscore) before the name.&lt;br /&gt;
&lt;br /&gt;
==== Ruby: ====&lt;br /&gt;
Ruby is dynamically typed and highly object-oriented, much more than Java and Python. In Ruby, it is said that “everything is an object”.  Every value happens to be an object in Ruby. You may call methods on a string, an integer just like you call on any object in Java. For ex,&lt;br /&gt;
&lt;br /&gt;
     (2.1143).size.times{puts “Hello!”}&lt;br /&gt;
&lt;br /&gt;
This example will display “Hello!” six times on your IRB as the size of the parameter &amp;quot;2.1143&amp;quot; is 6.&lt;br /&gt;
&lt;br /&gt;
== Comparison of object Oriented Languages ==&lt;br /&gt;
The following table compares the Properties of some of the well-known Object Oriented Languages.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 9pad case0%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Language&lt;br /&gt;
!Ruby&lt;br /&gt;
!Java&lt;br /&gt;
!C#&lt;br /&gt;
!C++&lt;br /&gt;
!Python&lt;br /&gt;
!SmallTalk&lt;br /&gt;
!Perl&lt;br /&gt;
|-&lt;br /&gt;
!Object Orientation&lt;br /&gt;
|Pure&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid/Multi-Paradigm&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Pure&lt;br /&gt;
|Add-on/Hybrid&lt;br /&gt;
|-&lt;br /&gt;
!Static/Dynamic Typing&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|-&lt;br /&gt;
!Generic Classes&lt;br /&gt;
|N/A&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|-&lt;br /&gt;
!Inheritance&lt;br /&gt;
|Single Class/Multiple Mixins&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Multiple&lt;br /&gt;
|Multiple&lt;br /&gt;
|Single&lt;br /&gt;
|Multiple&lt;br /&gt;
|- &lt;br /&gt;
!Feature Renaming&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|NO&lt;br /&gt;
|-&lt;br /&gt;
!Method Overloading&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Operator Overloading&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|-&lt;br /&gt;
!Class Variables or methods&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Access Control&lt;br /&gt;
|Public, Protected, Private&lt;br /&gt;
|Public, Protected, Package, Private&lt;br /&gt;
|Public, Protected, Private, Internal, Protected Internal&lt;br /&gt;
|Public, Protected, Private, Friends&lt;br /&gt;
|Name Mangling&lt;br /&gt;
|Protected Data, Public Methods&lt;br /&gt;
|None&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Ruby : Purely Object Oriented ==&lt;br /&gt;
&lt;br /&gt;
Ruby is considered to be a purely Object oriented Language, as everything in it is considered to be an object. Unlike Java, even primitives such as characters, punctuation, numbers and strings are treated as objects in Ruby. Ruby was designed to facilitate the enforcement of Object Oriented Methods. Ruby is a Dynamically Typed Language.&lt;br /&gt;
&lt;br /&gt;
=== Object oriented Concepts in ruby: ===&lt;br /&gt;
&lt;br /&gt;
Ruby is a singly rooted Object Oriented Programming language which has all its classes having the same superclass which is the class Object.&lt;br /&gt;
&lt;br /&gt;
==== Classes:====&lt;br /&gt;
[[Image:RubyClass.png|thumb|right|280px|'''''Classes in Ruby''''']]&lt;br /&gt;
&lt;br /&gt;
A class is used to define a blueprint of a data type. It does not contain any data, but it specifies what kind of variables and methods an object of the class will have.&lt;br /&gt;
A class in Ruby is defined using the keyword class followed by the classname. The variables and methods are defined within the class definition and the class definition terminates with the end keyword.&lt;br /&gt;
&lt;br /&gt;
For ex:&lt;br /&gt;
  class Rectangle&lt;br /&gt;
     def initialize(w,l)&lt;br /&gt;
        @width, @length = w, l&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
Here, the initialize method is used to set the values of the instance variables (@width and @length. The instance variables are the properties of the objects created of this class type. These instance variables can be accessed within the class using an @. These variables can be accessed from outside the class using the accessor methods which are defined inside the class.&lt;br /&gt;
&lt;br /&gt;
In the following case printLength and printWidth are public methods used to access the instance variables from outside the class. These are called accessor methods.&lt;br /&gt;
Similarly, to&lt;br /&gt;
&lt;br /&gt;
   class Rectangle&lt;br /&gt;
  # constructor method&lt;br /&gt;
  def initialize(w,l)&lt;br /&gt;
     @width, @length = w, l&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # accessor methods&lt;br /&gt;
  def printWidth&lt;br /&gt;
     @width&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def printLength&lt;br /&gt;
     @length&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # setter methods&lt;br /&gt;
  def setWidth=(value)&lt;br /&gt;
     @width = value&lt;br /&gt;
  end&lt;br /&gt;
  def setHeight=(value)&lt;br /&gt;
     @height = value&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   # create an object&lt;br /&gt;
     rectagle = Rectangle.new(10, 20)&lt;br /&gt;
&lt;br /&gt;
    # use accessor methods&lt;br /&gt;
      x = rectangle.printWidth()&lt;br /&gt;
      y = rectangle.printLength()&lt;br /&gt;
&lt;br /&gt;
      puts &amp;quot;Width of the Rectangle is : #{x}&amp;quot;&lt;br /&gt;
      puts &amp;quot;Length of the Rectangle is : #{y}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==== Class Methods and Class Variables:====&lt;br /&gt;
A class variable is shared between all instances of the class. The value of the class variable remains the same for all the objects of the class. A class variable is accessed within the class using two @ signs at the beginning of the variable name. The class variables must be initialized within the class.&lt;br /&gt;
&lt;br /&gt;
The class methods are defined using def self.methodname() and ends with an end delimiter. These methods are called using the class name itself, for example: classname.methodname&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Attributes and accessor methods: ===&lt;br /&gt;
&lt;br /&gt;
Every class in Ruby defines a set of attributes for its objects. Ruby provides accessor methods (famously known as getter-setter methods in Java) to access these variables. Ruby provides three kinds of accessor methods:&lt;br /&gt;
&lt;br /&gt;
    Class Car&lt;br /&gt;
       attr_accessor : name, company&lt;br /&gt;
       attr_reader: price&lt;br /&gt;
       attr_writer: color&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
attr_accessor creates a setter and getter method. The attr_reader method creates only the getter method. This is required in case of attributes that are not to be modified by other programs but their value may be viewed. The attr_writer creates the setter method. In case of Java, the setter and getter methods need to be written in 3 lines of code each whereas in Ruby, the methods are written just by specifying the type of accessor, which have already been coded using Metaprogramming.[link]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Access Control:====&lt;br /&gt;
The only easy way to change an object's state in Ruby is by calling one of its methods. Control access to the methods, and you have controlled access to the object. A good rule of the thumb is never to expose methods that could leave an object in an invalid state.&lt;br /&gt;
&lt;br /&gt;
Ruby gives you three levels of protection:&lt;br /&gt;
&lt;br /&gt;
''Public methods'' can be called by everyone - no access control is enforced. A class's instance methods (these do not belong only to one object; instead, every instance of the class can call them) are public by default; anyone can call them. The initialize method is always private.&lt;br /&gt;
&lt;br /&gt;
''Protected methods'' can be invoked only by objects of the defining class and its subclasses. Access is kept within the family. However, usage of protected is limited.&lt;br /&gt;
&lt;br /&gt;
''Private methods'' cannot be called with an explicit receiver - the receiver is always self. This means that private methods can be called only in the context of the current object; you cannot invoke another object's private methods.&lt;br /&gt;
&lt;br /&gt;
Access control is determined dynamically, as the program runs, not statically. You will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
Ex:&lt;br /&gt;
&lt;br /&gt;
    class Employee&lt;br /&gt;
       def initialize(name, age, salary)&lt;br /&gt;
           @name = name&lt;br /&gt;
           @age = age&lt;br /&gt;
           @salary = salary&lt;br /&gt;
       end&lt;br /&gt;
  &lt;br /&gt;
       def name&lt;br /&gt;
           @name&lt;br /&gt;
       end &lt;br /&gt;
       def age&lt;br /&gt;
          @age&lt;br /&gt;
       end&lt;br /&gt;
       def salary&lt;br /&gt;
           @salary&lt;br /&gt;
       end&lt;br /&gt;
&lt;br /&gt;
        public :name&lt;br /&gt;
        protected :age&lt;br /&gt;
        private :salary&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      emp = Employee.new('Jack', 25, 2000);&lt;br /&gt;
      puts emp.name&lt;br /&gt;
      #puts emp.age    #age protected. gives access violation error&lt;br /&gt;
      #puts emp.salary #salary private&lt;br /&gt;
&lt;br /&gt;
====Abstract methods====&lt;br /&gt;
&lt;br /&gt;
Ruby is a dynamically typed language and does not provide abstract classes as in Java or C++. It instead provides modules that can be included within any class. The reason why Ruby does not provide abstract classes is because of the idea that it only matters if two objects can perform a function, they don’t necessarily have to be inherited from the same class. This paradigm is provided by modules, where the method of a module can be redefined as required by a class that includes it.&lt;br /&gt;
&lt;br /&gt;
Also, if required the technique of abstract class can be simulated by something like this:&lt;br /&gt;
&lt;br /&gt;
  Class Vehicle&lt;br /&gt;
   def drive&lt;br /&gt;
   raise NotImplementedError.new(&amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The subclasses that inherit from the above class will have to redefine the method “drive” as required.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Ruby ===&lt;br /&gt;
&lt;br /&gt;
Ruby provides unbounded polymorphism as a dynamically typed language through the concept of “Duck-typing”. “Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface” [link]. This concept is explained by the famous definition as “When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.” This essentially means that in languages like Ruby, it is only checked if an object can implement a function and the type of the object is not taken into consideration. It can be explained better with the help of the following example&lt;br /&gt;
&lt;br /&gt;
  class Lion&lt;br /&gt;
    def roar&lt;br /&gt;
        puts “The Lion roars when hungry”&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
     def roar&lt;br /&gt;
        puts “The man roared in fury and anger”&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   def quality lion&lt;br /&gt;
    lion.roar&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def start&lt;br /&gt;
    leo = Lion.new&lt;br /&gt;
    jack = Person.new&lt;br /&gt;
    quality leo&lt;br /&gt;
    quality jack&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   start&lt;br /&gt;
&lt;br /&gt;
In the above example, leo is an object of class “Lion” and the “roar” method defined in its class is invoked. jack is an object of class “Person” and the corresponding method defined in the Person class is called for him. Although in the real world, both objects are not of the same type, Ruby allows unbounded polymorphism as both of them implement a particular aspect.&lt;br /&gt;
&lt;br /&gt;
Thus, the check for an invoked method is done at run-time in dynamically typed languages and the method defined on that object type is called. If the method is undefined, a “NoMethodError” exception is raised.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Python===&lt;br /&gt;
&lt;br /&gt;
Python also heavily uses duck typing to allow flexibility to programmers. It emphasizes on the interface rather than on the specifc type which allows for polymorphic substitution [link].&lt;br /&gt;
In Python, functions like type() or isistance() is used to check for usual methods to match the type of find out if an object is an instance of a particular class. However, in case of duck typing, the focus is on the attribute of that object, so it checks for the presence of a particular attribute in that object using hasattr().&lt;br /&gt;
&lt;br /&gt;
Example for duck typing in Python is where Python uses a “pickle” module to serialize and de-serialize an object. So, we can introduce a new class that creates a “Tape” object. Now, an object of this type can be passed to the pickle module. Python will not differentiate between a file object or a tape object as long as they both have read() and write() methods.&lt;br /&gt;
&lt;br /&gt;
   class Tapefile():&lt;br /&gt;
      “A tape file”&lt;br /&gt;
       def __init__(self,params):&lt;br /&gt;
          #various initialization for tape access&lt;br /&gt;
       pass&lt;br /&gt;
       def read(len):&lt;br /&gt;
          #use special methods to access tape&lt;br /&gt;
       pass&lt;br /&gt;
       def readline():&lt;br /&gt;
          #use special methods to read a whole line on tape&lt;br /&gt;
       pass&lt;br /&gt;
       def write(value):&lt;br /&gt;
          #use special methods to write on tape&lt;br /&gt;
       pass&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in other languages===&lt;br /&gt;
&lt;br /&gt;
Duck typing is considered to be a feature provided by dynamic languages. C++ as a static language checks for methods by parameter and signature types. Java does not support duck typing either but it is argued that it can be simulated by the use of interfaces. The concept of duck-typing is criticized as it requires the programmer to have a very wide understanding of the source code and that two objects may implement a method in completely different contexts and so it may result in strange errors. &lt;br /&gt;
&lt;br /&gt;
Although duck typing is considered to be a feature of dynamic languages, some static languages like C# and Boo support it by allowing type checking of classes by the compiler to occur at run time.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sthirum</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53168</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b ms</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53168"/>
		<updated>2011-10-20T19:22:59Z</updated>

		<summary type="html">&lt;p&gt;Sthirum: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Lecture 5: Object Oriented Programming in Ruby =&lt;br /&gt;
&lt;br /&gt;
This wiki tries to cover the Object Oriented concepts in ruby.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Lecture 5 &amp;lt;ref&amp;gt;{{Cite web|url=http://courses.ncsu.edu/csc517//common/lectures/notes/lec5.pdf |title=CSC 517 OOP in Ruby}}&amp;lt;/ref&amp;gt; of CSC 517 covered topics of [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented programming] in [http://www.ruby-lang.org/en/ Ruby]. Object oriented programming is a paradigm in which real world entities are represented as objects, where each object has its own attributes and set of functions that it can perform. This gives a data structure called an &amp;quot;object&amp;quot; that consists of data fields and operations. This paradigm presents features like [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) encapsulation], [http://en.wikipedia.org/wiki/Data_abstraction data abstraction], [http://en.wikipedia.org/wiki/Inheritance_(computer_science) inheritance] and [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This wiki page tries to cover the other OOP languages and their features in a succinct manner and talks about the features of Ruby in detail with examples. We have also tried to compare the various languages in terms of their features to provide a better understanding.&lt;br /&gt;
&lt;br /&gt;
The lecture also covered the concepts of duck typing in Ruby, closures and currying. The latter two have not been included as per the instructions by the instructor.&lt;br /&gt;
&lt;br /&gt;
== Object oriented languages: An Overview ==&lt;br /&gt;
&lt;br /&gt;
==== C++: ====&lt;br /&gt;
C++ is a statically typed, multi-paradigm programming language. It supports some features of object-oriented languages like encapsulation, polymorphism and inheritance. It provides access control by specifiers public, private and protected and also allows “friend” classes. C++  allows multiple inheritance which is eliminated in most other programming languages as it allows a class to inherit from more than one base class which results in an ambiguity effect called [http://en.wikipedia.org/wiki/Diamond_problem Diamond problem].&lt;br /&gt;
&lt;br /&gt;
==== Java: ====&lt;br /&gt;
Java is classified as a statically typed, hybrid object oriented language. Although it provides information hiding, inheritance and polymorphism, it has a few basic types incorporated that are not objects and some of the built-in operators are provided by basic arithmetic and not as messages to objects. [link]. For these reasons, it is not considered to be a “pure” object-oriented language.&lt;br /&gt;
&lt;br /&gt;
Java provides access control and supports private, public and protected access. It also allows to create packages to create and maintain namespaces. Java is designed not to provide multiple inheritance to avoid problems faced in C++. However, it presents a single-rooted hierarchy where “Object” is the ancestor of all classes (seen in Ruby too) and provides interfaces to extend/inherit functionality.&lt;br /&gt;
&lt;br /&gt;
==== Python: ====&lt;br /&gt;
Python is a dynamically typed, interpreted, object oriented language. It is argued that Python is not completely object oriented as it does not provide access control in the form of public, private or protected variables. It has different security mechanisms. However, it allows to emulate protected member by using the prefix '_' before the name and private member by the use of the prefix '__' (double underscore) before the name.&lt;br /&gt;
&lt;br /&gt;
==== Ruby: ====&lt;br /&gt;
Ruby is dynamically typed and highly object-oriented, much more than Java and Python. In Ruby, it is said that “everything is an object”.  Every value happens to be an object in Ruby. You may call methods on a string, an integer just like you call on any object in Java. For ex,&lt;br /&gt;
&lt;br /&gt;
     (2.1143).size.times{puts “Hello!”}&lt;br /&gt;
&lt;br /&gt;
This example will display “Hello!” six times on your IRB as the size of the parameter &amp;quot;2.1143&amp;quot; is 6.&lt;br /&gt;
&lt;br /&gt;
== Comparison of object Oriented Languages ==&lt;br /&gt;
The following table compares the Properties of some of the well-known Object Oriented Languages.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 9pad case0%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Language&lt;br /&gt;
!Ruby&lt;br /&gt;
!Java&lt;br /&gt;
!C#&lt;br /&gt;
!C++&lt;br /&gt;
!Python&lt;br /&gt;
!SmallTalk&lt;br /&gt;
!Perl&lt;br /&gt;
|-&lt;br /&gt;
!Object Orientation&lt;br /&gt;
|Pure&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid/Multi-Paradigm&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Pure&lt;br /&gt;
|Add-on/Hybrid&lt;br /&gt;
|-&lt;br /&gt;
!Static/Dynamic Typing&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|-&lt;br /&gt;
!Generic Classes&lt;br /&gt;
|N/A&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|-&lt;br /&gt;
!Inheritance&lt;br /&gt;
|Single Class/Multiple Mixins&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Multiple&lt;br /&gt;
|Multiple&lt;br /&gt;
|Single&lt;br /&gt;
|Multiple&lt;br /&gt;
|- &lt;br /&gt;
!Feature Renaming&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|NO&lt;br /&gt;
|-&lt;br /&gt;
!Method Overloading&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Operator Overloading&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|-&lt;br /&gt;
!Class Variables or methods&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Access Control&lt;br /&gt;
|Public, Protected, Private&lt;br /&gt;
|Public, Protected, Package, Private&lt;br /&gt;
|Public, Protected, Private, Internal, Protected Internal&lt;br /&gt;
|Public, Protected, Private, Friends&lt;br /&gt;
|Name Mangling&lt;br /&gt;
|Protected Data, Public Methods&lt;br /&gt;
|None&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Ruby : Purely Object Oriented ==&lt;br /&gt;
&lt;br /&gt;
Ruby is considered to be a purely Object oriented Language, as everything in it is considered to be an object. Unlike Java, even primitives such as characters, punctuation, numbers and strings are treated as objects in Ruby. Ruby was designed to facilitate the enforcement of Object Oriented Methods. Ruby is a Dynamically Typed Language.&lt;br /&gt;
&lt;br /&gt;
=== Object oriented Concepts in ruby: ===&lt;br /&gt;
&lt;br /&gt;
Ruby is a singly rooted Object Oriented Programming language which has all its classes having the same superclass which is the class Object.&lt;br /&gt;
&lt;br /&gt;
==== Classes:====&lt;br /&gt;
[[Image:RubyClass.png|thumb|right|280px|'''''Classes in Ruby''''']]&lt;br /&gt;
&lt;br /&gt;
A class is used to define a blueprint of a data type. It does not contain any data, but it specifies what kind of variables and methods an object of the class will have.&lt;br /&gt;
A class in Ruby is defined using the keyword class followed by the classname. The variables and methods are defined within the class definition and the class definition terminates with the end keyword.&lt;br /&gt;
&lt;br /&gt;
For ex:&lt;br /&gt;
  class Rectangle&lt;br /&gt;
     def initialize(w,l)&lt;br /&gt;
        @width, @length = w, l&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
Here, the initialize method is used to set the values of the instance variables (@width and @length. The instance variables are the properties of the objects created of this class type. These instance variables can be accessed within the class using an @. These variables can be accessed from outside the class using the accessor methods which are defined inside the class.&lt;br /&gt;
&lt;br /&gt;
In the following case printLength and printWidth are public methods used to access the instance variables from outside the class. These are called accessor methods.&lt;br /&gt;
Similarly, to&lt;br /&gt;
&lt;br /&gt;
   class Rectangle&lt;br /&gt;
  # constructor method&lt;br /&gt;
  def initialize(w,l)&lt;br /&gt;
     @width, @length = w, l&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # accessor methods&lt;br /&gt;
  def printWidth&lt;br /&gt;
     @width&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def printLength&lt;br /&gt;
     @length&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # setter methods&lt;br /&gt;
  def setWidth=(value)&lt;br /&gt;
     @width = value&lt;br /&gt;
  end&lt;br /&gt;
  def setHeight=(value)&lt;br /&gt;
     @height = value&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   # create an object&lt;br /&gt;
     rectagle = Rectangle.new(10, 20)&lt;br /&gt;
&lt;br /&gt;
    # use accessor methods&lt;br /&gt;
      x = rectangle.printWidth()&lt;br /&gt;
      y = rectangle.printLength()&lt;br /&gt;
&lt;br /&gt;
      puts &amp;quot;Width of the Rectangle is : #{x}&amp;quot;&lt;br /&gt;
      puts &amp;quot;Length of the Rectangle is : #{y}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==== Class Methods and Class Variables:====&lt;br /&gt;
A class variable is shared between all instances of the class. The value of the class variable remains the same for all the objects of the class. A class variable is accessed within the class using two @ signs at the beginning of the variable name. The class variables must be initialized within the class.&lt;br /&gt;
&lt;br /&gt;
The class methods are defined using def self.methodname() and ends with an end delimiter. These methods are called using the class name itself, for example: classname.methodname&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Attributes and accessor methods: ===&lt;br /&gt;
&lt;br /&gt;
Every class in Ruby defines a set of attributes for its objects. Ruby provides accessor methods (famously known as getter-setter methods in Java) to access these variables. Ruby provides three kinds of accessor methods:&lt;br /&gt;
&lt;br /&gt;
    Class Car&lt;br /&gt;
       attr_accessor : name, company&lt;br /&gt;
       attr_reader: price&lt;br /&gt;
       attr_writer: color&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
attr_accessor creates a setter and getter method. The attr_reader method creates only the getter method. This is required in case of attributes that are not to be modified by other programs but their value may be viewed. The attr_writer creates the setter method. In case of Java, the setter and getter methods need to be written in 3 lines of code each whereas in Ruby, the methods are written just by specifying the type of accessor, which have already been coded using Metaprogramming.[link]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Access Control:====&lt;br /&gt;
The only easy way to change an object's state in Ruby is by calling one of its methods. Control access to the methods, and you have controlled access to the object. A good rule of the thumb is never to expose methods that could leave an object in an invalid state.&lt;br /&gt;
&lt;br /&gt;
Ruby gives you three levels of protection:&lt;br /&gt;
&lt;br /&gt;
''Public methods'' can be called by everyone - no access control is enforced. A class's instance methods (these do not belong only to one object; instead, every instance of the class can call them) are public by default; anyone can call them. The initialize method is always private.&lt;br /&gt;
&lt;br /&gt;
''Protected methods'' can be invoked only by objects of the defining class and its subclasses. Access is kept within the family. However, usage of protected is limited.&lt;br /&gt;
&lt;br /&gt;
''Private methods'' cannot be called with an explicit receiver - the receiver is always self. This means that private methods can be called only in the context of the current object; you cannot invoke another object's private methods.&lt;br /&gt;
&lt;br /&gt;
Access control is determined dynamically, as the program runs, not statically. You will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
Ex:&lt;br /&gt;
&lt;br /&gt;
    class Employee&lt;br /&gt;
       def initialize(name, age, salary)&lt;br /&gt;
           @name = name&lt;br /&gt;
           @age = age&lt;br /&gt;
           @salary = salary&lt;br /&gt;
       end&lt;br /&gt;
  &lt;br /&gt;
       def name&lt;br /&gt;
           @name&lt;br /&gt;
       end &lt;br /&gt;
       def age&lt;br /&gt;
          @age&lt;br /&gt;
       end&lt;br /&gt;
       def salary&lt;br /&gt;
           @salary&lt;br /&gt;
       end&lt;br /&gt;
&lt;br /&gt;
        public :name&lt;br /&gt;
        protected :age&lt;br /&gt;
        private :salary&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      emp = Employee.new('Jack', 25, 2000);&lt;br /&gt;
      puts emp.name&lt;br /&gt;
      #puts emp.age    #age protected. gives access violation error&lt;br /&gt;
      #puts emp.salary #salary private&lt;br /&gt;
&lt;br /&gt;
====Abstract methods====&lt;br /&gt;
&lt;br /&gt;
Ruby is a dynamically typed language and does not provide abstract classes as in Java or C++. It instead provides modules that can be included within any class. The reason why Ruby does not provide abstract classes is because of the idea that it only matters if two objects can perform a function, they don’t necessarily have to be inherited from the same class. This paradigm is provided by modules, where the method of a module can be redefined as required by a class that includes it.&lt;br /&gt;
&lt;br /&gt;
Also, if required the technique of abstract class can be simulated by something like this:&lt;br /&gt;
&lt;br /&gt;
  Class Vehicle&lt;br /&gt;
   def drive&lt;br /&gt;
   raise NotImplementedError.new(&amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The subclasses that inherit from the above class will have to redefine the method “drive” as required.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Ruby ===&lt;br /&gt;
&lt;br /&gt;
Ruby provides unbounded polymorphism as a dynamically typed language through the concept of “Duck-typing”. “Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface” [link]. This concept is explained by the famous definition as “When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.” This essentially means that in languages like Ruby, it is only checked if an object can implement a function and the type of the object is not taken into consideration. It can be explained better with the help of the following example&lt;br /&gt;
&lt;br /&gt;
  class Lion&lt;br /&gt;
    def roar&lt;br /&gt;
        puts “The Lion roars when hungry”&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
     def roar&lt;br /&gt;
        puts “The man roared in fury and anger”&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   def quality lion&lt;br /&gt;
    lion.roar&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def start&lt;br /&gt;
    leo = Lion.new&lt;br /&gt;
    jack = Person.new&lt;br /&gt;
    quality leo&lt;br /&gt;
    quality jack&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   start&lt;br /&gt;
&lt;br /&gt;
In the above example, leo is an object of class “Lion” and the “roar” method defined in its class is invoked. jack is an object of class “Person” and the corresponding method defined in the Person class is called for him. Although in the real world, both objects are not of the same type, Ruby allows unbounded polymorphism as both of them implement a particular aspect.&lt;br /&gt;
&lt;br /&gt;
Thus, the check for an invoked method is done at run-time in dynamically typed languages and the method defined on that object type is called. If the method is undefined, a “NoMethodError” exception is raised.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Python===&lt;br /&gt;
&lt;br /&gt;
Python also heavily uses duck typing to allow flexibility to programmers. It emphasizes on the interface rather than on the specifc type which allows for polymorphic substitution [link].&lt;br /&gt;
In Python, functions like type() or isistance() is used to check for usual methods to match the type of find out if an object is an instance of a particular class. However, in case of duck typing, the focus is on the attribute of that object, so it checks for the presence of a particular attribute in that object using hasattr().&lt;br /&gt;
&lt;br /&gt;
Example for duck typing in Python is where Python uses a “pickle” module to serialize and de-serialize an object. So, we can introduce a new class that creates a “Tape” object. Now, an object of this type can be passed to the pickle module. Python will not differentiate between a file object or a tape object as long as they both have read() and write() methods.&lt;br /&gt;
&lt;br /&gt;
   class Tapefile():&lt;br /&gt;
      “A tape file”&lt;br /&gt;
       def __init__(self,params):&lt;br /&gt;
          #various initialization for tape access&lt;br /&gt;
       pass&lt;br /&gt;
       def read(len):&lt;br /&gt;
          #use special methods to access tape&lt;br /&gt;
       pass&lt;br /&gt;
       def readline():&lt;br /&gt;
          #use special methods to read a whole line on tape&lt;br /&gt;
       pass&lt;br /&gt;
       def write(value):&lt;br /&gt;
          #use special methods to write on tape&lt;br /&gt;
       pass&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in other languages===&lt;br /&gt;
&lt;br /&gt;
Duck typing is considered to be a feature provided by dynamic languages. C++ as a static language checks for methods by parameter and signature types. Java does not support duck typing either but it is argued that it can be simulated by the use of interfaces. The concept of duck-typing is criticized as it requires the programmer to have a very wide understanding of the source code and that two objects may implement a method in completely different contexts and so it may result in strange errors. &lt;br /&gt;
&lt;br /&gt;
Although duck typing is considered to be a feature of dynamic languages, some static languages like C# and Boo support it by allowing type checking of classes by the compiler to occur at run time.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sthirum</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53166</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b ms</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53166"/>
		<updated>2011-10-20T19:20:34Z</updated>

		<summary type="html">&lt;p&gt;Sthirum: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Lecture 5: Object Oriented Programming in Ruby =&lt;br /&gt;
&lt;br /&gt;
This wiki tries to cover the Object Oriented concepts in ruby.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Lecture 5 &amp;lt;ref name=OOP517&amp;gt;{{Cite web|url=http://courses.ncsu.edu/csc517//common/lectures/notes/lec5.pdf |title=CSC 517 OOP in Ruby}}&amp;lt;/ref&amp;gt; of CSC 517 covered topics of [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented programming] in [http://www.ruby-lang.org/en/ Ruby]. Object oriented programming is a paradigm in which real world entities are represented as objects, where each object has its own attributes and set of functions that it can perform. This gives a data structure called an &amp;quot;object&amp;quot; that consists of data fields and operations. This paradigm presents features like [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) encapsulation], [http://en.wikipedia.org/wiki/Data_abstraction data abstraction], [http://en.wikipedia.org/wiki/Inheritance_(computer_science) inheritance] and [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This wiki page tries to cover the other OOP languages and their features in a succinct manner and talks about the features of Ruby in detail with examples. We have also tried to compare the various languages in terms of their features to provide a better understanding.&lt;br /&gt;
&lt;br /&gt;
The lecture also covered the concepts of duck typing in Ruby, closures and currying. The latter two have not been included as per the instructions by the instructor.&lt;br /&gt;
&lt;br /&gt;
== Object oriented languages: An Overview ==&lt;br /&gt;
&lt;br /&gt;
==== C++: ====&lt;br /&gt;
C++ is a statically typed, multi-paradigm programming language. It supports some features of object-oriented languages like encapsulation, polymorphism and inheritance. It provides access control by specifiers public, private and protected and also allows “friend” classes. C++  allows multiple inheritance which is eliminated in most other programming languages as it allows a class to inherit from more than one base class which results in an ambiguity effect called [http://en.wikipedia.org/wiki/Diamond_problem Diamond problem].&lt;br /&gt;
&lt;br /&gt;
==== Java: ====&lt;br /&gt;
Java is classified as a statically typed, hybrid object oriented language. Although it provides information hiding, inheritance and polymorphism, it has a few basic types incorporated that are not objects and some of the built-in operators are provided by basic arithmetic and not as messages to objects. [link]. For these reasons, it is not considered to be a “pure” object-oriented language.&lt;br /&gt;
&lt;br /&gt;
Java provides access control and supports private, public and protected access. It also allows to create packages to create and maintain namespaces. Java is designed not to provide multiple inheritance to avoid problems faced in C++. However, it presents a single-rooted hierarchy where “Object” is the ancestor of all classes (seen in Ruby too) and provides interfaces to extend/inherit functionality.&lt;br /&gt;
&lt;br /&gt;
==== Python: ====&lt;br /&gt;
Python is a dynamically typed, interpreted, object oriented language. It is argued that Python is not completely object oriented as it does not provide access control in the form of public, private or protected variables. It has different security mechanisms. However, it allows to emulate protected member by using the prefix '_' before the name and private member by the use of the prefix '__' (double underscore) before the name.&lt;br /&gt;
&lt;br /&gt;
==== Ruby: ====&lt;br /&gt;
Ruby is dynamically typed and highly object-oriented, much more than Java and Python. In Ruby, it is said that “everything is an object”.  Every value happens to be an object in Ruby. You may call methods on a string, an integer just like you call on any object in Java. For ex,&lt;br /&gt;
&lt;br /&gt;
     (2.1143).size.times{puts “Hello!”}&lt;br /&gt;
&lt;br /&gt;
This example will display “Hello!” six times on your IRB as the size of the parameter &amp;quot;2.1143&amp;quot; is 6.&lt;br /&gt;
&lt;br /&gt;
== Comparison of object Oriented Languages ==&lt;br /&gt;
The following table compares the Properties of some of the well-known Object Oriented Languages.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 9pad case0%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Language&lt;br /&gt;
!Ruby&lt;br /&gt;
!Java&lt;br /&gt;
!C#&lt;br /&gt;
!C++&lt;br /&gt;
!Python&lt;br /&gt;
!SmallTalk&lt;br /&gt;
!Perl&lt;br /&gt;
|-&lt;br /&gt;
!Object Orientation&lt;br /&gt;
|Pure&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid/Multi-Paradigm&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Pure&lt;br /&gt;
|Add-on/Hybrid&lt;br /&gt;
|-&lt;br /&gt;
!Static/Dynamic Typing&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|-&lt;br /&gt;
!Generic Classes&lt;br /&gt;
|N/A&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|-&lt;br /&gt;
!Inheritance&lt;br /&gt;
|Single Class/Multiple Mixins&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Multiple&lt;br /&gt;
|Multiple&lt;br /&gt;
|Single&lt;br /&gt;
|Multiple&lt;br /&gt;
|- &lt;br /&gt;
!Feature Renaming&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|NO&lt;br /&gt;
|-&lt;br /&gt;
!Method Overloading&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Operator Overloading&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|-&lt;br /&gt;
!Class Variables or methods&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Access Control&lt;br /&gt;
|Public, Protected, Private&lt;br /&gt;
|Public, Protected, Package, Private&lt;br /&gt;
|Public, Protected, Private, Internal, Protected Internal&lt;br /&gt;
|Public, Protected, Private, Friends&lt;br /&gt;
|Name Mangling&lt;br /&gt;
|Protected Data, Public Methods&lt;br /&gt;
|None&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Ruby : Purely Object Oriented ==&lt;br /&gt;
&lt;br /&gt;
Ruby is considered to be a purely Object oriented Language, as everything in it is considered to be an object. Unlike Java, even primitives such as characters, punctuation, numbers and strings are treated as objects in Ruby. Ruby was designed to facilitate the enforcement of Object Oriented Methods. Ruby is a Dynamically Typed Language.&lt;br /&gt;
&lt;br /&gt;
=== Object oriented Concepts in ruby: ===&lt;br /&gt;
&lt;br /&gt;
Ruby is a singly rooted Object Oriented Programming language which has all its classes having the same superclass which is the class Object.&lt;br /&gt;
&lt;br /&gt;
==== Classes:====&lt;br /&gt;
[[Image:RubyClass.png|thumb|right|280px|'''''Classes in Ruby''''']]&lt;br /&gt;
&lt;br /&gt;
A class is used to define a blueprint of a data type. It does not contain any data, but it specifies what kind of variables and methods an object of the class will have.&lt;br /&gt;
A class in Ruby is defined using the keyword class followed by the classname. The variables and methods are defined within the class definition and the class definition terminates with the end keyword.&lt;br /&gt;
&lt;br /&gt;
For ex:&lt;br /&gt;
  class Rectangle&lt;br /&gt;
     def initialize(w,l)&lt;br /&gt;
        @width, @length = w, l&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
Here, the initialize method is used to set the values of the instance variables (@width and @length. The instance variables are the properties of the objects created of this class type. These instance variables can be accessed within the class using an @. These variables can be accessed from outside the class using the accessor methods which are defined inside the class.&lt;br /&gt;
&lt;br /&gt;
In the following case printLength and printWidth are public methods used to access the instance variables from outside the class. These are called accessor methods.&lt;br /&gt;
Similarly, to&lt;br /&gt;
&lt;br /&gt;
   class Rectangle&lt;br /&gt;
  # constructor method&lt;br /&gt;
  def initialize(w,l)&lt;br /&gt;
     @width, @length = w, l&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # accessor methods&lt;br /&gt;
  def printWidth&lt;br /&gt;
     @width&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def printLength&lt;br /&gt;
     @length&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # setter methods&lt;br /&gt;
  def setWidth=(value)&lt;br /&gt;
     @width = value&lt;br /&gt;
  end&lt;br /&gt;
  def setHeight=(value)&lt;br /&gt;
     @height = value&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   # create an object&lt;br /&gt;
     rectagle = Rectangle.new(10, 20)&lt;br /&gt;
&lt;br /&gt;
    # use accessor methods&lt;br /&gt;
      x = rectangle.printWidth()&lt;br /&gt;
      y = rectangle.printLength()&lt;br /&gt;
&lt;br /&gt;
      puts &amp;quot;Width of the Rectangle is : #{x}&amp;quot;&lt;br /&gt;
      puts &amp;quot;Length of the Rectangle is : #{y}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==== Class Methods and Class Variables:====&lt;br /&gt;
A class variable is shared between all instances of the class. The value of the class variable remains the same for all the objects of the class. A class variable is accessed within the class using two @ signs at the beginning of the variable name. The class variables must be initialized within the class.&lt;br /&gt;
&lt;br /&gt;
The class methods are defined using def self.methodname() and ends with an end delimiter. These methods are called using the class name itself, for example: classname.methodname&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Attributes and accessor methods: ===&lt;br /&gt;
&lt;br /&gt;
Every class in Ruby defines a set of attributes for its objects. Ruby provides accessor methods (famously known as getter-setter methods in Java) to access these variables. Ruby provides three kinds of accessor methods:&lt;br /&gt;
&lt;br /&gt;
    Class Car&lt;br /&gt;
       attr_accessor : name, company&lt;br /&gt;
       attr_reader: price&lt;br /&gt;
       attr_writer: color&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
attr_accessor creates a setter and getter method. The attr_reader method creates only the getter method. This is required in case of attributes that are not to be modified by other programs but their value may be viewed. The attr_writer creates the setter method. In case of Java, the setter and getter methods need to be written in 3 lines of code each whereas in Ruby, the methods are written just by specifying the type of accessor, which have already been coded using Metaprogramming.[link]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Access Control:====&lt;br /&gt;
The only easy way to change an object's state in Ruby is by calling one of its methods. Control access to the methods, and you have controlled access to the object. A good rule of the thumb is never to expose methods that could leave an object in an invalid state.&lt;br /&gt;
&lt;br /&gt;
Ruby gives you three levels of protection:&lt;br /&gt;
&lt;br /&gt;
''Public methods'' can be called by everyone - no access control is enforced. A class's instance methods (these do not belong only to one object; instead, every instance of the class can call them) are public by default; anyone can call them. The initialize method is always private.&lt;br /&gt;
&lt;br /&gt;
''Protected methods'' can be invoked only by objects of the defining class and its subclasses. Access is kept within the family. However, usage of protected is limited.&lt;br /&gt;
&lt;br /&gt;
''Private methods'' cannot be called with an explicit receiver - the receiver is always self. This means that private methods can be called only in the context of the current object; you cannot invoke another object's private methods.&lt;br /&gt;
&lt;br /&gt;
Access control is determined dynamically, as the program runs, not statically. You will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
Ex:&lt;br /&gt;
&lt;br /&gt;
    class Employee&lt;br /&gt;
       def initialize(name, age, salary)&lt;br /&gt;
           @name = name&lt;br /&gt;
           @age = age&lt;br /&gt;
           @salary = salary&lt;br /&gt;
       end&lt;br /&gt;
  &lt;br /&gt;
       def name&lt;br /&gt;
           @name&lt;br /&gt;
       end &lt;br /&gt;
       def age&lt;br /&gt;
          @age&lt;br /&gt;
       end&lt;br /&gt;
       def salary&lt;br /&gt;
           @salary&lt;br /&gt;
       end&lt;br /&gt;
&lt;br /&gt;
        public :name&lt;br /&gt;
        protected :age&lt;br /&gt;
        private :salary&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      emp = Employee.new('Jack', 25, 2000);&lt;br /&gt;
      puts emp.name&lt;br /&gt;
      #puts emp.age    #age protected. gives access violation error&lt;br /&gt;
      #puts emp.salary #salary private&lt;br /&gt;
&lt;br /&gt;
====Abstract methods====&lt;br /&gt;
&lt;br /&gt;
Ruby is a dynamically typed language and does not provide abstract classes as in Java or C++. It instead provides modules that can be included within any class. The reason why Ruby does not provide abstract classes is because of the idea that it only matters if two objects can perform a function, they don’t necessarily have to be inherited from the same class. This paradigm is provided by modules, where the method of a module can be redefined as required by a class that includes it.&lt;br /&gt;
&lt;br /&gt;
Also, if required the technique of abstract class can be simulated by something like this:&lt;br /&gt;
&lt;br /&gt;
  Class Vehicle&lt;br /&gt;
   def drive&lt;br /&gt;
   raise NotImplementedError.new(&amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The subclasses that inherit from the above class will have to redefine the method “drive” as required.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Ruby ===&lt;br /&gt;
&lt;br /&gt;
Ruby provides unbounded polymorphism as a dynamically typed language through the concept of “Duck-typing”. “Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface” [link]. This concept is explained by the famous definition as “When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.” This essentially means that in languages like Ruby, it is only checked if an object can implement a function and the type of the object is not taken into consideration. It can be explained better with the help of the following example&lt;br /&gt;
&lt;br /&gt;
  class Lion&lt;br /&gt;
    def roar&lt;br /&gt;
        puts “The Lion roars when hungry”&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
     def roar&lt;br /&gt;
        puts “The man roared in fury and anger”&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   def quality lion&lt;br /&gt;
    lion.roar&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def start&lt;br /&gt;
    leo = Lion.new&lt;br /&gt;
    jack = Person.new&lt;br /&gt;
    quality leo&lt;br /&gt;
    quality jack&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   start&lt;br /&gt;
&lt;br /&gt;
In the above example, leo is an object of class “Lion” and the “roar” method defined in its class is invoked. jack is an object of class “Person” and the corresponding method defined in the Person class is called for him. Although in the real world, both objects are not of the same type, Ruby allows unbounded polymorphism as both of them implement a particular aspect.&lt;br /&gt;
&lt;br /&gt;
Thus, the check for an invoked method is done at run-time in dynamically typed languages and the method defined on that object type is called. If the method is undefined, a “NoMethodError” exception is raised.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Python===&lt;br /&gt;
&lt;br /&gt;
Python also heavily uses duck typing to allow flexibility to programmers. It emphasizes on the interface rather than on the specifc type which allows for polymorphic substitution [link].&lt;br /&gt;
In Python, functions like type() or isistance() is used to check for usual methods to match the type of find out if an object is an instance of a particular class. However, in case of duck typing, the focus is on the attribute of that object, so it checks for the presence of a particular attribute in that object using hasattr().&lt;br /&gt;
&lt;br /&gt;
Example for duck typing in Python is where Python uses a “pickle” module to serialize and de-serialize an object. So, we can introduce a new class that creates a “Tape” object. Now, an object of this type can be passed to the pickle module. Python will not differentiate between a file object or a tape object as long as they both have read() and write() methods.&lt;br /&gt;
&lt;br /&gt;
   class Tapefile():&lt;br /&gt;
      “A tape file”&lt;br /&gt;
       def __init__(self,params):&lt;br /&gt;
          #various initialization for tape access&lt;br /&gt;
       pass&lt;br /&gt;
       def read(len):&lt;br /&gt;
          #use special methods to access tape&lt;br /&gt;
       pass&lt;br /&gt;
       def readline():&lt;br /&gt;
          #use special methods to read a whole line on tape&lt;br /&gt;
       pass&lt;br /&gt;
       def write(value):&lt;br /&gt;
          #use special methods to write on tape&lt;br /&gt;
       pass&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in other languages===&lt;br /&gt;
&lt;br /&gt;
Duck typing is considered to be a feature provided by dynamic languages. C++ as a static language checks for methods by parameter and signature types. Java does not support duck typing either but it is argued that it can be simulated by the use of interfaces. The concept of duck-typing is criticized as it requires the programmer to have a very wide understanding of the source code and that two objects may implement a method in completely different contexts and so it may result in strange errors. &lt;br /&gt;
&lt;br /&gt;
Although duck typing is considered to be a feature of dynamic languages, some static languages like C# and Boo support it by allowing type checking of classes by the compiler to occur at run time.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sthirum</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53165</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b ms</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53165"/>
		<updated>2011-10-20T19:19:12Z</updated>

		<summary type="html">&lt;p&gt;Sthirum: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Lecture 5: Object Oriented Programming in Ruby =&lt;br /&gt;
&lt;br /&gt;
This wiki tries to cover the Object Oriented concepts in ruby.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Lecture 5 &amp;lt;ref name=OOP&amp;gt;{{Cite web|url=http://courses.ncsu.edu/csc517//common/lectures/notes/lec5.pdf |title=CSC 517 OOP in Ruby}}&amp;lt;/ref&amp;gt; of CSC 517 covered topics of [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented programming] in [http://www.ruby-lang.org/en/ Ruby]. Object oriented programming is a paradigm in which real world entities are represented as objects, where each object has its own attributes and set of functions that it can perform. This gives a data structure called an &amp;quot;object&amp;quot; that consists of data fields and operations. This paradigm presents features like [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) encapsulation], [http://en.wikipedia.org/wiki/Data_abstraction data abstraction], [http://en.wikipedia.org/wiki/Inheritance_(computer_science) inheritance] and [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This wiki page tries to cover the other OOP languages and their features in a succinct manner and talks about the features of Ruby in detail with examples. We have also tried to compare the various languages in terms of their features to provide a better understanding.&lt;br /&gt;
&lt;br /&gt;
The lecture also covered the concepts of duck typing in Ruby, closures and currying. The latter two have not been included as per the instructions by the instructor.&lt;br /&gt;
&lt;br /&gt;
== Object oriented languages: An Overview ==&lt;br /&gt;
&lt;br /&gt;
==== C++: ====&lt;br /&gt;
C++ is a statically typed, multi-paradigm programming language. It supports some features of object-oriented languages like encapsulation, polymorphism and inheritance. It provides access control by specifiers public, private and protected and also allows “friend” classes. C++  allows multiple inheritance which is eliminated in most other programming languages as it allows a class to inherit from more than one base class which results in an ambiguity effect called [http://en.wikipedia.org/wiki/Diamond_problem Diamond problem].&lt;br /&gt;
&lt;br /&gt;
==== Java: ====&lt;br /&gt;
Java is classified as a statically typed, hybrid object oriented language. Although it provides information hiding, inheritance and polymorphism, it has a few basic types incorporated that are not objects and some of the built-in operators are provided by basic arithmetic and not as messages to objects. [link]. For these reasons, it is not considered to be a “pure” object-oriented language.&lt;br /&gt;
&lt;br /&gt;
Java provides access control and supports private, public and protected access. It also allows to create packages to create and maintain namespaces. Java is designed not to provide multiple inheritance to avoid problems faced in C++. However, it presents a single-rooted hierarchy where “Object” is the ancestor of all classes (seen in Ruby too) and provides interfaces to extend/inherit functionality.&lt;br /&gt;
&lt;br /&gt;
==== Python: ====&lt;br /&gt;
Python is a dynamically typed, interpreted, object oriented language. It is argued that Python is not completely object oriented as it does not provide access control in the form of public, private or protected variables. It has different security mechanisms. However, it allows to emulate protected member by using the prefix '_' before the name and private member by the use of the prefix '__' (double underscore) before the name.&lt;br /&gt;
&lt;br /&gt;
==== Ruby: ====&lt;br /&gt;
Ruby is dynamically typed and highly object-oriented, much more than Java and Python. In Ruby, it is said that “everything is an object”.  Every value happens to be an object in Ruby. You may call methods on a string, an integer just like you call on any object in Java. For ex,&lt;br /&gt;
&lt;br /&gt;
     (2.1143).size.times{puts “Hello!”}&lt;br /&gt;
&lt;br /&gt;
This example will display “Hello!” six times on your IRB as the size of the parameter &amp;quot;2.1143&amp;quot; is 6.&lt;br /&gt;
&lt;br /&gt;
== Comparison of object Oriented Languages ==&lt;br /&gt;
The following table compares the Properties of some of the well-known Object Oriented Languages.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 9pad case0%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Language&lt;br /&gt;
!Ruby&lt;br /&gt;
!Java&lt;br /&gt;
!C#&lt;br /&gt;
!C++&lt;br /&gt;
!Python&lt;br /&gt;
!SmallTalk&lt;br /&gt;
!Perl&lt;br /&gt;
|-&lt;br /&gt;
!Object Orientation&lt;br /&gt;
|Pure&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid/Multi-Paradigm&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Pure&lt;br /&gt;
|Add-on/Hybrid&lt;br /&gt;
|-&lt;br /&gt;
!Static/Dynamic Typing&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|-&lt;br /&gt;
!Generic Classes&lt;br /&gt;
|N/A&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|-&lt;br /&gt;
!Inheritance&lt;br /&gt;
|Single Class/Multiple Mixins&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Multiple&lt;br /&gt;
|Multiple&lt;br /&gt;
|Single&lt;br /&gt;
|Multiple&lt;br /&gt;
|- &lt;br /&gt;
!Feature Renaming&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|NO&lt;br /&gt;
|-&lt;br /&gt;
!Method Overloading&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Operator Overloading&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|-&lt;br /&gt;
!Class Variables or methods&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Access Control&lt;br /&gt;
|Public, Protected, Private&lt;br /&gt;
|Public, Protected, Package, Private&lt;br /&gt;
|Public, Protected, Private, Internal, Protected Internal&lt;br /&gt;
|Public, Protected, Private, Friends&lt;br /&gt;
|Name Mangling&lt;br /&gt;
|Protected Data, Public Methods&lt;br /&gt;
|None&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Ruby : Purely Object Oriented ==&lt;br /&gt;
&lt;br /&gt;
Ruby is considered to be a purely Object oriented Language, as everything in it is considered to be an object. Unlike Java, even primitives such as characters, punctuation, numbers and strings are treated as objects in Ruby. Ruby was designed to facilitate the enforcement of Object Oriented Methods. Ruby is a Dynamically Typed Language.&lt;br /&gt;
&lt;br /&gt;
=== Object oriented Concepts in ruby: ===&lt;br /&gt;
&lt;br /&gt;
Ruby is a singly rooted Object Oriented Programming language which has all its classes having the same superclass which is the class Object.&lt;br /&gt;
&lt;br /&gt;
==== Classes:====&lt;br /&gt;
[[Image:RubyClass.png|thumb|right|280px|'''''Classes in Ruby''''']]&lt;br /&gt;
&lt;br /&gt;
A class is used to define a blueprint of a data type. It does not contain any data, but it specifies what kind of variables and methods an object of the class will have.&lt;br /&gt;
A class in Ruby is defined using the keyword class followed by the classname. The variables and methods are defined within the class definition and the class definition terminates with the end keyword.&lt;br /&gt;
&lt;br /&gt;
For ex:&lt;br /&gt;
  class Rectangle&lt;br /&gt;
     def initialize(w,l)&lt;br /&gt;
        @width, @length = w, l&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
Here, the initialize method is used to set the values of the instance variables (@width and @length. The instance variables are the properties of the objects created of this class type. These instance variables can be accessed within the class using an @. These variables can be accessed from outside the class using the accessor methods which are defined inside the class.&lt;br /&gt;
&lt;br /&gt;
In the following case printLength and printWidth are public methods used to access the instance variables from outside the class. These are called accessor methods.&lt;br /&gt;
Similarly, to&lt;br /&gt;
&lt;br /&gt;
   class Rectangle&lt;br /&gt;
  # constructor method&lt;br /&gt;
  def initialize(w,l)&lt;br /&gt;
     @width, @length = w, l&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # accessor methods&lt;br /&gt;
  def printWidth&lt;br /&gt;
     @width&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def printLength&lt;br /&gt;
     @length&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # setter methods&lt;br /&gt;
  def setWidth=(value)&lt;br /&gt;
     @width = value&lt;br /&gt;
  end&lt;br /&gt;
  def setHeight=(value)&lt;br /&gt;
     @height = value&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   # create an object&lt;br /&gt;
     rectagle = Rectangle.new(10, 20)&lt;br /&gt;
&lt;br /&gt;
    # use accessor methods&lt;br /&gt;
      x = rectangle.printWidth()&lt;br /&gt;
      y = rectangle.printLength()&lt;br /&gt;
&lt;br /&gt;
      puts &amp;quot;Width of the Rectangle is : #{x}&amp;quot;&lt;br /&gt;
      puts &amp;quot;Length of the Rectangle is : #{y}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==== Class Methods and Class Variables:====&lt;br /&gt;
A class variable is shared between all instances of the class. The value of the class variable remains the same for all the objects of the class. A class variable is accessed within the class using two @ signs at the beginning of the variable name. The class variables must be initialized within the class.&lt;br /&gt;
&lt;br /&gt;
The class methods are defined using def self.methodname() and ends with an end delimiter. These methods are called using the class name itself, for example: classname.methodname&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Attributes and accessor methods: ===&lt;br /&gt;
&lt;br /&gt;
Every class in Ruby defines a set of attributes for its objects. Ruby provides accessor methods (famously known as getter-setter methods in Java) to access these variables. Ruby provides three kinds of accessor methods:&lt;br /&gt;
&lt;br /&gt;
    Class Car&lt;br /&gt;
       attr_accessor : name, company&lt;br /&gt;
       attr_reader: price&lt;br /&gt;
       attr_writer: color&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
attr_accessor creates a setter and getter method. The attr_reader method creates only the getter method. This is required in case of attributes that are not to be modified by other programs but their value may be viewed. The attr_writer creates the setter method. In case of Java, the setter and getter methods need to be written in 3 lines of code each whereas in Ruby, the methods are written just by specifying the type of accessor, which have already been coded using Metaprogramming.[link]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Access Control:====&lt;br /&gt;
The only easy way to change an object's state in Ruby is by calling one of its methods. Control access to the methods, and you have controlled access to the object. A good rule of the thumb is never to expose methods that could leave an object in an invalid state.&lt;br /&gt;
&lt;br /&gt;
Ruby gives you three levels of protection:&lt;br /&gt;
&lt;br /&gt;
''Public methods'' can be called by everyone - no access control is enforced. A class's instance methods (these do not belong only to one object; instead, every instance of the class can call them) are public by default; anyone can call them. The initialize method is always private.&lt;br /&gt;
&lt;br /&gt;
''Protected methods'' can be invoked only by objects of the defining class and its subclasses. Access is kept within the family. However, usage of protected is limited.&lt;br /&gt;
&lt;br /&gt;
''Private methods'' cannot be called with an explicit receiver - the receiver is always self. This means that private methods can be called only in the context of the current object; you cannot invoke another object's private methods.&lt;br /&gt;
&lt;br /&gt;
Access control is determined dynamically, as the program runs, not statically. You will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
Ex:&lt;br /&gt;
&lt;br /&gt;
    class Employee&lt;br /&gt;
       def initialize(name, age, salary)&lt;br /&gt;
           @name = name&lt;br /&gt;
           @age = age&lt;br /&gt;
           @salary = salary&lt;br /&gt;
       end&lt;br /&gt;
  &lt;br /&gt;
       def name&lt;br /&gt;
           @name&lt;br /&gt;
       end &lt;br /&gt;
       def age&lt;br /&gt;
          @age&lt;br /&gt;
       end&lt;br /&gt;
       def salary&lt;br /&gt;
           @salary&lt;br /&gt;
       end&lt;br /&gt;
&lt;br /&gt;
        public :name&lt;br /&gt;
        protected :age&lt;br /&gt;
        private :salary&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      emp = Employee.new('Jack', 25, 2000);&lt;br /&gt;
      puts emp.name&lt;br /&gt;
      #puts emp.age    #age protected. gives access violation error&lt;br /&gt;
      #puts emp.salary #salary private&lt;br /&gt;
&lt;br /&gt;
====Abstract methods====&lt;br /&gt;
&lt;br /&gt;
Ruby is a dynamically typed language and does not provide abstract classes as in Java or C++. It instead provides modules that can be included within any class. The reason why Ruby does not provide abstract classes is because of the idea that it only matters if two objects can perform a function, they don’t necessarily have to be inherited from the same class. This paradigm is provided by modules, where the method of a module can be redefined as required by a class that includes it.&lt;br /&gt;
&lt;br /&gt;
Also, if required the technique of abstract class can be simulated by something like this:&lt;br /&gt;
&lt;br /&gt;
  Class Vehicle&lt;br /&gt;
   def drive&lt;br /&gt;
   raise NotImplementedError.new(&amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The subclasses that inherit from the above class will have to redefine the method “drive” as required.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Ruby ===&lt;br /&gt;
&lt;br /&gt;
Ruby provides unbounded polymorphism as a dynamically typed language through the concept of “Duck-typing”. “Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface” [link]. This concept is explained by the famous definition as “When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.” This essentially means that in languages like Ruby, it is only checked if an object can implement a function and the type of the object is not taken into consideration. It can be explained better with the help of the following example&lt;br /&gt;
&lt;br /&gt;
  class Lion&lt;br /&gt;
    def roar&lt;br /&gt;
        puts “The Lion roars when hungry”&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
     def roar&lt;br /&gt;
        puts “The man roared in fury and anger”&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   def quality lion&lt;br /&gt;
    lion.roar&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def start&lt;br /&gt;
    leo = Lion.new&lt;br /&gt;
    jack = Person.new&lt;br /&gt;
    quality leo&lt;br /&gt;
    quality jack&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   start&lt;br /&gt;
&lt;br /&gt;
In the above example, leo is an object of class “Lion” and the “roar” method defined in its class is invoked. jack is an object of class “Person” and the corresponding method defined in the Person class is called for him. Although in the real world, both objects are not of the same type, Ruby allows unbounded polymorphism as both of them implement a particular aspect.&lt;br /&gt;
&lt;br /&gt;
Thus, the check for an invoked method is done at run-time in dynamically typed languages and the method defined on that object type is called. If the method is undefined, a “NoMethodError” exception is raised.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Python===&lt;br /&gt;
&lt;br /&gt;
Python also heavily uses duck typing to allow flexibility to programmers. It emphasizes on the interface rather than on the specifc type which allows for polymorphic substitution [link].&lt;br /&gt;
In Python, functions like type() or isistance() is used to check for usual methods to match the type of find out if an object is an instance of a particular class. However, in case of duck typing, the focus is on the attribute of that object, so it checks for the presence of a particular attribute in that object using hasattr().&lt;br /&gt;
&lt;br /&gt;
Example for duck typing in Python is where Python uses a “pickle” module to serialize and de-serialize an object. So, we can introduce a new class that creates a “Tape” object. Now, an object of this type can be passed to the pickle module. Python will not differentiate between a file object or a tape object as long as they both have read() and write() methods.&lt;br /&gt;
&lt;br /&gt;
   class Tapefile():&lt;br /&gt;
      “A tape file”&lt;br /&gt;
       def __init__(self,params):&lt;br /&gt;
          #various initialization for tape access&lt;br /&gt;
       pass&lt;br /&gt;
       def read(len):&lt;br /&gt;
          #use special methods to access tape&lt;br /&gt;
       pass&lt;br /&gt;
       def readline():&lt;br /&gt;
          #use special methods to read a whole line on tape&lt;br /&gt;
       pass&lt;br /&gt;
       def write(value):&lt;br /&gt;
          #use special methods to write on tape&lt;br /&gt;
       pass&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in other languages===&lt;br /&gt;
&lt;br /&gt;
Duck typing is considered to be a feature provided by dynamic languages. C++ as a static language checks for methods by parameter and signature types. Java does not support duck typing either but it is argued that it can be simulated by the use of interfaces. The concept of duck-typing is criticized as it requires the programmer to have a very wide understanding of the source code and that two objects may implement a method in completely different contexts and so it may result in strange errors. &lt;br /&gt;
&lt;br /&gt;
Although duck typing is considered to be a feature of dynamic languages, some static languages like C# and Boo support it by allowing type checking of classes by the compiler to occur at run time.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sthirum</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53163</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b ms</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53163"/>
		<updated>2011-10-20T19:17:25Z</updated>

		<summary type="html">&lt;p&gt;Sthirum: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Lecture 5: Object Oriented Programming in Ruby =&lt;br /&gt;
&lt;br /&gt;
This wiki tries to cover the Object Oriented concepts in ruby.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Lecture 5 &amp;lt;ref name=OOP&amp;gt;{{Cite web|url=http://courses.ncsu.edu/csc517//common/lectures/notes/lec5.pdf |title=CSC 517 OOP in Ruby}}&amp;lt;/ref&amp;gt; of CSC 517 covered topics of [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented programming] in [http://www.ruby-lang.org/en/ Ruby]. Object oriented programming is a paradigm in which real world entities are represented as objects, where each object has its own attributes and set of functions that it can perform. This gives a data structure called an &amp;quot;object&amp;quot; that consists of data fields and operations. This paradigm presents features like [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) encapsulation], [http://en.wikipedia.org/wiki/Data_abstraction data abstraction], [http://en.wikipedia.org/wiki/Inheritance_(computer_science) inheritance] and [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This wiki page tries to cover the other OOP languages and their features in a succinct manner and talks about the features of Ruby in detail with examples. We have also tried to compare the various languages in terms of their features to provide a better understanding.&lt;br /&gt;
&lt;br /&gt;
The lecture also covered the concepts of duck typing in Ruby, closures and currying. The latter two have not been included as per the instructions by the instructor.&lt;br /&gt;
&lt;br /&gt;
== Object oriented languages: An Overview ==&lt;br /&gt;
&lt;br /&gt;
==== C++: ====&lt;br /&gt;
C++ is a statically typed, multi-paradigm programming language. It supports some features of object-oriented languages like encapsulation, polymorphism and inheritance. It provides access control by specifiers public, private and protected and also allows “friend” classes. C++  allows multiple inheritance which is eliminated in most other programming languages as it allows a class to inherit from more than one base class which results in an ambiguity effect called [http://en.wikipedia.org/wiki/Diamond_problem Diamond problem].&lt;br /&gt;
&lt;br /&gt;
==== Java: ====&lt;br /&gt;
Java is classified as a statically typed, hybrid object oriented language. Although it provides information hiding, inheritance and polymorphism, it has a few basic types incorporated that are not objects and some of the built-in operators are provided by basic arithmetic and not as messages to objects. [link]. For these reasons, it is not considered to be a “pure” object-oriented language.&lt;br /&gt;
&lt;br /&gt;
Java provides access control and supports private, public and protected access. It also allows to create packages to create and maintain namespaces. Java is designed not to provide multiple inheritance to avoid problems faced in C++. However, it presents a single-rooted hierarchy where “Object” is the ancestor of all classes (seen in Ruby too) and provides interfaces to extend/inherit functionality.&lt;br /&gt;
&lt;br /&gt;
==== Python: ====&lt;br /&gt;
Python is a dynamically typed, interpreted, object oriented language. It is argued that Python is not completely object oriented as it does not provide access control in the form of public, private or protected variables. It has different security mechanisms. However, it allows to emulate protected member by using the prefix '_' before the name and private member by the use of the prefix '__' (double underscore) before the name.&lt;br /&gt;
&lt;br /&gt;
==== Ruby: ====&lt;br /&gt;
Ruby is dynamically typed and highly object-oriented, much more than Java and Python. In Ruby, it is said that “everything is an object”.  Every value happens to be an object in Ruby. You may call methods on a string, an integer just like you call on any object in Java. For ex,&lt;br /&gt;
&lt;br /&gt;
     (2.1143).size.times{puts “Hello!”}&lt;br /&gt;
&lt;br /&gt;
This example will display “Hello!” six times on your IRB as the size of the parameter &amp;quot;2.1143&amp;quot; is 6.&lt;br /&gt;
&lt;br /&gt;
== Comparison of object Oriented Languages ==&lt;br /&gt;
The following table compares the Properties of some of the well-known Object Oriented Languages.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 9pad case0%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Language&lt;br /&gt;
!Ruby&lt;br /&gt;
!Java&lt;br /&gt;
!C#&lt;br /&gt;
!C++&lt;br /&gt;
!Python&lt;br /&gt;
!SmallTalk&lt;br /&gt;
!Perl&lt;br /&gt;
|-&lt;br /&gt;
!Object Orientation&lt;br /&gt;
|Pure&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid/Multi-Paradigm&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Pure&lt;br /&gt;
|Add-on/Hybrid&lt;br /&gt;
|-&lt;br /&gt;
!Static/Dynamic Typing&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|-&lt;br /&gt;
!Generic Classes&lt;br /&gt;
|N/A&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|-&lt;br /&gt;
!Inheritance&lt;br /&gt;
|Single Class/Multiple Mixins&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Multiple&lt;br /&gt;
|Multiple&lt;br /&gt;
|Single&lt;br /&gt;
|Multiple&lt;br /&gt;
|- &lt;br /&gt;
!Feature Renaming&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|NO&lt;br /&gt;
|-&lt;br /&gt;
!Method Overloading&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Operator Overloading&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|-&lt;br /&gt;
!Class Variables or methods&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Access Control&lt;br /&gt;
|Public, Protected, Private&lt;br /&gt;
|Public, Protected, Package, Private&lt;br /&gt;
|Public, Protected, Private, Internal, Protected Internal&lt;br /&gt;
|Public, Protected, Private, Friends&lt;br /&gt;
|Name Mangling&lt;br /&gt;
|Protected Data, Public Methods&lt;br /&gt;
|None&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Ruby : Purely Object Oriented ==&lt;br /&gt;
&lt;br /&gt;
Ruby is considered to be a purely Object oriented Language, as everything in it is considered to be an object. Unlike Java, even primitives such as characters, punctuation, numbers and strings are treated as objects in Ruby. Ruby was designed to facilitate the enforcement of Object Oriented Methods. Ruby is a Dynamically Typed Language.&lt;br /&gt;
&lt;br /&gt;
=== Object oriented Concepts in ruby: ===&lt;br /&gt;
&lt;br /&gt;
Ruby is a singly rooted Object Oriented Programming language which has all its classes having the same superclass which is the class Object.&lt;br /&gt;
&lt;br /&gt;
==== Classes:====&lt;br /&gt;
[[Image:RubyClass.png|thumb|right|280px|'''''Classes in Ruby''''']]&lt;br /&gt;
&lt;br /&gt;
A class is used to define a blueprint of a data type. It does not contain any data, but it specifies what kind of variables and methods an object of the class will have.&lt;br /&gt;
A class in Ruby is defined using the keyword class followed by the classname. The variables and methods are defined within the class definition and the class definition terminates with the end keyword.&lt;br /&gt;
&lt;br /&gt;
For ex:&lt;br /&gt;
  class Rectangle&lt;br /&gt;
     def initialize(w,l)&lt;br /&gt;
        @width, @length = w, l&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
Here, the initialize method is used to set the values of the instance variables (@width and @length. The instance variables are the properties of the objects created of this class type. These instance variables can be accessed within the class using an @. These variables can be accessed from outside the class using the accessor methods which are defined inside the class.&lt;br /&gt;
&lt;br /&gt;
In the following case printLength and printWidth are public methods used to access the instance variables from outside the class. These are called accessor methods.&lt;br /&gt;
Similarly, to&lt;br /&gt;
&lt;br /&gt;
   class Rectangle&lt;br /&gt;
  # constructor method&lt;br /&gt;
  def initialize(w,l)&lt;br /&gt;
     @width, @length = w, l&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # accessor methods&lt;br /&gt;
  def printWidth&lt;br /&gt;
     @width&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def printLength&lt;br /&gt;
     @length&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # setter methods&lt;br /&gt;
  def setWidth=(value)&lt;br /&gt;
     @width = value&lt;br /&gt;
  end&lt;br /&gt;
  def setHeight=(value)&lt;br /&gt;
     @height = value&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   # create an object&lt;br /&gt;
     rectagle = Rectangle.new(10, 20)&lt;br /&gt;
&lt;br /&gt;
    # use accessor methods&lt;br /&gt;
      x = rectangle.printWidth()&lt;br /&gt;
      y = rectangle.printLength()&lt;br /&gt;
&lt;br /&gt;
      puts &amp;quot;Width of the Rectangle is : #{x}&amp;quot;&lt;br /&gt;
      puts &amp;quot;Length of the Rectangle is : #{y}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==== Class Methods and Class Variables:====&lt;br /&gt;
A class variable is shared between all instances of the class. The value of the class variable remains the same for all the objects of the class. A class variable is accessed within the class using two @ signs at the beginning of the variable name. The class variables must be initialized within the class.&lt;br /&gt;
&lt;br /&gt;
The class methods are defined using def self.methodname() and ends with an end delimiter. These methods are called using the class name itself, for example: classname.methodname&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Attributes and accessor methods: ===&lt;br /&gt;
&lt;br /&gt;
Every class in Ruby defines a set of attributes for its objects. Ruby provides accessor methods (famously known as getter-setter methods in Java) to access these variables. Ruby provides three kinds of accessor methods:&lt;br /&gt;
&lt;br /&gt;
    Class Car&lt;br /&gt;
       attr_accessor : name, company&lt;br /&gt;
       attr_reader: price&lt;br /&gt;
       attr_writer: color&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
attr_accessor creates a setter and getter method. The attr_reader method creates only the getter method. This is required in case of attributes that are not to be modified by other programs but their value may be viewed. The attr_writer creates the setter method. In case of Java, the setter and getter methods need to be written in 3 lines of code each whereas in Ruby, the methods are written just by specifying the type of accessor, which have already been coded using Metaprogramming.[link]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Access Control:====&lt;br /&gt;
The only easy way to change an object's state in Ruby is by calling one of its methods. Control access to the methods, and you have controlled access to the object. A good rule of the thumb is never to expose methods that could leave an object in an invalid state.&lt;br /&gt;
&lt;br /&gt;
Ruby gives you three levels of protection:&lt;br /&gt;
&lt;br /&gt;
''Public methods'' can be called by everyone - no access control is enforced. A class's instance methods (these do not belong only to one object; instead, every instance of the class can call them) are public by default; anyone can call them. The initialize method is always private.&lt;br /&gt;
&lt;br /&gt;
''Protected methods'' can be invoked only by objects of the defining class and its subclasses. Access is kept within the family. However, usage of protected is limited.&lt;br /&gt;
&lt;br /&gt;
''Private methods'' cannot be called with an explicit receiver - the receiver is always self. This means that private methods can be called only in the context of the current object; you cannot invoke another object's private methods.&lt;br /&gt;
&lt;br /&gt;
Access control is determined dynamically, as the program runs, not statically. You will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
Ex:&lt;br /&gt;
&lt;br /&gt;
    class Employee&lt;br /&gt;
       def initialize(name, age, salary)&lt;br /&gt;
           @name = name&lt;br /&gt;
           @age = age&lt;br /&gt;
           @salary = salary&lt;br /&gt;
       end&lt;br /&gt;
  &lt;br /&gt;
       def name&lt;br /&gt;
           @name&lt;br /&gt;
       end &lt;br /&gt;
       def age&lt;br /&gt;
          @age&lt;br /&gt;
       end&lt;br /&gt;
       def salary&lt;br /&gt;
           @salary&lt;br /&gt;
       end&lt;br /&gt;
&lt;br /&gt;
        public :name&lt;br /&gt;
        protected :age&lt;br /&gt;
        private :salary&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      emp = Employee.new('Jack', 25, 2000);&lt;br /&gt;
      puts emp.name&lt;br /&gt;
      #puts emp.age    #age protected. gives access violation error&lt;br /&gt;
      #puts emp.salary #salary private&lt;br /&gt;
&lt;br /&gt;
====Abstract methods====&lt;br /&gt;
&lt;br /&gt;
Ruby is a dynamically typed language and does not provide abstract classes as in Java or C++. It instead provides modules that can be included within any class. The reason why Ruby does not provide abstract classes is because of the idea that it only matters if two objects can perform a function, they don’t necessarily have to be inherited from the same class. This paradigm is provided by modules, where the method of a module can be redefined as required by a class that includes it.&lt;br /&gt;
&lt;br /&gt;
Also, if required the technique of abstract class can be simulated by something like this:&lt;br /&gt;
&lt;br /&gt;
  Class Vehicle&lt;br /&gt;
   def drive&lt;br /&gt;
   raise NotImplementedError.new(&amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The subclasses that inherit from the above class will have to redefine the method “drive” as required.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Ruby ===&lt;br /&gt;
&lt;br /&gt;
Ruby provides unbounded polymorphism as a dynamically typed language through the concept of “Duck-typing”. “Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface” [link]. This concept is explained by the famous definition as “When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.” This essentially means that in languages like Ruby, it is only checked if an object can implement a function and the type of the object is not taken into consideration. It can be explained better with the help of the following example&lt;br /&gt;
&lt;br /&gt;
  class Lion&lt;br /&gt;
    def roar&lt;br /&gt;
        puts “The Lion roars when hungry”&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
     def roar&lt;br /&gt;
        puts “The man roared in fury and anger”&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   def quality lion&lt;br /&gt;
    lion.roar&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def start&lt;br /&gt;
    leo = Lion.new&lt;br /&gt;
    jack = Person.new&lt;br /&gt;
    quality leo&lt;br /&gt;
    quality jack&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   start&lt;br /&gt;
&lt;br /&gt;
In the above example, leo is an object of class “Lion” and the “roar” method defined in its class is invoked. jack is an object of class “Person” and the corresponding method defined in the Person class is called for him. Although in the real world, both objects are not of the same type, Ruby allows unbounded polymorphism as both of them implement a particular aspect.&lt;br /&gt;
&lt;br /&gt;
Thus, the check for an invoked method is done at run-time in dynamically typed languages and the method defined on that object type is called. If the method is undefined, a “NoMethodError” exception is raised.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Python===&lt;br /&gt;
&lt;br /&gt;
Python also heavily uses duck typing to allow flexibility to programmers. It emphasizes on the interface rather than on the specifc type which allows for polymorphic substitution [link].&lt;br /&gt;
In Python, functions like type() or isistance() is used to check for usual methods to match the type of find out if an object is an instance of a particular class. However, in case of duck typing, the focus is on the attribute of that object, so it checks for the presence of a particular attribute in that object using hasattr().&lt;br /&gt;
&lt;br /&gt;
Example for duck typing in Python is where Python uses a “pickle” module to serialize and de-serialize an object. So, we can introduce a new class that creates a “Tape” object. Now, an object of this type can be passed to the pickle module. Python will not differentiate between a file object or a tape object as long as they both have read() and write() methods.&lt;br /&gt;
&lt;br /&gt;
   class Tapefile():&lt;br /&gt;
      “A tape file”&lt;br /&gt;
       def __init__(self,params):&lt;br /&gt;
          #various initialization for tape access&lt;br /&gt;
       pass&lt;br /&gt;
       def read(len):&lt;br /&gt;
          #use special methods to access tape&lt;br /&gt;
       pass&lt;br /&gt;
       def readline():&lt;br /&gt;
          #use special methods to read a whole line on tape&lt;br /&gt;
       pass&lt;br /&gt;
       def write(value):&lt;br /&gt;
          #use special methods to write on tape&lt;br /&gt;
       pass&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in other languages===&lt;br /&gt;
&lt;br /&gt;
Duck typing is considered to be a feature provided by dynamic languages. C++ as a static language checks for methods by parameter and signature types. Java does not support duck typing either but it is argued that it can be simulated by the use of interfaces. The concept of duck-typing is criticized as it requires the programmer to have a very wide understanding of the source code and that two objects may implement a method in completely different contexts and so it may result in strange errors. &lt;br /&gt;
&lt;br /&gt;
Although duck typing is considered to be a feature of dynamic languages, some static languages like C# and Boo support it by allowing type checking of classes by the compiler to occur at run time.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
{{Reflist}}&lt;/div&gt;</summary>
		<author><name>Sthirum</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53162</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b ms</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53162"/>
		<updated>2011-10-20T19:15:38Z</updated>

		<summary type="html">&lt;p&gt;Sthirum: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Lecture 5: Object Oriented Programming in Ruby =&lt;br /&gt;
&lt;br /&gt;
This wiki tries to cover the Object Oriented concepts in ruby.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Lecture 5 &amp;lt;ref name=OOP&amp;gt;{{Cite web|url=http://courses.ncsu.edu/csc517//common/lectures/notes/lec5.pdf |title=CSC 517 OOP in Ruby}}&amp;lt;/ref&amp;gt; of CSC 517 covered topics of [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented programming] in [http://www.ruby-lang.org/en/ Ruby]. Object oriented programming is a paradigm in which real world entities are represented as objects, where each object has its own attributes and set of functions that it can perform. This gives a data structure called an &amp;quot;object&amp;quot; that consists of data fields and operations. This paradigm presents features like [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) encapsulation], [http://en.wikipedia.org/wiki/Data_abstraction data abstraction], [http://en.wikipedia.org/wiki/Inheritance_(computer_science) inheritance] and [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This wiki page tries to cover the other OOP languages and their features in a succinct manner and talks about the features of Ruby in detail with examples. We have also tried to compare the various languages in terms of their features to provide a better understanding.&lt;br /&gt;
&lt;br /&gt;
The lecture also covered the concepts of duck typing in Ruby, closures and currying. The latter two have not been included as per the instructions by the instructor.&lt;br /&gt;
&lt;br /&gt;
== Object oriented languages: An Overview ==&lt;br /&gt;
&lt;br /&gt;
==== C++: ====&lt;br /&gt;
C++ is a statically typed, multi-paradigm programming language. It supports some features of object-oriented languages like encapsulation, polymorphism and inheritance. It provides access control by specifiers public, private and protected and also allows “friend” classes. C++  allows multiple inheritance which is eliminated in most other programming languages as it allows a class to inherit from more than one base class which results in an ambiguity effect called [http://en.wikipedia.org/wiki/Diamond_problem Diamond problem].&lt;br /&gt;
&lt;br /&gt;
==== Java: ====&lt;br /&gt;
Java is classified as a statically typed, hybrid object oriented language. Although it provides information hiding, inheritance and polymorphism, it has a few basic types incorporated that are not objects and some of the built-in operators are provided by basic arithmetic and not as messages to objects. [link]. For these reasons, it is not considered to be a “pure” object-oriented language.&lt;br /&gt;
&lt;br /&gt;
Java provides access control and supports private, public and protected access. It also allows to create packages to create and maintain namespaces. Java is designed not to provide multiple inheritance to avoid problems faced in C++. However, it presents a single-rooted hierarchy where “Object” is the ancestor of all classes (seen in Ruby too) and provides interfaces to extend/inherit functionality.&lt;br /&gt;
&lt;br /&gt;
==== Python: ====&lt;br /&gt;
Python is a dynamically typed, interpreted, object oriented language. It is argued that Python is not completely object oriented as it does not provide access control in the form of public, private or protected variables. It has different security mechanisms. However, it allows to emulate protected member by using the prefix '_' before the name and private member by the use of the prefix '__' (double underscore) before the name.&lt;br /&gt;
&lt;br /&gt;
==== Ruby: ====&lt;br /&gt;
Ruby is dynamically typed and highly object-oriented, much more than Java and Python. In Ruby, it is said that “everything is an object”.  Every value happens to be an object in Ruby. You may call methods on a string, an integer just like you call on any object in Java. For ex,&lt;br /&gt;
&lt;br /&gt;
     (2.1143).size.times{puts “Hello!”}&lt;br /&gt;
&lt;br /&gt;
This example will display “Hello!” six times on your IRB as the size of the parameter &amp;quot;2.1143&amp;quot; is 6.&lt;br /&gt;
&lt;br /&gt;
== Comparison of object Oriented Languages ==&lt;br /&gt;
The following table compares the Properties of some of the well-known Object Oriented Languages.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 9pad case0%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Language&lt;br /&gt;
!Ruby&lt;br /&gt;
!Java&lt;br /&gt;
!C#&lt;br /&gt;
!C++&lt;br /&gt;
!Python&lt;br /&gt;
!SmallTalk&lt;br /&gt;
!Perl&lt;br /&gt;
|-&lt;br /&gt;
!Object Orientation&lt;br /&gt;
|Pure&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid/Multi-Paradigm&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Pure&lt;br /&gt;
|Add-on/Hybrid&lt;br /&gt;
|-&lt;br /&gt;
!Static/Dynamic Typing&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|-&lt;br /&gt;
!Generic Classes&lt;br /&gt;
|N/A&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|-&lt;br /&gt;
!Inheritance&lt;br /&gt;
|Single Class/Multiple Mixins&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Multiple&lt;br /&gt;
|Multiple&lt;br /&gt;
|Single&lt;br /&gt;
|Multiple&lt;br /&gt;
|- &lt;br /&gt;
!Feature Renaming&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|NO&lt;br /&gt;
|-&lt;br /&gt;
!Method Overloading&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Operator Overloading&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|-&lt;br /&gt;
!Class Variables or methods&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Access Control&lt;br /&gt;
|Public, Protected, Private&lt;br /&gt;
|Public, Protected, Package, Private&lt;br /&gt;
|Public, Protected, Private, Internal, Protected Internal&lt;br /&gt;
|Public, Protected, Private, Friends&lt;br /&gt;
|Name Mangling&lt;br /&gt;
|Protected Data, Public Methods&lt;br /&gt;
|None&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Ruby : Purely Object Oriented ==&lt;br /&gt;
&lt;br /&gt;
Ruby is considered to be a purely Object oriented Language, as everything in it is considered to be an object. Unlike Java, even primitives such as characters, punctuation, numbers and strings are treated as objects in Ruby. Ruby was designed to facilitate the enforcement of Object Oriented Methods. Ruby is a Dynamically Typed Language.&lt;br /&gt;
&lt;br /&gt;
=== Object oriented Concepts in ruby: ===&lt;br /&gt;
&lt;br /&gt;
Ruby is a singly rooted Object Oriented Programming language which has all its classes having the same superclass which is the class Object.&lt;br /&gt;
&lt;br /&gt;
==== Classes:====&lt;br /&gt;
[[Image:RubyClass.png|thumb|right|280px|'''''Classes in Ruby''''']]&lt;br /&gt;
&lt;br /&gt;
A class is used to define a blueprint of a data type. It does not contain any data, but it specifies what kind of variables and methods an object of the class will have.&lt;br /&gt;
A class in Ruby is defined using the keyword class followed by the classname. The variables and methods are defined within the class definition and the class definition terminates with the end keyword.&lt;br /&gt;
&lt;br /&gt;
For ex:&lt;br /&gt;
  class Rectangle&lt;br /&gt;
     def initialize(w,l)&lt;br /&gt;
        @width, @length = w, l&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
Here, the initialize method is used to set the values of the instance variables (@width and @length. The instance variables are the properties of the objects created of this class type. These instance variables can be accessed within the class using an @. These variables can be accessed from outside the class using the accessor methods which are defined inside the class.&lt;br /&gt;
&lt;br /&gt;
In the following case printLength and printWidth are public methods used to access the instance variables from outside the class. These are called accessor methods.&lt;br /&gt;
Similarly, to&lt;br /&gt;
&lt;br /&gt;
   class Rectangle&lt;br /&gt;
  # constructor method&lt;br /&gt;
  def initialize(w,l)&lt;br /&gt;
     @width, @length = w, l&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # accessor methods&lt;br /&gt;
  def printWidth&lt;br /&gt;
     @width&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def printLength&lt;br /&gt;
     @length&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # setter methods&lt;br /&gt;
  def setWidth=(value)&lt;br /&gt;
     @width = value&lt;br /&gt;
  end&lt;br /&gt;
  def setHeight=(value)&lt;br /&gt;
     @height = value&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   # create an object&lt;br /&gt;
     rectagle = Rectangle.new(10, 20)&lt;br /&gt;
&lt;br /&gt;
    # use accessor methods&lt;br /&gt;
      x = rectangle.printWidth()&lt;br /&gt;
      y = rectangle.printLength()&lt;br /&gt;
&lt;br /&gt;
      puts &amp;quot;Width of the Rectangle is : #{x}&amp;quot;&lt;br /&gt;
      puts &amp;quot;Length of the Rectangle is : #{y}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==== Class Methods and Class Variables:====&lt;br /&gt;
A class variable is shared between all instances of the class. The value of the class variable remains the same for all the objects of the class. A class variable is accessed within the class using two @ signs at the beginning of the variable name. The class variables must be initialized within the class.&lt;br /&gt;
&lt;br /&gt;
The class methods are defined using def self.methodname() and ends with an end delimiter. These methods are called using the class name itself, for example: classname.methodname&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Attributes and accessor methods: ===&lt;br /&gt;
&lt;br /&gt;
Every class in Ruby defines a set of attributes for its objects. Ruby provides accessor methods (famously known as getter-setter methods in Java) to access these variables. Ruby provides three kinds of accessor methods:&lt;br /&gt;
&lt;br /&gt;
    Class Car&lt;br /&gt;
       attr_accessor : name, company&lt;br /&gt;
       attr_reader: price&lt;br /&gt;
       attr_writer: color&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
attr_accessor creates a setter and getter method. The attr_reader method creates only the getter method. This is required in case of attributes that are not to be modified by other programs but their value may be viewed. The attr_writer creates the setter method. In case of Java, the setter and getter methods need to be written in 3 lines of code each whereas in Ruby, the methods are written just by specifying the type of accessor, which have already been coded using Metaprogramming.[link]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Access Control:====&lt;br /&gt;
The only easy way to change an object's state in Ruby is by calling one of its methods. Control access to the methods, and you have controlled access to the object. A good rule of the thumb is never to expose methods that could leave an object in an invalid state.&lt;br /&gt;
&lt;br /&gt;
Ruby gives you three levels of protection:&lt;br /&gt;
&lt;br /&gt;
''Public methods'' can be called by everyone - no access control is enforced. A class's instance methods (these do not belong only to one object; instead, every instance of the class can call them) are public by default; anyone can call them. The initialize method is always private.&lt;br /&gt;
&lt;br /&gt;
''Protected methods'' can be invoked only by objects of the defining class and its subclasses. Access is kept within the family. However, usage of protected is limited.&lt;br /&gt;
&lt;br /&gt;
''Private methods'' cannot be called with an explicit receiver - the receiver is always self. This means that private methods can be called only in the context of the current object; you cannot invoke another object's private methods.&lt;br /&gt;
&lt;br /&gt;
Access control is determined dynamically, as the program runs, not statically. You will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
Ex:&lt;br /&gt;
&lt;br /&gt;
    class Employee&lt;br /&gt;
       def initialize(name, age, salary)&lt;br /&gt;
           @name = name&lt;br /&gt;
           @age = age&lt;br /&gt;
           @salary = salary&lt;br /&gt;
       end&lt;br /&gt;
  &lt;br /&gt;
       def name&lt;br /&gt;
           @name&lt;br /&gt;
       end &lt;br /&gt;
       def age&lt;br /&gt;
          @age&lt;br /&gt;
       end&lt;br /&gt;
       def salary&lt;br /&gt;
           @salary&lt;br /&gt;
       end&lt;br /&gt;
&lt;br /&gt;
        public :name&lt;br /&gt;
        protected :age&lt;br /&gt;
        private :salary&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      emp = Employee.new('Jack', 25, 2000);&lt;br /&gt;
      puts emp.name&lt;br /&gt;
      #puts emp.age    #age protected. gives access violation error&lt;br /&gt;
      #puts emp.salary #salary private&lt;br /&gt;
&lt;br /&gt;
====Abstract methods====&lt;br /&gt;
&lt;br /&gt;
Ruby is a dynamically typed language and does not provide abstract classes as in Java or C++. It instead provides modules that can be included within any class. The reason why Ruby does not provide abstract classes is because of the idea that it only matters if two objects can perform a function, they don’t necessarily have to be inherited from the same class. This paradigm is provided by modules, where the method of a module can be redefined as required by a class that includes it.&lt;br /&gt;
&lt;br /&gt;
Also, if required the technique of abstract class can be simulated by something like this:&lt;br /&gt;
&lt;br /&gt;
  Class Vehicle&lt;br /&gt;
   def drive&lt;br /&gt;
   raise NotImplementedError.new(&amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The subclasses that inherit from the above class will have to redefine the method “drive” as required.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Ruby ===&lt;br /&gt;
&lt;br /&gt;
Ruby provides unbounded polymorphism as a dynamically typed language through the concept of “Duck-typing”. “Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface” [link]. This concept is explained by the famous definition as “When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.” This essentially means that in languages like Ruby, it is only checked if an object can implement a function and the type of the object is not taken into consideration. It can be explained better with the help of the following example&lt;br /&gt;
&lt;br /&gt;
  class Lion&lt;br /&gt;
    def roar&lt;br /&gt;
        puts “The Lion roars when hungry”&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
     def roar&lt;br /&gt;
        puts “The man roared in fury and anger”&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   def quality lion&lt;br /&gt;
    lion.roar&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def start&lt;br /&gt;
    leo = Lion.new&lt;br /&gt;
    jack = Person.new&lt;br /&gt;
    quality leo&lt;br /&gt;
    quality jack&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   start&lt;br /&gt;
&lt;br /&gt;
In the above example, leo is an object of class “Lion” and the “roar” method defined in its class is invoked. jack is an object of class “Person” and the corresponding method defined in the Person class is called for him. Although in the real world, both objects are not of the same type, Ruby allows unbounded polymorphism as both of them implement a particular aspect.&lt;br /&gt;
&lt;br /&gt;
Thus, the check for an invoked method is done at run-time in dynamically typed languages and the method defined on that object type is called. If the method is undefined, a “NoMethodError” exception is raised.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Python===&lt;br /&gt;
&lt;br /&gt;
Python also heavily uses duck typing to allow flexibility to programmers. It emphasizes on the interface rather than on the specifc type which allows for polymorphic substitution [link].&lt;br /&gt;
In Python, functions like type() or isistance() is used to check for usual methods to match the type of find out if an object is an instance of a particular class. However, in case of duck typing, the focus is on the attribute of that object, so it checks for the presence of a particular attribute in that object using hasattr().&lt;br /&gt;
&lt;br /&gt;
Example for duck typing in Python is where Python uses a “pickle” module to serialize and de-serialize an object. So, we can introduce a new class that creates a “Tape” object. Now, an object of this type can be passed to the pickle module. Python will not differentiate between a file object or a tape object as long as they both have read() and write() methods.&lt;br /&gt;
&lt;br /&gt;
   class Tapefile():&lt;br /&gt;
      “A tape file”&lt;br /&gt;
       def __init__(self,params):&lt;br /&gt;
          #various initialization for tape access&lt;br /&gt;
       pass&lt;br /&gt;
       def read(len):&lt;br /&gt;
          #use special methods to access tape&lt;br /&gt;
       pass&lt;br /&gt;
       def readline():&lt;br /&gt;
          #use special methods to read a whole line on tape&lt;br /&gt;
       pass&lt;br /&gt;
       def write(value):&lt;br /&gt;
          #use special methods to write on tape&lt;br /&gt;
       pass&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in other languages===&lt;br /&gt;
&lt;br /&gt;
Duck typing is considered to be a feature provided by dynamic languages. C++ as a static language checks for methods by parameter and signature types. Java does not support duck typing either but it is argued that it can be simulated by the use of interfaces. The concept of duck-typing is criticized as it requires the programmer to have a very wide understanding of the source code and that two objects may implement a method in completely different contexts and so it may result in strange errors. &lt;br /&gt;
&lt;br /&gt;
Although duck typing is considered to be a feature of dynamic languages, some static languages like C# and Boo support it by allowing type checking of classes by the compiler to occur at run time.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;/div&gt;</summary>
		<author><name>Sthirum</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53161</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b ms</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53161"/>
		<updated>2011-10-20T19:15:08Z</updated>

		<summary type="html">&lt;p&gt;Sthirum: /* Duck Typing in other languages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Lecture 5: Object Oriented Programming in Ruby =&lt;br /&gt;
&lt;br /&gt;
This wiki tries to cover the Object Oriented concepts in ruby.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Lecture 5 &amp;lt;ref name=OOP&amp;gt;{{Cite web|url=http://courses.ncsu.edu/csc517//common/lectures/notes/lec5.pdf |title=CSC 517 OOP in Ruby}}&amp;lt;/ref&amp;gt; of CSC 517 covered topics of [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented programming] in [http://www.ruby-lang.org/en/ Ruby]. Object oriented programming is a paradigm in which real world entities are represented as objects, where each object has its own attributes and set of functions that it can perform. This gives a data structure called an &amp;quot;object&amp;quot; that consists of data fields and operations. This paradigm presents features like [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) encapsulation], [http://en.wikipedia.org/wiki/Data_abstraction data abstraction], [http://en.wikipedia.org/wiki/Inheritance_(computer_science) inheritance] and [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This wiki page tries to cover the other OOP languages and their features in a succinct manner and talks about the features of Ruby in detail with examples. We have also tried to compare the various languages in terms of their features to provide a better understanding.&lt;br /&gt;
&lt;br /&gt;
The lecture also covered the concepts of duck typing in Ruby, closures and currying. The latter two have not been included as per the instructions by the instructor.&lt;br /&gt;
&lt;br /&gt;
== Object oriented languages: An Overview ==&lt;br /&gt;
&lt;br /&gt;
==== C++: ====&lt;br /&gt;
C++ is a statically typed, multi-paradigm programming language. It supports some features of object-oriented languages like encapsulation, polymorphism and inheritance. It provides access control by specifiers public, private and protected and also allows “friend” classes. C++  allows multiple inheritance which is eliminated in most other programming languages as it allows a class to inherit from more than one base class which results in an ambiguity effect called [http://en.wikipedia.org/wiki/Diamond_problem Diamond problem].&lt;br /&gt;
&lt;br /&gt;
==== Java: ====&lt;br /&gt;
Java is classified as a statically typed, hybrid object oriented language. Although it provides information hiding, inheritance and polymorphism, it has a few basic types incorporated that are not objects and some of the built-in operators are provided by basic arithmetic and not as messages to objects. [link]. For these reasons, it is not considered to be a “pure” object-oriented language.&lt;br /&gt;
&lt;br /&gt;
Java provides access control and supports private, public and protected access. It also allows to create packages to create and maintain namespaces. Java is designed not to provide multiple inheritance to avoid problems faced in C++. However, it presents a single-rooted hierarchy where “Object” is the ancestor of all classes (seen in Ruby too) and provides interfaces to extend/inherit functionality.&lt;br /&gt;
&lt;br /&gt;
==== Python: ====&lt;br /&gt;
Python is a dynamically typed, interpreted, object oriented language. It is argued that Python is not completely object oriented as it does not provide access control in the form of public, private or protected variables. It has different security mechanisms. However, it allows to emulate protected member by using the prefix '_' before the name and private member by the use of the prefix '__' (double underscore) before the name.&lt;br /&gt;
&lt;br /&gt;
==== Ruby: ====&lt;br /&gt;
Ruby is dynamically typed and highly object-oriented, much more than Java and Python. In Ruby, it is said that “everything is an object”.  Every value happens to be an object in Ruby. You may call methods on a string, an integer just like you call on any object in Java. For ex,&lt;br /&gt;
&lt;br /&gt;
     (2.1143).size.times{puts “Hello!”}&lt;br /&gt;
&lt;br /&gt;
This example will display “Hello!” six times on your IRB as the size of the parameter &amp;quot;2.1143&amp;quot; is 6.&lt;br /&gt;
&lt;br /&gt;
== Comparison of object Oriented Languages ==&lt;br /&gt;
The following table compares the Properties of some of the well-known Object Oriented Languages.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 9pad case0%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Language&lt;br /&gt;
!Ruby&lt;br /&gt;
!Java&lt;br /&gt;
!C#&lt;br /&gt;
!C++&lt;br /&gt;
!Python&lt;br /&gt;
!SmallTalk&lt;br /&gt;
!Perl&lt;br /&gt;
|-&lt;br /&gt;
!Object Orientation&lt;br /&gt;
|Pure&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid/Multi-Paradigm&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Pure&lt;br /&gt;
|Add-on/Hybrid&lt;br /&gt;
|-&lt;br /&gt;
!Static/Dynamic Typing&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|-&lt;br /&gt;
!Generic Classes&lt;br /&gt;
|N/A&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|-&lt;br /&gt;
!Inheritance&lt;br /&gt;
|Single Class/Multiple Mixins&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Multiple&lt;br /&gt;
|Multiple&lt;br /&gt;
|Single&lt;br /&gt;
|Multiple&lt;br /&gt;
|- &lt;br /&gt;
!Feature Renaming&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|NO&lt;br /&gt;
|-&lt;br /&gt;
!Method Overloading&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Operator Overloading&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|-&lt;br /&gt;
!Class Variables or methods&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Access Control&lt;br /&gt;
|Public, Protected, Private&lt;br /&gt;
|Public, Protected, Package, Private&lt;br /&gt;
|Public, Protected, Private, Internal, Protected Internal&lt;br /&gt;
|Public, Protected, Private, Friends&lt;br /&gt;
|Name Mangling&lt;br /&gt;
|Protected Data, Public Methods&lt;br /&gt;
|None&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Ruby : Purely Object Oriented ==&lt;br /&gt;
&lt;br /&gt;
Ruby is considered to be a purely Object oriented Language, as everything in it is considered to be an object. Unlike Java, even primitives such as characters, punctuation, numbers and strings are treated as objects in Ruby. Ruby was designed to facilitate the enforcement of Object Oriented Methods. Ruby is a Dynamically Typed Language.&lt;br /&gt;
&lt;br /&gt;
=== Object oriented Concepts in ruby: ===&lt;br /&gt;
&lt;br /&gt;
Ruby is a singly rooted Object Oriented Programming language which has all its classes having the same superclass which is the class Object.&lt;br /&gt;
&lt;br /&gt;
==== Classes:====&lt;br /&gt;
[[Image:RubyClass.png|thumb|right|280px|'''''Classes in Ruby''''']]&lt;br /&gt;
&lt;br /&gt;
A class is used to define a blueprint of a data type. It does not contain any data, but it specifies what kind of variables and methods an object of the class will have.&lt;br /&gt;
A class in Ruby is defined using the keyword class followed by the classname. The variables and methods are defined within the class definition and the class definition terminates with the end keyword.&lt;br /&gt;
&lt;br /&gt;
For ex:&lt;br /&gt;
  class Rectangle&lt;br /&gt;
     def initialize(w,l)&lt;br /&gt;
        @width, @length = w, l&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
Here, the initialize method is used to set the values of the instance variables (@width and @length. The instance variables are the properties of the objects created of this class type. These instance variables can be accessed within the class using an @. These variables can be accessed from outside the class using the accessor methods which are defined inside the class.&lt;br /&gt;
&lt;br /&gt;
In the following case printLength and printWidth are public methods used to access the instance variables from outside the class. These are called accessor methods.&lt;br /&gt;
Similarly, to&lt;br /&gt;
&lt;br /&gt;
   class Rectangle&lt;br /&gt;
  # constructor method&lt;br /&gt;
  def initialize(w,l)&lt;br /&gt;
     @width, @length = w, l&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # accessor methods&lt;br /&gt;
  def printWidth&lt;br /&gt;
     @width&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def printLength&lt;br /&gt;
     @length&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # setter methods&lt;br /&gt;
  def setWidth=(value)&lt;br /&gt;
     @width = value&lt;br /&gt;
  end&lt;br /&gt;
  def setHeight=(value)&lt;br /&gt;
     @height = value&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   # create an object&lt;br /&gt;
     rectagle = Rectangle.new(10, 20)&lt;br /&gt;
&lt;br /&gt;
    # use accessor methods&lt;br /&gt;
      x = rectangle.printWidth()&lt;br /&gt;
      y = rectangle.printLength()&lt;br /&gt;
&lt;br /&gt;
      puts &amp;quot;Width of the Rectangle is : #{x}&amp;quot;&lt;br /&gt;
      puts &amp;quot;Length of the Rectangle is : #{y}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==== Class Methods and Class Variables:====&lt;br /&gt;
A class variable is shared between all instances of the class. The value of the class variable remains the same for all the objects of the class. A class variable is accessed within the class using two @ signs at the beginning of the variable name. The class variables must be initialized within the class.&lt;br /&gt;
&lt;br /&gt;
The class methods are defined using def self.methodname() and ends with an end delimiter. These methods are called using the class name itself, for example: classname.methodname&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Attributes and accessor methods: ===&lt;br /&gt;
&lt;br /&gt;
Every class in Ruby defines a set of attributes for its objects. Ruby provides accessor methods (famously known as getter-setter methods in Java) to access these variables. Ruby provides three kinds of accessor methods:&lt;br /&gt;
&lt;br /&gt;
    Class Car&lt;br /&gt;
       attr_accessor : name, company&lt;br /&gt;
       attr_reader: price&lt;br /&gt;
       attr_writer: color&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
attr_accessor creates a setter and getter method. The attr_reader method creates only the getter method. This is required in case of attributes that are not to be modified by other programs but their value may be viewed. The attr_writer creates the setter method. In case of Java, the setter and getter methods need to be written in 3 lines of code each whereas in Ruby, the methods are written just by specifying the type of accessor, which have already been coded using Metaprogramming.[link]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Access Control:====&lt;br /&gt;
The only easy way to change an object's state in Ruby is by calling one of its methods. Control access to the methods, and you have controlled access to the object. A good rule of the thumb is never to expose methods that could leave an object in an invalid state.&lt;br /&gt;
&lt;br /&gt;
Ruby gives you three levels of protection:&lt;br /&gt;
&lt;br /&gt;
''Public methods'' can be called by everyone - no access control is enforced. A class's instance methods (these do not belong only to one object; instead, every instance of the class can call them) are public by default; anyone can call them. The initialize method is always private.&lt;br /&gt;
&lt;br /&gt;
''Protected methods'' can be invoked only by objects of the defining class and its subclasses. Access is kept within the family. However, usage of protected is limited.&lt;br /&gt;
&lt;br /&gt;
''Private methods'' cannot be called with an explicit receiver - the receiver is always self. This means that private methods can be called only in the context of the current object; you cannot invoke another object's private methods.&lt;br /&gt;
&lt;br /&gt;
Access control is determined dynamically, as the program runs, not statically. You will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
Ex:&lt;br /&gt;
&lt;br /&gt;
    class Employee&lt;br /&gt;
       def initialize(name, age, salary)&lt;br /&gt;
           @name = name&lt;br /&gt;
           @age = age&lt;br /&gt;
           @salary = salary&lt;br /&gt;
       end&lt;br /&gt;
  &lt;br /&gt;
       def name&lt;br /&gt;
           @name&lt;br /&gt;
       end &lt;br /&gt;
       def age&lt;br /&gt;
          @age&lt;br /&gt;
       end&lt;br /&gt;
       def salary&lt;br /&gt;
           @salary&lt;br /&gt;
       end&lt;br /&gt;
&lt;br /&gt;
        public :name&lt;br /&gt;
        protected :age&lt;br /&gt;
        private :salary&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      emp = Employee.new('Jack', 25, 2000);&lt;br /&gt;
      puts emp.name&lt;br /&gt;
      #puts emp.age    #age protected. gives access violation error&lt;br /&gt;
      #puts emp.salary #salary private&lt;br /&gt;
&lt;br /&gt;
====Abstract methods====&lt;br /&gt;
&lt;br /&gt;
Ruby is a dynamically typed language and does not provide abstract classes as in Java or C++. It instead provides modules that can be included within any class. The reason why Ruby does not provide abstract classes is because of the idea that it only matters if two objects can perform a function, they don’t necessarily have to be inherited from the same class. This paradigm is provided by modules, where the method of a module can be redefined as required by a class that includes it.&lt;br /&gt;
&lt;br /&gt;
Also, if required the technique of abstract class can be simulated by something like this:&lt;br /&gt;
&lt;br /&gt;
  Class Vehicle&lt;br /&gt;
   def drive&lt;br /&gt;
   raise NotImplementedError.new(&amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The subclasses that inherit from the above class will have to redefine the method “drive” as required.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Ruby ===&lt;br /&gt;
&lt;br /&gt;
Ruby provides unbounded polymorphism as a dynamically typed language through the concept of “Duck-typing”. “Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface” [link]. This concept is explained by the famous definition as “When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.” This essentially means that in languages like Ruby, it is only checked if an object can implement a function and the type of the object is not taken into consideration. It can be explained better with the help of the following example&lt;br /&gt;
&lt;br /&gt;
  class Lion&lt;br /&gt;
    def roar&lt;br /&gt;
        puts “The Lion roars when hungry”&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
     def roar&lt;br /&gt;
        puts “The man roared in fury and anger”&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   def quality lion&lt;br /&gt;
    lion.roar&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def start&lt;br /&gt;
    leo = Lion.new&lt;br /&gt;
    jack = Person.new&lt;br /&gt;
    quality leo&lt;br /&gt;
    quality jack&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   start&lt;br /&gt;
&lt;br /&gt;
In the above example, leo is an object of class “Lion” and the “roar” method defined in its class is invoked. jack is an object of class “Person” and the corresponding method defined in the Person class is called for him. Although in the real world, both objects are not of the same type, Ruby allows unbounded polymorphism as both of them implement a particular aspect.&lt;br /&gt;
&lt;br /&gt;
Thus, the check for an invoked method is done at run-time in dynamically typed languages and the method defined on that object type is called. If the method is undefined, a “NoMethodError” exception is raised.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Python===&lt;br /&gt;
&lt;br /&gt;
Python also heavily uses duck typing to allow flexibility to programmers. It emphasizes on the interface rather than on the specifc type which allows for polymorphic substitution [link].&lt;br /&gt;
In Python, functions like type() or isistance() is used to check for usual methods to match the type of find out if an object is an instance of a particular class. However, in case of duck typing, the focus is on the attribute of that object, so it checks for the presence of a particular attribute in that object using hasattr().&lt;br /&gt;
&lt;br /&gt;
Example for duck typing in Python is where Python uses a “pickle” module to serialize and de-serialize an object. So, we can introduce a new class that creates a “Tape” object. Now, an object of this type can be passed to the pickle module. Python will not differentiate between a file object or a tape object as long as they both have read() and write() methods.&lt;br /&gt;
&lt;br /&gt;
   class Tapefile():&lt;br /&gt;
      “A tape file”&lt;br /&gt;
       def __init__(self,params):&lt;br /&gt;
          #various initialization for tape access&lt;br /&gt;
       pass&lt;br /&gt;
       def read(len):&lt;br /&gt;
          #use special methods to access tape&lt;br /&gt;
       pass&lt;br /&gt;
       def readline():&lt;br /&gt;
          #use special methods to read a whole line on tape&lt;br /&gt;
       pass&lt;br /&gt;
       def write(value):&lt;br /&gt;
          #use special methods to write on tape&lt;br /&gt;
       pass&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in other languages===&lt;br /&gt;
&lt;br /&gt;
Duck typing is considered to be a feature provided by dynamic languages. C++ as a static language checks for methods by parameter and signature types. Java does not support duck typing either but it is argued that it can be simulated by the use of interfaces. The concept of duck-typing is criticized as it requires the programmer to have a very wide understanding of the source code and that two objects may implement a method in completely different contexts and so it may result in strange errors. &lt;br /&gt;
&lt;br /&gt;
Although duck typing is considered to be a feature of dynamic languages, some static languages like C# and Boo support it by allowing type checking of classes by the compiler to occur at run time.&lt;br /&gt;
&lt;br /&gt;
=== References===&lt;/div&gt;</summary>
		<author><name>Sthirum</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53160</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b ms</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53160"/>
		<updated>2011-10-20T19:14:31Z</updated>

		<summary type="html">&lt;p&gt;Sthirum: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Lecture 5: Object Oriented Programming in Ruby =&lt;br /&gt;
&lt;br /&gt;
This wiki tries to cover the Object Oriented concepts in ruby.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Lecture 5 &amp;lt;ref name=OOP&amp;gt;{{Cite web|url=http://courses.ncsu.edu/csc517//common/lectures/notes/lec5.pdf |title=CSC 517 OOP in Ruby}}&amp;lt;/ref&amp;gt; of CSC 517 covered topics of [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented programming] in [http://www.ruby-lang.org/en/ Ruby]. Object oriented programming is a paradigm in which real world entities are represented as objects, where each object has its own attributes and set of functions that it can perform. This gives a data structure called an &amp;quot;object&amp;quot; that consists of data fields and operations. This paradigm presents features like [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) encapsulation], [http://en.wikipedia.org/wiki/Data_abstraction data abstraction], [http://en.wikipedia.org/wiki/Inheritance_(computer_science) inheritance] and [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This wiki page tries to cover the other OOP languages and their features in a succinct manner and talks about the features of Ruby in detail with examples. We have also tried to compare the various languages in terms of their features to provide a better understanding.&lt;br /&gt;
&lt;br /&gt;
The lecture also covered the concepts of duck typing in Ruby, closures and currying. The latter two have not been included as per the instructions by the instructor.&lt;br /&gt;
&lt;br /&gt;
== Object oriented languages: An Overview ==&lt;br /&gt;
&lt;br /&gt;
==== C++: ====&lt;br /&gt;
C++ is a statically typed, multi-paradigm programming language. It supports some features of object-oriented languages like encapsulation, polymorphism and inheritance. It provides access control by specifiers public, private and protected and also allows “friend” classes. C++  allows multiple inheritance which is eliminated in most other programming languages as it allows a class to inherit from more than one base class which results in an ambiguity effect called [http://en.wikipedia.org/wiki/Diamond_problem Diamond problem].&lt;br /&gt;
&lt;br /&gt;
==== Java: ====&lt;br /&gt;
Java is classified as a statically typed, hybrid object oriented language. Although it provides information hiding, inheritance and polymorphism, it has a few basic types incorporated that are not objects and some of the built-in operators are provided by basic arithmetic and not as messages to objects. [link]. For these reasons, it is not considered to be a “pure” object-oriented language.&lt;br /&gt;
&lt;br /&gt;
Java provides access control and supports private, public and protected access. It also allows to create packages to create and maintain namespaces. Java is designed not to provide multiple inheritance to avoid problems faced in C++. However, it presents a single-rooted hierarchy where “Object” is the ancestor of all classes (seen in Ruby too) and provides interfaces to extend/inherit functionality.&lt;br /&gt;
&lt;br /&gt;
==== Python: ====&lt;br /&gt;
Python is a dynamically typed, interpreted, object oriented language. It is argued that Python is not completely object oriented as it does not provide access control in the form of public, private or protected variables. It has different security mechanisms. However, it allows to emulate protected member by using the prefix '_' before the name and private member by the use of the prefix '__' (double underscore) before the name.&lt;br /&gt;
&lt;br /&gt;
==== Ruby: ====&lt;br /&gt;
Ruby is dynamically typed and highly object-oriented, much more than Java and Python. In Ruby, it is said that “everything is an object”.  Every value happens to be an object in Ruby. You may call methods on a string, an integer just like you call on any object in Java. For ex,&lt;br /&gt;
&lt;br /&gt;
     (2.1143).size.times{puts “Hello!”}&lt;br /&gt;
&lt;br /&gt;
This example will display “Hello!” six times on your IRB as the size of the parameter &amp;quot;2.1143&amp;quot; is 6.&lt;br /&gt;
&lt;br /&gt;
== Comparison of object Oriented Languages ==&lt;br /&gt;
The following table compares the Properties of some of the well-known Object Oriented Languages.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 9pad case0%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Language&lt;br /&gt;
!Ruby&lt;br /&gt;
!Java&lt;br /&gt;
!C#&lt;br /&gt;
!C++&lt;br /&gt;
!Python&lt;br /&gt;
!SmallTalk&lt;br /&gt;
!Perl&lt;br /&gt;
|-&lt;br /&gt;
!Object Orientation&lt;br /&gt;
|Pure&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid/Multi-Paradigm&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Pure&lt;br /&gt;
|Add-on/Hybrid&lt;br /&gt;
|-&lt;br /&gt;
!Static/Dynamic Typing&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|-&lt;br /&gt;
!Generic Classes&lt;br /&gt;
|N/A&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|-&lt;br /&gt;
!Inheritance&lt;br /&gt;
|Single Class/Multiple Mixins&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Multiple&lt;br /&gt;
|Multiple&lt;br /&gt;
|Single&lt;br /&gt;
|Multiple&lt;br /&gt;
|- &lt;br /&gt;
!Feature Renaming&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|NO&lt;br /&gt;
|-&lt;br /&gt;
!Method Overloading&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Operator Overloading&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|-&lt;br /&gt;
!Class Variables or methods&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Access Control&lt;br /&gt;
|Public, Protected, Private&lt;br /&gt;
|Public, Protected, Package, Private&lt;br /&gt;
|Public, Protected, Private, Internal, Protected Internal&lt;br /&gt;
|Public, Protected, Private, Friends&lt;br /&gt;
|Name Mangling&lt;br /&gt;
|Protected Data, Public Methods&lt;br /&gt;
|None&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Ruby : Purely Object Oriented ==&lt;br /&gt;
&lt;br /&gt;
Ruby is considered to be a purely Object oriented Language, as everything in it is considered to be an object. Unlike Java, even primitives such as characters, punctuation, numbers and strings are treated as objects in Ruby. Ruby was designed to facilitate the enforcement of Object Oriented Methods. Ruby is a Dynamically Typed Language.&lt;br /&gt;
&lt;br /&gt;
=== Object oriented Concepts in ruby: ===&lt;br /&gt;
&lt;br /&gt;
Ruby is a singly rooted Object Oriented Programming language which has all its classes having the same superclass which is the class Object.&lt;br /&gt;
&lt;br /&gt;
==== Classes:====&lt;br /&gt;
[[Image:RubyClass.png|thumb|right|280px|'''''Classes in Ruby''''']]&lt;br /&gt;
&lt;br /&gt;
A class is used to define a blueprint of a data type. It does not contain any data, but it specifies what kind of variables and methods an object of the class will have.&lt;br /&gt;
A class in Ruby is defined using the keyword class followed by the classname. The variables and methods are defined within the class definition and the class definition terminates with the end keyword.&lt;br /&gt;
&lt;br /&gt;
For ex:&lt;br /&gt;
  class Rectangle&lt;br /&gt;
     def initialize(w,l)&lt;br /&gt;
        @width, @length = w, l&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
Here, the initialize method is used to set the values of the instance variables (@width and @length. The instance variables are the properties of the objects created of this class type. These instance variables can be accessed within the class using an @. These variables can be accessed from outside the class using the accessor methods which are defined inside the class.&lt;br /&gt;
&lt;br /&gt;
In the following case printLength and printWidth are public methods used to access the instance variables from outside the class. These are called accessor methods.&lt;br /&gt;
Similarly, to&lt;br /&gt;
&lt;br /&gt;
   class Rectangle&lt;br /&gt;
  # constructor method&lt;br /&gt;
  def initialize(w,l)&lt;br /&gt;
     @width, @length = w, l&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # accessor methods&lt;br /&gt;
  def printWidth&lt;br /&gt;
     @width&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def printLength&lt;br /&gt;
     @length&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # setter methods&lt;br /&gt;
  def setWidth=(value)&lt;br /&gt;
     @width = value&lt;br /&gt;
  end&lt;br /&gt;
  def setHeight=(value)&lt;br /&gt;
     @height = value&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   # create an object&lt;br /&gt;
     rectagle = Rectangle.new(10, 20)&lt;br /&gt;
&lt;br /&gt;
    # use accessor methods&lt;br /&gt;
      x = rectangle.printWidth()&lt;br /&gt;
      y = rectangle.printLength()&lt;br /&gt;
&lt;br /&gt;
      puts &amp;quot;Width of the Rectangle is : #{x}&amp;quot;&lt;br /&gt;
      puts &amp;quot;Length of the Rectangle is : #{y}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==== Class Methods and Class Variables:====&lt;br /&gt;
A class variable is shared between all instances of the class. The value of the class variable remains the same for all the objects of the class. A class variable is accessed within the class using two @ signs at the beginning of the variable name. The class variables must be initialized within the class.&lt;br /&gt;
&lt;br /&gt;
The class methods are defined using def self.methodname() and ends with an end delimiter. These methods are called using the class name itself, for example: classname.methodname&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Attributes and accessor methods: ===&lt;br /&gt;
&lt;br /&gt;
Every class in Ruby defines a set of attributes for its objects. Ruby provides accessor methods (famously known as getter-setter methods in Java) to access these variables. Ruby provides three kinds of accessor methods:&lt;br /&gt;
&lt;br /&gt;
    Class Car&lt;br /&gt;
       attr_accessor : name, company&lt;br /&gt;
       attr_reader: price&lt;br /&gt;
       attr_writer: color&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
attr_accessor creates a setter and getter method. The attr_reader method creates only the getter method. This is required in case of attributes that are not to be modified by other programs but their value may be viewed. The attr_writer creates the setter method. In case of Java, the setter and getter methods need to be written in 3 lines of code each whereas in Ruby, the methods are written just by specifying the type of accessor, which have already been coded using Metaprogramming.[link]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Access Control:====&lt;br /&gt;
The only easy way to change an object's state in Ruby is by calling one of its methods. Control access to the methods, and you have controlled access to the object. A good rule of the thumb is never to expose methods that could leave an object in an invalid state.&lt;br /&gt;
&lt;br /&gt;
Ruby gives you three levels of protection:&lt;br /&gt;
&lt;br /&gt;
''Public methods'' can be called by everyone - no access control is enforced. A class's instance methods (these do not belong only to one object; instead, every instance of the class can call them) are public by default; anyone can call them. The initialize method is always private.&lt;br /&gt;
&lt;br /&gt;
''Protected methods'' can be invoked only by objects of the defining class and its subclasses. Access is kept within the family. However, usage of protected is limited.&lt;br /&gt;
&lt;br /&gt;
''Private methods'' cannot be called with an explicit receiver - the receiver is always self. This means that private methods can be called only in the context of the current object; you cannot invoke another object's private methods.&lt;br /&gt;
&lt;br /&gt;
Access control is determined dynamically, as the program runs, not statically. You will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
Ex:&lt;br /&gt;
&lt;br /&gt;
    class Employee&lt;br /&gt;
       def initialize(name, age, salary)&lt;br /&gt;
           @name = name&lt;br /&gt;
           @age = age&lt;br /&gt;
           @salary = salary&lt;br /&gt;
       end&lt;br /&gt;
  &lt;br /&gt;
       def name&lt;br /&gt;
           @name&lt;br /&gt;
       end &lt;br /&gt;
       def age&lt;br /&gt;
          @age&lt;br /&gt;
       end&lt;br /&gt;
       def salary&lt;br /&gt;
           @salary&lt;br /&gt;
       end&lt;br /&gt;
&lt;br /&gt;
        public :name&lt;br /&gt;
        protected :age&lt;br /&gt;
        private :salary&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      emp = Employee.new('Jack', 25, 2000);&lt;br /&gt;
      puts emp.name&lt;br /&gt;
      #puts emp.age    #age protected. gives access violation error&lt;br /&gt;
      #puts emp.salary #salary private&lt;br /&gt;
&lt;br /&gt;
====Abstract methods====&lt;br /&gt;
&lt;br /&gt;
Ruby is a dynamically typed language and does not provide abstract classes as in Java or C++. It instead provides modules that can be included within any class. The reason why Ruby does not provide abstract classes is because of the idea that it only matters if two objects can perform a function, they don’t necessarily have to be inherited from the same class. This paradigm is provided by modules, where the method of a module can be redefined as required by a class that includes it.&lt;br /&gt;
&lt;br /&gt;
Also, if required the technique of abstract class can be simulated by something like this:&lt;br /&gt;
&lt;br /&gt;
  Class Vehicle&lt;br /&gt;
   def drive&lt;br /&gt;
   raise NotImplementedError.new(&amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The subclasses that inherit from the above class will have to redefine the method “drive” as required.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Ruby ===&lt;br /&gt;
&lt;br /&gt;
Ruby provides unbounded polymorphism as a dynamically typed language through the concept of “Duck-typing”. “Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface” [link]. This concept is explained by the famous definition as “When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.” This essentially means that in languages like Ruby, it is only checked if an object can implement a function and the type of the object is not taken into consideration. It can be explained better with the help of the following example&lt;br /&gt;
&lt;br /&gt;
  class Lion&lt;br /&gt;
    def roar&lt;br /&gt;
        puts “The Lion roars when hungry”&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
     def roar&lt;br /&gt;
        puts “The man roared in fury and anger”&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   def quality lion&lt;br /&gt;
    lion.roar&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def start&lt;br /&gt;
    leo = Lion.new&lt;br /&gt;
    jack = Person.new&lt;br /&gt;
    quality leo&lt;br /&gt;
    quality jack&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   start&lt;br /&gt;
&lt;br /&gt;
In the above example, leo is an object of class “Lion” and the “roar” method defined in its class is invoked. jack is an object of class “Person” and the corresponding method defined in the Person class is called for him. Although in the real world, both objects are not of the same type, Ruby allows unbounded polymorphism as both of them implement a particular aspect.&lt;br /&gt;
&lt;br /&gt;
Thus, the check for an invoked method is done at run-time in dynamically typed languages and the method defined on that object type is called. If the method is undefined, a “NoMethodError” exception is raised.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Python===&lt;br /&gt;
&lt;br /&gt;
Python also heavily uses duck typing to allow flexibility to programmers. It emphasizes on the interface rather than on the specifc type which allows for polymorphic substitution [link].&lt;br /&gt;
In Python, functions like type() or isistance() is used to check for usual methods to match the type of find out if an object is an instance of a particular class. However, in case of duck typing, the focus is on the attribute of that object, so it checks for the presence of a particular attribute in that object using hasattr().&lt;br /&gt;
&lt;br /&gt;
Example for duck typing in Python is where Python uses a “pickle” module to serialize and de-serialize an object. So, we can introduce a new class that creates a “Tape” object. Now, an object of this type can be passed to the pickle module. Python will not differentiate between a file object or a tape object as long as they both have read() and write() methods.&lt;br /&gt;
&lt;br /&gt;
   class Tapefile():&lt;br /&gt;
      “A tape file”&lt;br /&gt;
       def __init__(self,params):&lt;br /&gt;
          #various initialization for tape access&lt;br /&gt;
       pass&lt;br /&gt;
       def read(len):&lt;br /&gt;
          #use special methods to access tape&lt;br /&gt;
       pass&lt;br /&gt;
       def readline():&lt;br /&gt;
          #use special methods to read a whole line on tape&lt;br /&gt;
       pass&lt;br /&gt;
       def write(value):&lt;br /&gt;
          #use special methods to write on tape&lt;br /&gt;
       pass&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in other languages===&lt;br /&gt;
&lt;br /&gt;
Duck typing is considered to be a feature provided by dynamic languages. C++ as a static language checks for methods by parameter and signature types. Java does not support duck typing either but it is argued that it can be simulated by the use of interfaces. The concept of duck-typing is criticized as it requires the programmer to have a very wide understanding of the source code and that two objects may implement a method in completely different contexts and so it may result in strange errors. &lt;br /&gt;
&lt;br /&gt;
Although duck typing is considered to be a feature of dynamic languages, some static languages like C# and Boo support it by allowing type checking of classes by the compiler to occur at run time.&lt;/div&gt;</summary>
		<author><name>Sthirum</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53129</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b ms</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53129"/>
		<updated>2011-10-20T18:35:43Z</updated>

		<summary type="html">&lt;p&gt;Sthirum: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Lecture 5: Object Oriented Programming in Ruby =&lt;br /&gt;
&lt;br /&gt;
This wiki tries to cover the Object Oriented concepts in ruby.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Lecture 5&amp;lt;ref&amp;gt;http://courses.ncsu.edu/csc517//common/lectures/notes/lec5.pdf &amp;lt;/ref&amp;gt; of CSC 517 covered topics of [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented programming] in [http://www.ruby-lang.org/en/ Ruby]. Object oriented programming is a paradigm in which real world entities are represented as objects, where each object has its own attributes and set of functions that it can perform. This gives a data structure called an &amp;quot;object&amp;quot; that consists of data fields and operations. This paradigm presents features like [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) encapsulation], [http://en.wikipedia.org/wiki/Data_abstraction data abstraction], [http://en.wikipedia.org/wiki/Inheritance_(computer_science)inheritance] and [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This wiki page tries to cover the other OOP languages and their features in a succinct manner and talks about the features of Ruby in detail with examples. We have also tried to compare the various languages in terms of their features to provide a better understanding.&lt;br /&gt;
&lt;br /&gt;
The lecture also covered the concepts of duck typing in Ruby, closures and currying. The latter two have not been included as per the instructions by the instructor.&lt;br /&gt;
&lt;br /&gt;
== Object oriented languages: An Overview ==&lt;br /&gt;
&lt;br /&gt;
==== C++: ====&lt;br /&gt;
C++ is a statically typed, multi-paradigm programming language. It supports some features of object-oriented languages like encapsulation, polymorphism and inheritance. It provides access control by specifiers public, private and protected and also allows “friend” classes. C++  allows multiple inheritance which is eliminated in most other programming languages as it allows a class to inherit from more than one base class which results in an ambiguity effect called [http://en.wikipedia.org/wiki/Diamond_problem Diamond problem].&lt;br /&gt;
&lt;br /&gt;
==== Java: ====&lt;br /&gt;
Java is classified as a statically typed, hybrid object oriented language. Although it provides information hiding, inheritance and polymorphism, it has a few basic types incorporated that are not objects and some of the built-in operators are provided by basic arithmetic and not as messages to objects. [link]. For these reasons, it is not considered to be a “pure” object-oriented language.&lt;br /&gt;
&lt;br /&gt;
Java provides access control and supports private, public and protected access. It also allows to create packages to create and maintain namespaces. Java is designed not to provide multiple inheritance to avoid problems faced in C++. However, it presents a single-rooted hierarchy where “Object” is the ancestor of all classes (seen in Ruby too) and provides interfaces to extend/inherit functionality.&lt;br /&gt;
&lt;br /&gt;
==== Python: ====&lt;br /&gt;
Python is a dynamically typed, interpreted, object oriented language. It is argued that Python is not completely object oriented as it does not provide access control in the form of public, private or protected variables. It has different security mechanisms. However, it allows to emulate protected member by using the prefix '_' before the name and private member by the use of the prefix '__' (double underscore) before the name.&lt;br /&gt;
&lt;br /&gt;
==== Ruby: ====&lt;br /&gt;
Ruby is dynamically typed and highly object-oriented, much more than Java and Python. In Ruby, it is said that “everything is an object”.  Every value happens to be an object in Ruby. You may call methods on a string, an integer just like you call on any object in Java. For ex,&lt;br /&gt;
&lt;br /&gt;
     (2.1143).size.times{puts “Hello!”}&lt;br /&gt;
&lt;br /&gt;
This example will display “Hello!” six times on your IRB as the size of the parameter &amp;quot;2.1143&amp;quot; is 6.&lt;br /&gt;
&lt;br /&gt;
== Comparison of object Oriented Languages ==&lt;br /&gt;
The following table compares the Properties of some of the well-known Object Oriented Languages.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 9pad case0%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Language&lt;br /&gt;
!Ruby&lt;br /&gt;
!Java&lt;br /&gt;
!C#&lt;br /&gt;
!C++&lt;br /&gt;
!Python&lt;br /&gt;
!SmallTalk&lt;br /&gt;
!Perl&lt;br /&gt;
|-&lt;br /&gt;
!Object Orientation&lt;br /&gt;
|Pure&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid/Multi-Paradigm&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Pure&lt;br /&gt;
|Add-on/Hybrid&lt;br /&gt;
|-&lt;br /&gt;
!Static/Dynamic Typing&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|-&lt;br /&gt;
!Generic Classes&lt;br /&gt;
|N/A&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|-&lt;br /&gt;
!Inheritance&lt;br /&gt;
|Single Class/Multiple Mixins&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Multiple&lt;br /&gt;
|Multiple&lt;br /&gt;
|Single&lt;br /&gt;
|Multiple&lt;br /&gt;
|- &lt;br /&gt;
!Feature Renaming&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|NO&lt;br /&gt;
|-&lt;br /&gt;
!Method Overloading&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Operator Overloading&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|-&lt;br /&gt;
!Class Variables or methods&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Access Control&lt;br /&gt;
|Public, Protected, Private&lt;br /&gt;
|Public, Protected, Package, Private&lt;br /&gt;
|Public, Protected, Private, Internal, Protected Internal&lt;br /&gt;
|Public, Protected, Private, Friends&lt;br /&gt;
|Name Mangling&lt;br /&gt;
|Protected Data, Public Methods&lt;br /&gt;
|None&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Ruby : Purely Object Oriented ==&lt;br /&gt;
&lt;br /&gt;
Ruby is considered to be a purely Object oriented Language, as everything in it is considered to be an object. Unlike Java, even primitives such as characters, punctuation, numbers and strings are treated as objects in Ruby. Ruby was designed to facilitate the enforcement of Object Oriented Methods. Ruby is a Dynamically Typed Language.&lt;br /&gt;
&lt;br /&gt;
=== Object oriented Concepts in ruby: ===&lt;br /&gt;
&lt;br /&gt;
Ruby is a singly rooted Object Oriented Programming language which has all its classes having the same superclass which is the class Object.&lt;br /&gt;
&lt;br /&gt;
==== Classes:====&lt;br /&gt;
[[Image:RubyClass.png|thumb|right|280px|'''''Classes in Ruby''''']]&lt;br /&gt;
&lt;br /&gt;
A class is used to define a blueprint of a data type. It does not contain any data, but it specifies what kind of variables and methods an object of the class will have.&lt;br /&gt;
A class in Ruby is defined using the keyword class followed by the classname. The variables and methods are defined within the class definition and the class definition terminates with the end keyword.&lt;br /&gt;
&lt;br /&gt;
For ex:&lt;br /&gt;
  class Rectangle&lt;br /&gt;
     def initialize(w,l)&lt;br /&gt;
        @width, @length = w, l&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
Here, the initialize method is used to set the values of the instance variables (@width and @length. The instance variables are the properties of the objects created of this class type. These instance variables can be accessed within the class using an @. These variables can be accessed from outside the class using the accessor methods which are defined inside the class.&lt;br /&gt;
&lt;br /&gt;
In the following case printLength and printWidth are public methods used to access the instance variables from outside the class. These are called accessor methods.&lt;br /&gt;
Similarly, to&lt;br /&gt;
&lt;br /&gt;
   class Rectangle&lt;br /&gt;
  # constructor method&lt;br /&gt;
  def initialize(w,l)&lt;br /&gt;
     @width, @length = w, l&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # accessor methods&lt;br /&gt;
  def printWidth&lt;br /&gt;
     @width&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def printLength&lt;br /&gt;
     @length&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # setter methods&lt;br /&gt;
  def setWidth=(value)&lt;br /&gt;
     @width = value&lt;br /&gt;
  end&lt;br /&gt;
  def setHeight=(value)&lt;br /&gt;
     @height = value&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   # create an object&lt;br /&gt;
     rectagle = Rectangle.new(10, 20)&lt;br /&gt;
&lt;br /&gt;
    # use accessor methods&lt;br /&gt;
      x = rectangle.printWidth()&lt;br /&gt;
      y = rectangle.printLength()&lt;br /&gt;
&lt;br /&gt;
      puts &amp;quot;Width of the Rectangle is : #{x}&amp;quot;&lt;br /&gt;
      puts &amp;quot;Length of the Rectangle is : #{y}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==== Class Methods and Class Variables:====&lt;br /&gt;
A class variable is shared between all instances of the class. The value of the class variable remains the same for all the objects of the class. A class variable is accessed within the class using two @ signs at the beginning of the variable name. The class variables must be initialized within the class.&lt;br /&gt;
&lt;br /&gt;
The class methods are defined using def self.methodname() and ends with an end delimiter. These methods are called using the class name itself, for example: classname.methodname&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Attributes and accessor methods: ===&lt;br /&gt;
&lt;br /&gt;
Every class in Ruby defines a set of attributes for its objects. Ruby provides accessor methods (famously known as getter-setter methods in Java) to access these variables. Ruby provides three kinds of accessor methods:&lt;br /&gt;
&lt;br /&gt;
    Class Car&lt;br /&gt;
       attr_accessor : name, company&lt;br /&gt;
       attr_reader: price&lt;br /&gt;
       attr_writer: color&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
attr_accessor creates a setter and getter method. The attr_reader method creates only the getter method. This is required in case of attributes that are not to be modified by other programs but their value may be viewed. The attr_writer creates the setter method. In case of Java, the setter and getter methods need to be written in 3 lines of code each whereas in Ruby, the methods are written just by specifying the type of accessor, which have already been coded using Metaprogramming.[link]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Access Control:====&lt;br /&gt;
The only easy way to change an object's state in Ruby is by calling one of its methods. Control access to the methods, and you have controlled access to the object. A good rule of the thumb is never to expose methods that could leave an object in an invalid state.&lt;br /&gt;
&lt;br /&gt;
Ruby gives you three levels of protection:&lt;br /&gt;
&lt;br /&gt;
''Public methods'' can be called by everyone - no access control is enforced. A class's instance methods (these do not belong only to one object; instead, every instance of the class can call them) are public by default; anyone can call them. The initialize method is always private.&lt;br /&gt;
&lt;br /&gt;
''Protected methods'' can be invoked only by objects of the defining class and its subclasses. Access is kept within the family. However, usage of protected is limited.&lt;br /&gt;
&lt;br /&gt;
''Private methods'' cannot be called with an explicit receiver - the receiver is always self. This means that private methods can be called only in the context of the current object; you cannot invoke another object's private methods.&lt;br /&gt;
&lt;br /&gt;
Access control is determined dynamically, as the program runs, not statically. You will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
Ex:&lt;br /&gt;
&lt;br /&gt;
    class Employee&lt;br /&gt;
       def initialize(name, age, salary)&lt;br /&gt;
           @name = name&lt;br /&gt;
           @age = age&lt;br /&gt;
           @salary = salary&lt;br /&gt;
       end&lt;br /&gt;
  &lt;br /&gt;
       def name&lt;br /&gt;
           @name&lt;br /&gt;
       end &lt;br /&gt;
       def age&lt;br /&gt;
          @age&lt;br /&gt;
       end&lt;br /&gt;
       def salary&lt;br /&gt;
           @salary&lt;br /&gt;
       end&lt;br /&gt;
&lt;br /&gt;
        public :name&lt;br /&gt;
        protected :age&lt;br /&gt;
        private :salary&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      emp = Employee.new('Jack', 25, 2000);&lt;br /&gt;
      puts emp.name&lt;br /&gt;
      #puts emp.age    #age protected. gives access violation error&lt;br /&gt;
      #puts emp.salary #salary private&lt;br /&gt;
&lt;br /&gt;
====Abstract methods====&lt;br /&gt;
&lt;br /&gt;
Ruby is a dynamically typed language and does not provide abstract classes as in Java or C++. It instead provides modules that can be included within any class. The reason why Ruby does not provide abstract classes is because of the idea that it only matters if two objects can perform a function, they don’t necessarily have to be inherited from the same class. This paradigm is provided by modules, where the method of a module can be redefined as required by a class that includes it.&lt;br /&gt;
&lt;br /&gt;
Also, if required the technique of abstract class can be simulated by something like this:&lt;br /&gt;
&lt;br /&gt;
  Class Vehicle&lt;br /&gt;
   def drive&lt;br /&gt;
   raise NotImplementedError.new(&amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The subclasses that inherit from the above class will have to redefine the method “drive” as required.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Ruby ===&lt;br /&gt;
&lt;br /&gt;
Ruby provides unbounded polymorphism as a dynamically typed language through the concept of “Duck-typing”. “Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface” [link]. This concept is explained by the famous definition as “When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.” This essentially means that in languages like Ruby, it is only checked if an object can implement a function and the type of the object is not taken into consideration. It can be explained better with the help of the following example&lt;br /&gt;
&lt;br /&gt;
  class Lion&lt;br /&gt;
    def roar&lt;br /&gt;
        puts “The Lion roars when hungry”&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
     def roar&lt;br /&gt;
        puts “The man roared in fury and anger”&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   def quality lion&lt;br /&gt;
    lion.roar&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def start&lt;br /&gt;
    leo = Lion.new&lt;br /&gt;
    jack = Person.new&lt;br /&gt;
    quality leo&lt;br /&gt;
    quality jack&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   start&lt;br /&gt;
&lt;br /&gt;
In the above example, leo is an object of class “Lion” and the “roar” method defined in its class is invoked. jack is an object of class “Person” and the corresponding method defined in the Person class is called for him. Although in the real world, both objects are not of the same type, Ruby allows unbounded polymorphism as both of them implement a particular aspect.&lt;br /&gt;
&lt;br /&gt;
Thus, the check for an invoked method is done at run-time in dynamically typed languages and the method defined on that object type is called. If the method is undefined, a “NoMethodError” exception is raised.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Python===&lt;br /&gt;
&lt;br /&gt;
Python also heavily uses duck typing to allow flexibility to programmers. It emphasizes on the interface rather than on the specifc type which allows for polymorphic substitution [link].&lt;br /&gt;
In Python, functions like type() or isistance() is used to check for usual methods to match the type of find out if an object is an instance of a particular class. However, in case of duck typing, the focus is on the attribute of that object, so it checks for the presence of a particular attribute in that object using hasattr().&lt;br /&gt;
&lt;br /&gt;
Example for duck typing in Python is where Python uses a “pickle” module to serialize and de-serialize an object. So, we can introduce a new class that creates a “Tape” object. Now, an object of this type can be passed to the pickle module. Python will not differentiate between a file object or a tape object as long as they both have read() and write() methods.&lt;br /&gt;
&lt;br /&gt;
   class Tapefile():&lt;br /&gt;
      “A tape file”&lt;br /&gt;
       def __init__(self,params):&lt;br /&gt;
          #various initialization for tape access&lt;br /&gt;
       pass&lt;br /&gt;
       def read(len):&lt;br /&gt;
          #use special methods to access tape&lt;br /&gt;
       pass&lt;br /&gt;
       def readline():&lt;br /&gt;
          #use special methods to read a whole line on tape&lt;br /&gt;
       pass&lt;br /&gt;
       def write(value):&lt;br /&gt;
          #use special methods to write on tape&lt;br /&gt;
       pass&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in other languages===&lt;br /&gt;
&lt;br /&gt;
Duck typing is considered to be a feature provided by dynamic languages. C++ as a static language checks for methods by parameter and signature types. Java does not support duck typing either but it is argued that it can be simulated by the use of interfaces. The concept of duck-typing is criticized as it requires the programmer to have a very wide understanding of the source code and that two objects may implement a method in completely different contexts and so it may result in strange errors. &lt;br /&gt;
&lt;br /&gt;
Although duck typing is considered to be a feature of dynamic languages, some static languages like C# and Boo support it by allowing type checking of classes by the compiler to occur at run time.&lt;/div&gt;</summary>
		<author><name>Sthirum</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53127</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b ms</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53127"/>
		<updated>2011-10-20T18:26:22Z</updated>

		<summary type="html">&lt;p&gt;Sthirum: /* C++: */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Lecture 5: Object Oriented Programming in Ruby =&lt;br /&gt;
&lt;br /&gt;
This wiki tries to cover the Object Oriented concepts in ruby.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Lecture 5 of CSC 517 covered topics of object oriented programming in Ruby. Object oriented programming is a paradigm in which real world entities are represented as objects, where each object has its own attributes and set of functions that it can perform. This gives a data structure called an &amp;quot;object&amp;quot; that consists of data fields and operations. This paradigm presents features like encapsulation, data abstraction, inheritance and polymorphism.&lt;br /&gt;
&lt;br /&gt;
This wiki page tries to cover the other OOP languages and their features in a succinct manner and talks about the features of Ruby in detail with examples. We have also tried to compare the various languages in terms of their features to provide a better understanding.&lt;br /&gt;
&lt;br /&gt;
The lecture also covered the concepts of duck typing in Ruby, closures and currying. The latter two have not been included as per the instructions by the instructor.&lt;br /&gt;
&lt;br /&gt;
== Object oriented languages: An Overview ==&lt;br /&gt;
&lt;br /&gt;
==== C++: ====&lt;br /&gt;
C++ is a statically typed, multi-paradigm programming language. It supports some features of object-oriented languages like encapsulation, polymorphism and inheritance. It provides access control by specifiers public, private and protected and also allows “friend” classes. C++  allows multiple inheritance which is eliminated in most other programming languages as it allows a class to inherit from more than one base class which results in an ambiguity effect called [http://en.wikipedia.org/wiki/Diamond_problem Diamond problem].&lt;br /&gt;
&lt;br /&gt;
==== Java: ====&lt;br /&gt;
Java is classified as a statically typed, hybrid object oriented language. Although it provides information hiding, inheritance and polymorphism, it has a few basic types incorporated that are not objects and some of the built-in operators are provided by basic arithmetic and not as messages to objects. [link]. For these reasons, it is not considered to be a “pure” object-oriented language.&lt;br /&gt;
&lt;br /&gt;
Java provides access control and supports private, public and protected access. It also allows to create packages to create and maintain namespaces. Java is designed not to provide multiple inheritance to avoid problems faced in C++. However, it presents a single-rooted hierarchy where “Object” is the ancestor of all classes (seen in Ruby too) and provides interfaces to extend/inherit functionality.&lt;br /&gt;
&lt;br /&gt;
==== Python: ====&lt;br /&gt;
Python is a dynamically typed, interpreted, object oriented language. It is argued that Python is not completely object oriented as it does not provide access control in the form of public, private or protected variables. It has different security mechanisms. However, it allows to emulate protected member by using the prefix '_' before the name and private member by the use of the prefix '__' (double underscore) before the name.&lt;br /&gt;
&lt;br /&gt;
==== Ruby: ====&lt;br /&gt;
Ruby is dynamically typed and highly object-oriented, much more than Java and Python. In Ruby, it is said that “everything is an object”.  Every value happens to be an object in Ruby. You may call methods on a string, an integer just like you call on any object in Java. For ex,&lt;br /&gt;
&lt;br /&gt;
     (2.1143).size.times{puts “Hello!”}&lt;br /&gt;
&lt;br /&gt;
This example will display “Hello!” six times on your IRB as the size of the parameter &amp;quot;2.1143&amp;quot; is 6.&lt;br /&gt;
&lt;br /&gt;
== Comparison of object Oriented Languages ==&lt;br /&gt;
The following table compares the Properties of some of the well-known Object Oriented Languages.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 9pad case0%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Language&lt;br /&gt;
!Ruby&lt;br /&gt;
!Java&lt;br /&gt;
!C#&lt;br /&gt;
!C++&lt;br /&gt;
!Python&lt;br /&gt;
!SmallTalk&lt;br /&gt;
!Perl&lt;br /&gt;
|-&lt;br /&gt;
!Object Orientation&lt;br /&gt;
|Pure&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid/Multi-Paradigm&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Pure&lt;br /&gt;
|Add-on/Hybrid&lt;br /&gt;
|-&lt;br /&gt;
!Static/Dynamic Typing&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|-&lt;br /&gt;
!Generic Classes&lt;br /&gt;
|N/A&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|-&lt;br /&gt;
!Inheritance&lt;br /&gt;
|Single Class/Multiple Mixins&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Multiple&lt;br /&gt;
|Multiple&lt;br /&gt;
|Single&lt;br /&gt;
|Multiple&lt;br /&gt;
|- &lt;br /&gt;
!Feature Renaming&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|NO&lt;br /&gt;
|-&lt;br /&gt;
!Method Overloading&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Operator Overloading&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|-&lt;br /&gt;
!Class Variables or methods&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Access Control&lt;br /&gt;
|Public, Protected, Private&lt;br /&gt;
|Public, Protected, Package, Private&lt;br /&gt;
|Public, Protected, Private, Internal, Protected Internal&lt;br /&gt;
|Public, Protected, Private, Friends&lt;br /&gt;
|Name Mangling&lt;br /&gt;
|Protected Data, Public Methods&lt;br /&gt;
|None&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Ruby : Purely Object Oriented ==&lt;br /&gt;
&lt;br /&gt;
Ruby is considered to be a purely Object oriented Language, as everything in it is considered to be an object. Unlike Java, even primitives such as characters, punctuation, numbers and strings are treated as objects in Ruby. Ruby was designed to facilitate the enforcement of Object Oriented Methods. Ruby is a Dynamically Typed Language.&lt;br /&gt;
&lt;br /&gt;
=== Object oriented Concepts in ruby: ===&lt;br /&gt;
&lt;br /&gt;
Ruby is a singly rooted Object Oriented Programming language which has all its classes having the same superclass which is the class Object.&lt;br /&gt;
&lt;br /&gt;
==== Classes:====&lt;br /&gt;
[[Image:RubyClass.png|thumb|right|280px|'''''Classes in Ruby''''']]&lt;br /&gt;
&lt;br /&gt;
A class is used to define a blueprint of a data type. It does not contain any data, but it specifies what kind of variables and methods an object of the class will have.&lt;br /&gt;
A class in Ruby is defined using the keyword class followed by the classname. The variables and methods are defined within the class definition and the class definition terminates with the end keyword.&lt;br /&gt;
&lt;br /&gt;
For ex:&lt;br /&gt;
  class Rectangle&lt;br /&gt;
     def initialize(w,l)&lt;br /&gt;
        @width, @length = w, l&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
Here, the initialize method is used to set the values of the instance variables (@width and @length. The instance variables are the properties of the objects created of this class type. These instance variables can be accessed within the class using an @. These variables can be accessed from outside the class using the accessor methods which are defined inside the class.&lt;br /&gt;
&lt;br /&gt;
In the following case printLength and printWidth are public methods used to access the instance variables from outside the class. These are called accessor methods.&lt;br /&gt;
Similarly, to&lt;br /&gt;
&lt;br /&gt;
   class Rectangle&lt;br /&gt;
  # constructor method&lt;br /&gt;
  def initialize(w,l)&lt;br /&gt;
     @width, @length = w, l&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # accessor methods&lt;br /&gt;
  def printWidth&lt;br /&gt;
     @width&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def printLength&lt;br /&gt;
     @length&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # setter methods&lt;br /&gt;
  def setWidth=(value)&lt;br /&gt;
     @width = value&lt;br /&gt;
  end&lt;br /&gt;
  def setHeight=(value)&lt;br /&gt;
     @height = value&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   # create an object&lt;br /&gt;
     rectagle = Rectangle.new(10, 20)&lt;br /&gt;
&lt;br /&gt;
    # use accessor methods&lt;br /&gt;
      x = rectangle.printWidth()&lt;br /&gt;
      y = rectangle.printLength()&lt;br /&gt;
&lt;br /&gt;
      puts &amp;quot;Width of the Rectangle is : #{x}&amp;quot;&lt;br /&gt;
      puts &amp;quot;Length of the Rectangle is : #{y}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==== Class Methods and Class Variables:====&lt;br /&gt;
A class variable is shared between all instances of the class. The value of the class variable remains the same for all the objects of the class. A class variable is accessed within the class using two @ signs at the beginning of the variable name. The class variables must be initialized within the class.&lt;br /&gt;
&lt;br /&gt;
The class methods are defined using def self.methodname() and ends with an end delimiter. These methods are called using the class name itself, for example: classname.methodname&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Attributes and accessor methods: ===&lt;br /&gt;
&lt;br /&gt;
Every class in Ruby defines a set of attributes for its objects. Ruby provides accessor methods (famously known as getter-setter methods in Java) to access these variables. Ruby provides three kinds of accessor methods:&lt;br /&gt;
&lt;br /&gt;
    Class Car&lt;br /&gt;
       attr_accessor : name, company&lt;br /&gt;
       attr_reader: price&lt;br /&gt;
       attr_writer: color&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
attr_accessor creates a setter and getter method. The attr_reader method creates only the getter method. This is required in case of attributes that are not to be modified by other programs but their value may be viewed. The attr_writer creates the setter method. In case of Java, the setter and getter methods need to be written in 3 lines of code each whereas in Ruby, the methods are written just by specifying the type of accessor, which have already been coded using Metaprogramming.[link]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Access Control:====&lt;br /&gt;
The only easy way to change an object's state in Ruby is by calling one of its methods. Control access to the methods, and you have controlled access to the object. A good rule of the thumb is never to expose methods that could leave an object in an invalid state.&lt;br /&gt;
&lt;br /&gt;
Ruby gives you three levels of protection:&lt;br /&gt;
&lt;br /&gt;
''Public methods'' can be called by everyone - no access control is enforced. A class's instance methods (these do not belong only to one object; instead, every instance of the class can call them) are public by default; anyone can call them. The initialize method is always private.&lt;br /&gt;
&lt;br /&gt;
''Protected methods'' can be invoked only by objects of the defining class and its subclasses. Access is kept within the family. However, usage of protected is limited.&lt;br /&gt;
&lt;br /&gt;
''Private methods'' cannot be called with an explicit receiver - the receiver is always self. This means that private methods can be called only in the context of the current object; you cannot invoke another object's private methods.&lt;br /&gt;
&lt;br /&gt;
Access control is determined dynamically, as the program runs, not statically. You will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
Ex:&lt;br /&gt;
&lt;br /&gt;
    class Employee&lt;br /&gt;
       def initialize(name, age, salary)&lt;br /&gt;
           @name = name&lt;br /&gt;
           @age = age&lt;br /&gt;
           @salary = salary&lt;br /&gt;
       end&lt;br /&gt;
  &lt;br /&gt;
       def name&lt;br /&gt;
           @name&lt;br /&gt;
       end &lt;br /&gt;
       def age&lt;br /&gt;
          @age&lt;br /&gt;
       end&lt;br /&gt;
       def salary&lt;br /&gt;
           @salary&lt;br /&gt;
       end&lt;br /&gt;
&lt;br /&gt;
        public :name&lt;br /&gt;
        protected :age&lt;br /&gt;
        private :salary&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      emp = Employee.new('Jack', 25, 2000);&lt;br /&gt;
      puts emp.name&lt;br /&gt;
      #puts emp.age    #age protected. gives access violation error&lt;br /&gt;
      #puts emp.salary #salary private&lt;br /&gt;
&lt;br /&gt;
====Abstract methods====&lt;br /&gt;
&lt;br /&gt;
Ruby is a dynamically typed language and does not provide abstract classes as in Java or C++. It instead provides modules that can be included within any class. The reason why Ruby does not provide abstract classes is because of the idea that it only matters if two objects can perform a function, they don’t necessarily have to be inherited from the same class. This paradigm is provided by modules, where the method of a module can be redefined as required by a class that includes it.&lt;br /&gt;
&lt;br /&gt;
Also, if required the technique of abstract class can be simulated by something like this:&lt;br /&gt;
&lt;br /&gt;
  Class Vehicle&lt;br /&gt;
   def drive&lt;br /&gt;
   raise NotImplementedError.new(&amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The subclasses that inherit from the above class will have to redefine the method “drive” as required.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Ruby ===&lt;br /&gt;
&lt;br /&gt;
Ruby provides unbounded polymorphism as a dynamically typed language through the concept of “Duck-typing”. “Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface” [link]. This concept is explained by the famous definition as “When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.” This essentially means that in languages like Ruby, it is only checked if an object can implement a function and the type of the object is not taken into consideration. It can be explained better with the help of the following example&lt;br /&gt;
&lt;br /&gt;
  class Lion&lt;br /&gt;
    def roar&lt;br /&gt;
        puts “The Lion roars when hungry”&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
     def roar&lt;br /&gt;
        puts “The man roared in fury and anger”&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   def quality lion&lt;br /&gt;
    lion.roar&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def start&lt;br /&gt;
    leo = Lion.new&lt;br /&gt;
    jack = Person.new&lt;br /&gt;
    quality leo&lt;br /&gt;
    quality jack&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   start&lt;br /&gt;
&lt;br /&gt;
In the above example, leo is an object of class “Lion” and the “roar” method defined in its class is invoked. jack is an object of class “Person” and the corresponding method defined in the Person class is called for him. Although in the real world, both objects are not of the same type, Ruby allows unbounded polymorphism as both of them implement a particular aspect.&lt;br /&gt;
&lt;br /&gt;
Thus, the check for an invoked method is done at run-time in dynamically typed languages and the method defined on that object type is called. If the method is undefined, a “NoMethodError” exception is raised.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Python===&lt;br /&gt;
&lt;br /&gt;
Python also heavily uses duck typing to allow flexibility to programmers. It emphasizes on the interface rather than on the specifc type which allows for polymorphic substitution [link].&lt;br /&gt;
In Python, functions like type() or isistance() is used to check for usual methods to match the type of find out if an object is an instance of a particular class. However, in case of duck typing, the focus is on the attribute of that object, so it checks for the presence of a particular attribute in that object using hasattr().&lt;br /&gt;
&lt;br /&gt;
Example for duck typing in Python is where Python uses a “pickle” module to serialize and de-serialize an object. So, we can introduce a new class that creates a “Tape” object. Now, an object of this type can be passed to the pickle module. Python will not differentiate between a file object or a tape object as long as they both have read() and write() methods.&lt;br /&gt;
&lt;br /&gt;
   class Tapefile():&lt;br /&gt;
      “A tape file”&lt;br /&gt;
       def __init__(self,params):&lt;br /&gt;
          #various initialization for tape access&lt;br /&gt;
       pass&lt;br /&gt;
       def read(len):&lt;br /&gt;
          #use special methods to access tape&lt;br /&gt;
       pass&lt;br /&gt;
       def readline():&lt;br /&gt;
          #use special methods to read a whole line on tape&lt;br /&gt;
       pass&lt;br /&gt;
       def write(value):&lt;br /&gt;
          #use special methods to write on tape&lt;br /&gt;
       pass&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in other languages===&lt;br /&gt;
&lt;br /&gt;
Duck typing is considered to be a feature provided by dynamic languages. C++ as a static language checks for methods by parameter and signature types. Java does not support duck typing either but it is argued that it can be simulated by the use of interfaces. The concept of duck-typing is criticized as it requires the programmer to have a very wide understanding of the source code and that two objects may implement a method in completely different contexts and so it may result in strange errors. &lt;br /&gt;
&lt;br /&gt;
Although duck typing is considered to be a feature of dynamic languages, some static languages like C# and Boo support it by allowing type checking of classes by the compiler to occur at run time.&lt;/div&gt;</summary>
		<author><name>Sthirum</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53087</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b ms</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53087"/>
		<updated>2011-10-20T17:09:09Z</updated>

		<summary type="html">&lt;p&gt;Sthirum: /* Python: */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Lecture 5: Object Oriented Programming in Ruby =&lt;br /&gt;
&lt;br /&gt;
This wiki tries to cover the Object Oriented concepts in ruby.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Lecture 5 of CSC 517 covered topics of object oriented programming in Ruby. Object oriented programming is a paradigm in which real world entities are represented as objects, where each object has its own attributes and set of functions that it can perform. This gives a data structure called an &amp;quot;object&amp;quot; that consists of data fields and operations. This paradigm presents features like encapsulation, data abstraction, inheritance and polymorphism.&lt;br /&gt;
&lt;br /&gt;
This wiki page tries to cover the other OOP languages and their features in a succinct manner and talks about the features of Ruby in detail with examples. We have also tried to compare the various languages in terms of their features to provide a better understanding.&lt;br /&gt;
&lt;br /&gt;
The lecture also covered the concepts of duck typing in Ruby, closures and currying. The latter two have not been included as per the instructions by the instructor.&lt;br /&gt;
&lt;br /&gt;
== Object oriented languages: An Overview ==&lt;br /&gt;
&lt;br /&gt;
==== C++: ====&lt;br /&gt;
C++ is a statically typed, multi-paradigm programming language. It supports some features of object-oriented languages like encapsulation, polymorphism and inheritance. It provides access control by specifiers public, private and protected and also allows “friend” classes. C++  allows multiple inheritance which is eliminated in most other programming languages as it allows a class to inherit from more than one base class which results in an ambiguity effect.&lt;br /&gt;
&lt;br /&gt;
==== Java: ====&lt;br /&gt;
Java is classified as a statically typed, hybrid object oriented language. Although it provides information hiding, inheritance and polymorphism, it has a few basic types incorporated that are not objects and some of the built-in operators are provided by basic arithmetic and not as messages to objects. [link]. For these reasons, it is not considered to be a “pure” object-oriented language.&lt;br /&gt;
&lt;br /&gt;
Java provides access control and supports private, public and protected access. It also allows to create packages to create and maintain namespaces. Java is designed not to provide multiple inheritance to avoid problems faced in C++. However, it presents a single-rooted hierarchy where “Object” is the ancestor of all classes (seen in Ruby too) and provides interfaces to extend/inherit functionality.&lt;br /&gt;
&lt;br /&gt;
==== Python: ====&lt;br /&gt;
Python is a dynamically typed, interpreted, object oriented language. It is argued that Python is not completely object oriented as it does not provide access control in the form of public, private or protected variables. It has different security mechanisms. However, it allows to emulate protected member by using the prefix '_' before the name and private member by the use of the prefix '__' (double underscore) before the name.&lt;br /&gt;
&lt;br /&gt;
==== Ruby: ====&lt;br /&gt;
Ruby is dynamically typed and highly object-oriented, much more than Java and Python. In Ruby, it is said that “everything is an object”.  Every value happens to be an object in Ruby. You may call methods on a string, an integer just like you call on any object in Java. For ex,&lt;br /&gt;
&lt;br /&gt;
     (2.1143).size.times{puts “Hello!”}&lt;br /&gt;
&lt;br /&gt;
This example will display “Hello!” six times on your IRB as the size of the parameter &amp;quot;2.1143&amp;quot; is 6.&lt;br /&gt;
&lt;br /&gt;
== Comparison of object Oriented Languages ==&lt;br /&gt;
The following table compares the Properties of some of the well-known Object Oriented Languages.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 9pad case0%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Language&lt;br /&gt;
!Ruby&lt;br /&gt;
!Java&lt;br /&gt;
!C#&lt;br /&gt;
!C++&lt;br /&gt;
!Python&lt;br /&gt;
!SmallTalk&lt;br /&gt;
!Perl&lt;br /&gt;
|-&lt;br /&gt;
!Object Orientation&lt;br /&gt;
|Pure&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid/Multi-Paradigm&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Pure&lt;br /&gt;
|Add-on/Hybrid&lt;br /&gt;
|-&lt;br /&gt;
!Static/Dynamic Typing&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|-&lt;br /&gt;
!Generic Classes&lt;br /&gt;
|N/A&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|-&lt;br /&gt;
!Inheritance&lt;br /&gt;
|Single Class/Multiple Mixins&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Multiple&lt;br /&gt;
|Multiple&lt;br /&gt;
|Single&lt;br /&gt;
|Multiple&lt;br /&gt;
|- &lt;br /&gt;
!Feature Renaming&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|NO&lt;br /&gt;
|-&lt;br /&gt;
!Method Overloading&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Operator Overloading&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|-&lt;br /&gt;
!Class Variables or methods&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Access Control&lt;br /&gt;
|Public, Protected, Private&lt;br /&gt;
|Public, Protected, Package, Private&lt;br /&gt;
|Public, Protected, Private, Internal, Protected Internal&lt;br /&gt;
|Public, Protected, Private, Friends&lt;br /&gt;
|Name Mangling&lt;br /&gt;
|Protected Data, Public Methods&lt;br /&gt;
|None&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Ruby : Purely Object Oriented ==&lt;br /&gt;
&lt;br /&gt;
Ruby is considered to be a purely Object oriented Language, as everything in it is considered to be an object. Unlike Java, even primitives such as characters, punctuation, numbers and strings are treated as objects in Ruby. Ruby was designed to facilitate the enforcement of Object Oriented Methods. Ruby is a Dynamically Typed Language.&lt;br /&gt;
&lt;br /&gt;
=== Object oriented Concepts in ruby: ===&lt;br /&gt;
&lt;br /&gt;
Ruby is a singly rooted Object Oriented Programming language which has all its classes having the same superclass which is the class Object.&lt;br /&gt;
&lt;br /&gt;
==== Classes:====&lt;br /&gt;
[[Image:RubyClass.png|thumb|right|280px|'''''Classes in Ruby''''']]&lt;br /&gt;
&lt;br /&gt;
A class is used to define a blueprint of a data type. It does not contain any data, but it specifies what kind of variables and methods an object of the class will have.&lt;br /&gt;
A class in Ruby is defined using the keyword class followed by the classname. The variables and methods are defined within the class definition and the class definition terminates with the end keyword.&lt;br /&gt;
&lt;br /&gt;
For ex:&lt;br /&gt;
  class Rectangle&lt;br /&gt;
     def initialize(w,l)&lt;br /&gt;
        @width, @length = w, l&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
Here, the initialize method is used to set the values of the instance variables (@width and @length. The instance variables are the properties of the objects created of this class type. These instance variables can be accessed within the class using an @. These variables can be accessed from outside the class using the accessor methods which are defined inside the class.&lt;br /&gt;
&lt;br /&gt;
In the following case printLength and printWidth are public methods used to access the instance variables from outside the class. These are called accessor methods.&lt;br /&gt;
Similarly, to&lt;br /&gt;
&lt;br /&gt;
   class Rectangle&lt;br /&gt;
  # constructor method&lt;br /&gt;
  def initialize(w,l)&lt;br /&gt;
     @width, @length = w, l&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # accessor methods&lt;br /&gt;
  def printWidth&lt;br /&gt;
     @width&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def printLength&lt;br /&gt;
     @length&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # setter methods&lt;br /&gt;
  def setWidth=(value)&lt;br /&gt;
     @width = value&lt;br /&gt;
  end&lt;br /&gt;
  def setHeight=(value)&lt;br /&gt;
     @height = value&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   # create an object&lt;br /&gt;
     rectagle = Rectangle.new(10, 20)&lt;br /&gt;
&lt;br /&gt;
    # use accessor methods&lt;br /&gt;
      x = rectangle.printWidth()&lt;br /&gt;
      y = rectangle.printLength()&lt;br /&gt;
&lt;br /&gt;
      puts &amp;quot;Width of the Rectangle is : #{x}&amp;quot;&lt;br /&gt;
      puts &amp;quot;Length of the Rectangle is : #{y}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==== Class Methods and Class Variables:====&lt;br /&gt;
A class variable is shared between all instances of the class. The value of the class variable remains the same for all the objects of the class. A class variable is accessed within the class using two @ signs at the beginning of the variable name. The class variables must be initialized within the class.&lt;br /&gt;
&lt;br /&gt;
The class methods are defined using def self.methodname() and ends with an end delimiter. These methods are called using the class name itself, for example: classname.methodname&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Attributes and accessor methods: ===&lt;br /&gt;
&lt;br /&gt;
Every class in Ruby defines a set of attributes for its objects. Ruby provides accessor methods (famously known as getter-setter methods in Java) to access these variables. Ruby provides three kinds of accessor methods:&lt;br /&gt;
&lt;br /&gt;
    Class Car&lt;br /&gt;
       attr_accessor : name, company&lt;br /&gt;
       attr_reader: price&lt;br /&gt;
       attr_writer: color&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
attr_accessor creates a setter and getter method. The attr_reader method creates only the getter method. This is required in case of attributes that are not to be modified by other programs but their value may be viewed. The attr_writer creates the setter method. In case of Java, the setter and getter methods need to be written in 3 lines of code each whereas in Ruby, the methods are written just by specifying the type of accessor, which have already been coded using Metaprogramming.[link]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Access Control:====&lt;br /&gt;
The only easy way to change an object's state in Ruby is by calling one of its methods. Control access to the methods, and you have controlled access to the object. A good rule of the thumb is never to expose methods that could leave an object in an invalid state.&lt;br /&gt;
&lt;br /&gt;
Ruby gives you three levels of protection:&lt;br /&gt;
&lt;br /&gt;
''Public methods'' can be called by everyone - no access control is enforced. A class's instance methods (these do not belong only to one object; instead, every instance of the class can call them) are public by default; anyone can call them. The initialize method is always private.&lt;br /&gt;
&lt;br /&gt;
''Protected methods'' can be invoked only by objects of the defining class and its subclasses. Access is kept within the family. However, usage of protected is limited.&lt;br /&gt;
&lt;br /&gt;
''Private methods'' cannot be called with an explicit receiver - the receiver is always self. This means that private methods can be called only in the context of the current object; you cannot invoke another object's private methods.&lt;br /&gt;
&lt;br /&gt;
Access control is determined dynamically, as the program runs, not statically. You will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
Ex:&lt;br /&gt;
&lt;br /&gt;
    class Employee&lt;br /&gt;
       def initialize(name, age, salary)&lt;br /&gt;
           @name = name&lt;br /&gt;
           @age = age&lt;br /&gt;
           @salary = salary&lt;br /&gt;
       end&lt;br /&gt;
  &lt;br /&gt;
       def name&lt;br /&gt;
           @name&lt;br /&gt;
       end &lt;br /&gt;
       def age&lt;br /&gt;
          @age&lt;br /&gt;
       end&lt;br /&gt;
       def salary&lt;br /&gt;
           @salary&lt;br /&gt;
       end&lt;br /&gt;
&lt;br /&gt;
        public :name&lt;br /&gt;
        protected :age&lt;br /&gt;
        private :salary&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      emp = Employee.new('Jack', 25, 2000);&lt;br /&gt;
      puts emp.name&lt;br /&gt;
      #puts emp.age    #age protected. gives access violation error&lt;br /&gt;
      #puts emp.salary #salary private&lt;br /&gt;
&lt;br /&gt;
====Abstract methods====&lt;br /&gt;
&lt;br /&gt;
Ruby is a dynamically typed language and does not provide abstract classes as in Java or C++. It instead provides modules that can be included within any class. The reason why Ruby does not provide abstract classes is because of the idea that it only matters if two objects can perform a function, they don’t necessarily have to be inherited from the same class. This paradigm is provided by modules, where the method of a module can be redefined as required by a class that includes it.&lt;br /&gt;
&lt;br /&gt;
Also, if required the technique of abstract class can be simulated by something like this:&lt;br /&gt;
&lt;br /&gt;
  Class Vehicle&lt;br /&gt;
   def drive&lt;br /&gt;
   raise NotImplementedError.new(&amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The subclasses that inherit from the above class will have to redefine the method “drive” as required.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Ruby ===&lt;br /&gt;
&lt;br /&gt;
Ruby provides unbounded polymorphism as a dynamically typed language through the concept of “Duck-typing”. “Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface” [link]. This concept is explained by the famous definition as “When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.” This essentially means that in languages like Ruby, it is only checked if an object can implement a function and the type of the object is not taken into consideration. It can be explained better with the help of the following example&lt;br /&gt;
&lt;br /&gt;
  class Lion&lt;br /&gt;
    def roar&lt;br /&gt;
        puts “The Lion roars when hungry”&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
     def roar&lt;br /&gt;
        puts “The man roared in fury and anger”&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   def quality lion&lt;br /&gt;
    lion.roar&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def start&lt;br /&gt;
    leo = Lion.new&lt;br /&gt;
    jack = Person.new&lt;br /&gt;
    quality leo&lt;br /&gt;
    quality jack&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   start&lt;br /&gt;
&lt;br /&gt;
In the above example, leo is an object of class “Lion” and the “roar” method defined in its class is invoked. jack is an object of class “Person” and the corresponding method defined in the Person class is called for him. Although in the real world, both objects are not of the same type, Ruby allows unbounded polymorphism as both of them implement a particular aspect.&lt;br /&gt;
&lt;br /&gt;
Thus, the check for an invoked method is done at run-time in dynamically typed languages and the method defined on that object type is called. If the method is undefined, a “NoMethodError” exception is raised.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Python===&lt;br /&gt;
&lt;br /&gt;
Python also heavily uses duck typing to allow flexibility to programmers. It emphasizes on the interface rather than on the specifc type which allows for polymorphic substitution [link].&lt;br /&gt;
In Python, functions like type() or isistance() is used to check for usual methods to match the type of find out if an object is an instance of a particular class. However, in case of duck typing, the focus is on the attribute of that object, so it checks for the presence of a particular attribute in that object using hasattr().&lt;br /&gt;
&lt;br /&gt;
Example for duck typing in Python is where Python uses a “pickle” module to serialize and de-serialize an object. So, we can introduce a new class that creates a “Tape” object. Now, an object of this type can be passed to the pickle module. Python will not differentiate between a file object or a tape object as long as they both have read() and write() methods.&lt;br /&gt;
&lt;br /&gt;
   class Tapefile():&lt;br /&gt;
      “A tape file”&lt;br /&gt;
       def __init__(self,params):&lt;br /&gt;
          #various initialization for tape access&lt;br /&gt;
       pass&lt;br /&gt;
       def read(len):&lt;br /&gt;
          #use special methods to access tape&lt;br /&gt;
       pass&lt;br /&gt;
       def readline():&lt;br /&gt;
          #use special methods to read a whole line on tape&lt;br /&gt;
       pass&lt;br /&gt;
       def write(value):&lt;br /&gt;
          #use special methods to write on tape&lt;br /&gt;
       pass&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in other languages===&lt;br /&gt;
&lt;br /&gt;
Duck typing is considered to be a feature provided by dynamic languages. C++ as a static language checks for methods by parameter and signature types. Java does not support duck typing either but it is argued that it can be simulated by the use of interfaces. The concept of duck-typing is criticized as it requires the programmer to have a very wide understanding of the source code and that two objects may implement a method in completely different contexts and so it may result in strange errors. &lt;br /&gt;
&lt;br /&gt;
Although duck typing is considered to be a feature of dynamic languages, some static languages like C# and Boo support it by allowing type checking of classes by the compiler to occur at run time.&lt;/div&gt;</summary>
		<author><name>Sthirum</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53085</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b ms</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53085"/>
		<updated>2011-10-20T16:49:58Z</updated>

		<summary type="html">&lt;p&gt;Sthirum: /* Access Control: */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Lecture 5: Object Oriented Programming in Ruby =&lt;br /&gt;
&lt;br /&gt;
This wiki tries to cover the Object Oriented concepts in ruby.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Lecture 5 of CSC 517 covered topics of object oriented programming in Ruby. Object oriented programming is a paradigm in which real world entities are represented as objects, where each object has its own attributes and set of functions that it can perform. This gives a data structure called an &amp;quot;object&amp;quot; that consists of data fields and operations. This paradigm presents features like encapsulation, data abstraction, inheritance and polymorphism.&lt;br /&gt;
&lt;br /&gt;
This wiki page tries to cover the other OOP languages and their features in a succinct manner and talks about the features of Ruby in detail with examples. We have also tried to compare the various languages in terms of their features to provide a better understanding.&lt;br /&gt;
&lt;br /&gt;
The lecture also covered the concepts of duck typing in Ruby, closures and currying. The latter two have not been included as per the instructions by the instructor.&lt;br /&gt;
&lt;br /&gt;
== Object oriented languages: An Overview ==&lt;br /&gt;
&lt;br /&gt;
==== C++: ====&lt;br /&gt;
C++ is a statically typed, multi-paradigm programming language. It supports some features of object-oriented languages like encapsulation, polymorphism and inheritance. It provides access control by specifiers public, private and protected and also allows “friend” classes. C++  allows multiple inheritance which is eliminated in most other programming languages as it allows a class to inherit from more than one base class which results in an ambiguity effect.&lt;br /&gt;
&lt;br /&gt;
==== Java: ====&lt;br /&gt;
Java is classified as a statically typed, hybrid object oriented language. Although it provides information hiding, inheritance and polymorphism, it has a few basic types incorporated that are not objects and some of the built-in operators are provided by basic arithmetic and not as messages to objects. [link]. For these reasons, it is not considered to be a “pure” object-oriented language.&lt;br /&gt;
&lt;br /&gt;
Java provides access control and supports private, public and protected access. It also allows to create packages to create and maintain namespaces. Java is designed not to provide multiple inheritance to avoid problems faced in C++. However, it presents a single-rooted hierarchy where “Object” is the ancestor of all classes (seen in Ruby too) and provides interfaces to extend/inherit functionality.&lt;br /&gt;
&lt;br /&gt;
==== Python: ====&lt;br /&gt;
Python is a dynamically typed, interpreted, object oriented language. It is argued that Python is not completely object oriented as it does not provide access control.&lt;br /&gt;
&lt;br /&gt;
==== Ruby: ====&lt;br /&gt;
Ruby is dynamically typed and highly object-oriented, much more than Java and Python. In Ruby, it is said that “everything is an object”.  Every value happens to be an object in Ruby. You may call methods on a string, an integer just like you call on any object in Java. For ex,&lt;br /&gt;
&lt;br /&gt;
     (2.1143).size.times{puts “Hello!”}&lt;br /&gt;
&lt;br /&gt;
This example will display “Hello!” six times on your IRB as the size of the parameter &amp;quot;2.1143&amp;quot; is 6.&lt;br /&gt;
&lt;br /&gt;
== Comparison of object Oriented Languages ==&lt;br /&gt;
The following table compares the Properties of some of the well-known Object Oriented Languages.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 9pad case0%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Language&lt;br /&gt;
!Ruby&lt;br /&gt;
!Java&lt;br /&gt;
!C#&lt;br /&gt;
!C++&lt;br /&gt;
!Python&lt;br /&gt;
!SmallTalk&lt;br /&gt;
!Perl&lt;br /&gt;
|-&lt;br /&gt;
!Object Orientation&lt;br /&gt;
|Pure&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid/Multi-Paradigm&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Pure&lt;br /&gt;
|Add-on/Hybrid&lt;br /&gt;
|-&lt;br /&gt;
!Static/Dynamic Typing&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|-&lt;br /&gt;
!Generic Classes&lt;br /&gt;
|N/A&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|-&lt;br /&gt;
!Inheritance&lt;br /&gt;
|Single Class/Multiple Mixins&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Multiple&lt;br /&gt;
|Multiple&lt;br /&gt;
|Single&lt;br /&gt;
|Multiple&lt;br /&gt;
|- &lt;br /&gt;
!Feature Renaming&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|NO&lt;br /&gt;
|-&lt;br /&gt;
!Method Overloading&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Operator Overloading&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|-&lt;br /&gt;
!Class Variables or methods&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Access Control&lt;br /&gt;
|Public, Protected, Private&lt;br /&gt;
|Public, Protected, Package, Private&lt;br /&gt;
|Public, Protected, Private, Internal, Protected Internal&lt;br /&gt;
|Public, Protected, Private, Friends&lt;br /&gt;
|Name Mangling&lt;br /&gt;
|Protected Data, Public Methods&lt;br /&gt;
|None&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Ruby : Purely Object Oriented ==&lt;br /&gt;
&lt;br /&gt;
Ruby is considered to be a purely Object oriented Language, as everything in it is considered to be an object. Unlike Java, even primitives such as characters, punctuation, numbers and strings are treated as objects in Ruby. Ruby was designed to facilitate the enforcement of Object Oriented Methods. Ruby is a Dynamically Typed Language.&lt;br /&gt;
&lt;br /&gt;
=== Object oriented Concepts in ruby: ===&lt;br /&gt;
&lt;br /&gt;
Ruby is a singly rooted Object Oriented Programming language which has all its classes having the same superclass which is the class Object.&lt;br /&gt;
&lt;br /&gt;
==== Classes:====&lt;br /&gt;
[[Image:RubyClass.png|thumb|right|280px|'''''Classes in Ruby''''']]&lt;br /&gt;
&lt;br /&gt;
A class is used to define a blueprint of a data type. It does not contain any data, but it specifies what kind of variables and methods an object of the class will have.&lt;br /&gt;
A class in Ruby is defined using the keyword class followed by the classname. The variables and methods are defined within the class definition and the class definition terminates with the end keyword.&lt;br /&gt;
&lt;br /&gt;
For ex:&lt;br /&gt;
  class Rectangle&lt;br /&gt;
     def initialize(w,l)&lt;br /&gt;
        @width, @length = w, l&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
Here, the initialize method is used to set the values of the instance variables (@width and @length. The instance variables are the properties of the objects created of this class type. These instance variables can be accessed within the class using an @. These variables can be accessed from outside the class using the accessor methods which are defined inside the class.&lt;br /&gt;
&lt;br /&gt;
In the following case printLength and printWidth are public methods used to access the instance variables from outside the class. These are called accessor methods.&lt;br /&gt;
Similarly, to&lt;br /&gt;
&lt;br /&gt;
   class Rectangle&lt;br /&gt;
  # constructor method&lt;br /&gt;
  def initialize(w,l)&lt;br /&gt;
     @width, @length = w, l&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # accessor methods&lt;br /&gt;
  def printWidth&lt;br /&gt;
     @width&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def printLength&lt;br /&gt;
     @length&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # setter methods&lt;br /&gt;
  def setWidth=(value)&lt;br /&gt;
     @width = value&lt;br /&gt;
  end&lt;br /&gt;
  def setHeight=(value)&lt;br /&gt;
     @height = value&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   # create an object&lt;br /&gt;
     rectagle = Rectangle.new(10, 20)&lt;br /&gt;
&lt;br /&gt;
    # use accessor methods&lt;br /&gt;
      x = rectangle.printWidth()&lt;br /&gt;
      y = rectangle.printLength()&lt;br /&gt;
&lt;br /&gt;
      puts &amp;quot;Width of the Rectangle is : #{x}&amp;quot;&lt;br /&gt;
      puts &amp;quot;Length of the Rectangle is : #{y}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==== Class Methods and Class Variables:====&lt;br /&gt;
A class variable is shared between all instances of the class. The value of the class variable remains the same for all the objects of the class. A class variable is accessed within the class using two @ signs at the beginning of the variable name. The class variables must be initialized within the class.&lt;br /&gt;
&lt;br /&gt;
The class methods are defined using def self.methodname() and ends with an end delimiter. These methods are called using the class name itself, for example: classname.methodname&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Attributes and accessor methods: ===&lt;br /&gt;
&lt;br /&gt;
Every class in Ruby defines a set of attributes for its objects. Ruby provides accessor methods (famously known as getter-setter methods in Java) to access these variables. Ruby provides three kinds of accessor methods:&lt;br /&gt;
&lt;br /&gt;
    Class Car&lt;br /&gt;
       attr_accessor : name, company&lt;br /&gt;
       attr_reader: price&lt;br /&gt;
       attr_writer: color&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
attr_accessor creates a setter and getter method. The attr_reader method creates only the getter method. This is required in case of attributes that are not to be modified by other programs but their value may be viewed. The attr_writer creates the setter method. In case of Java, the setter and getter methods need to be written in 3 lines of code each whereas in Ruby, the methods are written just by specifying the type of accessor, which have already been coded using Metaprogramming.[link]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Access Control:====&lt;br /&gt;
The only easy way to change an object's state in Ruby is by calling one of its methods. Control access to the methods, and you have controlled access to the object. A good rule of the thumb is never to expose methods that could leave an object in an invalid state.&lt;br /&gt;
&lt;br /&gt;
Ruby gives you three levels of protection:&lt;br /&gt;
&lt;br /&gt;
''Public methods'' can be called by everyone - no access control is enforced. A class's instance methods (these do not belong only to one object; instead, every instance of the class can call them) are public by default; anyone can call them. The initialize method is always private.&lt;br /&gt;
&lt;br /&gt;
''Protected methods'' can be invoked only by objects of the defining class and its subclasses. Access is kept within the family. However, usage of protected is limited.&lt;br /&gt;
&lt;br /&gt;
''Private methods'' cannot be called with an explicit receiver - the receiver is always self. This means that private methods can be called only in the context of the current object; you cannot invoke another object's private methods.&lt;br /&gt;
&lt;br /&gt;
Access control is determined dynamically, as the program runs, not statically. You will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
Ex:&lt;br /&gt;
&lt;br /&gt;
    class Employee&lt;br /&gt;
       def initialize(name, age, salary)&lt;br /&gt;
           @name = name&lt;br /&gt;
           @age = age&lt;br /&gt;
           @salary = salary&lt;br /&gt;
       end&lt;br /&gt;
  &lt;br /&gt;
       def name&lt;br /&gt;
           @name&lt;br /&gt;
       end &lt;br /&gt;
       def age&lt;br /&gt;
          @age&lt;br /&gt;
       end&lt;br /&gt;
       def salary&lt;br /&gt;
           @salary&lt;br /&gt;
       end&lt;br /&gt;
&lt;br /&gt;
        public :name&lt;br /&gt;
        protected :age&lt;br /&gt;
        private :salary&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      emp = Employee.new('Jack', 25, 2000);&lt;br /&gt;
      puts emp.name&lt;br /&gt;
      #puts emp.age    #age protected. gives access violation error&lt;br /&gt;
      #puts emp.salary #salary private&lt;br /&gt;
&lt;br /&gt;
====Abstract methods====&lt;br /&gt;
&lt;br /&gt;
Ruby is a dynamically typed language and does not provide abstract classes as in Java or C++. It instead provides modules that can be included within any class. The reason why Ruby does not provide abstract classes is because of the idea that it only matters if two objects can perform a function, they don’t necessarily have to be inherited from the same class. This paradigm is provided by modules, where the method of a module can be redefined as required by a class that includes it.&lt;br /&gt;
&lt;br /&gt;
Also, if required the technique of abstract class can be simulated by something like this:&lt;br /&gt;
&lt;br /&gt;
  Class Vehicle&lt;br /&gt;
   def drive&lt;br /&gt;
   raise NotImplementedError.new(&amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The subclasses that inherit from the above class will have to redefine the method “drive” as required.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Ruby ===&lt;br /&gt;
&lt;br /&gt;
Ruby provides unbounded polymorphism as a dynamically typed language through the concept of “Duck-typing”. “Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface” [link]. This concept is explained by the famous definition as “When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.” This essentially means that in languages like Ruby, it is only checked if an object can implement a function and the type of the object is not taken into consideration. It can be explained better with the help of the following example&lt;br /&gt;
&lt;br /&gt;
  class Lion&lt;br /&gt;
    def roar&lt;br /&gt;
        puts “The Lion roars when hungry”&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
     def roar&lt;br /&gt;
        puts “The man roared in fury and anger”&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   def quality lion&lt;br /&gt;
    lion.roar&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def start&lt;br /&gt;
    leo = Lion.new&lt;br /&gt;
    jack = Person.new&lt;br /&gt;
    quality leo&lt;br /&gt;
    quality jack&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   start&lt;br /&gt;
&lt;br /&gt;
In the above example, leo is an object of class “Lion” and the “roar” method defined in its class is invoked. jack is an object of class “Person” and the corresponding method defined in the Person class is called for him. Although in the real world, both objects are not of the same type, Ruby allows unbounded polymorphism as both of them implement a particular aspect.&lt;br /&gt;
&lt;br /&gt;
Thus, the check for an invoked method is done at run-time in dynamically typed languages and the method defined on that object type is called. If the method is undefined, a “NoMethodError” exception is raised.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Python===&lt;br /&gt;
&lt;br /&gt;
Python also heavily uses duck typing to allow flexibility to programmers. It emphasizes on the interface rather than on the specifc type which allows for polymorphic substitution [link].&lt;br /&gt;
In Python, functions like type() or isistance() is used to check for usual methods to match the type of find out if an object is an instance of a particular class. However, in case of duck typing, the focus is on the attribute of that object, so it checks for the presence of a particular attribute in that object using hasattr().&lt;br /&gt;
&lt;br /&gt;
Example for duck typing in Python is where Python uses a “pickle” module to serialize and de-serialize an object. So, we can introduce a new class that creates a “Tape” object. Now, an object of this type can be passed to the pickle module. Python will not differentiate between a file object or a tape object as long as they both have read() and write() methods.&lt;br /&gt;
&lt;br /&gt;
   class Tapefile():&lt;br /&gt;
      “A tape file”&lt;br /&gt;
       def __init__(self,params):&lt;br /&gt;
          #various initialization for tape access&lt;br /&gt;
       pass&lt;br /&gt;
       def read(len):&lt;br /&gt;
          #use special methods to access tape&lt;br /&gt;
       pass&lt;br /&gt;
       def readline():&lt;br /&gt;
          #use special methods to read a whole line on tape&lt;br /&gt;
       pass&lt;br /&gt;
       def write(value):&lt;br /&gt;
          #use special methods to write on tape&lt;br /&gt;
       pass&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in other languages===&lt;br /&gt;
&lt;br /&gt;
Duck typing is considered to be a feature provided by dynamic languages. C++ as a static language checks for methods by parameter and signature types. Java does not support duck typing either but it is argued that it can be simulated by the use of interfaces. The concept of duck-typing is criticized as it requires the programmer to have a very wide understanding of the source code and that two objects may implement a method in completely different contexts and so it may result in strange errors. &lt;br /&gt;
&lt;br /&gt;
Although duck typing is considered to be a feature of dynamic languages, some static languages like C# and Boo support it by allowing type checking of classes by the compiler to occur at run time.&lt;/div&gt;</summary>
		<author><name>Sthirum</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53077</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b ms</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53077"/>
		<updated>2011-10-20T16:34:21Z</updated>

		<summary type="html">&lt;p&gt;Sthirum: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Lecture 5: Object Oriented Programming in Ruby =&lt;br /&gt;
&lt;br /&gt;
This wiki tries to cover the Object Oriented concepts in ruby.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Lecture 5 of CSC 517 covered topics of object oriented programming in Ruby. Object oriented programming is a paradigm in which real world entities are represented as objects, where each object has its own attributes and set of functions that it can perform. This gives a data structure called an &amp;quot;object&amp;quot; that consists of data fields and operations. This paradigm presents features like encapsulation, data abstraction, inheritance and polymorphism.&lt;br /&gt;
&lt;br /&gt;
This wiki page tries to cover the other OOP languages and their features in a succinct manner and talks about the features of Ruby in detail with examples. We have also tried to compare the various languages in terms of their features to provide a better understanding.&lt;br /&gt;
&lt;br /&gt;
The lecture also covered the concepts of duck typing in Ruby, closures and currying. The latter two have not been included as per the instructions by the instructor.&lt;br /&gt;
&lt;br /&gt;
== Object oriented languages: An Overview ==&lt;br /&gt;
&lt;br /&gt;
==== C++: ====&lt;br /&gt;
C++ is a statically typed, multi-paradigm programming language. It supports some features of object-oriented languages like encapsulation, polymorphism and inheritance. It provides access control by specifiers public, private and protected and also allows “friend” classes. C++  allows multiple inheritance which is eliminated in most other programming languages as it allows a class to inherit from more than one base class which results in an ambiguity effect.&lt;br /&gt;
&lt;br /&gt;
==== Java: ====&lt;br /&gt;
Java is classified as a statically typed, hybrid object oriented language. Although it provides information hiding, inheritance and polymorphism, it has a few basic types incorporated that are not objects and some of the built-in operators are provided by basic arithmetic and not as messages to objects. [link]. For these reasons, it is not considered to be a “pure” object-oriented language.&lt;br /&gt;
&lt;br /&gt;
Java provides access control and supports private, public and protected access. It also allows to create packages to create and maintain namespaces. Java is designed not to provide multiple inheritance to avoid problems faced in C++. However, it presents a single-rooted hierarchy where “Object” is the ancestor of all classes (seen in Ruby too) and provides interfaces to extend/inherit functionality.&lt;br /&gt;
&lt;br /&gt;
==== Python: ====&lt;br /&gt;
Python is a dynamically typed, interpreted, object oriented language. It is argued that Python is not completely object oriented as it does not provide access control.&lt;br /&gt;
&lt;br /&gt;
==== Ruby: ====&lt;br /&gt;
Ruby is dynamically typed and highly object-oriented, much more than Java and Python. In Ruby, it is said that “everything is an object”.  Every value happens to be an object in Ruby. You may call methods on a string, an integer just like you call on any object in Java. For ex,&lt;br /&gt;
&lt;br /&gt;
     (2.1143).size.times{puts “Hello!”}&lt;br /&gt;
&lt;br /&gt;
This example will display “Hello!” six times on your IRB as the size of the parameter &amp;quot;2.1143&amp;quot; is 6.&lt;br /&gt;
&lt;br /&gt;
== Comparison of object Oriented Languages ==&lt;br /&gt;
The following table compares the Properties of some of the well-known Object Oriented Languages.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 9pad case0%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Language&lt;br /&gt;
!Ruby&lt;br /&gt;
!Java&lt;br /&gt;
!C#&lt;br /&gt;
!C++&lt;br /&gt;
!Python&lt;br /&gt;
!SmallTalk&lt;br /&gt;
!Perl&lt;br /&gt;
|-&lt;br /&gt;
!Object Orientation&lt;br /&gt;
|Pure&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid/Multi-Paradigm&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Pure&lt;br /&gt;
|Add-on/Hybrid&lt;br /&gt;
|-&lt;br /&gt;
!Static/Dynamic Typing&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|-&lt;br /&gt;
!Generic Classes&lt;br /&gt;
|N/A&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|-&lt;br /&gt;
!Inheritance&lt;br /&gt;
|Single Class/Multiple Mixins&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Multiple&lt;br /&gt;
|Multiple&lt;br /&gt;
|Single&lt;br /&gt;
|Multiple&lt;br /&gt;
|- &lt;br /&gt;
!Feature Renaming&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|NO&lt;br /&gt;
|-&lt;br /&gt;
!Method Overloading&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Operator Overloading&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|-&lt;br /&gt;
!Class Variables or methods&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Access Control&lt;br /&gt;
|Public, Protected, Private&lt;br /&gt;
|Public, Protected, Package, Private&lt;br /&gt;
|Public, Protected, Private, Internal, Protected Internal&lt;br /&gt;
|Public, Protected, Private, Friends&lt;br /&gt;
|Name Mangling&lt;br /&gt;
|Protected Data, Public Methods&lt;br /&gt;
|None&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Ruby : Purely Object Oriented ==&lt;br /&gt;
&lt;br /&gt;
Ruby is considered to be a purely Object oriented Language, as everything in it is considered to be an object. Unlike Java, even primitives such as characters, punctuation, numbers and strings are treated as objects in Ruby. Ruby was designed to facilitate the enforcement of Object Oriented Methods. Ruby is a Dynamically Typed Language.&lt;br /&gt;
&lt;br /&gt;
=== Object oriented Concepts in ruby: ===&lt;br /&gt;
&lt;br /&gt;
Ruby is a singly rooted Object Oriented Programming language which has all its classes having the same superclass which is the class Object.&lt;br /&gt;
&lt;br /&gt;
==== Classes:====&lt;br /&gt;
[[Image:RubyClass.png|thumb|right|280px|'''''Classes in Ruby''''']]&lt;br /&gt;
&lt;br /&gt;
A class is used to define a blueprint of a data type. It does not contain any data, but it specifies what kind of variables and methods an object of the class will have.&lt;br /&gt;
A class in Ruby is defined using the keyword class followed by the classname. The variables and methods are defined within the class definition and the class definition terminates with the end keyword.&lt;br /&gt;
&lt;br /&gt;
For ex:&lt;br /&gt;
  class Rectangle&lt;br /&gt;
     def initialize(w,l)&lt;br /&gt;
        @width, @length = w, l&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
Here, the initialize method is used to set the values of the instance variables (@width and @length. The instance variables are the properties of the objects created of this class type. These instance variables can be accessed within the class using an @. These variables can be accessed from outside the class using the accessor methods which are defined inside the class.&lt;br /&gt;
&lt;br /&gt;
In the following case printLength and printWidth are public methods used to access the instance variables from outside the class. These are called accessor methods.&lt;br /&gt;
Similarly, to&lt;br /&gt;
&lt;br /&gt;
   class Rectangle&lt;br /&gt;
  # constructor method&lt;br /&gt;
  def initialize(w,l)&lt;br /&gt;
     @width, @length = w, l&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # accessor methods&lt;br /&gt;
  def printWidth&lt;br /&gt;
     @width&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def printLength&lt;br /&gt;
     @length&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # setter methods&lt;br /&gt;
  def setWidth=(value)&lt;br /&gt;
     @width = value&lt;br /&gt;
  end&lt;br /&gt;
  def setHeight=(value)&lt;br /&gt;
     @height = value&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   # create an object&lt;br /&gt;
     rectagle = Rectangle.new(10, 20)&lt;br /&gt;
&lt;br /&gt;
    # use accessor methods&lt;br /&gt;
      x = rectangle.printWidth()&lt;br /&gt;
      y = rectangle.printLength()&lt;br /&gt;
&lt;br /&gt;
      puts &amp;quot;Width of the Rectangle is : #{x}&amp;quot;&lt;br /&gt;
      puts &amp;quot;Length of the Rectangle is : #{y}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==== Class Methods and Class Variables:====&lt;br /&gt;
A class variable is shared between all instances of the class. The value of the class variable remains the same for all the objects of the class. A class variable is accessed within the class using two @ signs at the beginning of the variable name. The class variables must be initialized within the class.&lt;br /&gt;
&lt;br /&gt;
The class methods are defined using def self.methodname() and ends with an end delimiter. These methods are called using the class name itself, for example: classname.methodname&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Attributes and accessor methods: ===&lt;br /&gt;
&lt;br /&gt;
Every class in Ruby defines a set of attributes for its objects. Ruby provides accessor methods (famously known as getter-setter methods in Java) to access these variables. Ruby provides three kinds of accessor methods:&lt;br /&gt;
&lt;br /&gt;
    Class Car&lt;br /&gt;
       attr_accessor : name, company&lt;br /&gt;
       attr_reader: price&lt;br /&gt;
       attr_writer: color&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
attr_accessor creates a setter and getter method. The attr_reader method creates only the getter method. This is required in case of attributes that are not to be modified by other programs but their value may be viewed. The attr_writer creates the setter method. In case of Java, the setter and getter methods need to be written in 3 lines of code each whereas in Ruby, the methods are written just by specifying the type of accessor, which have already been coded using Metaprogramming.[link]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Access Control:====&lt;br /&gt;
The only easy way to change an object's state in Ruby is by calling one of its methods. Control access to the methods, and you have controlled access to the object. A good rule of the thumb is never to expose methods that could leave an object in an invalid state.&lt;br /&gt;
&lt;br /&gt;
Ruby gives you three levels of protection:&lt;br /&gt;
&lt;br /&gt;
''Public methods'' can be called by everyone - no access control is enforced. A class's instance methods (these do not belong only to one object; instead, every instance of the class can call them) are public by default; anyone can call them. The initialize method is always private.&lt;br /&gt;
&lt;br /&gt;
''Protected methods'' can be invoked only by objects of the defining class and its subclasses. Access is kept within the family. However, usage of protected is limited.&lt;br /&gt;
&lt;br /&gt;
''Private methods'' cannot be called with an explicit receiver - the receiver is always self. This means that private methods can be called only in the context of the current object; you cannot invoke another object's private methods.&lt;br /&gt;
&lt;br /&gt;
Access control is determined dynamically, as the program runs, not statically. You will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
&lt;br /&gt;
====Abstract methods====&lt;br /&gt;
&lt;br /&gt;
Ruby is a dynamically typed language and does not provide abstract classes as in Java or C++. It instead provides modules that can be included within any class. The reason why Ruby does not provide abstract classes is because of the idea that it only matters if two objects can perform a function, they don’t necessarily have to be inherited from the same class. This paradigm is provided by modules, where the method of a module can be redefined as required by a class that includes it.&lt;br /&gt;
&lt;br /&gt;
Also, if required the technique of abstract class can be simulated by something like this:&lt;br /&gt;
&lt;br /&gt;
  Class Vehicle&lt;br /&gt;
   def drive&lt;br /&gt;
   raise NotImplementedError.new(&amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The subclasses that inherit from the above class will have to redefine the method “drive” as required.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Ruby ===&lt;br /&gt;
&lt;br /&gt;
Ruby provides unbounded polymorphism as a dynamically typed language through the concept of “Duck-typing”. “Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface” [link]. This concept is explained by the famous definition as “When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.” This essentially means that in languages like Ruby, it is only checked if an object can implement a function and the type of the object is not taken into consideration. It can be explained better with the help of the following example&lt;br /&gt;
&lt;br /&gt;
  class Lion&lt;br /&gt;
    def roar&lt;br /&gt;
        puts “The Lion roars when hungry”&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
     def roar&lt;br /&gt;
        puts “The man roared in fury and anger”&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   def quality lion&lt;br /&gt;
    lion.roar&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def start&lt;br /&gt;
    leo = Lion.new&lt;br /&gt;
    jack = Person.new&lt;br /&gt;
    quality leo&lt;br /&gt;
    quality jack&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   start&lt;br /&gt;
&lt;br /&gt;
In the above example, leo is an object of class “Lion” and the “roar” method defined in its class is invoked. jack is an object of class “Person” and the corresponding method defined in the Person class is called for him. Although in the real world, both objects are not of the same type, Ruby allows unbounded polymorphism as both of them implement a particular aspect.&lt;br /&gt;
&lt;br /&gt;
Thus, the check for an invoked method is done at run-time in dynamically typed languages and the method defined on that object type is called. If the method is undefined, a “NoMethodError” exception is raised.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Python===&lt;br /&gt;
&lt;br /&gt;
Python also heavily uses duck typing to allow flexibility to programmers. It emphasizes on the interface rather than on the specifc type which allows for polymorphic substitution [link].&lt;br /&gt;
In Python, functions like type() or isistance() is used to check for usual methods to match the type of find out if an object is an instance of a particular class. However, in case of duck typing, the focus is on the attribute of that object, so it checks for the presence of a particular attribute in that object using hasattr().&lt;br /&gt;
&lt;br /&gt;
Example for duck typing in Python is where Python uses a “pickle” module to serialize and de-serialize an object. So, we can introduce a new class that creates a “Tape” object. Now, an object of this type can be passed to the pickle module. Python will not differentiate between a file object or a tape object as long as they both have read() and write() methods.&lt;br /&gt;
&lt;br /&gt;
   class Tapefile():&lt;br /&gt;
      “A tape file”&lt;br /&gt;
       def __init__(self,params):&lt;br /&gt;
          #various initialization for tape access&lt;br /&gt;
       pass&lt;br /&gt;
       def read(len):&lt;br /&gt;
          #use special methods to access tape&lt;br /&gt;
       pass&lt;br /&gt;
       def readline():&lt;br /&gt;
          #use special methods to read a whole line on tape&lt;br /&gt;
       pass&lt;br /&gt;
       def write(value):&lt;br /&gt;
          #use special methods to write on tape&lt;br /&gt;
       pass&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in other languages===&lt;br /&gt;
&lt;br /&gt;
Duck typing is considered to be a feature provided by dynamic languages. C++ as a static language checks for methods by parameter and signature types. Java does not support duck typing either but it is argued that it can be simulated by the use of interfaces. The concept of duck-typing is criticized as it requires the programmer to have a very wide understanding of the source code and that two objects may implement a method in completely different contexts and so it may result in strange errors. &lt;br /&gt;
&lt;br /&gt;
Although duck typing is considered to be a feature of dynamic languages, some static languages like C# and Boo support it by allowing type checking of classes by the compiler to occur at run time.&lt;/div&gt;</summary>
		<author><name>Sthirum</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53071</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b ms</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53071"/>
		<updated>2011-10-20T16:16:51Z</updated>

		<summary type="html">&lt;p&gt;Sthirum: /* Duck Typing in Python */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Lecture 5: Object Oriented Programming in Ruby =&lt;br /&gt;
&lt;br /&gt;
This wiki tries to cover the Object Oriented concepts in ruby.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Object oriented languages: An Overview ==&lt;br /&gt;
&lt;br /&gt;
==== C++: ====&lt;br /&gt;
C++ is a statically typed, multi-paradigm programming language. It supports some features of object-oriented languages like encapsulation, polymorphism and inheritance. It provides access control by specifiers public, private and protected and also allows “friend” classes. C++  allows multiple inheritance which is eliminated in most other programming languages as it allows a class to inherit from more than one base class which results in an ambiguity effect.&lt;br /&gt;
&lt;br /&gt;
==== Java: ====&lt;br /&gt;
Java is classified as a statically typed, hybrid object oriented language. Although it provides information hiding, inheritance and polymorphism, it has a few basic types incorporated that are not objects and some of the built-in operators are provided by basic arithmetic and not as messages to objects. [link]. For these reasons, it is not considered to be a “pure” object-oriented language.&lt;br /&gt;
&lt;br /&gt;
Java provides access control and supports private, public and protected access. It also allows to create packages to create and maintain namespaces. Java is designed not to provide multiple inheritance to avoid problems faced in C++. However, it presents a single-rooted hierarchy where “Object” is the ancestor of all classes (seen in Ruby too) and provides interfaces to extend/inherit functionality.&lt;br /&gt;
&lt;br /&gt;
==== Python: ====&lt;br /&gt;
Python is a dynamically typed, interpreted, object oriented language. It is argued that Python is not completely object oriented as it does not provide access control.&lt;br /&gt;
&lt;br /&gt;
==== Ruby: ====&lt;br /&gt;
Ruby is dynamically typed and highly object-oriented, much more than Java and Python. In Ruby, it is said that “everything is an object”.  Every value happens to be an object in Ruby. You may call methods on a string, an integer just like you call on any object in Java. For ex,&lt;br /&gt;
&lt;br /&gt;
     (2.1143).size.times{puts “Hello!”}&lt;br /&gt;
&lt;br /&gt;
This example will display “Hello!” six times on your IRB as the size of the parameter &amp;quot;2.1143&amp;quot; is 6.&lt;br /&gt;
&lt;br /&gt;
== Comparison of object Oriented Languages ==&lt;br /&gt;
The following table compares the Properties of some of the well-known Object Oriented Languages.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 9pad case0%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Language&lt;br /&gt;
!Ruby&lt;br /&gt;
!Java&lt;br /&gt;
!C#&lt;br /&gt;
!C++&lt;br /&gt;
!Python&lt;br /&gt;
!SmallTalk&lt;br /&gt;
!Perl&lt;br /&gt;
|-&lt;br /&gt;
!Object Orientation&lt;br /&gt;
|Pure&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid/Multi-Paradigm&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Pure&lt;br /&gt;
|Add-on/Hybrid&lt;br /&gt;
|-&lt;br /&gt;
!Static/Dynamic Typing&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|-&lt;br /&gt;
!Generic Classes&lt;br /&gt;
|N/A&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|-&lt;br /&gt;
!Inheritance&lt;br /&gt;
|Single Class/Multiple Mixins&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Multiple&lt;br /&gt;
|Multiple&lt;br /&gt;
|Single&lt;br /&gt;
|Multiple&lt;br /&gt;
|- &lt;br /&gt;
!Feature Renaming&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|NO&lt;br /&gt;
|-&lt;br /&gt;
!Method Overloading&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Operator Overloading&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|-&lt;br /&gt;
!Class Variables or methods&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Access Control&lt;br /&gt;
|Public, Protected, Private&lt;br /&gt;
|Public, Protected, Package, Private&lt;br /&gt;
|Public, Protected, Private, Internal, Protected Internal&lt;br /&gt;
|Public, Protected, Private, Friends&lt;br /&gt;
|Name Mangling&lt;br /&gt;
|Protected Data, Public Methods&lt;br /&gt;
|None&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Ruby : Purely Object Oriented ==&lt;br /&gt;
&lt;br /&gt;
Ruby is considered to be a purely Object oriented Language, as everything in it is considered to be an object. Unlike Java, even primitives such as characters, punctuation, numbers and strings are treated as objects in Ruby. Ruby was designed to facilitate the enforcement of Object Oriented Methods. Ruby is a Dynamically Typed Language.&lt;br /&gt;
&lt;br /&gt;
=== Object oriented Concepts in ruby: ===&lt;br /&gt;
&lt;br /&gt;
Ruby is a singly rooted Object Oriented Programming language which has all its classes having the same superclass which is the class Object.&lt;br /&gt;
&lt;br /&gt;
==== Classes:====&lt;br /&gt;
[[Image:RubyClass.png|thumb|right|280px|'''''Classes in Ruby''''']]&lt;br /&gt;
&lt;br /&gt;
A class is used to define a blueprint of a data type. It does not contain any data, but it specifies what kind of variables and methods an object of the class will have.&lt;br /&gt;
A class in Ruby is defined using the keyword class followed by the classname. The variables and methods are defined within the class definition and the class definition terminates with the end keyword.&lt;br /&gt;
&lt;br /&gt;
For ex:&lt;br /&gt;
  class Rectangle&lt;br /&gt;
     def initialize(w,l)&lt;br /&gt;
        @width, @length = w, l&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
Here, the initialize method is used to set the values of the instance variables (@width and @length. The instance variables are the properties of the objects created of this class type. These instance variables can be accessed within the class using an @. These variables can be accessed from outside the class using the accessor methods which are defined inside the class.&lt;br /&gt;
&lt;br /&gt;
In the following case printLength and printWidth are public methods used to access the instance variables from outside the class. These are called accessor methods.&lt;br /&gt;
Similarly, to&lt;br /&gt;
&lt;br /&gt;
   class Rectangle&lt;br /&gt;
  # constructor method&lt;br /&gt;
  def initialize(w,l)&lt;br /&gt;
     @width, @length = w, l&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # accessor methods&lt;br /&gt;
  def printWidth&lt;br /&gt;
     @width&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def printLength&lt;br /&gt;
     @length&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # setter methods&lt;br /&gt;
  def setWidth=(value)&lt;br /&gt;
     @width = value&lt;br /&gt;
  end&lt;br /&gt;
  def setHeight=(value)&lt;br /&gt;
     @height = value&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   # create an object&lt;br /&gt;
     rectagle = Rectangle.new(10, 20)&lt;br /&gt;
&lt;br /&gt;
    # use accessor methods&lt;br /&gt;
      x = rectangle.printWidth()&lt;br /&gt;
      y = rectangle.printLength()&lt;br /&gt;
&lt;br /&gt;
      puts &amp;quot;Width of the Rectangle is : #{x}&amp;quot;&lt;br /&gt;
      puts &amp;quot;Length of the Rectangle is : #{y}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==== Class Methods and Class Variables:====&lt;br /&gt;
A class variable is shared between all instances of the class. The value of the class variable remains the same for all the objects of the class. A class variable is accessed within the class using two @ signs at the beginning of the variable name. The class variables must be initialized within the class.&lt;br /&gt;
&lt;br /&gt;
The class methods are defined using def self.methodname() and ends with an end delimiter. These methods are called using the class name itself, for example: classname.methodname&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Attributes and accessor methods: ===&lt;br /&gt;
&lt;br /&gt;
Every class in Ruby defines a set of attributes for its objects. Ruby provides accessor methods (famously known as getter-setter methods in Java) to access these variables. Ruby provides three kinds of accessor methods:&lt;br /&gt;
&lt;br /&gt;
    Class Car&lt;br /&gt;
       attr_accessor : name, company&lt;br /&gt;
       attr_reader: price&lt;br /&gt;
       attr_writer: color&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
attr_accessor creates a setter and getter method. The attr_reader method creates only the getter method. This is required in case of attributes that are not to be modified by other programs but their value may be viewed. The attr_writer creates the setter method. In case of Java, the setter and getter methods need to be written in 3 lines of code each whereas in Ruby, the methods are written just by specifying the type of accessor, which have already been coded using Metaprogramming.[link]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Access Control:====&lt;br /&gt;
The only easy way to change an object's state in Ruby is by calling one of its methods. Control access to the methods, and you have controlled access to the object. A good rule of the thumb is never to expose methods that could leave an object in an invalid state.&lt;br /&gt;
&lt;br /&gt;
Ruby gives you three levels of protection:&lt;br /&gt;
&lt;br /&gt;
''Public methods'' can be called by everyone - no access control is enforced. A class's instance methods (these do not belong only to one object; instead, every instance of the class can call them) are public by default; anyone can call them. The initialize method is always private.&lt;br /&gt;
&lt;br /&gt;
''Protected methods'' can be invoked only by objects of the defining class and its subclasses. Access is kept within the family. However, usage of protected is limited.&lt;br /&gt;
&lt;br /&gt;
''Private methods'' cannot be called with an explicit receiver - the receiver is always self. This means that private methods can be called only in the context of the current object; you cannot invoke another object's private methods.&lt;br /&gt;
&lt;br /&gt;
Access control is determined dynamically, as the program runs, not statically. You will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
&lt;br /&gt;
====Abstract methods====&lt;br /&gt;
&lt;br /&gt;
Ruby is a dynamically typed language and does not provide abstract classes as in Java or C++. It instead provides modules that can be included within any class. The reason why Ruby does not provide abstract classes is because of the idea that it only matters if two objects can perform a function, they don’t necessarily have to be inherited from the same class. This paradigm is provided by modules, where the method of a module can be redefined as required by a class that includes it.&lt;br /&gt;
&lt;br /&gt;
Also, if required the technique of abstract class can be simulated by something like this:&lt;br /&gt;
&lt;br /&gt;
  Class Vehicle&lt;br /&gt;
   def drive&lt;br /&gt;
   raise NotImplementedError.new(&amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The subclasses that inherit from the above class will have to redefine the method “drive” as required.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Ruby ===&lt;br /&gt;
&lt;br /&gt;
Ruby provides unbounded polymorphism as a dynamically typed language through the concept of “Duck-typing”. “Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface” [link]. This concept is explained by the famous definition as “When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.” This essentially means that in languages like Ruby, it is only checked if an object can implement a function and the type of the object is not taken into consideration. It can be explained better with the help of the following example&lt;br /&gt;
&lt;br /&gt;
  class Lion&lt;br /&gt;
    def roar&lt;br /&gt;
        puts “The Lion roars when hungry”&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
     def roar&lt;br /&gt;
        puts “The man roared in fury and anger”&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   def quality lion&lt;br /&gt;
    lion.roar&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def start&lt;br /&gt;
    leo = Lion.new&lt;br /&gt;
    jack = Person.new&lt;br /&gt;
    quality leo&lt;br /&gt;
    quality jack&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   start&lt;br /&gt;
&lt;br /&gt;
In the above example, leo is an object of class “Lion” and the “roar” method defined in its class is invoked. jack is an object of class “Person” and the corresponding method defined in the Person class is called for him. Although in the real world, both objects are not of the same type, Ruby allows unbounded polymorphism as both of them implement a particular aspect.&lt;br /&gt;
&lt;br /&gt;
Thus, the check for an invoked method is done at run-time in dynamically typed languages and the method defined on that object type is called. If the method is undefined, a “NoMethodError” exception is raised.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Python===&lt;br /&gt;
&lt;br /&gt;
Python also heavily uses duck typing to allow flexibility to programmers. It emphasizes on the interface rather than on the specifc type which allows for polymorphic substitution [link].&lt;br /&gt;
In Python, functions like type() or isistance() is used to check for usual methods to match the type of find out if an object is an instance of a particular class. However, in case of duck typing, the focus is on the attribute of that object, so it checks for the presence of a particular attribute in that object using hasattr().&lt;br /&gt;
&lt;br /&gt;
Example for duck typing in Python is where Python uses a “pickle” module to serialize and de-serialize an object. So, we can introduce a new class that creates a “Tape” object. Now, an object of this type can be passed to the pickle module. Python will not differentiate between a file object or a tape object as long as they both have read() and write() methods.&lt;br /&gt;
&lt;br /&gt;
   class Tapefile():&lt;br /&gt;
      “A tape file”&lt;br /&gt;
       def __init__(self,params):&lt;br /&gt;
          #various initialization for tape access&lt;br /&gt;
       pass&lt;br /&gt;
       def read(len):&lt;br /&gt;
          #use special methods to access tape&lt;br /&gt;
       pass&lt;br /&gt;
       def readline():&lt;br /&gt;
          #use special methods to read a whole line on tape&lt;br /&gt;
       pass&lt;br /&gt;
       def write(value):&lt;br /&gt;
          #use special methods to write on tape&lt;br /&gt;
       pass&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in other languages===&lt;br /&gt;
&lt;br /&gt;
Duck typing is considered to be a feature provided by dynamic languages. C++ as a static language checks for methods by parameter and signature types. Java does not support duck typing either but it is argued that it can be simulated by the use of interfaces. The concept of duck-typing is criticized as it requires the programmer to have a very wide understanding of the source code and that two objects may implement a method in completely different contexts and so it may result in strange errors. &lt;br /&gt;
&lt;br /&gt;
Although duck typing is considered to be a feature of dynamic languages, some static languages like C# and Boo support it by allowing type checking of classes by the compiler to occur at run time.&lt;/div&gt;</summary>
		<author><name>Sthirum</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53064</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b ms</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=53064"/>
		<updated>2011-10-20T16:01:25Z</updated>

		<summary type="html">&lt;p&gt;Sthirum: /* Duck Typing in Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Lecture 5: Object Oriented Programming in Ruby =&lt;br /&gt;
&lt;br /&gt;
This wiki tries to cover the Object Oriented concepts in ruby.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Object oriented languages: An Overview ==&lt;br /&gt;
&lt;br /&gt;
==== C++: ====&lt;br /&gt;
C++ is a statically typed, multi-paradigm programming language. It supports some features of object-oriented languages like encapsulation, polymorphism and inheritance. It provides access control by specifiers public, private and protected and also allows “friend” classes. C++  allows multiple inheritance which is eliminated in most other programming languages as it allows a class to inherit from more than one base class which results in an ambiguity effect.&lt;br /&gt;
&lt;br /&gt;
==== Java: ====&lt;br /&gt;
Java is classified as a statically typed, hybrid object oriented language. Although it provides information hiding, inheritance and polymorphism, it has a few basic types incorporated that are not objects and some of the built-in operators are provided by basic arithmetic and not as messages to objects. [link]. For these reasons, it is not considered to be a “pure” object-oriented language.&lt;br /&gt;
&lt;br /&gt;
Java provides access control and supports private, public and protected access. It also allows to create packages to create and maintain namespaces. Java is designed not to provide multiple inheritance to avoid problems faced in C++. However, it presents a single-rooted hierarchy where “Object” is the ancestor of all classes (seen in Ruby too) and provides interfaces to extend/inherit functionality.&lt;br /&gt;
&lt;br /&gt;
==== Python: ====&lt;br /&gt;
Python is a dynamically typed, interpreted, object oriented language. It is argued that Python is not completely object oriented as it does not provide access control.&lt;br /&gt;
&lt;br /&gt;
==== Ruby: ====&lt;br /&gt;
Ruby is dynamically typed and highly object-oriented, much more than Java and Python. In Ruby, it is said that “everything is an object”.  Every value happens to be an object in Ruby. You may call methods on a string, an integer just like you call on any object in Java. For ex,&lt;br /&gt;
&lt;br /&gt;
     (2.1143).size.times{puts “Hello!”}&lt;br /&gt;
&lt;br /&gt;
This example will display “Hello!” six times on your IRB as the size of the parameter &amp;quot;2.1143&amp;quot; is 6.&lt;br /&gt;
&lt;br /&gt;
== Comparison of object Oriented Languages ==&lt;br /&gt;
The following table compares the Properties of some of the well-known Object Oriented Languages.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 9pad case0%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Language&lt;br /&gt;
!Ruby&lt;br /&gt;
!Java&lt;br /&gt;
!C#&lt;br /&gt;
!C++&lt;br /&gt;
!Python&lt;br /&gt;
!SmallTalk&lt;br /&gt;
!Perl&lt;br /&gt;
|-&lt;br /&gt;
!Object Orientation&lt;br /&gt;
|Pure&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid/Multi-Paradigm&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Pure&lt;br /&gt;
|Add-on/Hybrid&lt;br /&gt;
|-&lt;br /&gt;
!Static/Dynamic Typing&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|-&lt;br /&gt;
!Generic Classes&lt;br /&gt;
|N/A&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|-&lt;br /&gt;
!Inheritance&lt;br /&gt;
|Single Class/Multiple Mixins&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Multiple&lt;br /&gt;
|Multiple&lt;br /&gt;
|Single&lt;br /&gt;
|Multiple&lt;br /&gt;
|- &lt;br /&gt;
!Feature Renaming&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|NO&lt;br /&gt;
|-&lt;br /&gt;
!Method Overloading&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Operator Overloading&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|-&lt;br /&gt;
!Class Variables or methods&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Access Control&lt;br /&gt;
|Public, Protected, Private&lt;br /&gt;
|Public, Protected, Package, Private&lt;br /&gt;
|Public, Protected, Private, Internal, Protected Internal&lt;br /&gt;
|Public, Protected, Private, Friends&lt;br /&gt;
|Name Mangling&lt;br /&gt;
|Protected Data, Public Methods&lt;br /&gt;
|None&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Ruby : Purely Object Oriented ==&lt;br /&gt;
&lt;br /&gt;
Ruby is considered to be a purely Object oriented Language, as everything in it is considered to be an object. Unlike Java, even primitives such as characters, punctuation, numbers and strings are treated as objects in Ruby. Ruby was designed to facilitate the enforcement of Object Oriented Methods. Ruby is a Dynamically Typed Language.&lt;br /&gt;
&lt;br /&gt;
=== Object oriented Concepts in ruby: ===&lt;br /&gt;
&lt;br /&gt;
Ruby is a singly rooted Object Oriented Programming language which has all its classes having the same superclass which is the class Object.&lt;br /&gt;
&lt;br /&gt;
==== Classes:====&lt;br /&gt;
[[Image:RubyClass.png|thumb|right|280px|'''''Classes in Ruby''''']]&lt;br /&gt;
&lt;br /&gt;
A class is used to define a blueprint of a data type. It does not contain any data, but it specifies what kind of variables and methods an object of the class will have.&lt;br /&gt;
A class in Ruby is defined using the keyword class followed by the classname. The variables and methods are defined within the class definition and the class definition terminates with the end keyword.&lt;br /&gt;
&lt;br /&gt;
For ex:&lt;br /&gt;
  class Rectangle&lt;br /&gt;
     def initialize(w,l)&lt;br /&gt;
        @width, @length = w, l&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
Here, the initialize method is used to set the values of the instance variables (@width and @length. The instance variables are the properties of the objects created of this class type. These instance variables can be accessed within the class using an @. These variables can be accessed from outside the class using the accessor methods which are defined inside the class.&lt;br /&gt;
&lt;br /&gt;
In the following case printLength and printWidth are public methods used to access the instance variables from outside the class. These are called accessor methods.&lt;br /&gt;
Similarly, to&lt;br /&gt;
&lt;br /&gt;
   class Rectangle&lt;br /&gt;
  # constructor method&lt;br /&gt;
  def initialize(w,l)&lt;br /&gt;
     @width, @length = w, l&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # accessor methods&lt;br /&gt;
  def printWidth&lt;br /&gt;
     @width&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def printLength&lt;br /&gt;
     @length&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # setter methods&lt;br /&gt;
  def setWidth=(value)&lt;br /&gt;
     @width = value&lt;br /&gt;
  end&lt;br /&gt;
  def setHeight=(value)&lt;br /&gt;
     @height = value&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   # create an object&lt;br /&gt;
     rectagle = Rectangle.new(10, 20)&lt;br /&gt;
&lt;br /&gt;
    # use accessor methods&lt;br /&gt;
      x = rectangle.printWidth()&lt;br /&gt;
      y = rectangle.printLength()&lt;br /&gt;
&lt;br /&gt;
      puts &amp;quot;Width of the Rectangle is : #{x}&amp;quot;&lt;br /&gt;
      puts &amp;quot;Length of the Rectangle is : #{y}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==== Class Methods and Class Variables:====&lt;br /&gt;
A class variable is shared between all instances of the class. The value of the class variable remains the same for all the objects of the class. A class variable is accessed within the class using two @ signs at the beginning of the variable name. The class variables must be initialized within the class.&lt;br /&gt;
&lt;br /&gt;
The class methods are defined using def self.methodname() and ends with an end delimiter. These methods are called using the class name itself, for example: classname.methodname&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Attributes and accessor methods: ===&lt;br /&gt;
&lt;br /&gt;
Every class in Ruby defines a set of attributes for its objects. Ruby provides accessor methods (famously known as getter-setter methods in Java) to access these variables. Ruby provides three kinds of accessor methods:&lt;br /&gt;
&lt;br /&gt;
    Class Car&lt;br /&gt;
       attr_accessor : name, company&lt;br /&gt;
       attr_reader: price&lt;br /&gt;
       attr_writer: color&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
attr_accessor creates a setter and getter method. The attr_reader method creates only the getter method. This is required in case of attributes that are not to be modified by other programs but their value may be viewed. The attr_writer creates the setter method. In case of Java, the setter and getter methods need to be written in 3 lines of code each whereas in Ruby, the methods are written just by specifying the type of accessor, which have already been coded using Metaprogramming.[link]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Access Control:====&lt;br /&gt;
The only easy way to change an object's state in Ruby is by calling one of its methods. Control access to the methods, and you have controlled access to the object. A good rule of the thumb is never to expose methods that could leave an object in an invalid state.&lt;br /&gt;
&lt;br /&gt;
Ruby gives you three levels of protection:&lt;br /&gt;
&lt;br /&gt;
''Public methods'' can be called by everyone - no access control is enforced. A class's instance methods (these do not belong only to one object; instead, every instance of the class can call them) are public by default; anyone can call them. The initialize method is always private.&lt;br /&gt;
&lt;br /&gt;
''Protected methods'' can be invoked only by objects of the defining class and its subclasses. Access is kept within the family. However, usage of protected is limited.&lt;br /&gt;
&lt;br /&gt;
''Private methods'' cannot be called with an explicit receiver - the receiver is always self. This means that private methods can be called only in the context of the current object; you cannot invoke another object's private methods.&lt;br /&gt;
&lt;br /&gt;
Access control is determined dynamically, as the program runs, not statically. You will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
&lt;br /&gt;
====Abstract methods====&lt;br /&gt;
&lt;br /&gt;
Ruby is a dynamically typed language and does not provide abstract classes as in Java or C++. It instead provides modules that can be included within any class. The reason why Ruby does not provide abstract classes is because of the idea that it only matters if two objects can perform a function, they don’t necessarily have to be inherited from the same class. This paradigm is provided by modules, where the method of a module can be redefined as required by a class that includes it.&lt;br /&gt;
&lt;br /&gt;
Also, if required the technique of abstract class can be simulated by something like this:&lt;br /&gt;
&lt;br /&gt;
  Class Vehicle&lt;br /&gt;
   def drive&lt;br /&gt;
   raise NotImplementedError.new(&amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The subclasses that inherit from the above class will have to redefine the method “drive” as required.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Ruby ===&lt;br /&gt;
&lt;br /&gt;
Ruby provides unbounded polymorphism as a dynamically typed language through the concept of “Duck-typing”. “Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface” [link]. This concept is explained by the famous definition as “When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.” This essentially means that in languages like Ruby, it is only checked if an object can implement a function and the type of the object is not taken into consideration. It can be explained better with the help of the following example&lt;br /&gt;
&lt;br /&gt;
  class Lion&lt;br /&gt;
    def roar&lt;br /&gt;
        puts “The Lion roars when hungry”&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
     def roar&lt;br /&gt;
        puts “The man roared in fury and anger”&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   def quality lion&lt;br /&gt;
    lion.roar&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def start&lt;br /&gt;
    leo = Lion.new&lt;br /&gt;
    jack = Person.new&lt;br /&gt;
    quality leo&lt;br /&gt;
    quality jack&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   start&lt;br /&gt;
&lt;br /&gt;
In the above example, leo is an object of class “Lion” and the “roar” method defined in its class is invoked. jack is an object of class “Person” and the corresponding method defined in the Person class is called for him. Although in the real world, both objects are not of the same type, Ruby allows unbounded polymorphism as both of them implement a particular aspect.&lt;br /&gt;
&lt;br /&gt;
Thus, the check for an invoked method is done at run-time in dynamically typed languages and the method defined on that object type is called. If the method is undefined, a “NoMethodError” exception is raised.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Python===&lt;br /&gt;
&lt;br /&gt;
Python also heavily uses duck typing to allow flexibility to programmers. It emphasizes on the interface rather than on the specifc type which allows for polymorphic substitution [link].&lt;br /&gt;
In Python, functions like type() or isistance() is used to check for usual methods to match the type of find out if an object is an instance of a particular class. However, in case of duck typing, the focus is on the attribute of that object, so it checks for the presence of a particular attribute in that object using hasattr().&lt;br /&gt;
&lt;br /&gt;
Example for duck typing in Python is where Python uses a “pickle” module to serialize and de-serialize an object. So, we can introduce a new class that creates a “Tape” object. Now, an object of this type can be passed to the pickle module. Python will not differentiate between a file object or a tape object as long as they both have read() and write() methods.&lt;br /&gt;
&lt;br /&gt;
   class Tapefile():&lt;br /&gt;
      “A tape file”&lt;br /&gt;
       def __init__(self,params):&lt;br /&gt;
          #various initialization for tape access&lt;br /&gt;
       pass&lt;br /&gt;
       def read(len):&lt;br /&gt;
          #use special methods to access tape&lt;br /&gt;
       pass&lt;br /&gt;
       def readline():&lt;br /&gt;
          #use special methods to read a whole line on tape&lt;br /&gt;
       pass&lt;br /&gt;
       def write(value):&lt;br /&gt;
          #use special methods to write on tape&lt;br /&gt;
       pass&lt;/div&gt;</summary>
		<author><name>Sthirum</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=52501</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b ms</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=52501"/>
		<updated>2011-10-18T23:36:47Z</updated>

		<summary type="html">&lt;p&gt;Sthirum: /* Attributes and accessor methods: */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Lecture 5: Object Oriented Programming in Ruby =&lt;br /&gt;
&lt;br /&gt;
This wiki tries to cover the Object Oriented concepts in ruby.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Object oriented languages: An Overview ==&lt;br /&gt;
&lt;br /&gt;
==== C++: ====&lt;br /&gt;
C++ is a statically typed, multi-paradigm programming language. It supports some features of object-oriented languages like encapsulation, polymorphism and inheritance. It provides access control by specifiers public, private and protected and also allows “friend” classes. C++  allows multiple inheritance which is eliminated in most other programming languages as it allows a class to inherit from more than one base class which results in an ambiguity effect.&lt;br /&gt;
&lt;br /&gt;
==== Java: ====&lt;br /&gt;
Java is classified as a statically typed, hybrid object oriented language. Although it provides information hiding, inheritance and polymorphism, it has a few basic types incorporated that are not objects and some of the built-in operators are provided by basic arithmetic and not as messages to objects. [link]. For these reasons, it is not considered to be a “pure” object-oriented language.&lt;br /&gt;
&lt;br /&gt;
Java provides access control and supports private, public and protected access. It also allows to create packages to create and maintain namespaces. Java is designed not to provide multiple inheritance to avoid problems faced in C++. However, it presents a single-rooted hierarchy where “Object” is the ancestor of all classes (seen in Ruby too) and provides interfaces to extend/inherit functionality.&lt;br /&gt;
&lt;br /&gt;
==== Python: ====&lt;br /&gt;
Python is a dynamically typed, interpreted, object oriented language. It is argued that Python is not completely object oriented as it does not provide access control.&lt;br /&gt;
&lt;br /&gt;
==== Ruby: ====&lt;br /&gt;
Ruby is dynamically typed and highly object-oriented, much more than Java and Python. In Ruby, it is said that “everything is an object”.  Every value happens to be an object in Ruby. You may call methods on a string, an integer just like you call on any object in Java. For ex,&lt;br /&gt;
&lt;br /&gt;
     (2.1143).size.times{puts “Hello!”}&lt;br /&gt;
&lt;br /&gt;
This example will display “Hello!” six times on your IRB as the size of the parameter &amp;quot;2.1143&amp;quot; is 6.&lt;br /&gt;
&lt;br /&gt;
== Comparison of object Oriented Languages ==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 9pad case0%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Language&lt;br /&gt;
!Ruby&lt;br /&gt;
!Java&lt;br /&gt;
!C#&lt;br /&gt;
!C++&lt;br /&gt;
!Python&lt;br /&gt;
!SmallTalk&lt;br /&gt;
!Perl&lt;br /&gt;
|-&lt;br /&gt;
!Object Orientation&lt;br /&gt;
|Pure&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid/Multi-Paradigm&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Pure&lt;br /&gt;
|Add-on/Hybrid&lt;br /&gt;
|-&lt;br /&gt;
!Static/Dynamic Typing&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|-&lt;br /&gt;
!Generic Classes&lt;br /&gt;
|N/A&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|-&lt;br /&gt;
!Inheritance&lt;br /&gt;
|Single Class/Multiple Mixins&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Multiple&lt;br /&gt;
|Multiple&lt;br /&gt;
|Single&lt;br /&gt;
|Multiple&lt;br /&gt;
|- &lt;br /&gt;
!Feature Renaming&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|NO&lt;br /&gt;
|-&lt;br /&gt;
!Method Overloading&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Operator Overloading&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|-&lt;br /&gt;
!Class Variables or methods&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Access Control&lt;br /&gt;
|Public, Protected, Private&lt;br /&gt;
|Public, Protected, Package, Private&lt;br /&gt;
|Public, Protected, Private, Internal, Protected Internal&lt;br /&gt;
|Public, Protected, Private, Friends&lt;br /&gt;
|Name Mangling&lt;br /&gt;
|Protected Data, Public Methods&lt;br /&gt;
|None&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
== Ruby : Purely Object Oriented ==&lt;br /&gt;
&lt;br /&gt;
Ruby is considered to be a purely Object oriented Language, as everything in it is considered to be an object. Unlike Java, even primitives such as characters, punctuation, numbers and strings are treated as objects in Ruby. Ruby was designed to facilitate the enforcement of Object Oriented Methods. Ruby is a Dynamically Typed Language.&lt;br /&gt;
&lt;br /&gt;
=== Object oriented Concepts in ruby: ===&lt;br /&gt;
&lt;br /&gt;
Ruby is a singly rooted Object Oriented Programming language which has all its classes having the same superclass which is the class Object.&lt;br /&gt;
&lt;br /&gt;
==== Classes:====&lt;br /&gt;
&lt;br /&gt;
A class is used to define a blueprint of a data type. It does not contain any data, but it specifies what kind of variables and methods an object of the class will have.&lt;br /&gt;
A class in Ruby is defined using the keyword class followed by the classname. The variables and methods are defined within the class definition and the class definition terminates with the end keyword.&lt;br /&gt;
&lt;br /&gt;
For ex:&lt;br /&gt;
  class Rectangle&lt;br /&gt;
     def initialize(w,l)&lt;br /&gt;
        @width, @length = w, l&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
Here, the initialize method is used to set the values of the instance variables (@width and @length. The instance variables are the properties of the objects created of this class type. These instance variables can be accessed within the class using an @. These variables can be accessed from outside the class using the accessor methods which are defined inside the class.&lt;br /&gt;
&lt;br /&gt;
In the following case printLength and printWidth are public methods used to access the instance variables from outside the class. These are called accessor methods.&lt;br /&gt;
Similarly, to&lt;br /&gt;
&lt;br /&gt;
   class Rectangle&lt;br /&gt;
  # constructor method&lt;br /&gt;
  def initialize(w,l)&lt;br /&gt;
     @width, @length = w, l&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # accessor methods&lt;br /&gt;
  def printWidth&lt;br /&gt;
     @width&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def printLength&lt;br /&gt;
     @length&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # setter methods&lt;br /&gt;
  def setWidth=(value)&lt;br /&gt;
     @width = value&lt;br /&gt;
  end&lt;br /&gt;
  def setHeight=(value)&lt;br /&gt;
     @height = value&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   # create an object&lt;br /&gt;
     rectagle = Rectangle.new(10, 20)&lt;br /&gt;
&lt;br /&gt;
    # use accessor methods&lt;br /&gt;
      x = rectangle.printWidth()&lt;br /&gt;
      y = rectangle.printLength()&lt;br /&gt;
&lt;br /&gt;
      puts &amp;quot;Width of the Rectangle is : #{x}&amp;quot;&lt;br /&gt;
      puts &amp;quot;Length of the Rectangle is : #{y}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==== Class Methods and Class Variables:====&lt;br /&gt;
A class variable is shared between all instances of the class. The value of the class variable remains the same for all the objects of the class. A class variable is accessed within the class using two @ signs at the beginning of the variable name. The class variables must be initialized within the class.&lt;br /&gt;
&lt;br /&gt;
The class methods are defined using def self.methodname() and ends with an end delimiter. These methods are called using the class name itself, for example: classname.methodname&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Attributes and accessor methods: ===&lt;br /&gt;
&lt;br /&gt;
Every class in Ruby defines a set of attributes for its objects. Ruby provides accessor methods (famously known as getter-setter methods in Java) to access these variables. Ruby provides three kinds of accessor methods:&lt;br /&gt;
&lt;br /&gt;
    Class Car&lt;br /&gt;
       attr_accessor : name, company&lt;br /&gt;
       attr_reader: price&lt;br /&gt;
       attr_writer: color&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
attr_accessor creates a setter and getter method. The attr_reader method creates only the getter method. This is required in case of attributes that are not to be modified by other programs but their value may be viewed. The attr_writer creates the setter method. In case of Java, the setter and getter methods need to be written in 3 lines of code each whereas in Ruby, the methods are written just by specifying the type of accessor, which have already been coded using Metaprogramming.[link]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Access Control:=====&lt;br /&gt;
Just like any other o-o programming language, Ruby allows to exercise control over the attributes and methods of a class. Ruby provides 3 levels of protection at instance methods level.&lt;br /&gt;
Public:&lt;br /&gt;
In this level objects from outside the class can call the method. All methods except initialize are public by default.&lt;br /&gt;
Private:&lt;br /&gt;
These methods can be call only by the objects of the same class. Initialize methods are private by default.&lt;br /&gt;
Protected:&lt;br /&gt;
These methods can be called either by objects of the same class or of the class derived from this class.&lt;br /&gt;
&lt;br /&gt;
The methods are given access levels using the following syntax.&lt;br /&gt;
Private :method1, :method2&lt;br /&gt;
&lt;br /&gt;
====Abstract methods====&lt;br /&gt;
&lt;br /&gt;
Ruby is a dynamically typed language and does not provide abstract classes as in Java or C++. It instead provides modules that can be included within any class. The reason why Ruby does not provide abstract classes is because of the idea that it only matters if two objects can perform a function, they don’t necessarily have to be inherited from the same class. This paradigm is provided by modules, where the method of a module can be redefined as required by a class that includes it.&lt;br /&gt;
&lt;br /&gt;
Also, if required the technique of abstract class can be simulated by something like this:&lt;br /&gt;
&lt;br /&gt;
  Class Vehicle&lt;br /&gt;
   def drive&lt;br /&gt;
   raise NotImplementedError.new(&amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The subclasses that inherit from the above class will have to redefine the method “drive” as required.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Ruby ===&lt;br /&gt;
&lt;br /&gt;
Ruby provides unbounded polymorphism as a dynamically typed language through the concept of “Duck-typing”. “Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface” [link]. This concept is explained by the famous definition as “When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.” This essentially means that in languages like Ruby, it is only checked if an object can implement a function and the type of the object is not taken into consideration. It can be explained better with the help of the following example&lt;br /&gt;
&lt;br /&gt;
  class Lion&lt;br /&gt;
    def roar&lt;br /&gt;
        puts “The Lion roars when hungry”&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
     def roar&lt;br /&gt;
        puts “The man roared in fury and anger”&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   def quality lion&lt;br /&gt;
    lion.roar&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def start&lt;br /&gt;
    leo = Lion.new&lt;br /&gt;
    jack = Person.new&lt;br /&gt;
    quality leo&lt;br /&gt;
    quality jack&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   start&lt;br /&gt;
&lt;br /&gt;
In the above example, leo is an object of class “Lion” and the “roar” method defined in its class is invoked. jack is an object of class “Person” and the corresponding method defined in the Person class is called for him. Although in the real world, both objects are not of the same type, Ruby allows unbounded polymorphism as both of them implement a particular aspect.&lt;br /&gt;
&lt;br /&gt;
Thus, the check for an invoked method is done at run-time in dynamically typed languages and the method defined on that object type is called. If the method is undefined, a “NoMethodError” exception is raised.&lt;/div&gt;</summary>
		<author><name>Sthirum</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=52500</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b ms</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=52500"/>
		<updated>2011-10-18T23:33:45Z</updated>

		<summary type="html">&lt;p&gt;Sthirum: /* Classes: */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Lecture 5: Object Oriented Programming in Ruby =&lt;br /&gt;
&lt;br /&gt;
This wiki tries to cover the Object Oriented concepts in ruby.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Object oriented languages: An Overview ==&lt;br /&gt;
&lt;br /&gt;
==== C++: ====&lt;br /&gt;
C++ is a statically typed, multi-paradigm programming language. It supports some features of object-oriented languages like encapsulation, polymorphism and inheritance. It provides access control by specifiers public, private and protected and also allows “friend” classes. C++  allows multiple inheritance which is eliminated in most other programming languages as it allows a class to inherit from more than one base class which results in an ambiguity effect.&lt;br /&gt;
&lt;br /&gt;
==== Java: ====&lt;br /&gt;
Java is classified as a statically typed, hybrid object oriented language. Although it provides information hiding, inheritance and polymorphism, it has a few basic types incorporated that are not objects and some of the built-in operators are provided by basic arithmetic and not as messages to objects. [link]. For these reasons, it is not considered to be a “pure” object-oriented language.&lt;br /&gt;
&lt;br /&gt;
Java provides access control and supports private, public and protected access. It also allows to create packages to create and maintain namespaces. Java is designed not to provide multiple inheritance to avoid problems faced in C++. However, it presents a single-rooted hierarchy where “Object” is the ancestor of all classes (seen in Ruby too) and provides interfaces to extend/inherit functionality.&lt;br /&gt;
&lt;br /&gt;
==== Python: ====&lt;br /&gt;
Python is a dynamically typed, interpreted, object oriented language. It is argued that Python is not completely object oriented as it does not provide access control.&lt;br /&gt;
&lt;br /&gt;
==== Ruby: ====&lt;br /&gt;
Ruby is dynamically typed and highly object-oriented, much more than Java and Python. In Ruby, it is said that “everything is an object”.  Every value happens to be an object in Ruby. You may call methods on a string, an integer just like you call on any object in Java. For ex,&lt;br /&gt;
&lt;br /&gt;
     (2.1143).size.times{puts “Hello!”}&lt;br /&gt;
&lt;br /&gt;
This example will display “Hello!” six times on your IRB as the size of the parameter &amp;quot;2.1143&amp;quot; is 6.&lt;br /&gt;
&lt;br /&gt;
== Comparison of object Oriented Languages ==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 9pad case0%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Language&lt;br /&gt;
!Ruby&lt;br /&gt;
!Java&lt;br /&gt;
!C#&lt;br /&gt;
!C++&lt;br /&gt;
!Python&lt;br /&gt;
!SmallTalk&lt;br /&gt;
!Perl&lt;br /&gt;
|-&lt;br /&gt;
!Object Orientation&lt;br /&gt;
|Pure&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid/Multi-Paradigm&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Pure&lt;br /&gt;
|Add-on/Hybrid&lt;br /&gt;
|-&lt;br /&gt;
!Static/Dynamic Typing&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|-&lt;br /&gt;
!Generic Classes&lt;br /&gt;
|N/A&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|-&lt;br /&gt;
!Inheritance&lt;br /&gt;
|Single Class/Multiple Mixins&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Multiple&lt;br /&gt;
|Multiple&lt;br /&gt;
|Single&lt;br /&gt;
|Multiple&lt;br /&gt;
|- &lt;br /&gt;
!Feature Renaming&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|NO&lt;br /&gt;
|-&lt;br /&gt;
!Method Overloading&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Operator Overloading&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|-&lt;br /&gt;
!Class Variables or methods&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Access Control&lt;br /&gt;
|Public, Protected, Private&lt;br /&gt;
|Public, Protected, Package, Private&lt;br /&gt;
|Public, Protected, Private, Internal, Protected Internal&lt;br /&gt;
|Public, Protected, Private, Friends&lt;br /&gt;
|Name Mangling&lt;br /&gt;
|Protected Data, Public Methods&lt;br /&gt;
|None&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
== Ruby : Purely Object Oriented ==&lt;br /&gt;
&lt;br /&gt;
Ruby is considered to be a purely Object oriented Language, as everything in it is considered to be an object. Unlike Java, even primitives such as characters, punctuation, numbers and strings are treated as objects in Ruby. Ruby was designed to facilitate the enforcement of Object Oriented Methods. Ruby is a Dynamically Typed Language.&lt;br /&gt;
&lt;br /&gt;
=== Object oriented Concepts in ruby: ===&lt;br /&gt;
&lt;br /&gt;
Ruby is a singly rooted Object Oriented Programming language which has all its classes having the same superclass which is the class Object.&lt;br /&gt;
&lt;br /&gt;
==== Classes:====&lt;br /&gt;
&lt;br /&gt;
A class is used to define a blueprint of a data type. It does not contain any data, but it specifies what kind of variables and methods an object of the class will have.&lt;br /&gt;
A class in Ruby is defined using the keyword class followed by the classname. The variables and methods are defined within the class definition and the class definition terminates with the end keyword.&lt;br /&gt;
&lt;br /&gt;
For ex:&lt;br /&gt;
  class Rectangle&lt;br /&gt;
     def initialize(w,l)&lt;br /&gt;
        @width, @length = w, l&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
Here, the initialize method is used to set the values of the instance variables (@width and @length. The instance variables are the properties of the objects created of this class type. These instance variables can be accessed within the class using an @. These variables can be accessed from outside the class using the accessor methods which are defined inside the class.&lt;br /&gt;
&lt;br /&gt;
In the following case printLength and printWidth are public methods used to access the instance variables from outside the class. These are called accessor methods.&lt;br /&gt;
Similarly, to&lt;br /&gt;
&lt;br /&gt;
   class Rectangle&lt;br /&gt;
  # constructor method&lt;br /&gt;
  def initialize(w,l)&lt;br /&gt;
     @width, @length = w, l&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # accessor methods&lt;br /&gt;
  def printWidth&lt;br /&gt;
     @width&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def printLength&lt;br /&gt;
     @length&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # setter methods&lt;br /&gt;
  def setWidth=(value)&lt;br /&gt;
     @width = value&lt;br /&gt;
  end&lt;br /&gt;
  def setHeight=(value)&lt;br /&gt;
     @height = value&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   # create an object&lt;br /&gt;
     rectagle = Rectangle.new(10, 20)&lt;br /&gt;
&lt;br /&gt;
    # use accessor methods&lt;br /&gt;
      x = rectangle.printWidth()&lt;br /&gt;
      y = rectangle.printLength()&lt;br /&gt;
&lt;br /&gt;
      puts &amp;quot;Width of the Rectangle is : #{x}&amp;quot;&lt;br /&gt;
      puts &amp;quot;Length of the Rectangle is : #{y}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==== Class Methods and Class Variables:====&lt;br /&gt;
A class variable is shared between all instances of the class. The value of the class variable remains the same for all the objects of the class. A class variable is accessed within the class using two @ signs at the beginning of the variable name. The class variables must be initialized within the class.&lt;br /&gt;
&lt;br /&gt;
The class methods are defined using def self.methodname() and ends with an end delimiter. These methods are called using the class name itself, for example: classname.methodname&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Attributes and accessor methods: ===&lt;br /&gt;
&lt;br /&gt;
Every class in Ruby defines a set of attributes for its objects. Ruby provides accessor methods (famously known as getter-setter methods in Java) to access these variables. Ruby provides three kinds of accessor methods:&lt;br /&gt;
&lt;br /&gt;
Class Car&lt;br /&gt;
    attr_accessor : name, company&lt;br /&gt;
    attr_reader: price&lt;br /&gt;
    attr_writer: color&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
attr_accessor creates a setter and getter method. The attr_reader method creates only the getter method. This is required in case of attributes that are not to be modified by other programs but their value may be viewed. The attr_writer creates the setter method. In case of Java, the setter and getter methods need to be written in 3 lines of code each whereas in Ruby, the methods are written just by specifying the type of accessor, which have already been coded using Metaprogramming.[link]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Access Control:=====&lt;br /&gt;
Just like any other o-o programming language, Ruby allows to exercise control over the attributes and methods of a class. Ruby provides 3 levels of protection at instance methods level.&lt;br /&gt;
Public:&lt;br /&gt;
In this level objects from outside the class can call the method. All methods except initialize are public by default.&lt;br /&gt;
Private:&lt;br /&gt;
These methods can be call only by the objects of the same class. Initialize methods are private by default.&lt;br /&gt;
Protected:&lt;br /&gt;
These methods can be called either by objects of the same class or of the class derived from this class.&lt;br /&gt;
&lt;br /&gt;
The methods are given access levels using the following syntax.&lt;br /&gt;
Private :method1, :method2&lt;br /&gt;
&lt;br /&gt;
====Abstract methods====&lt;br /&gt;
&lt;br /&gt;
Ruby is a dynamically typed language and does not provide abstract classes as in Java or C++. It instead provides modules that can be included within any class. The reason why Ruby does not provide abstract classes is because of the idea that it only matters if two objects can perform a function, they don’t necessarily have to be inherited from the same class. This paradigm is provided by modules, where the method of a module can be redefined as required by a class that includes it.&lt;br /&gt;
&lt;br /&gt;
Also, if required the technique of abstract class can be simulated by something like this:&lt;br /&gt;
&lt;br /&gt;
  Class Vehicle&lt;br /&gt;
   def drive&lt;br /&gt;
   raise NotImplementedError.new(&amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The subclasses that inherit from the above class will have to redefine the method “drive” as required.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Ruby ===&lt;br /&gt;
&lt;br /&gt;
Ruby provides unbounded polymorphism as a dynamically typed language through the concept of “Duck-typing”. “Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface” [link]. This concept is explained by the famous definition as “When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.” This essentially means that in languages like Ruby, it is only checked if an object can implement a function and the type of the object is not taken into consideration. It can be explained better with the help of the following example&lt;br /&gt;
&lt;br /&gt;
  class Lion&lt;br /&gt;
    def roar&lt;br /&gt;
        puts “The Lion roars when hungry”&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
     def roar&lt;br /&gt;
        puts “The man roared in fury and anger”&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   def quality lion&lt;br /&gt;
    lion.roar&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def start&lt;br /&gt;
    leo = Lion.new&lt;br /&gt;
    jack = Person.new&lt;br /&gt;
    quality leo&lt;br /&gt;
    quality jack&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   start&lt;br /&gt;
&lt;br /&gt;
In the above example, leo is an object of class “Lion” and the “roar” method defined in its class is invoked. jack is an object of class “Person” and the corresponding method defined in the Person class is called for him. Although in the real world, both objects are not of the same type, Ruby allows unbounded polymorphism as both of them implement a particular aspect.&lt;br /&gt;
&lt;br /&gt;
Thus, the check for an invoked method is done at run-time in dynamically typed languages and the method defined on that object type is called. If the method is undefined, a “NoMethodError” exception is raised.&lt;/div&gt;</summary>
		<author><name>Sthirum</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=52460</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b ms</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_ms&amp;diff=52460"/>
		<updated>2011-10-18T17:49:39Z</updated>

		<summary type="html">&lt;p&gt;Sthirum: /* Ruby: */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Lecture 5: Object Oriented Programming in Ruby =&lt;br /&gt;
&lt;br /&gt;
This wiki tries to cover the Object Oriented concepts in ruby.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Object oriented languages: An Overview ==&lt;br /&gt;
&lt;br /&gt;
==== C++: ====&lt;br /&gt;
C++ is a statically typed, multi-paradigm programming language. It supports some features of object-oriented languages like encapsulation, polymorphism and inheritance. It provides access control by specifiers public, private and protected and also allows “friend” classes. C++  allows multiple inheritance which is eliminated in most other programming languages as it allows a class to inherit from more than one base class which results in an ambiguity effect.&lt;br /&gt;
&lt;br /&gt;
==== Java: ====&lt;br /&gt;
Java is classified as a statically typed, hybrid object oriented language. Although it provides information hiding, inheritance and polymorphism, it has a few basic types incorporated that are not objects and some of the built-in operators are provided by basic arithmetic and not as messages to objects. [link]. For these reasons, it is not considered to be a “pure” object-oriented language.&lt;br /&gt;
&lt;br /&gt;
Java provides access control and supports private, public and protected access. It also allows to create packages to create and maintain namespaces. Java is designed not to provide multiple inheritance to avoid problems faced in C++. However, it presents a single-rooted hierarchy where “Object” is the ancestor of all classes (seen in Ruby too) and provides interfaces to extend/inherit functionality.&lt;br /&gt;
&lt;br /&gt;
==== Python: ====&lt;br /&gt;
Python is a dynamically typed, interpreted, object oriented language. It is argued that Python is not completely object oriented as it does not provide access control.&lt;br /&gt;
&lt;br /&gt;
==== Ruby: ====&lt;br /&gt;
Ruby is dynamically typed and highly object-oriented, much more than Java and Python. In Ruby, it is said that “everything is an object”.  Every value happens to be an object in Ruby. You may call methods on a string, an integer just like you call on any object in Java. For ex,&lt;br /&gt;
&lt;br /&gt;
     (2.1143).size.times{puts “Hello!”}&lt;br /&gt;
&lt;br /&gt;
This example will display “Hello!” six times on your IRB as the size of the parameter &amp;quot;2.1143&amp;quot; is 6.&lt;br /&gt;
&lt;br /&gt;
== Comparison of object Oriented Languages ==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 9pad case0%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Language&lt;br /&gt;
!Ruby&lt;br /&gt;
!Java&lt;br /&gt;
!C#&lt;br /&gt;
!C++&lt;br /&gt;
!Python&lt;br /&gt;
!SmallTalk&lt;br /&gt;
!Perl&lt;br /&gt;
|-&lt;br /&gt;
!Object Orientation&lt;br /&gt;
|Pure&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Hybrid/Multi-Paradigm&lt;br /&gt;
|Hybrid&lt;br /&gt;
|Pure&lt;br /&gt;
|Add-on/Hybrid&lt;br /&gt;
|-&lt;br /&gt;
!Static/Dynamic Typing&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Static&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|Dynamic&lt;br /&gt;
|-&lt;br /&gt;
!Generic Classes&lt;br /&gt;
|N/A&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|N/A&lt;br /&gt;
|-&lt;br /&gt;
!Inheritance&lt;br /&gt;
|Single Class/Multiple Mixins&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Single Class/Multiple Interface&lt;br /&gt;
|Multiple&lt;br /&gt;
|Multiple&lt;br /&gt;
|Single&lt;br /&gt;
|Multiple&lt;br /&gt;
|- &lt;br /&gt;
!Feature Renaming&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|NO&lt;br /&gt;
|-&lt;br /&gt;
!Method Overloading&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Operator Overloading&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|-&lt;br /&gt;
!Class Variables or methods&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|Yes&lt;br /&gt;
|No&lt;br /&gt;
|-&lt;br /&gt;
!Access Control&lt;br /&gt;
|Public, Protected, Private&lt;br /&gt;
|Public, Protected, Package, Private&lt;br /&gt;
|Public, Protected, Private, Internal, Protected Internal&lt;br /&gt;
|Public, Protected, Private, Friends&lt;br /&gt;
|Name Mangling&lt;br /&gt;
|Protected Data, Public Methods&lt;br /&gt;
|None&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
== Ruby : Purely Object Oriented ==&lt;br /&gt;
&lt;br /&gt;
Ruby is considered to be a purely Object oriented Language, as everything in it is considered to be an object. Unlike Java, even primitives such as characters, punctuation, numbers and strings are treated as objects in Ruby. Ruby was designed to facilitate the enforcement of Object Oriented Methods. Ruby is a Dynamically Typed Language.&lt;br /&gt;
&lt;br /&gt;
=== Object oriented Concepts in ruby: ===&lt;br /&gt;
&lt;br /&gt;
Ruby is a singly rooted Object Oriented Programming language which has all its classes having the same superclass which is the class Object.&lt;br /&gt;
&lt;br /&gt;
==== Classes:====&lt;br /&gt;
&lt;br /&gt;
A class is used to define a blueprint of a data type. It does not contain any data, but it specifies what kind of variables and methods an object of the class will have.&lt;br /&gt;
A class in Ruby is defined using the keyword class followed by the classname. The variables and methods are defined within the class definition and the class definition terminates with the end keyword.&lt;br /&gt;
&lt;br /&gt;
For ex:&lt;br /&gt;
class Rectangle&lt;br /&gt;
  def initialize(w,l)&lt;br /&gt;
     @width, @length = w, l&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
Here, the initialize method is used to set the values of the instance variables (@width and @length. The instance variables are the properties of the objects created of this class type. These instance variables can be accessed within the class using an @. These variables can be accessed from outside the class using the accessor methods which are defined inside the class.&lt;br /&gt;
&lt;br /&gt;
In the following case printLength and printWidth are public methods used to access the instance variables from outside the class. These are called accessor methods.&lt;br /&gt;
Similarly, to&lt;br /&gt;
&lt;br /&gt;
   class Rectangle&lt;br /&gt;
  # constructor method&lt;br /&gt;
  def initialize(w,l)&lt;br /&gt;
     @width, @length = w, l&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # accessor methods&lt;br /&gt;
  def printWidth&lt;br /&gt;
     @width&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def printLength&lt;br /&gt;
     @length&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # setter methods&lt;br /&gt;
  def setWidth=(value)&lt;br /&gt;
     @width = value&lt;br /&gt;
  end&lt;br /&gt;
  def setHeight=(value)&lt;br /&gt;
     @height = value&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   # create an object&lt;br /&gt;
     rectagle = Rectangle.new(10, 20)&lt;br /&gt;
&lt;br /&gt;
    # use accessor methods&lt;br /&gt;
      x = rectangle.printWidth()&lt;br /&gt;
      y = rectangle.printLength()&lt;br /&gt;
&lt;br /&gt;
      puts &amp;quot;Width of the Rectangle is : #{x}&amp;quot;&lt;br /&gt;
      puts &amp;quot;Length of the Rectangle is : #{y}&amp;quot;&lt;br /&gt;
==== Class Methods and Class Variables:====&lt;br /&gt;
A class variable is shared between all instances of the class. The value of the class variable remains the same for all the objects of the class. A class variable is accessed within the class using two @ signs at the beginning of the variable name. The class variables must be initialized within the class.&lt;br /&gt;
&lt;br /&gt;
The class methods are defined using def self.methodname() and ends with an end delimiter. These methods are called using the class name itself, for example: classname.methodname&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Attributes and accessor methods: ===&lt;br /&gt;
&lt;br /&gt;
Every class in Ruby defines a set of attributes for its objects. Ruby provides accessor methods (famously known as getter-setter methods in Java) to access these variables. Ruby provides three kinds of accessor methods:&lt;br /&gt;
&lt;br /&gt;
Class Car&lt;br /&gt;
    attr_accessor : name, company&lt;br /&gt;
    attr_reader: price&lt;br /&gt;
    attr_writer: color&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
attr_accessor creates a setter and getter method. The attr_reader method creates only the getter method. This is required in case of attributes that are not to be modified by other programs but their value may be viewed. The attr_writer creates the setter method. In case of Java, the setter and getter methods need to be written in 3 lines of code each whereas in Ruby, the methods are written just by specifying the type of accessor, which have already been coded using Metaprogramming.[link]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Access Control:=====&lt;br /&gt;
Just like any other o-o programming language, Ruby allows to exercise control over the attributes and methods of a class. Ruby provides 3 levels of protection at instance methods level.&lt;br /&gt;
Public:&lt;br /&gt;
In this level objects from outside the class can call the method. All methods except initialize are public by default.&lt;br /&gt;
Private:&lt;br /&gt;
These methods can be call only by the objects of the same class. Initialize methods are private by default.&lt;br /&gt;
Protected:&lt;br /&gt;
These methods can be called either by objects of the same class or of the class derived from this class.&lt;br /&gt;
&lt;br /&gt;
The methods are given access levels using the following syntax.&lt;br /&gt;
Private :method1, :method2&lt;br /&gt;
&lt;br /&gt;
====Abstract methods====&lt;br /&gt;
&lt;br /&gt;
Ruby is a dynamically typed language and does not provide abstract classes as in Java or C++. It instead provides modules that can be included within any class. The reason why Ruby does not provide abstract classes is because of the idea that it only matters if two objects can perform a function, they don’t necessarily have to be inherited from the same class. This paradigm is provided by modules, where the method of a module can be redefined as required by a class that includes it.&lt;br /&gt;
&lt;br /&gt;
Also, if required the technique of abstract class can be simulated by something like this:&lt;br /&gt;
&lt;br /&gt;
  Class Vehicle&lt;br /&gt;
   def drive&lt;br /&gt;
   raise NotImplementedError.new(&amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The subclasses that inherit from the above class will have to redefine the method “drive” as required.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing in Ruby ===&lt;br /&gt;
&lt;br /&gt;
Ruby provides unbounded polymorphism as a dynamically typed language through the concept of “Duck-typing”. “Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface” [link]. This concept is explained by the famous definition as “When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.” This essentially means that in languages like Ruby, it is only checked if an object can implement a function and the type of the object is not taken into consideration. It can be explained better with the help of the following example&lt;br /&gt;
&lt;br /&gt;
  class Lion&lt;br /&gt;
    def roar&lt;br /&gt;
        puts “The Lion roars when hungry”&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
     def roar&lt;br /&gt;
        puts “The man roared in fury and anger”&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   def quality lion&lt;br /&gt;
    lion.roar&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def start&lt;br /&gt;
    leo = Lion.new&lt;br /&gt;
    jack = Person.new&lt;br /&gt;
    quality leo&lt;br /&gt;
    quality jack&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   start&lt;br /&gt;
&lt;br /&gt;
In the above example, leo is an object of class “Lion” and the “roar” method defined in its class is invoked. jack is an object of class “Person” and the corresponding method defined in the Person class is called for him. Although in the real world, both objects are not of the same type, Ruby allows unbounded polymorphism as both of them implement a particular aspect.&lt;br /&gt;
&lt;br /&gt;
Thus, the check for an invoked method is done at run-time in dynamically typed languages and the method defined on that object type is called. If the method is undefined, a “NoMethodError” exception is raised.&lt;/div&gt;</summary>
		<author><name>Sthirum</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011&amp;diff=52401</id>
		<title>CSC/ECE 517 Fall 2011</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011&amp;diff=52401"/>
		<updated>2011-10-18T03:35:24Z</updated>

		<summary type="html">&lt;p&gt;Sthirum: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Link title]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1a ms]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1a cs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1a ri]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1a lj]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1b sa]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1b ds]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1b tj]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1c cm]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1c sj]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1c ka]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1d sr]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e vs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e aa]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1a sc]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e dm]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e an]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e sa]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e lm]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1g vn]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1f rs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1f sv]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1g jn]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1h ps]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e sm]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1i zf]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1g rn]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1i cl]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1d ss]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1i lj]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1h hs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1d gs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2b ns]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2b jp]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2a av]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2f jm]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2e ad]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2e kt]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2e gp]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2b qu]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2c bs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2c rs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2a ca]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2b rv]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2c ds]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2b sa]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2f vh]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2e ps]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch3 3a oe]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch3 3h rr]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch3 3h ss]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch3 4b js]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch3 4b ms]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4i aa]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4i sd]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4d mt]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4d ls]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4d ch]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4c ap]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4h sv]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4e cl]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4a ga]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4f sl]]&lt;br /&gt;
&lt;br /&gt;
*[[trial]]&lt;/div&gt;</summary>
		<author><name>Sthirum</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2c_bs&amp;diff=51689</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2c bs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2c_bs&amp;diff=51689"/>
		<updated>2011-10-02T01:45:05Z</updated>

		<summary type="html">&lt;p&gt;Sthirum: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
An [http://en.wikipedia.org/wiki/Interface_(computing) interface] &amp;lt;ref&amp;gt;interface&amp;lt;/ref&amp;gt; in the [http://en.wikipedia.org/wiki/Java_(programming_language) Java] programming language is an abstract type that is used to specify an interface that classes must implement. Interface may only contain [http://en.wikipedia.org/wiki/Method_signature method signature] and constant declarations. An interface may never contain method definitions. Interfaces cannot be instantiated. A class that implements an interface must implement all of the methods described in the interface, or be an [http://en.wikipedia.org/wiki/Abstract_type abstract] class. Object references in Java may be specified to be of an interface type; in which case, they must either be null, or be bound to an object that implements the interface. &lt;br /&gt;
&lt;br /&gt;
In object-oriented programming languages, a [http://en.wikipedia.org/wiki/Mixin mixin] &amp;lt;ref&amp;gt; mixin &amp;lt;/ref&amp;gt; is a class that provides a certain functionality to be inherited or just reused by a subclass. Some of the functionality of mixins is provided by interfaces in popular languages like Java and [http://en.wikipedia.org/wiki/C_Sharp_(programming_language) C#]. However, an interface only specifies what the class must support and cannot provide an implementation. Another class, providing an implementation and dependent with the interface, is needed for [http://en.wikipedia.org/wiki/Code_refactoring refactoring] common behavior into a single place. Interfaces combined with aspect-oriented programming can produce full-fledged mixins in languages that support such features, such as C# or Java. &lt;br /&gt;
One benefit of using interfaces is that they simulate [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance]. All classes in Java other than [https://matrix.senecac.on.ca:8443/wiki/javaSE/index.php/Java.lang.Object java.lang.Object], must have exactly one base class; multiple inheritance of classes is not allowed.&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
===Mixins in Ruby=== &lt;br /&gt;
In Java you just have [http://en.wikipedia.org/wiki/Class_(computer_programming) classes] (both abstract and concrete) and interfaces. The [http://en.wikipedia.org/wiki/Ruby Ruby] language provides classes,[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html modules], and a mix of both. Inheriting from a mixin is not a form of specialization but is rather a means of collecting functionality. A class may inherit most or all of its functionality from one or more mixins through multiple inheritance. A mixin can also be viewed as an interface with implemented methods. When a class includes a mixin, the class implements the interface and includes, rather than inherits, all the mixins’ attributes (fields, properties) and methods. They become part of the class during compilation. A mixin can defer definition and binding of methods until runtime, though attributes and instantiation parameters are still defined at compile time. In the Ruby language a mixin is a class that is mixed with a module. &lt;br /&gt;
&lt;br /&gt;
====Module====&lt;br /&gt;
&lt;br /&gt;
A [http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html module]&amp;lt;ref&amp;gt;module&amp;lt;/ref&amp;gt; is a degenerate abstract class. A module can’t be instantiated and no class can directly extend it but it can fully implement methods. A class can leverage the implementation of a module by including the module’s methods. A module can define methods that can be shared in different and separate classes either at the class or instance level.We can view an example &amp;lt;ref&amp;gt; http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/&amp;lt;/ref&amp;gt; module that converts a numeric value to its English word.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # Convert a integer value to English.&lt;br /&gt;
  module Stringify&lt;br /&gt;
    # Requires an instance variable @value&lt;br /&gt;
    def stringify&lt;br /&gt;
      if @value == 1&lt;br /&gt;
        &amp;quot;One&amp;quot;&lt;br /&gt;
      elsif @value == 2&lt;br /&gt;
        &amp;quot;Two&amp;quot;&lt;br /&gt;
      elsif @value == 3&lt;br /&gt;
        &amp;quot;Three&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The Stringify module above makes use of a @value [http://en.wikipedia.org/wiki/Instance_variable instance variable]. The class that will be mixed with this module needs to define and set a @value instance variable since the Stringify module uses it but does not define it. In addition to instance variables a module could invoke methods not defined in the module but in the class that it will be mixed with.&lt;br /&gt;
&lt;br /&gt;
A [http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/ Self contained module] can also be written in Ruby where the methods defined in a module can be used as static methods independent of their implementation within a class.&lt;br /&gt;
&lt;br /&gt;
'''Self Contained Module Example: '''&lt;br /&gt;
&lt;br /&gt;
  # A Math module akin to Java Math class.&lt;br /&gt;
  module Math&lt;br /&gt;
    # Could be called as a class, static, method&lt;br /&gt;
    def add(val_one, val_two)&lt;br /&gt;
      BigInteger.new(val_one + val_two)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The methods in the Math module are intended to be invoked like class methods, also known as [http://en.wikipedia.org/wiki/Method_(computer_programming)#Static_methods static] methods. The add method in the Math module accepts two integer values and returns an [http://en.wikipedia.org/wiki/Instance_(computer_science) instance] of BigInteger.&lt;br /&gt;
&lt;br /&gt;
====Using Mixins====&lt;br /&gt;
&lt;br /&gt;
Implementation of the class and module are joined, interwined, combined, etc. A mixin is a different mechanism to the extend construct used to add concrete implementation to a class. With a mixin you can extend from a module instead of a class. &lt;br /&gt;
&lt;br /&gt;
The example of self-contained module shows how the method returns a BigInteger sum after accepting two integer parameters. The following example is the implementation of BigInteger class. The BigInteger class defines one constructor and directly inherits one method from the Number base class. To mix in the methods implemented in the Stringify and Math modules with the BigInteger class you will note the usage of the include and extend methods, respectively.&lt;br /&gt;
&lt;br /&gt;
  # Base Number class&lt;br /&gt;
  class Number&lt;br /&gt;
    def intValue&lt;br /&gt;
      @value&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  # BigInteger extends Number&lt;br /&gt;
  class BigInteger &amp;lt; Number&lt;br /&gt;
 &lt;br /&gt;
  # Add instance methods from Stringify&lt;br /&gt;
  include Stringify&lt;br /&gt;
 &lt;br /&gt;
  # Add class methods from Math&lt;br /&gt;
  extend Math&lt;br /&gt;
 &lt;br /&gt;
  # Add a constructor with one parameter&lt;br /&gt;
    def initialize(value)&lt;br /&gt;
      @value = value&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Using the class:&lt;br /&gt;
&lt;br /&gt;
  # Create a new object&lt;br /&gt;
  bigint1 = BigInteger.new(10)&lt;br /&gt;
  # Call a method inherited from the base class&lt;br /&gt;
  puts bigint1.intValue   # --&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
The extend method will mix a module’s methods at the class level. The method defined in the Math module can be used as a class/static method.&lt;br /&gt;
&lt;br /&gt;
  # Call class method extended from Math&lt;br /&gt;
  bigint2 = BigInteger.add(-2, 4)&lt;br /&gt;
  puts bigint2.intValue   # --&amp;gt; 2&lt;br /&gt;
&lt;br /&gt;
The include method will mix a module’s methods at the instance level, meaning that the methods will become instance methods. The method defined in the Stringify module can be used as an instance method.&lt;br /&gt;
&lt;br /&gt;
  # Call a method included from Stringify&lt;br /&gt;
  puts bigint2.stringify   # --&amp;gt; 'Two'&lt;br /&gt;
&lt;br /&gt;
There is another use of the extend method. You can enhance an object instance by mixing it with a module at run time. This ‘CurrencyFormatter’ is used to extend an object, changing it’s responsibilities at runtime.&lt;br /&gt;
&lt;br /&gt;
  # Format a numeric value as a currency&lt;br /&gt;
  module CurrencyFormatter&lt;br /&gt;
    def format&lt;br /&gt;
      &amp;quot;$#{@value}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
To mix an object instance with a module you can do the following:&lt;br /&gt;
&lt;br /&gt;
  # Add the module methods to&lt;br /&gt;
  # this object instance, only!&lt;br /&gt;
  bigint2.extend CurrencyFormatter&lt;br /&gt;
  puts bigint2.format   # --&amp;gt; '$2'&lt;br /&gt;
&lt;br /&gt;
Calling the extend method on an instance will only extend that one object; objects of the same class will not be extended with the new functionality.&lt;br /&gt;
&lt;br /&gt;
  puts bigint1.format   # will generate an error&lt;br /&gt;
&lt;br /&gt;
Modules that will be mixed with a class via the include or extend method could define something like a constructor or initializer method to the module. The module initializer method will be invoked at the time the module is mixed with a class. When a class extends a module the module’s self.extended method will be invoked:&lt;br /&gt;
&lt;br /&gt;
  module Math&lt;br /&gt;
    def self.extended(base)&lt;br /&gt;
      # Initialize module.&lt;br /&gt;
    end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
The self prefix indicates that the method is a static module level method. The base parameter in the static extended method will be either an instance object or class object of the class that extended the module depending whether you extend a object or class, respectively.&lt;br /&gt;
When a class includes a module the module’s self.included method will be invoked.&lt;br /&gt;
&lt;br /&gt;
  module Stringify&lt;br /&gt;
    def self.included(base)&lt;br /&gt;
      # Initialize module.&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The base parameter will be a class object for the class that includes the module.&lt;br /&gt;
It is important to note that inside the included and extended initializer methods you can include and extend other modules. &lt;br /&gt;
&lt;br /&gt;
  module Stringify&lt;br /&gt;
    def self.included(base)&lt;br /&gt;
      base.extend SomeOtherModule&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===Interfaces in Java===&lt;br /&gt;
Interfaces define a standardized set of commands that a class will obey. The commands are a set of methods that a class implements. The interface definition states the names of the methods and their return types and argument signatures. There is no executable body for any method. Example: &lt;br /&gt;
  interface Bicycle {&lt;br /&gt;
       void changeCadence(int newValue);   // wheel revolutions per minute&lt;br /&gt;
       void changeGear(int newValue);&lt;br /&gt;
       void speedUp(int increment);&lt;br /&gt;
       void applyBrakes(int decrement);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Implementation is independent to each class that implements the interface. Once a class implements an interface, the Java compiler knows that an instance of the class will contain the specified set of methods. Therefore, it will allow you to call those methods for an object referenced by a variable whose type is the interface. Implementing an interface enables a class to be &amp;quot;plugged in&amp;quot; in any situation that requires a specific behavior (manifested through the set of methods).&lt;br /&gt;
&lt;br /&gt;
  class ACMEBicycle implements Bicycle {&lt;br /&gt;
    //implementation specific to this class&lt;br /&gt;
       int cadence = 0;&lt;br /&gt;
       int speed = 0;&lt;br /&gt;
       int gear = 1;&lt;br /&gt;
       void changeCadence(int newValue) {&lt;br /&gt;
            cadence = newValue;&lt;br /&gt;
       }&lt;br /&gt;
       void changeGear(int newValue) {&lt;br /&gt;
            gear = newValue;&lt;br /&gt;
       }&lt;br /&gt;
       void speedUp(int increment) {&lt;br /&gt;
            speed = speed + increment;   &lt;br /&gt;
       }&lt;br /&gt;
       void applyBrakes(int decrement) {&lt;br /&gt;
            speed = speed - decrement;&lt;br /&gt;
       }&lt;br /&gt;
       void printStates() {&lt;br /&gt;
            System.out.println(&amp;quot;cadence:&amp;quot;+cadence+&amp;quot; speed:&amp;quot;+speed+&amp;quot; gear:&amp;quot;+gear);&lt;br /&gt;
       }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
  class MOUNTAINBicycle implements Bicycle {&lt;br /&gt;
  int cadence = 2;&lt;br /&gt;
       int speed = 3;&lt;br /&gt;
       int gear = 5;&lt;br /&gt;
       void changeCadence(int newValue) {&lt;br /&gt;
            cadence = newValue + 10;&lt;br /&gt;
       }&lt;br /&gt;
       void changeGear(int newValue) {&lt;br /&gt;
            gear = newValue + 4;&lt;br /&gt;
       }&lt;br /&gt;
       void speedUp(int increment) {&lt;br /&gt;
            speed = speed + increment + 10;   &lt;br /&gt;
       }&lt;br /&gt;
       void applyBrakes(int decrement) {&lt;br /&gt;
            speed = speed – decrement – 10;&lt;br /&gt;
       }&lt;br /&gt;
       void printStates() {&lt;br /&gt;
            System.out.println(&amp;quot;cadence:&amp;quot;+cadence+&amp;quot; speed:&amp;quot;+speed+&amp;quot; gear:&amp;quot;+gear);&lt;br /&gt;
       }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Using an interface rather than inheritance to specify a certain set of methods allows a class to inherit from some other class. In other words, if a class needs two different sets of methods, so it can behave like two different types of things, it could inherit one set from class A, and use an interface B to specify the other.&lt;br /&gt;
You could then reference one of these objects with either an A reference or a B reference. Implementing an interface allows a class to become more formal about the behavior it promises to provide. Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler. If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Mixins vs Interfaces==&lt;br /&gt;
Because of this mixin feature, a developer can add arbitrary methods and modify behavior of core classes at runtime. This is amazingly powerful if you are trying to write plugins and extensions to the [http://en.wikipedia.org/wiki/Software_framework framework].  Because you can add functionality to existing objects, users can install their [http://en.wikipedia.org/wiki/Plug-in_(computing) plugin] and start taking advantage of new functionality without having to make changes to the objects that they are instantiating in their application. &lt;br /&gt;
&lt;br /&gt;
In frameworks written in other languages, such as Java, plugging in new functionality means that you need to change how your objects are instantiated. This will require code changes and/or potentially configuration changes which makes it hard to develop, hard to maintain, and a pain for plugin developers to support.  But because of the mixin feature, Rails plugin developers can customize the base objects and the users of the plugins do not have to change any of their code or configuration logic.&lt;br /&gt;
&lt;br /&gt;
===Differences===&lt;br /&gt;
&lt;br /&gt;
These is a general comparision: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Mixins in Ruby&lt;br /&gt;
! Interfaces in Java&lt;br /&gt;
|-&lt;br /&gt;
| 'include' or 'extend' keywords used to include the module in the class.&lt;br /&gt;
| 'implement' keyword is used to implement interface&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Need not implement the module methods in the class.&lt;br /&gt;
| Should implement the interface methods. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| The methods are not copied into the class in which they are included. They are referenced.&lt;br /&gt;
| The methods’ implementation is in the class itself.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| The included method definition is decided at ‘run time’. If the definition changes while running the function points to the changed code.&lt;br /&gt;
| The definition is known compile time and will not change until the code is compiled and run again. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Modules inclusion does not provide a superclass and subclass relationship.&lt;br /&gt;
| Interface extending provides a superclass and subclass relationship. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| When we use mixins we included only a module of the class. &lt;br /&gt;
| We include the entire interface class. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Mainly for code reuse and does not provide any hierarchy. &lt;br /&gt;
| It provides a hierarchy. &lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Functionality and Behavior Comparison===&lt;br /&gt;
The functionality and behavior of Mixins in Ruby is compared with Interfaces in Java&lt;br /&gt;
&lt;br /&gt;
====Comparable Functionality====&lt;br /&gt;
Sometime we are required to compare the class members of different objects of the same class and/or sort them. This is the comparable functionality that we are trying to achieve for a given class.  This functionality can be achieved by both the Ruby using mixins and Java using interfaces. There is not much difference using both.&lt;br /&gt;
&lt;br /&gt;
'''Ruby'''&lt;br /&gt;
&lt;br /&gt;
The [http://www.ruby-doc.org/core/classes/Comparable.html Comparable] mixin is used by classes whose objects may be ordered. The class must define the &amp;lt;=&amp;gt; operator, which compares the receiver against another object, returning -1, 0, or +1 depending on whether the receiver is less than, equal to, or greater than the other object. Comparable uses &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method between? as seen in the following example&amp;lt;ref&amp;gt;http://corelib.rubyonrails.org/classes/Comparable.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  class SizeMatters&lt;br /&gt;
     include Comparable&lt;br /&gt;
     attr :str&lt;br /&gt;
     def &amp;lt;=&amp;gt;(anOther)&lt;br /&gt;
       str.size &amp;lt;=&amp;gt; anOther.str.size&lt;br /&gt;
     end&lt;br /&gt;
     def initialize(str)&lt;br /&gt;
       @str = str&lt;br /&gt;
     end&lt;br /&gt;
     def inspect&lt;br /&gt;
       @str&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
''using class:'' &lt;br /&gt;
&lt;br /&gt;
   s1 = SizeMatters.new(&amp;quot;Z&amp;quot;)&lt;br /&gt;
   s2 = SizeMatters.new(&amp;quot;YY&amp;quot;)&lt;br /&gt;
   s3 = SizeMatters.new(&amp;quot;XXX&amp;quot;)&lt;br /&gt;
   s4 = SizeMatters.new(&amp;quot;WWWW&amp;quot;)&lt;br /&gt;
   s5 = SizeMatters.new(&amp;quot;VVVVV&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
''output:''&lt;br /&gt;
   s1 &amp;lt; s2                       #=&amp;gt; true&lt;br /&gt;
   s4.between?(s1, s3)           #=&amp;gt; false&lt;br /&gt;
   s4.between?(s3, s5)           #=&amp;gt; true&lt;br /&gt;
   [ s3, s2, s5, s4, s1 ].sort   #=&amp;gt; [Z, YY, XXX, WWWW, VVVVV]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Java'''&lt;br /&gt;
&lt;br /&gt;
In Java, [http://www.java-tips.org/java-se-tips/java.lang/how-to-use-comparable-interface.html comparable functionality] is achieved by extending the “Comparable” interface. List of objects that implement this interface can be sorted automatically by sort method of the list interface. The interface in the following example &amp;lt;ref&amp;gt; http://www.java-tips.org/java-se-tips/java.lang/how-to-use-comparable-interface.html &amp;lt;/ref&amp;gt; has compareTo() method that is used by the sort() method of the list. Here compareTo() method can also be used to compare two objects with respect to the implementation in compareTo() implementation in the derived class. It returns ‘0’ , ‘1’ and ‘-1’ corresponding to whether one is equal to , greater than or lesser than other.&lt;br /&gt;
In this example code Employee class is implementing Comparable interface and have method compareTo().&lt;br /&gt;
  public class Employee implements Comparable {&lt;br /&gt;
    int EmpID;&lt;br /&gt;
    String Ename;&lt;br /&gt;
    double Sal;&lt;br /&gt;
    static int i;&lt;br /&gt;
    public Employee() {&lt;br /&gt;
        EmpID = i++;&lt;br /&gt;
        Ename = &amp;quot;dont know&amp;quot;;&lt;br /&gt;
        Sal = 0.0;&lt;br /&gt;
    }&lt;br /&gt;
    public Employee(String ename, double sal) {&lt;br /&gt;
        EmpID = i++;&lt;br /&gt;
        Ename = ename;&lt;br /&gt;
        Sal = sal;&lt;br /&gt;
    }&lt;br /&gt;
    public String toString() {&lt;br /&gt;
        return &amp;quot;EmpID &amp;quot; + EmpID + &amp;quot;\n&amp;quot; + &amp;quot;Ename &amp;quot; + Ename + &amp;quot;\n&amp;quot; + &amp;quot;Sal&amp;quot; + Sal;&lt;br /&gt;
    }&lt;br /&gt;
    public int compareTo(Object o1) {&lt;br /&gt;
        if (this.Sal == ((Employee) o1).Sal)&lt;br /&gt;
            return 0;&lt;br /&gt;
        else if ((this.Sal) &amp;gt; ((Employee) o1).Sal)&lt;br /&gt;
            return 1;&lt;br /&gt;
        else&lt;br /&gt;
            return -1;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
ComparableDemo.java is showing the use of this interface. This class first makes a list of objects of type Employee and call sort method of java.util.Collections, which internally uses compareTo() method of Employee class and sort the list accordingly.&lt;br /&gt;
 &lt;br /&gt;
  import java.util.*;&lt;br /&gt;
  public class ComparableDemo{&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
        List ts1 = new ArrayList();&lt;br /&gt;
        ts1.add(new Employee (&amp;quot;Tom&amp;quot;,40000.00));&lt;br /&gt;
        ts1.add(new Employee (&amp;quot;Harry&amp;quot;,20000.00));&lt;br /&gt;
        ts1.add(new Employee (&amp;quot;Maggie&amp;quot;,50000.00));&lt;br /&gt;
        ts1.add(new Employee (&amp;quot;Chris&amp;quot;,70000.00));&lt;br /&gt;
        Collections.sort(ts1);&lt;br /&gt;
        Iterator itr = ts1.iterator();&lt;br /&gt;
        while(itr.hasNext()){&lt;br /&gt;
            Object element = itr.next();&lt;br /&gt;
            System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
''Output:''&lt;br /&gt;
  &lt;br /&gt;
  EmpID 1&lt;br /&gt;
  Ename Harry&lt;br /&gt;
  Sal    20000.0&lt;br /&gt;
  EmpID 0&lt;br /&gt;
  Ename Tom&lt;br /&gt;
  Sal    40000.0&lt;br /&gt;
  EmpID 2&lt;br /&gt;
  Ename Maggie&lt;br /&gt;
  Sal    50000.0&lt;br /&gt;
  EmpID 3&lt;br /&gt;
  Ename Chris&lt;br /&gt;
  Sal    70000.0&lt;br /&gt;
&lt;br /&gt;
====Singleton Behaviour====&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Singleton_pattern Singleton] is a class guaranteed to have only a single instance, with a single global access point to that instance. It can be implemented in Ruby and Java. In Ruby we can include a module to acquire all the functionality of the singleton module. The code is already there and we are just reusing the code and adding that similar functionality into our class. Here our intention is to add the functionality of singleton to the class which is achieved using mixins. In java, implementation of interfaces means that the class which implements the interface becomes a type of that class, which is not the intention here. So, there is not interface which class can implement to get the functionality in itself. It has to implement the functionality itself without implementing any interface. &lt;br /&gt;
&lt;br /&gt;
'''Ruby'''&lt;br /&gt;
&lt;br /&gt;
When you include the singleton method inside your class it makes the new method private so that you cannot use it. Including ‘singleton’ adds a class method called instance that instantiates only one instance of the class&lt;br /&gt;
&lt;br /&gt;
  # require singleton lib&lt;br /&gt;
  require 'singleton'&lt;br /&gt;
  class AppConfig&lt;br /&gt;
    # mixin the singleton module&lt;br /&gt;
    include Singleton&lt;br /&gt;
    # do the actual app configuration&lt;br /&gt;
    def load_config(file)&lt;br /&gt;
      # do your work here&lt;br /&gt;
      puts &amp;quot;Application configuration file was loaded from file: #{file}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
''output:''&lt;br /&gt;
&lt;br /&gt;
  conf1 = AppConfig.instance&lt;br /&gt;
  conf1.load_config &amp;quot;/home/khelll/conf.yml&amp;quot;       #=&amp;gt;Application configuration file was loaded from file: /home/khelll/conf.yml&lt;br /&gt;
  conf2 = AppConfig.instance&lt;br /&gt;
  puts conf1 == conf2      #=&amp;gt;true&lt;br /&gt;
  # notice the following 2 lines won’t work&lt;br /&gt;
  AppConfig.new rescue(puts $!)   #=&amp;gt; new method is private&lt;br /&gt;
&lt;br /&gt;
====Enumerable Behaviour====&lt;br /&gt;
This [http://www.ruby-doc.org/core/classes/Enumerable.html enumerable] functionality adds support to the array members of the class. We can achieve this functionality using mixins in Ruby while it is not possible to do it in Java using interfaces.&lt;br /&gt;
&lt;br /&gt;
'''Implementation in Ruby:''' &lt;br /&gt;
&lt;br /&gt;
It adds the functionality of map, inject, select etc to the class which includes the ‘Enumerable’ module. The class which includes should represent a collection of items. Enumerable requires that your class contain an each method that serves up the items in the collection. All the other Enumerable methods rely on this. &lt;br /&gt;
Example:&lt;br /&gt;
This is a Team class that manages a group of members &amp;lt;ref&amp;gt;http://kconrails.com/2010/11/30/ruby-enumerable-primer-part-1-the-basics/&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
  class Team&lt;br /&gt;
    include Enumerable&lt;br /&gt;
    attr_accessor :members&lt;br /&gt;
    def initialize&lt;br /&gt;
      @members = []&lt;br /&gt;
    end&lt;br /&gt;
    def each &amp;amp;block&lt;br /&gt;
      @members.each{|member| block.call(member)}&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Enumerable requires that your class contain an each method that serves up the items in the collection. All the other Enumerablemethods rely on this. Now we can use the map method, for instance:&lt;br /&gt;
&lt;br /&gt;
  team = Team.new&lt;br /&gt;
  team.members = ['joshua', 'gabriel', 'jacob']&lt;br /&gt;
  team.map{|member| member.capitalize}   # =&amp;gt; [&amp;quot;Joshua&amp;quot;, &amp;quot;Gabriel&amp;quot;, &amp;quot;Jacob&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
Now we can call any Enumerable methods on our team object itself, and it will assume we want to work with the members array within.&lt;br /&gt;
&lt;br /&gt;
====Data Mapper Behaviour====&lt;br /&gt;
&lt;br /&gt;
[http://datamapper.org/docs/ DataMapper] is an API which comes with the ability to use the same API to talk to a multitude of different datastores. There are adapters for the usual RDBMS suspects, NoSQL stores, various file formats and even some popular webservices. This is implemented in ruby using mixins. We need not use interfaces to implement.&lt;br /&gt;
&lt;br /&gt;
'''Implementation in Ruby:'''&lt;br /&gt;
  class Post&lt;br /&gt;
       include DataMapper::Resource&lt;br /&gt;
       property :id, Serial&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
The point to take away from this is not that Ruby Mixins are more powerful than Java Interfaces. The mixin feature of Ruby allows for the development of easy to use but powerful plugins that will be hard for any non-Ruby based framework to compete with. Java has many other ways of implementing the functionality mixins are capable of. It is just that it does not use Interfaces to achieve the functionality. Even though both of them are a way of achieving multiple inheritance in their respective languages, they achieve quiet different goals. Inheritance in Java is mainly to overcome the confusion which arises with multiple inheritance, while Mixins in Ruby is mainly to add an additional functionality to the existing class apart from the ‘primary functionality’ of the class. &lt;br /&gt;
&lt;br /&gt;
When a class implements Interface, it indicates that it is of the type interface class and forms a hierarchy with the interface class, while ruby mixins are mainly intended for code reuse and including an additional functionality to the class. It does not form any hierarchy as it is only using module of the class.&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* http://en.wikipedia.org/wiki/Interface_(Java)&lt;br /&gt;
* http://en.wikipedia.org/wiki/Mixin&lt;br /&gt;
* http://carboni.ca/blog/p/Modules-How-Do-They-Work&lt;br /&gt;
* http://rubylearning.com/satishtalim/modules_mixins.html&lt;br /&gt;
* http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-&lt;br /&gt;
* http://datamapper.org/&lt;br /&gt;
* http://www.dsource.org/projects/tutorials/wiki/SingletonPattern &lt;br /&gt;
* http://kconrails.com/2010/11/30/ruby-enumerable-primer-part-1-the-basics/&lt;br /&gt;
* http://stackoverflow.com/questions/575920/is-a-ruby-module-equivalent-to-a-java-interface&lt;/div&gt;</summary>
		<author><name>Sthirum</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2c_bs&amp;diff=51688</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2c bs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2c_bs&amp;diff=51688"/>
		<updated>2011-10-02T01:40:24Z</updated>

		<summary type="html">&lt;p&gt;Sthirum: /* Comparable Functionality */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
An [http://en.wikipedia.org/wiki/Interface_(computing) interface] &amp;lt;ref&amp;gt;interface&amp;lt;/ref&amp;gt; in the [http://en.wikipedia.org/wiki/Java_(programming_language) Java] programming language is an abstract type that is used to specify an interface that classes must implement. Interface may only contain [http://en.wikipedia.org/wiki/Method_signature method signature] and constant declarations. An interface may never contain method definitions. Interfaces cannot be instantiated. A class that implements an interface must implement all of the methods described in the interface, or be an [http://en.wikipedia.org/wiki/Abstract_type abstract] class. Object references in Java may be specified to be of an interface type; in which case, they must either be null, or be bound to an object that implements the interface. &lt;br /&gt;
&lt;br /&gt;
In object-oriented programming languages, a [http://en.wikipedia.org/wiki/Mixin mixin] &amp;lt;ref&amp;gt; mixin &amp;lt;/ref&amp;gt; is a class that provides a certain functionality to be inherited or just reused by a subclass. Some of the functionality of mixins is provided by interfaces in popular languages like Java and [http://en.wikipedia.org/wiki/C_Sharp_(programming_language) C#]. However, an interface only specifies what the class must support and cannot provide an implementation. Another class, providing an implementation and dependent with the interface, is needed for [http://en.wikipedia.org/wiki/Code_refactoring refactoring] common behavior into a single place. Interfaces combined with aspect-oriented programming can produce full-fledged mixins in languages that support such features, such as C# or Java. &lt;br /&gt;
One benefit of using interfaces is that they simulate [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance]. All classes in Java other than [https://matrix.senecac.on.ca:8443/wiki/javaSE/index.php/Java.lang.Object java.lang.Object], must have exactly one base class; multiple inheritance of classes is not allowed.&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
===Mixins in Ruby=== &lt;br /&gt;
In Java you just have [http://en.wikipedia.org/wiki/Class_(computer_programming) classes] (both abstract and concrete) and interfaces. The [http://en.wikipedia.org/wiki/Ruby Ruby] language provides classes,[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html modules], and a mix of both. Inheriting from a mixin is not a form of specialization but is rather a means of collecting functionality. A class may inherit most or all of its functionality from one or more mixins through multiple inheritance. A mixin can also be viewed as an interface with implemented methods. When a class includes a mixin, the class implements the interface and includes, rather than inherits, all the mixins’ attributes (fields, properties) and methods. They become part of the class during compilation. A mixin can defer definition and binding of methods until runtime, though attributes and instantiation parameters are still defined at compile time. In the Ruby language a mixin is a class that is mixed with a module. &lt;br /&gt;
&lt;br /&gt;
====Module====&lt;br /&gt;
&lt;br /&gt;
A [http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html module]&amp;lt;ref&amp;gt;module&amp;lt;/ref&amp;gt; is a degenerate abstract class. A module can’t be instantiated and no class can directly extend it but it can fully implement methods. A class can leverage the implementation of a module by including the module’s methods. A module can define methods that can be shared in different and separate classes either at the class or instance level.We can view an example &amp;lt;ref&amp;gt; http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/&amp;lt;/ref&amp;gt; module that converts a numeric value to its English word.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # Convert a integer value to English.&lt;br /&gt;
  module Stringify&lt;br /&gt;
    # Requires an instance variable @value&lt;br /&gt;
    def stringify&lt;br /&gt;
      if @value == 1&lt;br /&gt;
        &amp;quot;One&amp;quot;&lt;br /&gt;
      elsif @value == 2&lt;br /&gt;
        &amp;quot;Two&amp;quot;&lt;br /&gt;
      elsif @value == 3&lt;br /&gt;
        &amp;quot;Three&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The Stringify module above makes use of a @value [http://en.wikipedia.org/wiki/Instance_variable instance variable]. The class that will be mixed with this module needs to define and set a @value instance variable since the Stringify module uses it but does not define it. In addition to instance variables a module could invoke methods not defined in the module but in the class that it will be mixed with.&lt;br /&gt;
&lt;br /&gt;
A [http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/ Self contained module] can also be written in Ruby where the methods defined in a module can be used as static methods independent of their implementation within a class.&lt;br /&gt;
&lt;br /&gt;
'''Self Contained Module Example: '''&lt;br /&gt;
&lt;br /&gt;
  # A Math module akin to Java Math class.&lt;br /&gt;
  module Math&lt;br /&gt;
    # Could be called as a class, static, method&lt;br /&gt;
    def add(val_one, val_two)&lt;br /&gt;
      BigInteger.new(val_one + val_two)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The methods in the Math module are intended to be invoked like class methods, also known as [http://en.wikipedia.org/wiki/Method_(computer_programming)#Static_methods static] methods. The add method in the Math module accepts two integer values and returns an [http://en.wikipedia.org/wiki/Instance_(computer_science) instance] of BigInteger.&lt;br /&gt;
&lt;br /&gt;
====Using Mixins====&lt;br /&gt;
&lt;br /&gt;
Implementation of the class and module are joined, interwined, combined, etc. A mixin is a different mechanism to the extend construct used to add concrete implementation to a class. With a mixin you can extend from a module instead of a class. &lt;br /&gt;
&lt;br /&gt;
The example of self-contained module shows how the method returns a BigInteger sum after accepting two integer parameters. The following example is the implementation of BigInteger class. The BigInteger class defines one constructor and directly inherits one method from the Number base class. To mix in the methods implemented in the Stringify and Math modules with the BigInteger class you will note the usage of the include and extend methods, respectively.&lt;br /&gt;
&lt;br /&gt;
  # Base Number class&lt;br /&gt;
  class Number&lt;br /&gt;
    def intValue&lt;br /&gt;
      @value&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  # BigInteger extends Number&lt;br /&gt;
  class BigInteger &amp;lt; Number&lt;br /&gt;
 &lt;br /&gt;
  # Add instance methods from Stringify&lt;br /&gt;
  include Stringify&lt;br /&gt;
 &lt;br /&gt;
  # Add class methods from Math&lt;br /&gt;
  extend Math&lt;br /&gt;
 &lt;br /&gt;
  # Add a constructor with one parameter&lt;br /&gt;
    def initialize(value)&lt;br /&gt;
      @value = value&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Using the class:&lt;br /&gt;
&lt;br /&gt;
  # Create a new object&lt;br /&gt;
  bigint1 = BigInteger.new(10)&lt;br /&gt;
  # Call a method inherited from the base class&lt;br /&gt;
  puts bigint1.intValue   # --&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
The extend method will mix a module’s methods at the class level. The method defined in the Math module can be used as a class/static method.&lt;br /&gt;
&lt;br /&gt;
  # Call class method extended from Math&lt;br /&gt;
  bigint2 = BigInteger.add(-2, 4)&lt;br /&gt;
  puts bigint2.intValue   # --&amp;gt; 2&lt;br /&gt;
&lt;br /&gt;
The include method will mix a module’s methods at the instance level, meaning that the methods will become instance methods. The method defined in the Stringify module can be used as an instance method.&lt;br /&gt;
&lt;br /&gt;
  # Call a method included from Stringify&lt;br /&gt;
  puts bigint2.stringify   # --&amp;gt; 'Two'&lt;br /&gt;
&lt;br /&gt;
There is another use of the extend method. You can enhance an object instance by mixing it with a module at run time. This ‘CurrencyFormatter’ is used to extend an object, changing it’s responsibilities at runtime.&lt;br /&gt;
&lt;br /&gt;
  # Format a numeric value as a currency&lt;br /&gt;
  module CurrencyFormatter&lt;br /&gt;
    def format&lt;br /&gt;
      &amp;quot;$#{@value}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
To mix an object instance with a module you can do the following:&lt;br /&gt;
&lt;br /&gt;
  # Add the module methods to&lt;br /&gt;
  # this object instance, only!&lt;br /&gt;
  bigint2.extend CurrencyFormatter&lt;br /&gt;
  puts bigint2.format   # --&amp;gt; '$2'&lt;br /&gt;
&lt;br /&gt;
Calling the extend method on an instance will only extend that one object; objects of the same class will not be extended with the new functionality.&lt;br /&gt;
&lt;br /&gt;
  puts bigint1.format   # will generate an error&lt;br /&gt;
&lt;br /&gt;
Modules that will be mixed with a class via the include or extend method could define something like a constructor or initializer method to the module. The module initializer method will be invoked at the time the module is mixed with a class. When a class extends a module the module’s self.extended method will be invoked:&lt;br /&gt;
&lt;br /&gt;
  module Math&lt;br /&gt;
    def self.extended(base)&lt;br /&gt;
      # Initialize module.&lt;br /&gt;
    end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
The self prefix indicates that the method is a static module level method. The base parameter in the static extended method will be either an instance object or class object of the class that extended the module depending whether you extend a object or class, respectively.&lt;br /&gt;
When a class includes a module the module’s self.included method will be invoked.&lt;br /&gt;
&lt;br /&gt;
  module Stringify&lt;br /&gt;
    def self.included(base)&lt;br /&gt;
      # Initialize module.&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The base parameter will be a class object for the class that includes the module.&lt;br /&gt;
It is important to note that inside the included and extended initializer methods you can include and extend other modules. &lt;br /&gt;
&lt;br /&gt;
  module Stringify&lt;br /&gt;
    def self.included(base)&lt;br /&gt;
      base.extend SomeOtherModule&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===Interfaces in Java===&lt;br /&gt;
Interfaces define a standardized set of commands that a class will obey. The commands are a set of methods that a class implements. The interface definition states the names of the methods and their return types and argument signatures. There is no executable body for any method. Example: &lt;br /&gt;
  interface Bicycle {&lt;br /&gt;
       void changeCadence(int newValue);   // wheel revolutions per minute&lt;br /&gt;
       void changeGear(int newValue);&lt;br /&gt;
       void speedUp(int increment);&lt;br /&gt;
       void applyBrakes(int decrement);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Implementation is independent to each class that implements the interface. Once a class implements an interface, the Java compiler knows that an instance of the class will contain the specified set of methods. Therefore, it will allow you to call those methods for an object referenced by a variable whose type is the interface. Implementing an interface enables a class to be &amp;quot;plugged in&amp;quot; in any situation that requires a specific behavior (manifested through the set of methods).&lt;br /&gt;
&lt;br /&gt;
  class ACMEBicycle implements Bicycle {&lt;br /&gt;
    //implementation specific to this class&lt;br /&gt;
       int cadence = 0;&lt;br /&gt;
       int speed = 0;&lt;br /&gt;
       int gear = 1;&lt;br /&gt;
       void changeCadence(int newValue) {&lt;br /&gt;
            cadence = newValue;&lt;br /&gt;
       }&lt;br /&gt;
       void changeGear(int newValue) {&lt;br /&gt;
            gear = newValue;&lt;br /&gt;
       }&lt;br /&gt;
       void speedUp(int increment) {&lt;br /&gt;
            speed = speed + increment;   &lt;br /&gt;
       }&lt;br /&gt;
       void applyBrakes(int decrement) {&lt;br /&gt;
            speed = speed - decrement;&lt;br /&gt;
       }&lt;br /&gt;
       void printStates() {&lt;br /&gt;
            System.out.println(&amp;quot;cadence:&amp;quot;+cadence+&amp;quot; speed:&amp;quot;+speed+&amp;quot; gear:&amp;quot;+gear);&lt;br /&gt;
       }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
  class MOUNTAINBicycle implements Bicycle {&lt;br /&gt;
  int cadence = 2;&lt;br /&gt;
       int speed = 3;&lt;br /&gt;
       int gear = 5;&lt;br /&gt;
       void changeCadence(int newValue) {&lt;br /&gt;
            cadence = newValue + 10;&lt;br /&gt;
       }&lt;br /&gt;
       void changeGear(int newValue) {&lt;br /&gt;
            gear = newValue + 4;&lt;br /&gt;
       }&lt;br /&gt;
       void speedUp(int increment) {&lt;br /&gt;
            speed = speed + increment + 10;   &lt;br /&gt;
       }&lt;br /&gt;
       void applyBrakes(int decrement) {&lt;br /&gt;
            speed = speed – decrement – 10;&lt;br /&gt;
       }&lt;br /&gt;
       void printStates() {&lt;br /&gt;
            System.out.println(&amp;quot;cadence:&amp;quot;+cadence+&amp;quot; speed:&amp;quot;+speed+&amp;quot; gear:&amp;quot;+gear);&lt;br /&gt;
       }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Using an interface rather than inheritance to specify a certain set of methods allows a class to inherit from some other class. In other words, if a class needs two different sets of methods, so it can behave like two different types of things, it could inherit one set from class A, and use an interface B to specify the other.&lt;br /&gt;
You could then reference one of these objects with either an A reference or a B reference. Implementing an interface allows a class to become more formal about the behavior it promises to provide. Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler. If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Mixins vs Interfaces==&lt;br /&gt;
Because of this mixin feature, a developer can add arbitrary methods and modify behavior of core classes at runtime. This is amazingly powerful if you are trying to write plugins and extensions to the [http://en.wikipedia.org/wiki/Software_framework framework].  Because you can add functionality to existing objects, users can install their [http://en.wikipedia.org/wiki/Plug-in_(computing) plugin] and start taking advantage of new functionality without having to make changes to the objects that they are instantiating in their application. &lt;br /&gt;
&lt;br /&gt;
In frameworks written in other languages, such as Java, plugging in new functionality means that you need to change how your objects are instantiated. This will require code changes and/or potentially configuration changes which makes it hard to develop, hard to maintain, and a pain for plugin developers to support.  But because of the mixin feature, Rails plugin developers can customize the base objects and the users of the plugins do not have to change any of their code or configuration logic.&lt;br /&gt;
&lt;br /&gt;
===Differences===&lt;br /&gt;
&lt;br /&gt;
These is a general comparision: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Mixins in Ruby&lt;br /&gt;
! Interfaces in Java&lt;br /&gt;
|-&lt;br /&gt;
| 'include' or 'extend' keywords used to include the module in the class.&lt;br /&gt;
| 'implement' keyword is used to implement interface&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Need not implement the module methods in the class.&lt;br /&gt;
| Should implement the interface methods. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| The methods are not copied into the class in which they are included. They are referenced.&lt;br /&gt;
| The methods’ implementation is in the class itself.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| The included method definition is decided at ‘run time’. If the definition changes while running the function points to the changed code.&lt;br /&gt;
| The definition is known compile time and will not change until the code is compiled and run again. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Modules inclusion does not provide a superclass and subclass relationship.&lt;br /&gt;
| Interface extending provides a superclass and subclass relationship. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| When we use mixins we included only a module of the class. &lt;br /&gt;
| We include the entire interface class. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Mainly for code reuse and does not provide any hierarchy. &lt;br /&gt;
| It provides a hierarchy. &lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Functionality and Behavior Comparison===&lt;br /&gt;
The functionality and behavior of Mixins in Ruby is compared with Interfaces in Java&lt;br /&gt;
&lt;br /&gt;
====Comparable Functionality====&lt;br /&gt;
Sometime we are required to compare the class members of different objects of the same class and/or sort them. This is the comparable functionality that we are trying to achieve for a given class.  This functionality can be achieved by both the Ruby using mixins and Java using interfaces. There is not much difference using both.&lt;br /&gt;
&lt;br /&gt;
'''Ruby'''&lt;br /&gt;
&lt;br /&gt;
The [http://www.ruby-doc.org/core/classes/Comparable.html Comparable] mixin is used by classes whose objects may be ordered. The class must define the &amp;lt;=&amp;gt; operator, which compares the receiver against another object, returning -1, 0, or +1 depending on whether the receiver is less than, equal to, or greater than the other object. Comparable uses &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method between? as seen in the following example&amp;lt;ref&amp;gt;http://corelib.rubyonrails.org/classes/Comparable.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  class SizeMatters&lt;br /&gt;
     include Comparable&lt;br /&gt;
     attr :str&lt;br /&gt;
     def &amp;lt;=&amp;gt;(anOther)&lt;br /&gt;
       str.size &amp;lt;=&amp;gt; anOther.str.size&lt;br /&gt;
     end&lt;br /&gt;
     def initialize(str)&lt;br /&gt;
       @str = str&lt;br /&gt;
     end&lt;br /&gt;
     def inspect&lt;br /&gt;
       @str&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
''using class:'' &lt;br /&gt;
&lt;br /&gt;
   s1 = SizeMatters.new(&amp;quot;Z&amp;quot;)&lt;br /&gt;
   s2 = SizeMatters.new(&amp;quot;YY&amp;quot;)&lt;br /&gt;
   s3 = SizeMatters.new(&amp;quot;XXX&amp;quot;)&lt;br /&gt;
   s4 = SizeMatters.new(&amp;quot;WWWW&amp;quot;)&lt;br /&gt;
   s5 = SizeMatters.new(&amp;quot;VVVVV&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
''output:''&lt;br /&gt;
   s1 &amp;lt; s2                       #=&amp;gt; true&lt;br /&gt;
   s4.between?(s1, s3)           #=&amp;gt; false&lt;br /&gt;
   s4.between?(s3, s5)           #=&amp;gt; true&lt;br /&gt;
   [ s3, s2, s5, s4, s1 ].sort   #=&amp;gt; [Z, YY, XXX, WWWW, VVVVV]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Java'''&lt;br /&gt;
&lt;br /&gt;
In Java, [http://www.java-tips.org/java-se-tips/java.lang/how-to-use-comparable-interface.html comparable functionality] is achieved by extending the “Comparable” interface. List of objects that implement this interface can be sorted automatically by sort method of the list interface. The interface in the following example &amp;lt;ref&amp;gt; http://www.java-tips.org/java-se-tips/java.lang/how-to-use-comparable-interface.html &amp;lt;/ref&amp;gt; has compareTo() method that is used by the sort() method of the list. Here compareTo() method can also be used to compare two objects with respect to the implementation in compareTo() implementation in the derived class. It returns ‘0’ , ‘1’ and ‘-1’ corresponding to whether one is equal to , greater than or lesser than other.&lt;br /&gt;
In this example code Employee class is implementing Comparable interface and have method compareTo().&lt;br /&gt;
  public class Employee implements Comparable {&lt;br /&gt;
    int EmpID;&lt;br /&gt;
    String Ename;&lt;br /&gt;
    double Sal;&lt;br /&gt;
    static int i;&lt;br /&gt;
    public Employee() {&lt;br /&gt;
        EmpID = i++;&lt;br /&gt;
        Ename = &amp;quot;dont know&amp;quot;;&lt;br /&gt;
        Sal = 0.0;&lt;br /&gt;
    }&lt;br /&gt;
    public Employee(String ename, double sal) {&lt;br /&gt;
        EmpID = i++;&lt;br /&gt;
        Ename = ename;&lt;br /&gt;
        Sal = sal;&lt;br /&gt;
    }&lt;br /&gt;
    public String toString() {&lt;br /&gt;
        return &amp;quot;EmpID &amp;quot; + EmpID + &amp;quot;\n&amp;quot; + &amp;quot;Ename &amp;quot; + Ename + &amp;quot;\n&amp;quot; + &amp;quot;Sal&amp;quot; + Sal;&lt;br /&gt;
    }&lt;br /&gt;
    public int compareTo(Object o1) {&lt;br /&gt;
        if (this.Sal == ((Employee) o1).Sal)&lt;br /&gt;
            return 0;&lt;br /&gt;
        else if ((this.Sal) &amp;gt; ((Employee) o1).Sal)&lt;br /&gt;
            return 1;&lt;br /&gt;
        else&lt;br /&gt;
            return -1;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
ComparableDemo.java is showing the use of this interface. This class first makes a list of objects of type Employee and call sort method of java.util.Collections, which internally uses compareTo() method of Employee class and sort the list accordingly.&lt;br /&gt;
 &lt;br /&gt;
  import java.util.*;&lt;br /&gt;
  public class ComparableDemo{&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
        List ts1 = new ArrayList();&lt;br /&gt;
        ts1.add(new Employee (&amp;quot;Tom&amp;quot;,40000.00));&lt;br /&gt;
        ts1.add(new Employee (&amp;quot;Harry&amp;quot;,20000.00));&lt;br /&gt;
        ts1.add(new Employee (&amp;quot;Maggie&amp;quot;,50000.00));&lt;br /&gt;
        ts1.add(new Employee (&amp;quot;Chris&amp;quot;,70000.00));&lt;br /&gt;
        Collections.sort(ts1);&lt;br /&gt;
        Iterator itr = ts1.iterator();&lt;br /&gt;
        while(itr.hasNext()){&lt;br /&gt;
            Object element = itr.next();&lt;br /&gt;
            System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
''Output:''&lt;br /&gt;
  &lt;br /&gt;
  EmpID 1&lt;br /&gt;
  Ename Harry&lt;br /&gt;
  Sal    20000.0&lt;br /&gt;
  EmpID 0&lt;br /&gt;
  Ename Tom&lt;br /&gt;
  Sal    40000.0&lt;br /&gt;
  EmpID 2&lt;br /&gt;
  Ename Maggie&lt;br /&gt;
  Sal    50000.0&lt;br /&gt;
  EmpID 3&lt;br /&gt;
  Ename Chris&lt;br /&gt;
  Sal    70000.0&lt;br /&gt;
&lt;br /&gt;
====Singleton Behaviour====&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Singleton_pattern Singleton] is a class guaranteed to have only a single instance, with a single global access point to that instance. It can be implemented in Ruby and Java. In Ruby we can include a module to acquire all the functionality of the singleton module. The code is already there and we are just reusing the code and adding that similar functionality into our class. Here our intention is to add the functionality of singleton to the class which is achieved using mixins. In java, implementation of interfaces means that the class which implements the interface becomes a type of that class, which is not the intention here. So, there is not interface which class can implement to get the functionality in itself. It has to implement the functionality itself without implementing any interface. &lt;br /&gt;
&lt;br /&gt;
'''Ruby'''&lt;br /&gt;
&lt;br /&gt;
When you include the singleton method inside your class it makes the new method private so that you cannot use it. Including ‘singleton’ adds a class method called instance that instantiates only one instance of the class&lt;br /&gt;
&lt;br /&gt;
  # require singleton lib&lt;br /&gt;
  require 'singleton'&lt;br /&gt;
  class AppConfig&lt;br /&gt;
    # mixin the singleton module&lt;br /&gt;
    include Singleton&lt;br /&gt;
    # do the actual app configuration&lt;br /&gt;
    def load_config(file)&lt;br /&gt;
      # do your work here&lt;br /&gt;
      puts &amp;quot;Application configuration file was loaded from file: #{file}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
''output:''&lt;br /&gt;
&lt;br /&gt;
  conf1 = AppConfig.instance&lt;br /&gt;
  conf1.load_config &amp;quot;/home/khelll/conf.yml&amp;quot;       #=&amp;gt;Application configuration file was loaded from file: /home/khelll/conf.yml&lt;br /&gt;
  conf2 = AppConfig.instance&lt;br /&gt;
  puts conf1 == conf2      #=&amp;gt;true&lt;br /&gt;
  # notice the following 2 lines won’t work&lt;br /&gt;
  AppConfig.new rescue(puts $!)   #=&amp;gt; new method is private&lt;br /&gt;
&lt;br /&gt;
====Enumerable Behaviour====&lt;br /&gt;
This [http://www.ruby-doc.org/core/classes/Enumerable.html enumerable] functionality adds support to the array members of the class. We can achieve this functionality using mixins in Ruby while it is not possible to do it in Java using interfaces.&lt;br /&gt;
&lt;br /&gt;
'''Implementation in Ruby:''' &lt;br /&gt;
&lt;br /&gt;
It adds the functionality of map, inject, select etc to the class which includes the ‘Enumerable’ module. The class which includes should represent a collection of items. Enumerable requires that your class contain an each method that serves up the items in the collection. All the other Enumerable methods rely on this. &lt;br /&gt;
Example:&lt;br /&gt;
This is a Team class that manages a group of members &amp;lt;ref&amp;gt;http://kconrails.com/2010/11/30/ruby-enumerable-primer-part-1-the-basics/&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
  class Team&lt;br /&gt;
    include Enumerable&lt;br /&gt;
    attr_accessor :members&lt;br /&gt;
    def initialize&lt;br /&gt;
      @members = []&lt;br /&gt;
    end&lt;br /&gt;
    def each &amp;amp;block&lt;br /&gt;
      @members.each{|member| block.call(member)}&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Enumerable requires that your class contain an each method that serves up the items in the collection. All the other Enumerablemethods rely on this. Now we can use the map method, for instance:&lt;br /&gt;
&lt;br /&gt;
  team = Team.new&lt;br /&gt;
  team.members = ['joshua', 'gabriel', 'jacob']&lt;br /&gt;
  team.map{|member| member.capitalize}   # =&amp;gt; [&amp;quot;Joshua&amp;quot;, &amp;quot;Gabriel&amp;quot;, &amp;quot;Jacob&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
Now we can call any Enumerable methods on our team object itself, and it will assume we want to work with the members array within.&lt;br /&gt;
&lt;br /&gt;
====Data Mapper Behaviour====&lt;br /&gt;
&lt;br /&gt;
[http://datamapper.org/docs/ DataMapper] is an API which comes with the ability to use the same API to talk to a multitude of different datastores. There are adapters for the usual RDBMS suspects, NoSQL stores, various file formats and even some popular webservices. This is implemented in ruby using mixins. We need not use interfaces to implement.&lt;br /&gt;
&lt;br /&gt;
'''Implementation in Ruby:'''&lt;br /&gt;
  class Post&lt;br /&gt;
       include DataMapper::Resource&lt;br /&gt;
       property :id, Serial&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
The point to take away from this is not that Ruby Mixins are more powerful than Java Interfaces. The mixin feature of Ruby allows for the development of easy to use but powerful plugins that will be hard for any non-Ruby based framework to compete with. Java has many other ways of implementing the functionality mixins are capable of. It is just that it does not use Interfaces to achieve the functionality. Even though both of them are a way of achieving multiple inheritance in their respective languages, they achieve quiet different goals. Inheritance in Java is mainly to overcome the confusion which arises with multiple inheritance, while Mixins in Ruby is mainly to add an additional functionality to the existing class apart from the ‘primary functionality’ of the class. &lt;br /&gt;
&lt;br /&gt;
When a class implements Interface, it indicates that it is of the type interface class and forms a hierarchy with the interface class, while ruby mixins are mainly intended for code reuse and including an additional functionality to the class. It does not form any hierarchy as it is only using module of the class.&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* http://en.wikipedia.org/wiki/Interface_(Java)&lt;br /&gt;
* http://en.wikipedia.org/wiki/Mixin&lt;br /&gt;
* http://carboni.ca/blog/p/Modules-How-Do-They-Work&lt;br /&gt;
* http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/&lt;br /&gt;
* http://rubylearning.com/satishtalim/modules_mixins.html&lt;br /&gt;
* http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-&lt;br /&gt;
* http://datamapper.org/&lt;br /&gt;
* http://www.dsource.org/projects/tutorials/wiki/SingletonPattern &lt;br /&gt;
* http://corelib.rubyonrails.org/classes/Comparable.html&lt;br /&gt;
* http://kconrails.com/2010/11/30/ruby-enumerable-primer-part-1-the-basics/&lt;br /&gt;
* http://stackoverflow.com/questions/575920/is-a-ruby-module-equivalent-to-a-java-interface&lt;/div&gt;</summary>
		<author><name>Sthirum</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2c_bs&amp;diff=51687</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2c bs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2c_bs&amp;diff=51687"/>
		<updated>2011-10-02T01:37:11Z</updated>

		<summary type="html">&lt;p&gt;Sthirum: /* Enumerable Behaviour */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
An [http://en.wikipedia.org/wiki/Interface_(computing) interface] &amp;lt;ref&amp;gt;interface&amp;lt;/ref&amp;gt; in the [http://en.wikipedia.org/wiki/Java_(programming_language) Java] programming language is an abstract type that is used to specify an interface that classes must implement. Interface may only contain [http://en.wikipedia.org/wiki/Method_signature method signature] and constant declarations. An interface may never contain method definitions. Interfaces cannot be instantiated. A class that implements an interface must implement all of the methods described in the interface, or be an [http://en.wikipedia.org/wiki/Abstract_type abstract] class. Object references in Java may be specified to be of an interface type; in which case, they must either be null, or be bound to an object that implements the interface. &lt;br /&gt;
&lt;br /&gt;
In object-oriented programming languages, a [http://en.wikipedia.org/wiki/Mixin mixin] &amp;lt;ref&amp;gt; mixin &amp;lt;/ref&amp;gt; is a class that provides a certain functionality to be inherited or just reused by a subclass. Some of the functionality of mixins is provided by interfaces in popular languages like Java and [http://en.wikipedia.org/wiki/C_Sharp_(programming_language) C#]. However, an interface only specifies what the class must support and cannot provide an implementation. Another class, providing an implementation and dependent with the interface, is needed for [http://en.wikipedia.org/wiki/Code_refactoring refactoring] common behavior into a single place. Interfaces combined with aspect-oriented programming can produce full-fledged mixins in languages that support such features, such as C# or Java. &lt;br /&gt;
One benefit of using interfaces is that they simulate [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance]. All classes in Java other than [https://matrix.senecac.on.ca:8443/wiki/javaSE/index.php/Java.lang.Object java.lang.Object], must have exactly one base class; multiple inheritance of classes is not allowed.&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
===Mixins in Ruby=== &lt;br /&gt;
In Java you just have [http://en.wikipedia.org/wiki/Class_(computer_programming) classes] (both abstract and concrete) and interfaces. The [http://en.wikipedia.org/wiki/Ruby Ruby] language provides classes,[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html modules], and a mix of both. Inheriting from a mixin is not a form of specialization but is rather a means of collecting functionality. A class may inherit most or all of its functionality from one or more mixins through multiple inheritance. A mixin can also be viewed as an interface with implemented methods. When a class includes a mixin, the class implements the interface and includes, rather than inherits, all the mixins’ attributes (fields, properties) and methods. They become part of the class during compilation. A mixin can defer definition and binding of methods until runtime, though attributes and instantiation parameters are still defined at compile time. In the Ruby language a mixin is a class that is mixed with a module. &lt;br /&gt;
&lt;br /&gt;
====Module====&lt;br /&gt;
&lt;br /&gt;
A [http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html module]&amp;lt;ref&amp;gt;module&amp;lt;/ref&amp;gt; is a degenerate abstract class. A module can’t be instantiated and no class can directly extend it but it can fully implement methods. A class can leverage the implementation of a module by including the module’s methods. A module can define methods that can be shared in different and separate classes either at the class or instance level.We can view an example &amp;lt;ref&amp;gt; http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/&amp;lt;/ref&amp;gt; module that converts a numeric value to its English word.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # Convert a integer value to English.&lt;br /&gt;
  module Stringify&lt;br /&gt;
    # Requires an instance variable @value&lt;br /&gt;
    def stringify&lt;br /&gt;
      if @value == 1&lt;br /&gt;
        &amp;quot;One&amp;quot;&lt;br /&gt;
      elsif @value == 2&lt;br /&gt;
        &amp;quot;Two&amp;quot;&lt;br /&gt;
      elsif @value == 3&lt;br /&gt;
        &amp;quot;Three&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The Stringify module above makes use of a @value [http://en.wikipedia.org/wiki/Instance_variable instance variable]. The class that will be mixed with this module needs to define and set a @value instance variable since the Stringify module uses it but does not define it. In addition to instance variables a module could invoke methods not defined in the module but in the class that it will be mixed with.&lt;br /&gt;
&lt;br /&gt;
A [http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/ Self contained module] can also be written in Ruby where the methods defined in a module can be used as static methods independent of their implementation within a class.&lt;br /&gt;
&lt;br /&gt;
'''Self Contained Module Example: '''&lt;br /&gt;
&lt;br /&gt;
  # A Math module akin to Java Math class.&lt;br /&gt;
  module Math&lt;br /&gt;
    # Could be called as a class, static, method&lt;br /&gt;
    def add(val_one, val_two)&lt;br /&gt;
      BigInteger.new(val_one + val_two)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The methods in the Math module are intended to be invoked like class methods, also known as [http://en.wikipedia.org/wiki/Method_(computer_programming)#Static_methods static] methods. The add method in the Math module accepts two integer values and returns an [http://en.wikipedia.org/wiki/Instance_(computer_science) instance] of BigInteger.&lt;br /&gt;
&lt;br /&gt;
====Using Mixins====&lt;br /&gt;
&lt;br /&gt;
Implementation of the class and module are joined, interwined, combined, etc. A mixin is a different mechanism to the extend construct used to add concrete implementation to a class. With a mixin you can extend from a module instead of a class. &lt;br /&gt;
&lt;br /&gt;
The example of self-contained module shows how the method returns a BigInteger sum after accepting two integer parameters. The following example is the implementation of BigInteger class. The BigInteger class defines one constructor and directly inherits one method from the Number base class. To mix in the methods implemented in the Stringify and Math modules with the BigInteger class you will note the usage of the include and extend methods, respectively.&lt;br /&gt;
&lt;br /&gt;
  # Base Number class&lt;br /&gt;
  class Number&lt;br /&gt;
    def intValue&lt;br /&gt;
      @value&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  # BigInteger extends Number&lt;br /&gt;
  class BigInteger &amp;lt; Number&lt;br /&gt;
 &lt;br /&gt;
  # Add instance methods from Stringify&lt;br /&gt;
  include Stringify&lt;br /&gt;
 &lt;br /&gt;
  # Add class methods from Math&lt;br /&gt;
  extend Math&lt;br /&gt;
 &lt;br /&gt;
  # Add a constructor with one parameter&lt;br /&gt;
    def initialize(value)&lt;br /&gt;
      @value = value&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Using the class:&lt;br /&gt;
&lt;br /&gt;
  # Create a new object&lt;br /&gt;
  bigint1 = BigInteger.new(10)&lt;br /&gt;
  # Call a method inherited from the base class&lt;br /&gt;
  puts bigint1.intValue   # --&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
The extend method will mix a module’s methods at the class level. The method defined in the Math module can be used as a class/static method.&lt;br /&gt;
&lt;br /&gt;
  # Call class method extended from Math&lt;br /&gt;
  bigint2 = BigInteger.add(-2, 4)&lt;br /&gt;
  puts bigint2.intValue   # --&amp;gt; 2&lt;br /&gt;
&lt;br /&gt;
The include method will mix a module’s methods at the instance level, meaning that the methods will become instance methods. The method defined in the Stringify module can be used as an instance method.&lt;br /&gt;
&lt;br /&gt;
  # Call a method included from Stringify&lt;br /&gt;
  puts bigint2.stringify   # --&amp;gt; 'Two'&lt;br /&gt;
&lt;br /&gt;
There is another use of the extend method. You can enhance an object instance by mixing it with a module at run time. This ‘CurrencyFormatter’ is used to extend an object, changing it’s responsibilities at runtime.&lt;br /&gt;
&lt;br /&gt;
  # Format a numeric value as a currency&lt;br /&gt;
  module CurrencyFormatter&lt;br /&gt;
    def format&lt;br /&gt;
      &amp;quot;$#{@value}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
To mix an object instance with a module you can do the following:&lt;br /&gt;
&lt;br /&gt;
  # Add the module methods to&lt;br /&gt;
  # this object instance, only!&lt;br /&gt;
  bigint2.extend CurrencyFormatter&lt;br /&gt;
  puts bigint2.format   # --&amp;gt; '$2'&lt;br /&gt;
&lt;br /&gt;
Calling the extend method on an instance will only extend that one object; objects of the same class will not be extended with the new functionality.&lt;br /&gt;
&lt;br /&gt;
  puts bigint1.format   # will generate an error&lt;br /&gt;
&lt;br /&gt;
Modules that will be mixed with a class via the include or extend method could define something like a constructor or initializer method to the module. The module initializer method will be invoked at the time the module is mixed with a class. When a class extends a module the module’s self.extended method will be invoked:&lt;br /&gt;
&lt;br /&gt;
  module Math&lt;br /&gt;
    def self.extended(base)&lt;br /&gt;
      # Initialize module.&lt;br /&gt;
    end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
The self prefix indicates that the method is a static module level method. The base parameter in the static extended method will be either an instance object or class object of the class that extended the module depending whether you extend a object or class, respectively.&lt;br /&gt;
When a class includes a module the module’s self.included method will be invoked.&lt;br /&gt;
&lt;br /&gt;
  module Stringify&lt;br /&gt;
    def self.included(base)&lt;br /&gt;
      # Initialize module.&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The base parameter will be a class object for the class that includes the module.&lt;br /&gt;
It is important to note that inside the included and extended initializer methods you can include and extend other modules. &lt;br /&gt;
&lt;br /&gt;
  module Stringify&lt;br /&gt;
    def self.included(base)&lt;br /&gt;
      base.extend SomeOtherModule&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===Interfaces in Java===&lt;br /&gt;
Interfaces define a standardized set of commands that a class will obey. The commands are a set of methods that a class implements. The interface definition states the names of the methods and their return types and argument signatures. There is no executable body for any method. Example: &lt;br /&gt;
  interface Bicycle {&lt;br /&gt;
       void changeCadence(int newValue);   // wheel revolutions per minute&lt;br /&gt;
       void changeGear(int newValue);&lt;br /&gt;
       void speedUp(int increment);&lt;br /&gt;
       void applyBrakes(int decrement);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Implementation is independent to each class that implements the interface. Once a class implements an interface, the Java compiler knows that an instance of the class will contain the specified set of methods. Therefore, it will allow you to call those methods for an object referenced by a variable whose type is the interface. Implementing an interface enables a class to be &amp;quot;plugged in&amp;quot; in any situation that requires a specific behavior (manifested through the set of methods).&lt;br /&gt;
&lt;br /&gt;
  class ACMEBicycle implements Bicycle {&lt;br /&gt;
    //implementation specific to this class&lt;br /&gt;
       int cadence = 0;&lt;br /&gt;
       int speed = 0;&lt;br /&gt;
       int gear = 1;&lt;br /&gt;
       void changeCadence(int newValue) {&lt;br /&gt;
            cadence = newValue;&lt;br /&gt;
       }&lt;br /&gt;
       void changeGear(int newValue) {&lt;br /&gt;
            gear = newValue;&lt;br /&gt;
       }&lt;br /&gt;
       void speedUp(int increment) {&lt;br /&gt;
            speed = speed + increment;   &lt;br /&gt;
       }&lt;br /&gt;
       void applyBrakes(int decrement) {&lt;br /&gt;
            speed = speed - decrement;&lt;br /&gt;
       }&lt;br /&gt;
       void printStates() {&lt;br /&gt;
            System.out.println(&amp;quot;cadence:&amp;quot;+cadence+&amp;quot; speed:&amp;quot;+speed+&amp;quot; gear:&amp;quot;+gear);&lt;br /&gt;
       }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
  class MOUNTAINBicycle implements Bicycle {&lt;br /&gt;
  int cadence = 2;&lt;br /&gt;
       int speed = 3;&lt;br /&gt;
       int gear = 5;&lt;br /&gt;
       void changeCadence(int newValue) {&lt;br /&gt;
            cadence = newValue + 10;&lt;br /&gt;
       }&lt;br /&gt;
       void changeGear(int newValue) {&lt;br /&gt;
            gear = newValue + 4;&lt;br /&gt;
       }&lt;br /&gt;
       void speedUp(int increment) {&lt;br /&gt;
            speed = speed + increment + 10;   &lt;br /&gt;
       }&lt;br /&gt;
       void applyBrakes(int decrement) {&lt;br /&gt;
            speed = speed – decrement – 10;&lt;br /&gt;
       }&lt;br /&gt;
       void printStates() {&lt;br /&gt;
            System.out.println(&amp;quot;cadence:&amp;quot;+cadence+&amp;quot; speed:&amp;quot;+speed+&amp;quot; gear:&amp;quot;+gear);&lt;br /&gt;
       }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Using an interface rather than inheritance to specify a certain set of methods allows a class to inherit from some other class. In other words, if a class needs two different sets of methods, so it can behave like two different types of things, it could inherit one set from class A, and use an interface B to specify the other.&lt;br /&gt;
You could then reference one of these objects with either an A reference or a B reference. Implementing an interface allows a class to become more formal about the behavior it promises to provide. Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler. If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Mixins vs Interfaces==&lt;br /&gt;
Because of this mixin feature, a developer can add arbitrary methods and modify behavior of core classes at runtime. This is amazingly powerful if you are trying to write plugins and extensions to the [http://en.wikipedia.org/wiki/Software_framework framework].  Because you can add functionality to existing objects, users can install their [http://en.wikipedia.org/wiki/Plug-in_(computing) plugin] and start taking advantage of new functionality without having to make changes to the objects that they are instantiating in their application. &lt;br /&gt;
&lt;br /&gt;
In frameworks written in other languages, such as Java, plugging in new functionality means that you need to change how your objects are instantiated. This will require code changes and/or potentially configuration changes which makes it hard to develop, hard to maintain, and a pain for plugin developers to support.  But because of the mixin feature, Rails plugin developers can customize the base objects and the users of the plugins do not have to change any of their code or configuration logic.&lt;br /&gt;
&lt;br /&gt;
===Differences===&lt;br /&gt;
&lt;br /&gt;
These is a general comparision: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Mixins in Ruby&lt;br /&gt;
! Interfaces in Java&lt;br /&gt;
|-&lt;br /&gt;
| 'include' or 'extend' keywords used to include the module in the class.&lt;br /&gt;
| 'implement' keyword is used to implement interface&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Need not implement the module methods in the class.&lt;br /&gt;
| Should implement the interface methods. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| The methods are not copied into the class in which they are included. They are referenced.&lt;br /&gt;
| The methods’ implementation is in the class itself.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| The included method definition is decided at ‘run time’. If the definition changes while running the function points to the changed code.&lt;br /&gt;
| The definition is known compile time and will not change until the code is compiled and run again. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Modules inclusion does not provide a superclass and subclass relationship.&lt;br /&gt;
| Interface extending provides a superclass and subclass relationship. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| When we use mixins we included only a module of the class. &lt;br /&gt;
| We include the entire interface class. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Mainly for code reuse and does not provide any hierarchy. &lt;br /&gt;
| It provides a hierarchy. &lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Functionality and Behavior Comparison===&lt;br /&gt;
The functionality and behavior of Mixins in Ruby is compared with Interfaces in Java&lt;br /&gt;
&lt;br /&gt;
====Comparable Functionality====&lt;br /&gt;
Sometime we are required to compare the class members of different objects of the same class and/or sort them. This is the comparable functionality that we are trying to achieve for a given class.  This functionality can be achieved by both the Ruby using mixins and Java using interfaces. There is not much difference using both.&lt;br /&gt;
&lt;br /&gt;
'''Ruby'''&lt;br /&gt;
&lt;br /&gt;
The [http://www.ruby-doc.org/core/classes/Comparable.html Comparable] mixin is used by classes whose objects may be ordered. The class must define the &amp;lt;=&amp;gt; operator, which compares the receiver against another object, returning -1, 0, or +1 depending on whether the receiver is less than, equal to, or greater than the other object. Comparable uses &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method between?.&lt;br /&gt;
&lt;br /&gt;
  class SizeMatters&lt;br /&gt;
     include Comparable&lt;br /&gt;
     attr :str&lt;br /&gt;
     def &amp;lt;=&amp;gt;(anOther)&lt;br /&gt;
       str.size &amp;lt;=&amp;gt; anOther.str.size&lt;br /&gt;
     end&lt;br /&gt;
     def initialize(str)&lt;br /&gt;
       @str = str&lt;br /&gt;
     end&lt;br /&gt;
     def inspect&lt;br /&gt;
       @str&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
''using class:'' &lt;br /&gt;
&lt;br /&gt;
   s1 = SizeMatters.new(&amp;quot;Z&amp;quot;)&lt;br /&gt;
   s2 = SizeMatters.new(&amp;quot;YY&amp;quot;)&lt;br /&gt;
   s3 = SizeMatters.new(&amp;quot;XXX&amp;quot;)&lt;br /&gt;
   s4 = SizeMatters.new(&amp;quot;WWWW&amp;quot;)&lt;br /&gt;
   s5 = SizeMatters.new(&amp;quot;VVVVV&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
''output:''&lt;br /&gt;
   s1 &amp;lt; s2                       #=&amp;gt; true&lt;br /&gt;
   s4.between?(s1, s3)           #=&amp;gt; false&lt;br /&gt;
   s4.between?(s3, s5)           #=&amp;gt; true&lt;br /&gt;
   [ s3, s2, s5, s4, s1 ].sort   #=&amp;gt; [Z, YY, XXX, WWWW, VVVVV]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Java'''&lt;br /&gt;
&lt;br /&gt;
In Java, [http://www.java-tips.org/java-se-tips/java.lang/how-to-use-comparable-interface.html comparable functionality] is achieved by extending the “Comparable” interface. List of objects that implement this interface can be sorted automatically by sort method of the list interface. The interface in the following example &amp;lt;ref&amp;gt; http://www.java-tips.org/java-se-tips/java.lang/how-to-use-comparable-interface.html &amp;lt;/ref&amp;gt; has compareTo() method that is used by the sort() method of the list. Here compareTo() method can also be used to compare two objects with respect to the implementation in compareTo() implementation in the derived class. It returns ‘0’ , ‘1’ and ‘-1’ corresponding to whether one is equal to , greater than or lesser than other.&lt;br /&gt;
In this example code Employee class is implementing Comparable interface and have method compareTo().&lt;br /&gt;
  public class Employee implements Comparable {&lt;br /&gt;
    int EmpID;&lt;br /&gt;
    String Ename;&lt;br /&gt;
    double Sal;&lt;br /&gt;
    static int i;&lt;br /&gt;
    public Employee() {&lt;br /&gt;
        EmpID = i++;&lt;br /&gt;
        Ename = &amp;quot;dont know&amp;quot;;&lt;br /&gt;
        Sal = 0.0;&lt;br /&gt;
    }&lt;br /&gt;
    public Employee(String ename, double sal) {&lt;br /&gt;
        EmpID = i++;&lt;br /&gt;
        Ename = ename;&lt;br /&gt;
        Sal = sal;&lt;br /&gt;
    }&lt;br /&gt;
    public String toString() {&lt;br /&gt;
        return &amp;quot;EmpID &amp;quot; + EmpID + &amp;quot;\n&amp;quot; + &amp;quot;Ename &amp;quot; + Ename + &amp;quot;\n&amp;quot; + &amp;quot;Sal&amp;quot; + Sal;&lt;br /&gt;
    }&lt;br /&gt;
    public int compareTo(Object o1) {&lt;br /&gt;
        if (this.Sal == ((Employee) o1).Sal)&lt;br /&gt;
            return 0;&lt;br /&gt;
        else if ((this.Sal) &amp;gt; ((Employee) o1).Sal)&lt;br /&gt;
            return 1;&lt;br /&gt;
        else&lt;br /&gt;
            return -1;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
ComparableDemo.java is showing the use of this interface. This class first makes a list of objects of type Employee and call sort method of java.util.Collections, which internally uses compareTo() method of Employee class and sort the list accordingly.&lt;br /&gt;
 &lt;br /&gt;
  import java.util.*;&lt;br /&gt;
  public class ComparableDemo{&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
        List ts1 = new ArrayList();&lt;br /&gt;
        ts1.add(new Employee (&amp;quot;Tom&amp;quot;,40000.00));&lt;br /&gt;
        ts1.add(new Employee (&amp;quot;Harry&amp;quot;,20000.00));&lt;br /&gt;
        ts1.add(new Employee (&amp;quot;Maggie&amp;quot;,50000.00));&lt;br /&gt;
        ts1.add(new Employee (&amp;quot;Chris&amp;quot;,70000.00));&lt;br /&gt;
        Collections.sort(ts1);&lt;br /&gt;
        Iterator itr = ts1.iterator();&lt;br /&gt;
        while(itr.hasNext()){&lt;br /&gt;
            Object element = itr.next();&lt;br /&gt;
            System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
''Output:''&lt;br /&gt;
  &lt;br /&gt;
  EmpID 1&lt;br /&gt;
  Ename Harry&lt;br /&gt;
  Sal    20000.0&lt;br /&gt;
  EmpID 0&lt;br /&gt;
  Ename Tom&lt;br /&gt;
  Sal    40000.0&lt;br /&gt;
  EmpID 2&lt;br /&gt;
  Ename Maggie&lt;br /&gt;
  Sal    50000.0&lt;br /&gt;
  EmpID 3&lt;br /&gt;
  Ename Chris&lt;br /&gt;
  Sal    70000.0&lt;br /&gt;
&lt;br /&gt;
====Singleton Behaviour====&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Singleton_pattern Singleton] is a class guaranteed to have only a single instance, with a single global access point to that instance. It can be implemented in Ruby and Java. In Ruby we can include a module to acquire all the functionality of the singleton module. The code is already there and we are just reusing the code and adding that similar functionality into our class. Here our intention is to add the functionality of singleton to the class which is achieved using mixins. In java, implementation of interfaces means that the class which implements the interface becomes a type of that class, which is not the intention here. So, there is not interface which class can implement to get the functionality in itself. It has to implement the functionality itself without implementing any interface. &lt;br /&gt;
&lt;br /&gt;
'''Ruby'''&lt;br /&gt;
&lt;br /&gt;
When you include the singleton method inside your class it makes the new method private so that you cannot use it. Including ‘singleton’ adds a class method called instance that instantiates only one instance of the class&lt;br /&gt;
&lt;br /&gt;
  # require singleton lib&lt;br /&gt;
  require 'singleton'&lt;br /&gt;
  class AppConfig&lt;br /&gt;
    # mixin the singleton module&lt;br /&gt;
    include Singleton&lt;br /&gt;
    # do the actual app configuration&lt;br /&gt;
    def load_config(file)&lt;br /&gt;
      # do your work here&lt;br /&gt;
      puts &amp;quot;Application configuration file was loaded from file: #{file}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
''output:''&lt;br /&gt;
&lt;br /&gt;
  conf1 = AppConfig.instance&lt;br /&gt;
  conf1.load_config &amp;quot;/home/khelll/conf.yml&amp;quot;       #=&amp;gt;Application configuration file was loaded from file: /home/khelll/conf.yml&lt;br /&gt;
  conf2 = AppConfig.instance&lt;br /&gt;
  puts conf1 == conf2      #=&amp;gt;true&lt;br /&gt;
  # notice the following 2 lines won’t work&lt;br /&gt;
  AppConfig.new rescue(puts $!)   #=&amp;gt; new method is private&lt;br /&gt;
&lt;br /&gt;
====Enumerable Behaviour====&lt;br /&gt;
This [http://www.ruby-doc.org/core/classes/Enumerable.html enumerable] functionality adds support to the array members of the class. We can achieve this functionality using mixins in Ruby while it is not possible to do it in Java using interfaces.&lt;br /&gt;
&lt;br /&gt;
'''Implementation in Ruby:''' &lt;br /&gt;
&lt;br /&gt;
It adds the functionality of map, inject, select etc to the class which includes the ‘Enumerable’ module. The class which includes should represent a collection of items. Enumerable requires that your class contain an each method that serves up the items in the collection. All the other Enumerable methods rely on this. &lt;br /&gt;
Example:&lt;br /&gt;
This is a Team class that manages a group of members &amp;lt;ref&amp;gt;http://kconrails.com/2010/11/30/ruby-enumerable-primer-part-1-the-basics/&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
  class Team&lt;br /&gt;
    include Enumerable&lt;br /&gt;
    attr_accessor :members&lt;br /&gt;
    def initialize&lt;br /&gt;
      @members = []&lt;br /&gt;
    end&lt;br /&gt;
    def each &amp;amp;block&lt;br /&gt;
      @members.each{|member| block.call(member)}&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Enumerable requires that your class contain an each method that serves up the items in the collection. All the other Enumerablemethods rely on this. Now we can use the map method, for instance:&lt;br /&gt;
&lt;br /&gt;
  team = Team.new&lt;br /&gt;
  team.members = ['joshua', 'gabriel', 'jacob']&lt;br /&gt;
  team.map{|member| member.capitalize}   # =&amp;gt; [&amp;quot;Joshua&amp;quot;, &amp;quot;Gabriel&amp;quot;, &amp;quot;Jacob&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
Now we can call any Enumerable methods on our team object itself, and it will assume we want to work with the members array within.&lt;br /&gt;
&lt;br /&gt;
====Data Mapper Behaviour====&lt;br /&gt;
&lt;br /&gt;
[http://datamapper.org/docs/ DataMapper] is an API which comes with the ability to use the same API to talk to a multitude of different datastores. There are adapters for the usual RDBMS suspects, NoSQL stores, various file formats and even some popular webservices. This is implemented in ruby using mixins. We need not use interfaces to implement.&lt;br /&gt;
&lt;br /&gt;
'''Implementation in Ruby:'''&lt;br /&gt;
  class Post&lt;br /&gt;
       include DataMapper::Resource&lt;br /&gt;
       property :id, Serial&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
The point to take away from this is not that Ruby Mixins are more powerful than Java Interfaces. The mixin feature of Ruby allows for the development of easy to use but powerful plugins that will be hard for any non-Ruby based framework to compete with. Java has many other ways of implementing the functionality mixins are capable of. It is just that it does not use Interfaces to achieve the functionality. Even though both of them are a way of achieving multiple inheritance in their respective languages, they achieve quiet different goals. Inheritance in Java is mainly to overcome the confusion which arises with multiple inheritance, while Mixins in Ruby is mainly to add an additional functionality to the existing class apart from the ‘primary functionality’ of the class. &lt;br /&gt;
&lt;br /&gt;
When a class implements Interface, it indicates that it is of the type interface class and forms a hierarchy with the interface class, while ruby mixins are mainly intended for code reuse and including an additional functionality to the class. It does not form any hierarchy as it is only using module of the class.&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* http://en.wikipedia.org/wiki/Interface_(Java)&lt;br /&gt;
* http://en.wikipedia.org/wiki/Mixin&lt;br /&gt;
* http://carboni.ca/blog/p/Modules-How-Do-They-Work&lt;br /&gt;
* http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/&lt;br /&gt;
* http://rubylearning.com/satishtalim/modules_mixins.html&lt;br /&gt;
* http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-&lt;br /&gt;
* http://datamapper.org/&lt;br /&gt;
* http://www.dsource.org/projects/tutorials/wiki/SingletonPattern &lt;br /&gt;
* http://corelib.rubyonrails.org/classes/Comparable.html&lt;br /&gt;
* http://kconrails.com/2010/11/30/ruby-enumerable-primer-part-1-the-basics/&lt;br /&gt;
* http://stackoverflow.com/questions/575920/is-a-ruby-module-equivalent-to-a-java-interface&lt;/div&gt;</summary>
		<author><name>Sthirum</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2c_bs&amp;diff=51686</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2c bs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2c_bs&amp;diff=51686"/>
		<updated>2011-10-02T01:32:47Z</updated>

		<summary type="html">&lt;p&gt;Sthirum: /* Comparable Functionality */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
An [http://en.wikipedia.org/wiki/Interface_(computing) interface] &amp;lt;ref&amp;gt;interface&amp;lt;/ref&amp;gt; in the [http://en.wikipedia.org/wiki/Java_(programming_language) Java] programming language is an abstract type that is used to specify an interface that classes must implement. Interface may only contain [http://en.wikipedia.org/wiki/Method_signature method signature] and constant declarations. An interface may never contain method definitions. Interfaces cannot be instantiated. A class that implements an interface must implement all of the methods described in the interface, or be an [http://en.wikipedia.org/wiki/Abstract_type abstract] class. Object references in Java may be specified to be of an interface type; in which case, they must either be null, or be bound to an object that implements the interface. &lt;br /&gt;
&lt;br /&gt;
In object-oriented programming languages, a [http://en.wikipedia.org/wiki/Mixin mixin] &amp;lt;ref&amp;gt; mixin &amp;lt;/ref&amp;gt; is a class that provides a certain functionality to be inherited or just reused by a subclass. Some of the functionality of mixins is provided by interfaces in popular languages like Java and [http://en.wikipedia.org/wiki/C_Sharp_(programming_language) C#]. However, an interface only specifies what the class must support and cannot provide an implementation. Another class, providing an implementation and dependent with the interface, is needed for [http://en.wikipedia.org/wiki/Code_refactoring refactoring] common behavior into a single place. Interfaces combined with aspect-oriented programming can produce full-fledged mixins in languages that support such features, such as C# or Java. &lt;br /&gt;
One benefit of using interfaces is that they simulate [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance]. All classes in Java other than [https://matrix.senecac.on.ca:8443/wiki/javaSE/index.php/Java.lang.Object java.lang.Object], must have exactly one base class; multiple inheritance of classes is not allowed.&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
===Mixins in Ruby=== &lt;br /&gt;
In Java you just have [http://en.wikipedia.org/wiki/Class_(computer_programming) classes] (both abstract and concrete) and interfaces. The [http://en.wikipedia.org/wiki/Ruby Ruby] language provides classes,[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html modules], and a mix of both. Inheriting from a mixin is not a form of specialization but is rather a means of collecting functionality. A class may inherit most or all of its functionality from one or more mixins through multiple inheritance. A mixin can also be viewed as an interface with implemented methods. When a class includes a mixin, the class implements the interface and includes, rather than inherits, all the mixins’ attributes (fields, properties) and methods. They become part of the class during compilation. A mixin can defer definition and binding of methods until runtime, though attributes and instantiation parameters are still defined at compile time. In the Ruby language a mixin is a class that is mixed with a module. &lt;br /&gt;
&lt;br /&gt;
====Module====&lt;br /&gt;
&lt;br /&gt;
A [http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html module]&amp;lt;ref&amp;gt;module&amp;lt;/ref&amp;gt; is a degenerate abstract class. A module can’t be instantiated and no class can directly extend it but it can fully implement methods. A class can leverage the implementation of a module by including the module’s methods. A module can define methods that can be shared in different and separate classes either at the class or instance level.We can view an example &amp;lt;ref&amp;gt; http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/&amp;lt;/ref&amp;gt; module that converts a numeric value to its English word.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # Convert a integer value to English.&lt;br /&gt;
  module Stringify&lt;br /&gt;
    # Requires an instance variable @value&lt;br /&gt;
    def stringify&lt;br /&gt;
      if @value == 1&lt;br /&gt;
        &amp;quot;One&amp;quot;&lt;br /&gt;
      elsif @value == 2&lt;br /&gt;
        &amp;quot;Two&amp;quot;&lt;br /&gt;
      elsif @value == 3&lt;br /&gt;
        &amp;quot;Three&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The Stringify module above makes use of a @value [http://en.wikipedia.org/wiki/Instance_variable instance variable]. The class that will be mixed with this module needs to define and set a @value instance variable since the Stringify module uses it but does not define it. In addition to instance variables a module could invoke methods not defined in the module but in the class that it will be mixed with.&lt;br /&gt;
&lt;br /&gt;
A [http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/ Self contained module] can also be written in Ruby where the methods defined in a module can be used as static methods independent of their implementation within a class.&lt;br /&gt;
&lt;br /&gt;
'''Self Contained Module Example: '''&lt;br /&gt;
&lt;br /&gt;
  # A Math module akin to Java Math class.&lt;br /&gt;
  module Math&lt;br /&gt;
    # Could be called as a class, static, method&lt;br /&gt;
    def add(val_one, val_two)&lt;br /&gt;
      BigInteger.new(val_one + val_two)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The methods in the Math module are intended to be invoked like class methods, also known as [http://en.wikipedia.org/wiki/Method_(computer_programming)#Static_methods static] methods. The add method in the Math module accepts two integer values and returns an [http://en.wikipedia.org/wiki/Instance_(computer_science) instance] of BigInteger.&lt;br /&gt;
&lt;br /&gt;
====Using Mixins====&lt;br /&gt;
&lt;br /&gt;
Implementation of the class and module are joined, interwined, combined, etc. A mixin is a different mechanism to the extend construct used to add concrete implementation to a class. With a mixin you can extend from a module instead of a class. &lt;br /&gt;
&lt;br /&gt;
The example of self-contained module shows how the method returns a BigInteger sum after accepting two integer parameters. The following example is the implementation of BigInteger class. The BigInteger class defines one constructor and directly inherits one method from the Number base class. To mix in the methods implemented in the Stringify and Math modules with the BigInteger class you will note the usage of the include and extend methods, respectively.&lt;br /&gt;
&lt;br /&gt;
  # Base Number class&lt;br /&gt;
  class Number&lt;br /&gt;
    def intValue&lt;br /&gt;
      @value&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  # BigInteger extends Number&lt;br /&gt;
  class BigInteger &amp;lt; Number&lt;br /&gt;
 &lt;br /&gt;
  # Add instance methods from Stringify&lt;br /&gt;
  include Stringify&lt;br /&gt;
 &lt;br /&gt;
  # Add class methods from Math&lt;br /&gt;
  extend Math&lt;br /&gt;
 &lt;br /&gt;
  # Add a constructor with one parameter&lt;br /&gt;
    def initialize(value)&lt;br /&gt;
      @value = value&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Using the class:&lt;br /&gt;
&lt;br /&gt;
  # Create a new object&lt;br /&gt;
  bigint1 = BigInteger.new(10)&lt;br /&gt;
  # Call a method inherited from the base class&lt;br /&gt;
  puts bigint1.intValue   # --&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
The extend method will mix a module’s methods at the class level. The method defined in the Math module can be used as a class/static method.&lt;br /&gt;
&lt;br /&gt;
  # Call class method extended from Math&lt;br /&gt;
  bigint2 = BigInteger.add(-2, 4)&lt;br /&gt;
  puts bigint2.intValue   # --&amp;gt; 2&lt;br /&gt;
&lt;br /&gt;
The include method will mix a module’s methods at the instance level, meaning that the methods will become instance methods. The method defined in the Stringify module can be used as an instance method.&lt;br /&gt;
&lt;br /&gt;
  # Call a method included from Stringify&lt;br /&gt;
  puts bigint2.stringify   # --&amp;gt; 'Two'&lt;br /&gt;
&lt;br /&gt;
There is another use of the extend method. You can enhance an object instance by mixing it with a module at run time. This ‘CurrencyFormatter’ is used to extend an object, changing it’s responsibilities at runtime.&lt;br /&gt;
&lt;br /&gt;
  # Format a numeric value as a currency&lt;br /&gt;
  module CurrencyFormatter&lt;br /&gt;
    def format&lt;br /&gt;
      &amp;quot;$#{@value}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
To mix an object instance with a module you can do the following:&lt;br /&gt;
&lt;br /&gt;
  # Add the module methods to&lt;br /&gt;
  # this object instance, only!&lt;br /&gt;
  bigint2.extend CurrencyFormatter&lt;br /&gt;
  puts bigint2.format   # --&amp;gt; '$2'&lt;br /&gt;
&lt;br /&gt;
Calling the extend method on an instance will only extend that one object; objects of the same class will not be extended with the new functionality.&lt;br /&gt;
&lt;br /&gt;
  puts bigint1.format   # will generate an error&lt;br /&gt;
&lt;br /&gt;
Modules that will be mixed with a class via the include or extend method could define something like a constructor or initializer method to the module. The module initializer method will be invoked at the time the module is mixed with a class. When a class extends a module the module’s self.extended method will be invoked:&lt;br /&gt;
&lt;br /&gt;
  module Math&lt;br /&gt;
    def self.extended(base)&lt;br /&gt;
      # Initialize module.&lt;br /&gt;
    end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
The self prefix indicates that the method is a static module level method. The base parameter in the static extended method will be either an instance object or class object of the class that extended the module depending whether you extend a object or class, respectively.&lt;br /&gt;
When a class includes a module the module’s self.included method will be invoked.&lt;br /&gt;
&lt;br /&gt;
  module Stringify&lt;br /&gt;
    def self.included(base)&lt;br /&gt;
      # Initialize module.&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The base parameter will be a class object for the class that includes the module.&lt;br /&gt;
It is important to note that inside the included and extended initializer methods you can include and extend other modules. &lt;br /&gt;
&lt;br /&gt;
  module Stringify&lt;br /&gt;
    def self.included(base)&lt;br /&gt;
      base.extend SomeOtherModule&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===Interfaces in Java===&lt;br /&gt;
Interfaces define a standardized set of commands that a class will obey. The commands are a set of methods that a class implements. The interface definition states the names of the methods and their return types and argument signatures. There is no executable body for any method. Example: &lt;br /&gt;
  interface Bicycle {&lt;br /&gt;
       void changeCadence(int newValue);   // wheel revolutions per minute&lt;br /&gt;
       void changeGear(int newValue);&lt;br /&gt;
       void speedUp(int increment);&lt;br /&gt;
       void applyBrakes(int decrement);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Implementation is independent to each class that implements the interface. Once a class implements an interface, the Java compiler knows that an instance of the class will contain the specified set of methods. Therefore, it will allow you to call those methods for an object referenced by a variable whose type is the interface. Implementing an interface enables a class to be &amp;quot;plugged in&amp;quot; in any situation that requires a specific behavior (manifested through the set of methods).&lt;br /&gt;
&lt;br /&gt;
  class ACMEBicycle implements Bicycle {&lt;br /&gt;
    //implementation specific to this class&lt;br /&gt;
       int cadence = 0;&lt;br /&gt;
       int speed = 0;&lt;br /&gt;
       int gear = 1;&lt;br /&gt;
       void changeCadence(int newValue) {&lt;br /&gt;
            cadence = newValue;&lt;br /&gt;
       }&lt;br /&gt;
       void changeGear(int newValue) {&lt;br /&gt;
            gear = newValue;&lt;br /&gt;
       }&lt;br /&gt;
       void speedUp(int increment) {&lt;br /&gt;
            speed = speed + increment;   &lt;br /&gt;
       }&lt;br /&gt;
       void applyBrakes(int decrement) {&lt;br /&gt;
            speed = speed - decrement;&lt;br /&gt;
       }&lt;br /&gt;
       void printStates() {&lt;br /&gt;
            System.out.println(&amp;quot;cadence:&amp;quot;+cadence+&amp;quot; speed:&amp;quot;+speed+&amp;quot; gear:&amp;quot;+gear);&lt;br /&gt;
       }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
  class MOUNTAINBicycle implements Bicycle {&lt;br /&gt;
  int cadence = 2;&lt;br /&gt;
       int speed = 3;&lt;br /&gt;
       int gear = 5;&lt;br /&gt;
       void changeCadence(int newValue) {&lt;br /&gt;
            cadence = newValue + 10;&lt;br /&gt;
       }&lt;br /&gt;
       void changeGear(int newValue) {&lt;br /&gt;
            gear = newValue + 4;&lt;br /&gt;
       }&lt;br /&gt;
       void speedUp(int increment) {&lt;br /&gt;
            speed = speed + increment + 10;   &lt;br /&gt;
       }&lt;br /&gt;
       void applyBrakes(int decrement) {&lt;br /&gt;
            speed = speed – decrement – 10;&lt;br /&gt;
       }&lt;br /&gt;
       void printStates() {&lt;br /&gt;
            System.out.println(&amp;quot;cadence:&amp;quot;+cadence+&amp;quot; speed:&amp;quot;+speed+&amp;quot; gear:&amp;quot;+gear);&lt;br /&gt;
       }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Using an interface rather than inheritance to specify a certain set of methods allows a class to inherit from some other class. In other words, if a class needs two different sets of methods, so it can behave like two different types of things, it could inherit one set from class A, and use an interface B to specify the other.&lt;br /&gt;
You could then reference one of these objects with either an A reference or a B reference. Implementing an interface allows a class to become more formal about the behavior it promises to provide. Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler. If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Mixins vs Interfaces==&lt;br /&gt;
Because of this mixin feature, a developer can add arbitrary methods and modify behavior of core classes at runtime. This is amazingly powerful if you are trying to write plugins and extensions to the [http://en.wikipedia.org/wiki/Software_framework framework].  Because you can add functionality to existing objects, users can install their [http://en.wikipedia.org/wiki/Plug-in_(computing) plugin] and start taking advantage of new functionality without having to make changes to the objects that they are instantiating in their application. &lt;br /&gt;
&lt;br /&gt;
In frameworks written in other languages, such as Java, plugging in new functionality means that you need to change how your objects are instantiated. This will require code changes and/or potentially configuration changes which makes it hard to develop, hard to maintain, and a pain for plugin developers to support.  But because of the mixin feature, Rails plugin developers can customize the base objects and the users of the plugins do not have to change any of their code or configuration logic.&lt;br /&gt;
&lt;br /&gt;
===Differences===&lt;br /&gt;
&lt;br /&gt;
These is a general comparision: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Mixins in Ruby&lt;br /&gt;
! Interfaces in Java&lt;br /&gt;
|-&lt;br /&gt;
| 'include' or 'extend' keywords used to include the module in the class.&lt;br /&gt;
| 'implement' keyword is used to implement interface&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Need not implement the module methods in the class.&lt;br /&gt;
| Should implement the interface methods. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| The methods are not copied into the class in which they are included. They are referenced.&lt;br /&gt;
| The methods’ implementation is in the class itself.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| The included method definition is decided at ‘run time’. If the definition changes while running the function points to the changed code.&lt;br /&gt;
| The definition is known compile time and will not change until the code is compiled and run again. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Modules inclusion does not provide a superclass and subclass relationship.&lt;br /&gt;
| Interface extending provides a superclass and subclass relationship. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| When we use mixins we included only a module of the class. &lt;br /&gt;
| We include the entire interface class. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Mainly for code reuse and does not provide any hierarchy. &lt;br /&gt;
| It provides a hierarchy. &lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Functionality and Behavior Comparison===&lt;br /&gt;
The functionality and behavior of Mixins in Ruby is compared with Interfaces in Java&lt;br /&gt;
&lt;br /&gt;
====Comparable Functionality====&lt;br /&gt;
Sometime we are required to compare the class members of different objects of the same class and/or sort them. This is the comparable functionality that we are trying to achieve for a given class.  This functionality can be achieved by both the Ruby using mixins and Java using interfaces. There is not much difference using both.&lt;br /&gt;
&lt;br /&gt;
'''Ruby'''&lt;br /&gt;
&lt;br /&gt;
The [http://www.ruby-doc.org/core/classes/Comparable.html Comparable] mixin is used by classes whose objects may be ordered. The class must define the &amp;lt;=&amp;gt; operator, which compares the receiver against another object, returning -1, 0, or +1 depending on whether the receiver is less than, equal to, or greater than the other object. Comparable uses &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method between?.&lt;br /&gt;
&lt;br /&gt;
  class SizeMatters&lt;br /&gt;
     include Comparable&lt;br /&gt;
     attr :str&lt;br /&gt;
     def &amp;lt;=&amp;gt;(anOther)&lt;br /&gt;
       str.size &amp;lt;=&amp;gt; anOther.str.size&lt;br /&gt;
     end&lt;br /&gt;
     def initialize(str)&lt;br /&gt;
       @str = str&lt;br /&gt;
     end&lt;br /&gt;
     def inspect&lt;br /&gt;
       @str&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
''using class:'' &lt;br /&gt;
&lt;br /&gt;
   s1 = SizeMatters.new(&amp;quot;Z&amp;quot;)&lt;br /&gt;
   s2 = SizeMatters.new(&amp;quot;YY&amp;quot;)&lt;br /&gt;
   s3 = SizeMatters.new(&amp;quot;XXX&amp;quot;)&lt;br /&gt;
   s4 = SizeMatters.new(&amp;quot;WWWW&amp;quot;)&lt;br /&gt;
   s5 = SizeMatters.new(&amp;quot;VVVVV&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
''output:''&lt;br /&gt;
   s1 &amp;lt; s2                       #=&amp;gt; true&lt;br /&gt;
   s4.between?(s1, s3)           #=&amp;gt; false&lt;br /&gt;
   s4.between?(s3, s5)           #=&amp;gt; true&lt;br /&gt;
   [ s3, s2, s5, s4, s1 ].sort   #=&amp;gt; [Z, YY, XXX, WWWW, VVVVV]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Java'''&lt;br /&gt;
&lt;br /&gt;
In Java, [http://www.java-tips.org/java-se-tips/java.lang/how-to-use-comparable-interface.html comparable functionality] is achieved by extending the “Comparable” interface. List of objects that implement this interface can be sorted automatically by sort method of the list interface. The interface in the following example &amp;lt;ref&amp;gt; http://www.java-tips.org/java-se-tips/java.lang/how-to-use-comparable-interface.html &amp;lt;/ref&amp;gt; has compareTo() method that is used by the sort() method of the list. Here compareTo() method can also be used to compare two objects with respect to the implementation in compareTo() implementation in the derived class. It returns ‘0’ , ‘1’ and ‘-1’ corresponding to whether one is equal to , greater than or lesser than other.&lt;br /&gt;
In this example code Employee class is implementing Comparable interface and have method compareTo().&lt;br /&gt;
  public class Employee implements Comparable {&lt;br /&gt;
    int EmpID;&lt;br /&gt;
    String Ename;&lt;br /&gt;
    double Sal;&lt;br /&gt;
    static int i;&lt;br /&gt;
    public Employee() {&lt;br /&gt;
        EmpID = i++;&lt;br /&gt;
        Ename = &amp;quot;dont know&amp;quot;;&lt;br /&gt;
        Sal = 0.0;&lt;br /&gt;
    }&lt;br /&gt;
    public Employee(String ename, double sal) {&lt;br /&gt;
        EmpID = i++;&lt;br /&gt;
        Ename = ename;&lt;br /&gt;
        Sal = sal;&lt;br /&gt;
    }&lt;br /&gt;
    public String toString() {&lt;br /&gt;
        return &amp;quot;EmpID &amp;quot; + EmpID + &amp;quot;\n&amp;quot; + &amp;quot;Ename &amp;quot; + Ename + &amp;quot;\n&amp;quot; + &amp;quot;Sal&amp;quot; + Sal;&lt;br /&gt;
    }&lt;br /&gt;
    public int compareTo(Object o1) {&lt;br /&gt;
        if (this.Sal == ((Employee) o1).Sal)&lt;br /&gt;
            return 0;&lt;br /&gt;
        else if ((this.Sal) &amp;gt; ((Employee) o1).Sal)&lt;br /&gt;
            return 1;&lt;br /&gt;
        else&lt;br /&gt;
            return -1;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
ComparableDemo.java is showing the use of this interface. This class first makes a list of objects of type Employee and call sort method of java.util.Collections, which internally uses compareTo() method of Employee class and sort the list accordingly.&lt;br /&gt;
 &lt;br /&gt;
  import java.util.*;&lt;br /&gt;
  public class ComparableDemo{&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
        List ts1 = new ArrayList();&lt;br /&gt;
        ts1.add(new Employee (&amp;quot;Tom&amp;quot;,40000.00));&lt;br /&gt;
        ts1.add(new Employee (&amp;quot;Harry&amp;quot;,20000.00));&lt;br /&gt;
        ts1.add(new Employee (&amp;quot;Maggie&amp;quot;,50000.00));&lt;br /&gt;
        ts1.add(new Employee (&amp;quot;Chris&amp;quot;,70000.00));&lt;br /&gt;
        Collections.sort(ts1);&lt;br /&gt;
        Iterator itr = ts1.iterator();&lt;br /&gt;
        while(itr.hasNext()){&lt;br /&gt;
            Object element = itr.next();&lt;br /&gt;
            System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
''Output:''&lt;br /&gt;
  &lt;br /&gt;
  EmpID 1&lt;br /&gt;
  Ename Harry&lt;br /&gt;
  Sal    20000.0&lt;br /&gt;
  EmpID 0&lt;br /&gt;
  Ename Tom&lt;br /&gt;
  Sal    40000.0&lt;br /&gt;
  EmpID 2&lt;br /&gt;
  Ename Maggie&lt;br /&gt;
  Sal    50000.0&lt;br /&gt;
  EmpID 3&lt;br /&gt;
  Ename Chris&lt;br /&gt;
  Sal    70000.0&lt;br /&gt;
&lt;br /&gt;
====Singleton Behaviour====&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Singleton_pattern Singleton] is a class guaranteed to have only a single instance, with a single global access point to that instance. It can be implemented in Ruby and Java. In Ruby we can include a module to acquire all the functionality of the singleton module. The code is already there and we are just reusing the code and adding that similar functionality into our class. Here our intention is to add the functionality of singleton to the class which is achieved using mixins. In java, implementation of interfaces means that the class which implements the interface becomes a type of that class, which is not the intention here. So, there is not interface which class can implement to get the functionality in itself. It has to implement the functionality itself without implementing any interface. &lt;br /&gt;
&lt;br /&gt;
'''Ruby'''&lt;br /&gt;
&lt;br /&gt;
When you include the singleton method inside your class it makes the new method private so that you cannot use it. Including ‘singleton’ adds a class method called instance that instantiates only one instance of the class&lt;br /&gt;
&lt;br /&gt;
  # require singleton lib&lt;br /&gt;
  require 'singleton'&lt;br /&gt;
  class AppConfig&lt;br /&gt;
    # mixin the singleton module&lt;br /&gt;
    include Singleton&lt;br /&gt;
    # do the actual app configuration&lt;br /&gt;
    def load_config(file)&lt;br /&gt;
      # do your work here&lt;br /&gt;
      puts &amp;quot;Application configuration file was loaded from file: #{file}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
''output:''&lt;br /&gt;
&lt;br /&gt;
  conf1 = AppConfig.instance&lt;br /&gt;
  conf1.load_config &amp;quot;/home/khelll/conf.yml&amp;quot;       #=&amp;gt;Application configuration file was loaded from file: /home/khelll/conf.yml&lt;br /&gt;
  conf2 = AppConfig.instance&lt;br /&gt;
  puts conf1 == conf2      #=&amp;gt;true&lt;br /&gt;
  # notice the following 2 lines won’t work&lt;br /&gt;
  AppConfig.new rescue(puts $!)   #=&amp;gt; new method is private&lt;br /&gt;
&lt;br /&gt;
====Enumerable Behaviour====&lt;br /&gt;
This [http://www.ruby-doc.org/core/classes/Enumerable.html enumerable] functionality adds support to the array members of the class. We can achieve this functionality using mixins in Ruby while it is not possible to do it in Java using interfaces.&lt;br /&gt;
&lt;br /&gt;
'''Implementation in Ruby:''' &lt;br /&gt;
&lt;br /&gt;
It adds the functionality of map, inject, select etc to the class which includes the ‘Enumerable’ module. The class which includes should represent a collection of items. Enumerable requires that your class contain an each method that serves up the items in the collection. All the other Enumerable methods rely on this. &lt;br /&gt;
Example:&lt;br /&gt;
This is a Team class that manages a group of members:&lt;br /&gt;
&lt;br /&gt;
  class Team&lt;br /&gt;
    include Enumerable&lt;br /&gt;
    attr_accessor :members&lt;br /&gt;
    def initialize&lt;br /&gt;
      @members = []&lt;br /&gt;
    end&lt;br /&gt;
    def each &amp;amp;block&lt;br /&gt;
      @members.each{|member| block.call(member)}&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Enumerable requires that your class contain an each method that serves up the items in the collection. All the other Enumerablemethods rely on this. Now we can use the map method, for instance:&lt;br /&gt;
&lt;br /&gt;
  team = Team.new&lt;br /&gt;
  team.members = ['joshua', 'gabriel', 'jacob']&lt;br /&gt;
  team.map{|member| member.capitalize}   # =&amp;gt; [&amp;quot;Joshua&amp;quot;, &amp;quot;Gabriel&amp;quot;, &amp;quot;Jacob&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
Now we can call any Enumerable methods on our team object itself, and it will assume we want to work with the members array within.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Data Mapper Behaviour====&lt;br /&gt;
&lt;br /&gt;
[http://datamapper.org/docs/ DataMapper] is an API which comes with the ability to use the same API to talk to a multitude of different datastores. There are adapters for the usual RDBMS suspects, NoSQL stores, various file formats and even some popular webservices. This is implemented in ruby using mixins. We need not use interfaces to implement.&lt;br /&gt;
&lt;br /&gt;
'''Implementation in Ruby:'''&lt;br /&gt;
  class Post&lt;br /&gt;
       include DataMapper::Resource&lt;br /&gt;
       property :id, Serial&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
The point to take away from this is not that Ruby Mixins are more powerful than Java Interfaces. The mixin feature of Ruby allows for the development of easy to use but powerful plugins that will be hard for any non-Ruby based framework to compete with. Java has many other ways of implementing the functionality mixins are capable of. It is just that it does not use Interfaces to achieve the functionality. Even though both of them are a way of achieving multiple inheritance in their respective languages, they achieve quiet different goals. Inheritance in Java is mainly to overcome the confusion which arises with multiple inheritance, while Mixins in Ruby is mainly to add an additional functionality to the existing class apart from the ‘primary functionality’ of the class. &lt;br /&gt;
&lt;br /&gt;
When a class implements Interface, it indicates that it is of the type interface class and forms a hierarchy with the interface class, while ruby mixins are mainly intended for code reuse and including an additional functionality to the class. It does not form any hierarchy as it is only using module of the class.&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* http://en.wikipedia.org/wiki/Interface_(Java)&lt;br /&gt;
* http://en.wikipedia.org/wiki/Mixin&lt;br /&gt;
* http://carboni.ca/blog/p/Modules-How-Do-They-Work&lt;br /&gt;
* http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/&lt;br /&gt;
* http://rubylearning.com/satishtalim/modules_mixins.html&lt;br /&gt;
* http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-&lt;br /&gt;
* http://datamapper.org/&lt;br /&gt;
* http://www.dsource.org/projects/tutorials/wiki/SingletonPattern &lt;br /&gt;
* http://corelib.rubyonrails.org/classes/Comparable.html&lt;br /&gt;
* http://kconrails.com/2010/11/30/ruby-enumerable-primer-part-1-the-basics/&lt;br /&gt;
* http://stackoverflow.com/questions/575920/is-a-ruby-module-equivalent-to-a-java-interface&lt;/div&gt;</summary>
		<author><name>Sthirum</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2c_bs&amp;diff=51685</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2c bs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2c_bs&amp;diff=51685"/>
		<updated>2011-10-02T01:30:50Z</updated>

		<summary type="html">&lt;p&gt;Sthirum: /* Comparable Functionality */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
An [http://en.wikipedia.org/wiki/Interface_(computing) interface] &amp;lt;ref&amp;gt;interface&amp;lt;/ref&amp;gt; in the [http://en.wikipedia.org/wiki/Java_(programming_language) Java] programming language is an abstract type that is used to specify an interface that classes must implement. Interface may only contain [http://en.wikipedia.org/wiki/Method_signature method signature] and constant declarations. An interface may never contain method definitions. Interfaces cannot be instantiated. A class that implements an interface must implement all of the methods described in the interface, or be an [http://en.wikipedia.org/wiki/Abstract_type abstract] class. Object references in Java may be specified to be of an interface type; in which case, they must either be null, or be bound to an object that implements the interface. &lt;br /&gt;
&lt;br /&gt;
In object-oriented programming languages, a [http://en.wikipedia.org/wiki/Mixin mixin] &amp;lt;ref&amp;gt; mixin &amp;lt;/ref&amp;gt; is a class that provides a certain functionality to be inherited or just reused by a subclass. Some of the functionality of mixins is provided by interfaces in popular languages like Java and [http://en.wikipedia.org/wiki/C_Sharp_(programming_language) C#]. However, an interface only specifies what the class must support and cannot provide an implementation. Another class, providing an implementation and dependent with the interface, is needed for [http://en.wikipedia.org/wiki/Code_refactoring refactoring] common behavior into a single place. Interfaces combined with aspect-oriented programming can produce full-fledged mixins in languages that support such features, such as C# or Java. &lt;br /&gt;
One benefit of using interfaces is that they simulate [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance]. All classes in Java other than [https://matrix.senecac.on.ca:8443/wiki/javaSE/index.php/Java.lang.Object java.lang.Object], must have exactly one base class; multiple inheritance of classes is not allowed.&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
===Mixins in Ruby=== &lt;br /&gt;
In Java you just have [http://en.wikipedia.org/wiki/Class_(computer_programming) classes] (both abstract and concrete) and interfaces. The [http://en.wikipedia.org/wiki/Ruby Ruby] language provides classes,[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html modules], and a mix of both. Inheriting from a mixin is not a form of specialization but is rather a means of collecting functionality. A class may inherit most or all of its functionality from one or more mixins through multiple inheritance. A mixin can also be viewed as an interface with implemented methods. When a class includes a mixin, the class implements the interface and includes, rather than inherits, all the mixins’ attributes (fields, properties) and methods. They become part of the class during compilation. A mixin can defer definition and binding of methods until runtime, though attributes and instantiation parameters are still defined at compile time. In the Ruby language a mixin is a class that is mixed with a module. &lt;br /&gt;
&lt;br /&gt;
====Module====&lt;br /&gt;
&lt;br /&gt;
A [http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html module]&amp;lt;ref&amp;gt;module&amp;lt;/ref&amp;gt; is a degenerate abstract class. A module can’t be instantiated and no class can directly extend it but it can fully implement methods. A class can leverage the implementation of a module by including the module’s methods. A module can define methods that can be shared in different and separate classes either at the class or instance level.We can view an example &amp;lt;ref&amp;gt; http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/&amp;lt;/ref&amp;gt; module that converts a numeric value to its English word.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # Convert a integer value to English.&lt;br /&gt;
  module Stringify&lt;br /&gt;
    # Requires an instance variable @value&lt;br /&gt;
    def stringify&lt;br /&gt;
      if @value == 1&lt;br /&gt;
        &amp;quot;One&amp;quot;&lt;br /&gt;
      elsif @value == 2&lt;br /&gt;
        &amp;quot;Two&amp;quot;&lt;br /&gt;
      elsif @value == 3&lt;br /&gt;
        &amp;quot;Three&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The Stringify module above makes use of a @value [http://en.wikipedia.org/wiki/Instance_variable instance variable]. The class that will be mixed with this module needs to define and set a @value instance variable since the Stringify module uses it but does not define it. In addition to instance variables a module could invoke methods not defined in the module but in the class that it will be mixed with.&lt;br /&gt;
&lt;br /&gt;
A [http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/ Self contained module] can also be written in Ruby where the methods defined in a module can be used as static methods independent of their implementation within a class.&lt;br /&gt;
&lt;br /&gt;
'''Self Contained Module Example: '''&lt;br /&gt;
&lt;br /&gt;
  # A Math module akin to Java Math class.&lt;br /&gt;
  module Math&lt;br /&gt;
    # Could be called as a class, static, method&lt;br /&gt;
    def add(val_one, val_two)&lt;br /&gt;
      BigInteger.new(val_one + val_two)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The methods in the Math module are intended to be invoked like class methods, also known as [http://en.wikipedia.org/wiki/Method_(computer_programming)#Static_methods static] methods. The add method in the Math module accepts two integer values and returns an [http://en.wikipedia.org/wiki/Instance_(computer_science) instance] of BigInteger.&lt;br /&gt;
&lt;br /&gt;
====Using Mixins====&lt;br /&gt;
&lt;br /&gt;
Implementation of the class and module are joined, interwined, combined, etc. A mixin is a different mechanism to the extend construct used to add concrete implementation to a class. With a mixin you can extend from a module instead of a class. &lt;br /&gt;
&lt;br /&gt;
The example of self-contained module shows how the method returns a BigInteger sum after accepting two integer parameters. The following example is the implementation of BigInteger class. The BigInteger class defines one constructor and directly inherits one method from the Number base class. To mix in the methods implemented in the Stringify and Math modules with the BigInteger class you will note the usage of the include and extend methods, respectively.&lt;br /&gt;
&lt;br /&gt;
  # Base Number class&lt;br /&gt;
  class Number&lt;br /&gt;
    def intValue&lt;br /&gt;
      @value&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  # BigInteger extends Number&lt;br /&gt;
  class BigInteger &amp;lt; Number&lt;br /&gt;
 &lt;br /&gt;
  # Add instance methods from Stringify&lt;br /&gt;
  include Stringify&lt;br /&gt;
 &lt;br /&gt;
  # Add class methods from Math&lt;br /&gt;
  extend Math&lt;br /&gt;
 &lt;br /&gt;
  # Add a constructor with one parameter&lt;br /&gt;
    def initialize(value)&lt;br /&gt;
      @value = value&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Using the class:&lt;br /&gt;
&lt;br /&gt;
  # Create a new object&lt;br /&gt;
  bigint1 = BigInteger.new(10)&lt;br /&gt;
  # Call a method inherited from the base class&lt;br /&gt;
  puts bigint1.intValue   # --&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
The extend method will mix a module’s methods at the class level. The method defined in the Math module can be used as a class/static method.&lt;br /&gt;
&lt;br /&gt;
  # Call class method extended from Math&lt;br /&gt;
  bigint2 = BigInteger.add(-2, 4)&lt;br /&gt;
  puts bigint2.intValue   # --&amp;gt; 2&lt;br /&gt;
&lt;br /&gt;
The include method will mix a module’s methods at the instance level, meaning that the methods will become instance methods. The method defined in the Stringify module can be used as an instance method.&lt;br /&gt;
&lt;br /&gt;
  # Call a method included from Stringify&lt;br /&gt;
  puts bigint2.stringify   # --&amp;gt; 'Two'&lt;br /&gt;
&lt;br /&gt;
There is another use of the extend method. You can enhance an object instance by mixing it with a module at run time. This ‘CurrencyFormatter’ is used to extend an object, changing it’s responsibilities at runtime.&lt;br /&gt;
&lt;br /&gt;
  # Format a numeric value as a currency&lt;br /&gt;
  module CurrencyFormatter&lt;br /&gt;
    def format&lt;br /&gt;
      &amp;quot;$#{@value}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
To mix an object instance with a module you can do the following:&lt;br /&gt;
&lt;br /&gt;
  # Add the module methods to&lt;br /&gt;
  # this object instance, only!&lt;br /&gt;
  bigint2.extend CurrencyFormatter&lt;br /&gt;
  puts bigint2.format   # --&amp;gt; '$2'&lt;br /&gt;
&lt;br /&gt;
Calling the extend method on an instance will only extend that one object; objects of the same class will not be extended with the new functionality.&lt;br /&gt;
&lt;br /&gt;
  puts bigint1.format   # will generate an error&lt;br /&gt;
&lt;br /&gt;
Modules that will be mixed with a class via the include or extend method could define something like a constructor or initializer method to the module. The module initializer method will be invoked at the time the module is mixed with a class. When a class extends a module the module’s self.extended method will be invoked:&lt;br /&gt;
&lt;br /&gt;
  module Math&lt;br /&gt;
    def self.extended(base)&lt;br /&gt;
      # Initialize module.&lt;br /&gt;
    end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
The self prefix indicates that the method is a static module level method. The base parameter in the static extended method will be either an instance object or class object of the class that extended the module depending whether you extend a object or class, respectively.&lt;br /&gt;
When a class includes a module the module’s self.included method will be invoked.&lt;br /&gt;
&lt;br /&gt;
  module Stringify&lt;br /&gt;
    def self.included(base)&lt;br /&gt;
      # Initialize module.&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The base parameter will be a class object for the class that includes the module.&lt;br /&gt;
It is important to note that inside the included and extended initializer methods you can include and extend other modules. &lt;br /&gt;
&lt;br /&gt;
  module Stringify&lt;br /&gt;
    def self.included(base)&lt;br /&gt;
      base.extend SomeOtherModule&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===Interfaces in Java===&lt;br /&gt;
Interfaces define a standardized set of commands that a class will obey. The commands are a set of methods that a class implements. The interface definition states the names of the methods and their return types and argument signatures. There is no executable body for any method. Example: &lt;br /&gt;
  interface Bicycle {&lt;br /&gt;
       void changeCadence(int newValue);   // wheel revolutions per minute&lt;br /&gt;
       void changeGear(int newValue);&lt;br /&gt;
       void speedUp(int increment);&lt;br /&gt;
       void applyBrakes(int decrement);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Implementation is independent to each class that implements the interface. Once a class implements an interface, the Java compiler knows that an instance of the class will contain the specified set of methods. Therefore, it will allow you to call those methods for an object referenced by a variable whose type is the interface. Implementing an interface enables a class to be &amp;quot;plugged in&amp;quot; in any situation that requires a specific behavior (manifested through the set of methods).&lt;br /&gt;
&lt;br /&gt;
  class ACMEBicycle implements Bicycle {&lt;br /&gt;
    //implementation specific to this class&lt;br /&gt;
       int cadence = 0;&lt;br /&gt;
       int speed = 0;&lt;br /&gt;
       int gear = 1;&lt;br /&gt;
       void changeCadence(int newValue) {&lt;br /&gt;
            cadence = newValue;&lt;br /&gt;
       }&lt;br /&gt;
       void changeGear(int newValue) {&lt;br /&gt;
            gear = newValue;&lt;br /&gt;
       }&lt;br /&gt;
       void speedUp(int increment) {&lt;br /&gt;
            speed = speed + increment;   &lt;br /&gt;
       }&lt;br /&gt;
       void applyBrakes(int decrement) {&lt;br /&gt;
            speed = speed - decrement;&lt;br /&gt;
       }&lt;br /&gt;
       void printStates() {&lt;br /&gt;
            System.out.println(&amp;quot;cadence:&amp;quot;+cadence+&amp;quot; speed:&amp;quot;+speed+&amp;quot; gear:&amp;quot;+gear);&lt;br /&gt;
       }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
  class MOUNTAINBicycle implements Bicycle {&lt;br /&gt;
  int cadence = 2;&lt;br /&gt;
       int speed = 3;&lt;br /&gt;
       int gear = 5;&lt;br /&gt;
       void changeCadence(int newValue) {&lt;br /&gt;
            cadence = newValue + 10;&lt;br /&gt;
       }&lt;br /&gt;
       void changeGear(int newValue) {&lt;br /&gt;
            gear = newValue + 4;&lt;br /&gt;
       }&lt;br /&gt;
       void speedUp(int increment) {&lt;br /&gt;
            speed = speed + increment + 10;   &lt;br /&gt;
       }&lt;br /&gt;
       void applyBrakes(int decrement) {&lt;br /&gt;
            speed = speed – decrement – 10;&lt;br /&gt;
       }&lt;br /&gt;
       void printStates() {&lt;br /&gt;
            System.out.println(&amp;quot;cadence:&amp;quot;+cadence+&amp;quot; speed:&amp;quot;+speed+&amp;quot; gear:&amp;quot;+gear);&lt;br /&gt;
       }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Using an interface rather than inheritance to specify a certain set of methods allows a class to inherit from some other class. In other words, if a class needs two different sets of methods, so it can behave like two different types of things, it could inherit one set from class A, and use an interface B to specify the other.&lt;br /&gt;
You could then reference one of these objects with either an A reference or a B reference. Implementing an interface allows a class to become more formal about the behavior it promises to provide. Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler. If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Mixins vs Interfaces==&lt;br /&gt;
Because of this mixin feature, a developer can add arbitrary methods and modify behavior of core classes at runtime. This is amazingly powerful if you are trying to write plugins and extensions to the [http://en.wikipedia.org/wiki/Software_framework framework].  Because you can add functionality to existing objects, users can install their [http://en.wikipedia.org/wiki/Plug-in_(computing) plugin] and start taking advantage of new functionality without having to make changes to the objects that they are instantiating in their application. &lt;br /&gt;
&lt;br /&gt;
In frameworks written in other languages, such as Java, plugging in new functionality means that you need to change how your objects are instantiated. This will require code changes and/or potentially configuration changes which makes it hard to develop, hard to maintain, and a pain for plugin developers to support.  But because of the mixin feature, Rails plugin developers can customize the base objects and the users of the plugins do not have to change any of their code or configuration logic.&lt;br /&gt;
&lt;br /&gt;
===Differences===&lt;br /&gt;
&lt;br /&gt;
These is a general comparision: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Mixins in Ruby&lt;br /&gt;
! Interfaces in Java&lt;br /&gt;
|-&lt;br /&gt;
| 'include' or 'extend' keywords used to include the module in the class.&lt;br /&gt;
| 'implement' keyword is used to implement interface&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Need not implement the module methods in the class.&lt;br /&gt;
| Should implement the interface methods. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| The methods are not copied into the class in which they are included. They are referenced.&lt;br /&gt;
| The methods’ implementation is in the class itself.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| The included method definition is decided at ‘run time’. If the definition changes while running the function points to the changed code.&lt;br /&gt;
| The definition is known compile time and will not change until the code is compiled and run again. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Modules inclusion does not provide a superclass and subclass relationship.&lt;br /&gt;
| Interface extending provides a superclass and subclass relationship. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| When we use mixins we included only a module of the class. &lt;br /&gt;
| We include the entire interface class. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Mainly for code reuse and does not provide any hierarchy. &lt;br /&gt;
| It provides a hierarchy. &lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Functionality and Behavior Comparison===&lt;br /&gt;
The functionality and behavior of Mixins in Ruby is compared with Interfaces in Java&lt;br /&gt;
&lt;br /&gt;
====Comparable Functionality====&lt;br /&gt;
Sometime we are required to compare the class members of different objects of the same class and/or sort them. This is the comparable functionality that we are trying to achieve for a given class.  This functionality can be achieved by both the Ruby using mixins and Java using interfaces. There is not much difference using both.&lt;br /&gt;
&lt;br /&gt;
'''Ruby'''&lt;br /&gt;
&lt;br /&gt;
The [http://www.ruby-doc.org/core/classes/Comparable.html Comparable] mixin is used by classes whose objects may be ordered. The class must define the &amp;lt;=&amp;gt; operator, which compares the receiver against another object, returning -1, 0, or +1 depending on whether the receiver is less than, equal to, or greater than the other object. Comparable uses &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method between?.&lt;br /&gt;
&lt;br /&gt;
  class SizeMatters&lt;br /&gt;
     include Comparable&lt;br /&gt;
     attr :str&lt;br /&gt;
     def &amp;lt;=&amp;gt;(anOther)&lt;br /&gt;
       str.size &amp;lt;=&amp;gt; anOther.str.size&lt;br /&gt;
     end&lt;br /&gt;
     def initialize(str)&lt;br /&gt;
       @str = str&lt;br /&gt;
     end&lt;br /&gt;
     def inspect&lt;br /&gt;
       @str&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
''using class:'' &lt;br /&gt;
&lt;br /&gt;
   s1 = SizeMatters.new(&amp;quot;Z&amp;quot;)&lt;br /&gt;
   s2 = SizeMatters.new(&amp;quot;YY&amp;quot;)&lt;br /&gt;
   s3 = SizeMatters.new(&amp;quot;XXX&amp;quot;)&lt;br /&gt;
   s4 = SizeMatters.new(&amp;quot;WWWW&amp;quot;)&lt;br /&gt;
   s5 = SizeMatters.new(&amp;quot;VVVVV&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
''output:''&lt;br /&gt;
   s1 &amp;lt; s2                       #=&amp;gt; true&lt;br /&gt;
   s4.between?(s1, s3)           #=&amp;gt; false&lt;br /&gt;
   s4.between?(s3, s5)           #=&amp;gt; true&lt;br /&gt;
   [ s3, s2, s5, s4, s1 ].sort   #=&amp;gt; [Z, YY, XXX, WWWW, VVVVV]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Java'''&lt;br /&gt;
&lt;br /&gt;
In Java, [http://www.java-tips.org/java-se-tips/java.lang/how-to-use-comparable-interface.html comparable functionality] is achieved by extending the “Comparable” interface. List of objects that implement this interface can be sorted automatically by sort method of the list interface. This interface has compareTo() method that is used by the sort() method of the list. Here compareTo() method can also be used to compare two objects with respect to the implementation in compareTo() implementation in the derived class. It returns ‘0’ , ‘1’ and ‘-1’ corresponding to whether one is equal to , greater than or lesser than other.&lt;br /&gt;
In this example code Employee class is implementing Comparable interface and have method compareTo().&lt;br /&gt;
  public class Employee implements Comparable {&lt;br /&gt;
    int EmpID;&lt;br /&gt;
    String Ename;&lt;br /&gt;
    double Sal;&lt;br /&gt;
    static int i;&lt;br /&gt;
    public Employee() {&lt;br /&gt;
        EmpID = i++;&lt;br /&gt;
        Ename = &amp;quot;dont know&amp;quot;;&lt;br /&gt;
        Sal = 0.0;&lt;br /&gt;
    }&lt;br /&gt;
    public Employee(String ename, double sal) {&lt;br /&gt;
        EmpID = i++;&lt;br /&gt;
        Ename = ename;&lt;br /&gt;
        Sal = sal;&lt;br /&gt;
    }&lt;br /&gt;
    public String toString() {&lt;br /&gt;
        return &amp;quot;EmpID &amp;quot; + EmpID + &amp;quot;\n&amp;quot; + &amp;quot;Ename &amp;quot; + Ename + &amp;quot;\n&amp;quot; + &amp;quot;Sal&amp;quot; + Sal;&lt;br /&gt;
    }&lt;br /&gt;
    public int compareTo(Object o1) {&lt;br /&gt;
        if (this.Sal == ((Employee) o1).Sal)&lt;br /&gt;
            return 0;&lt;br /&gt;
        else if ((this.Sal) &amp;gt; ((Employee) o1).Sal)&lt;br /&gt;
            return 1;&lt;br /&gt;
        else&lt;br /&gt;
            return -1;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
ComparableDemo.java is showing the use of this interface. This class first makes a list of objects of type Employee and call sort method of java.util.Collections, which internally uses compareTo() method of Employee class and sort the list accordingly.&lt;br /&gt;
 &lt;br /&gt;
  import java.util.*;&lt;br /&gt;
  public class ComparableDemo{&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
        List ts1 = new ArrayList();&lt;br /&gt;
        ts1.add(new Employee (&amp;quot;Tom&amp;quot;,40000.00));&lt;br /&gt;
        ts1.add(new Employee (&amp;quot;Harry&amp;quot;,20000.00));&lt;br /&gt;
        ts1.add(new Employee (&amp;quot;Maggie&amp;quot;,50000.00));&lt;br /&gt;
        ts1.add(new Employee (&amp;quot;Chris&amp;quot;,70000.00));&lt;br /&gt;
        Collections.sort(ts1);&lt;br /&gt;
        Iterator itr = ts1.iterator();&lt;br /&gt;
        while(itr.hasNext()){&lt;br /&gt;
            Object element = itr.next();&lt;br /&gt;
            System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
''Output:''&lt;br /&gt;
  &lt;br /&gt;
  EmpID 1&lt;br /&gt;
  Ename Harry&lt;br /&gt;
  Sal    20000.0&lt;br /&gt;
  EmpID 0&lt;br /&gt;
  Ename Tom&lt;br /&gt;
  Sal    40000.0&lt;br /&gt;
  EmpID 2&lt;br /&gt;
  Ename Maggie&lt;br /&gt;
  Sal    50000.0&lt;br /&gt;
  EmpID 3&lt;br /&gt;
  Ename Chris&lt;br /&gt;
  Sal    70000.0&lt;br /&gt;
&lt;br /&gt;
====Singleton Behaviour====&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Singleton_pattern Singleton] is a class guaranteed to have only a single instance, with a single global access point to that instance. It can be implemented in Ruby and Java. In Ruby we can include a module to acquire all the functionality of the singleton module. The code is already there and we are just reusing the code and adding that similar functionality into our class. Here our intention is to add the functionality of singleton to the class which is achieved using mixins. In java, implementation of interfaces means that the class which implements the interface becomes a type of that class, which is not the intention here. So, there is not interface which class can implement to get the functionality in itself. It has to implement the functionality itself without implementing any interface. &lt;br /&gt;
&lt;br /&gt;
'''Ruby'''&lt;br /&gt;
&lt;br /&gt;
When you include the singleton method inside your class it makes the new method private so that you cannot use it. Including ‘singleton’ adds a class method called instance that instantiates only one instance of the class&lt;br /&gt;
&lt;br /&gt;
  # require singleton lib&lt;br /&gt;
  require 'singleton'&lt;br /&gt;
  class AppConfig&lt;br /&gt;
    # mixin the singleton module&lt;br /&gt;
    include Singleton&lt;br /&gt;
    # do the actual app configuration&lt;br /&gt;
    def load_config(file)&lt;br /&gt;
      # do your work here&lt;br /&gt;
      puts &amp;quot;Application configuration file was loaded from file: #{file}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
''output:''&lt;br /&gt;
&lt;br /&gt;
  conf1 = AppConfig.instance&lt;br /&gt;
  conf1.load_config &amp;quot;/home/khelll/conf.yml&amp;quot;       #=&amp;gt;Application configuration file was loaded from file: /home/khelll/conf.yml&lt;br /&gt;
  conf2 = AppConfig.instance&lt;br /&gt;
  puts conf1 == conf2      #=&amp;gt;true&lt;br /&gt;
  # notice the following 2 lines won’t work&lt;br /&gt;
  AppConfig.new rescue(puts $!)   #=&amp;gt; new method is private&lt;br /&gt;
&lt;br /&gt;
====Enumerable Behaviour====&lt;br /&gt;
This [http://www.ruby-doc.org/core/classes/Enumerable.html enumerable] functionality adds support to the array members of the class. We can achieve this functionality using mixins in Ruby while it is not possible to do it in Java using interfaces.&lt;br /&gt;
&lt;br /&gt;
'''Implementation in Ruby:''' &lt;br /&gt;
&lt;br /&gt;
It adds the functionality of map, inject, select etc to the class which includes the ‘Enumerable’ module. The class which includes should represent a collection of items. Enumerable requires that your class contain an each method that serves up the items in the collection. All the other Enumerable methods rely on this. &lt;br /&gt;
Example:&lt;br /&gt;
This is a Team class that manages a group of members:&lt;br /&gt;
&lt;br /&gt;
  class Team&lt;br /&gt;
    include Enumerable&lt;br /&gt;
    attr_accessor :members&lt;br /&gt;
    def initialize&lt;br /&gt;
      @members = []&lt;br /&gt;
    end&lt;br /&gt;
    def each &amp;amp;block&lt;br /&gt;
      @members.each{|member| block.call(member)}&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Enumerable requires that your class contain an each method that serves up the items in the collection. All the other Enumerablemethods rely on this. Now we can use the map method, for instance:&lt;br /&gt;
&lt;br /&gt;
  team = Team.new&lt;br /&gt;
  team.members = ['joshua', 'gabriel', 'jacob']&lt;br /&gt;
  team.map{|member| member.capitalize}   # =&amp;gt; [&amp;quot;Joshua&amp;quot;, &amp;quot;Gabriel&amp;quot;, &amp;quot;Jacob&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
Now we can call any Enumerable methods on our team object itself, and it will assume we want to work with the members array within.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Data Mapper Behaviour====&lt;br /&gt;
&lt;br /&gt;
[http://datamapper.org/docs/ DataMapper] is an API which comes with the ability to use the same API to talk to a multitude of different datastores. There are adapters for the usual RDBMS suspects, NoSQL stores, various file formats and even some popular webservices. This is implemented in ruby using mixins. We need not use interfaces to implement.&lt;br /&gt;
&lt;br /&gt;
'''Implementation in Ruby:'''&lt;br /&gt;
  class Post&lt;br /&gt;
       include DataMapper::Resource&lt;br /&gt;
       property :id, Serial&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
The point to take away from this is not that Ruby Mixins are more powerful than Java Interfaces. The mixin feature of Ruby allows for the development of easy to use but powerful plugins that will be hard for any non-Ruby based framework to compete with. Java has many other ways of implementing the functionality mixins are capable of. It is just that it does not use Interfaces to achieve the functionality. Even though both of them are a way of achieving multiple inheritance in their respective languages, they achieve quiet different goals. Inheritance in Java is mainly to overcome the confusion which arises with multiple inheritance, while Mixins in Ruby is mainly to add an additional functionality to the existing class apart from the ‘primary functionality’ of the class. &lt;br /&gt;
&lt;br /&gt;
When a class implements Interface, it indicates that it is of the type interface class and forms a hierarchy with the interface class, while ruby mixins are mainly intended for code reuse and including an additional functionality to the class. It does not form any hierarchy as it is only using module of the class.&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* http://en.wikipedia.org/wiki/Interface_(Java)&lt;br /&gt;
* http://en.wikipedia.org/wiki/Mixin&lt;br /&gt;
* http://carboni.ca/blog/p/Modules-How-Do-They-Work&lt;br /&gt;
* http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/&lt;br /&gt;
* http://rubylearning.com/satishtalim/modules_mixins.html&lt;br /&gt;
* http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-&lt;br /&gt;
* http://datamapper.org/&lt;br /&gt;
* http://www.dsource.org/projects/tutorials/wiki/SingletonPattern &lt;br /&gt;
* http://corelib.rubyonrails.org/classes/Comparable.html&lt;br /&gt;
* http://kconrails.com/2010/11/30/ruby-enumerable-primer-part-1-the-basics/&lt;br /&gt;
* http://stackoverflow.com/questions/575920/is-a-ruby-module-equivalent-to-a-java-interface&lt;/div&gt;</summary>
		<author><name>Sthirum</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2c_bs&amp;diff=51684</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2c bs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2c_bs&amp;diff=51684"/>
		<updated>2011-10-02T01:26:32Z</updated>

		<summary type="html">&lt;p&gt;Sthirum: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
An [http://en.wikipedia.org/wiki/Interface_(computing) interface] &amp;lt;ref&amp;gt;interface&amp;lt;/ref&amp;gt; in the [http://en.wikipedia.org/wiki/Java_(programming_language) Java] programming language is an abstract type that is used to specify an interface that classes must implement. Interface may only contain [http://en.wikipedia.org/wiki/Method_signature method signature] and constant declarations. An interface may never contain method definitions. Interfaces cannot be instantiated. A class that implements an interface must implement all of the methods described in the interface, or be an [http://en.wikipedia.org/wiki/Abstract_type abstract] class. Object references in Java may be specified to be of an interface type; in which case, they must either be null, or be bound to an object that implements the interface. &lt;br /&gt;
&lt;br /&gt;
In object-oriented programming languages, a [http://en.wikipedia.org/wiki/Mixin mixin] &amp;lt;ref&amp;gt; mixin &amp;lt;/ref&amp;gt; is a class that provides a certain functionality to be inherited or just reused by a subclass. Some of the functionality of mixins is provided by interfaces in popular languages like Java and [http://en.wikipedia.org/wiki/C_Sharp_(programming_language) C#]. However, an interface only specifies what the class must support and cannot provide an implementation. Another class, providing an implementation and dependent with the interface, is needed for [http://en.wikipedia.org/wiki/Code_refactoring refactoring] common behavior into a single place. Interfaces combined with aspect-oriented programming can produce full-fledged mixins in languages that support such features, such as C# or Java. &lt;br /&gt;
One benefit of using interfaces is that they simulate [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance]. All classes in Java other than [https://matrix.senecac.on.ca:8443/wiki/javaSE/index.php/Java.lang.Object java.lang.Object], must have exactly one base class; multiple inheritance of classes is not allowed.&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
===Mixins in Ruby=== &lt;br /&gt;
In Java you just have [http://en.wikipedia.org/wiki/Class_(computer_programming) classes] (both abstract and concrete) and interfaces. The [http://en.wikipedia.org/wiki/Ruby Ruby] language provides classes,[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html modules], and a mix of both. Inheriting from a mixin is not a form of specialization but is rather a means of collecting functionality. A class may inherit most or all of its functionality from one or more mixins through multiple inheritance. A mixin can also be viewed as an interface with implemented methods. When a class includes a mixin, the class implements the interface and includes, rather than inherits, all the mixins’ attributes (fields, properties) and methods. They become part of the class during compilation. A mixin can defer definition and binding of methods until runtime, though attributes and instantiation parameters are still defined at compile time. In the Ruby language a mixin is a class that is mixed with a module. &lt;br /&gt;
&lt;br /&gt;
====Module====&lt;br /&gt;
&lt;br /&gt;
A [http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html module]&amp;lt;ref&amp;gt;module&amp;lt;/ref&amp;gt; is a degenerate abstract class. A module can’t be instantiated and no class can directly extend it but it can fully implement methods. A class can leverage the implementation of a module by including the module’s methods. A module can define methods that can be shared in different and separate classes either at the class or instance level.We can view an example &amp;lt;ref&amp;gt; http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/&amp;lt;/ref&amp;gt; module that converts a numeric value to its English word.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # Convert a integer value to English.&lt;br /&gt;
  module Stringify&lt;br /&gt;
    # Requires an instance variable @value&lt;br /&gt;
    def stringify&lt;br /&gt;
      if @value == 1&lt;br /&gt;
        &amp;quot;One&amp;quot;&lt;br /&gt;
      elsif @value == 2&lt;br /&gt;
        &amp;quot;Two&amp;quot;&lt;br /&gt;
      elsif @value == 3&lt;br /&gt;
        &amp;quot;Three&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The Stringify module above makes use of a @value [http://en.wikipedia.org/wiki/Instance_variable instance variable]. The class that will be mixed with this module needs to define and set a @value instance variable since the Stringify module uses it but does not define it. In addition to instance variables a module could invoke methods not defined in the module but in the class that it will be mixed with.&lt;br /&gt;
&lt;br /&gt;
A [http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/ Self contained module] can also be written in Ruby where the methods defined in a module can be used as static methods independent of their implementation within a class.&lt;br /&gt;
&lt;br /&gt;
'''Self Contained Module Example: '''&lt;br /&gt;
&lt;br /&gt;
  # A Math module akin to Java Math class.&lt;br /&gt;
  module Math&lt;br /&gt;
    # Could be called as a class, static, method&lt;br /&gt;
    def add(val_one, val_two)&lt;br /&gt;
      BigInteger.new(val_one + val_two)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The methods in the Math module are intended to be invoked like class methods, also known as [http://en.wikipedia.org/wiki/Method_(computer_programming)#Static_methods static] methods. The add method in the Math module accepts two integer values and returns an [http://en.wikipedia.org/wiki/Instance_(computer_science) instance] of BigInteger.&lt;br /&gt;
&lt;br /&gt;
====Using Mixins====&lt;br /&gt;
&lt;br /&gt;
Implementation of the class and module are joined, interwined, combined, etc. A mixin is a different mechanism to the extend construct used to add concrete implementation to a class. With a mixin you can extend from a module instead of a class. &lt;br /&gt;
&lt;br /&gt;
The example of self-contained module shows how the method returns a BigInteger sum after accepting two integer parameters. The following example is the implementation of BigInteger class. The BigInteger class defines one constructor and directly inherits one method from the Number base class. To mix in the methods implemented in the Stringify and Math modules with the BigInteger class you will note the usage of the include and extend methods, respectively.&lt;br /&gt;
&lt;br /&gt;
  # Base Number class&lt;br /&gt;
  class Number&lt;br /&gt;
    def intValue&lt;br /&gt;
      @value&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  # BigInteger extends Number&lt;br /&gt;
  class BigInteger &amp;lt; Number&lt;br /&gt;
 &lt;br /&gt;
  # Add instance methods from Stringify&lt;br /&gt;
  include Stringify&lt;br /&gt;
 &lt;br /&gt;
  # Add class methods from Math&lt;br /&gt;
  extend Math&lt;br /&gt;
 &lt;br /&gt;
  # Add a constructor with one parameter&lt;br /&gt;
    def initialize(value)&lt;br /&gt;
      @value = value&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Using the class:&lt;br /&gt;
&lt;br /&gt;
  # Create a new object&lt;br /&gt;
  bigint1 = BigInteger.new(10)&lt;br /&gt;
  # Call a method inherited from the base class&lt;br /&gt;
  puts bigint1.intValue   # --&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
The extend method will mix a module’s methods at the class level. The method defined in the Math module can be used as a class/static method.&lt;br /&gt;
&lt;br /&gt;
  # Call class method extended from Math&lt;br /&gt;
  bigint2 = BigInteger.add(-2, 4)&lt;br /&gt;
  puts bigint2.intValue   # --&amp;gt; 2&lt;br /&gt;
&lt;br /&gt;
The include method will mix a module’s methods at the instance level, meaning that the methods will become instance methods. The method defined in the Stringify module can be used as an instance method.&lt;br /&gt;
&lt;br /&gt;
  # Call a method included from Stringify&lt;br /&gt;
  puts bigint2.stringify   # --&amp;gt; 'Two'&lt;br /&gt;
&lt;br /&gt;
There is another use of the extend method. You can enhance an object instance by mixing it with a module at run time. This ‘CurrencyFormatter’ is used to extend an object, changing it’s responsibilities at runtime.&lt;br /&gt;
&lt;br /&gt;
  # Format a numeric value as a currency&lt;br /&gt;
  module CurrencyFormatter&lt;br /&gt;
    def format&lt;br /&gt;
      &amp;quot;$#{@value}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
To mix an object instance with a module you can do the following:&lt;br /&gt;
&lt;br /&gt;
  # Add the module methods to&lt;br /&gt;
  # this object instance, only!&lt;br /&gt;
  bigint2.extend CurrencyFormatter&lt;br /&gt;
  puts bigint2.format   # --&amp;gt; '$2'&lt;br /&gt;
&lt;br /&gt;
Calling the extend method on an instance will only extend that one object; objects of the same class will not be extended with the new functionality.&lt;br /&gt;
&lt;br /&gt;
  puts bigint1.format   # will generate an error&lt;br /&gt;
&lt;br /&gt;
Modules that will be mixed with a class via the include or extend method could define something like a constructor or initializer method to the module. The module initializer method will be invoked at the time the module is mixed with a class. When a class extends a module the module’s self.extended method will be invoked:&lt;br /&gt;
&lt;br /&gt;
  module Math&lt;br /&gt;
    def self.extended(base)&lt;br /&gt;
      # Initialize module.&lt;br /&gt;
    end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
The self prefix indicates that the method is a static module level method. The base parameter in the static extended method will be either an instance object or class object of the class that extended the module depending whether you extend a object or class, respectively.&lt;br /&gt;
When a class includes a module the module’s self.included method will be invoked.&lt;br /&gt;
&lt;br /&gt;
  module Stringify&lt;br /&gt;
    def self.included(base)&lt;br /&gt;
      # Initialize module.&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The base parameter will be a class object for the class that includes the module.&lt;br /&gt;
It is important to note that inside the included and extended initializer methods you can include and extend other modules. &lt;br /&gt;
&lt;br /&gt;
  module Stringify&lt;br /&gt;
    def self.included(base)&lt;br /&gt;
      base.extend SomeOtherModule&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===Interfaces in Java===&lt;br /&gt;
Interfaces define a standardized set of commands that a class will obey. The commands are a set of methods that a class implements. The interface definition states the names of the methods and their return types and argument signatures. There is no executable body for any method. Example: &lt;br /&gt;
  interface Bicycle {&lt;br /&gt;
       void changeCadence(int newValue);   // wheel revolutions per minute&lt;br /&gt;
       void changeGear(int newValue);&lt;br /&gt;
       void speedUp(int increment);&lt;br /&gt;
       void applyBrakes(int decrement);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Implementation is independent to each class that implements the interface. Once a class implements an interface, the Java compiler knows that an instance of the class will contain the specified set of methods. Therefore, it will allow you to call those methods for an object referenced by a variable whose type is the interface. Implementing an interface enables a class to be &amp;quot;plugged in&amp;quot; in any situation that requires a specific behavior (manifested through the set of methods).&lt;br /&gt;
&lt;br /&gt;
  class ACMEBicycle implements Bicycle {&lt;br /&gt;
    //implementation specific to this class&lt;br /&gt;
       int cadence = 0;&lt;br /&gt;
       int speed = 0;&lt;br /&gt;
       int gear = 1;&lt;br /&gt;
       void changeCadence(int newValue) {&lt;br /&gt;
            cadence = newValue;&lt;br /&gt;
       }&lt;br /&gt;
       void changeGear(int newValue) {&lt;br /&gt;
            gear = newValue;&lt;br /&gt;
       }&lt;br /&gt;
       void speedUp(int increment) {&lt;br /&gt;
            speed = speed + increment;   &lt;br /&gt;
       }&lt;br /&gt;
       void applyBrakes(int decrement) {&lt;br /&gt;
            speed = speed - decrement;&lt;br /&gt;
       }&lt;br /&gt;
       void printStates() {&lt;br /&gt;
            System.out.println(&amp;quot;cadence:&amp;quot;+cadence+&amp;quot; speed:&amp;quot;+speed+&amp;quot; gear:&amp;quot;+gear);&lt;br /&gt;
       }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
  class MOUNTAINBicycle implements Bicycle {&lt;br /&gt;
  int cadence = 2;&lt;br /&gt;
       int speed = 3;&lt;br /&gt;
       int gear = 5;&lt;br /&gt;
       void changeCadence(int newValue) {&lt;br /&gt;
            cadence = newValue + 10;&lt;br /&gt;
       }&lt;br /&gt;
       void changeGear(int newValue) {&lt;br /&gt;
            gear = newValue + 4;&lt;br /&gt;
       }&lt;br /&gt;
       void speedUp(int increment) {&lt;br /&gt;
            speed = speed + increment + 10;   &lt;br /&gt;
       }&lt;br /&gt;
       void applyBrakes(int decrement) {&lt;br /&gt;
            speed = speed – decrement – 10;&lt;br /&gt;
       }&lt;br /&gt;
       void printStates() {&lt;br /&gt;
            System.out.println(&amp;quot;cadence:&amp;quot;+cadence+&amp;quot; speed:&amp;quot;+speed+&amp;quot; gear:&amp;quot;+gear);&lt;br /&gt;
       }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Using an interface rather than inheritance to specify a certain set of methods allows a class to inherit from some other class. In other words, if a class needs two different sets of methods, so it can behave like two different types of things, it could inherit one set from class A, and use an interface B to specify the other.&lt;br /&gt;
You could then reference one of these objects with either an A reference or a B reference. Implementing an interface allows a class to become more formal about the behavior it promises to provide. Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler. If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Mixins vs Interfaces==&lt;br /&gt;
Because of this mixin feature, a developer can add arbitrary methods and modify behavior of core classes at runtime. This is amazingly powerful if you are trying to write plugins and extensions to the [http://en.wikipedia.org/wiki/Software_framework framework].  Because you can add functionality to existing objects, users can install their [http://en.wikipedia.org/wiki/Plug-in_(computing) plugin] and start taking advantage of new functionality without having to make changes to the objects that they are instantiating in their application. &lt;br /&gt;
&lt;br /&gt;
In frameworks written in other languages, such as Java, plugging in new functionality means that you need to change how your objects are instantiated. This will require code changes and/or potentially configuration changes which makes it hard to develop, hard to maintain, and a pain for plugin developers to support.  But because of the mixin feature, Rails plugin developers can customize the base objects and the users of the plugins do not have to change any of their code or configuration logic.&lt;br /&gt;
&lt;br /&gt;
===Differences===&lt;br /&gt;
&lt;br /&gt;
These is a general comparision: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Mixins in Ruby&lt;br /&gt;
! Interfaces in Java&lt;br /&gt;
|-&lt;br /&gt;
| 'include' or 'extend' keywords used to include the module in the class.&lt;br /&gt;
| 'implement' keyword is used to implement interface&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Need not implement the module methods in the class.&lt;br /&gt;
| Should implement the interface methods. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| The methods are not copied into the class in which they are included. They are referenced.&lt;br /&gt;
| The methods’ implementation is in the class itself.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| The included method definition is decided at ‘run time’. If the definition changes while running the function points to the changed code.&lt;br /&gt;
| The definition is known compile time and will not change until the code is compiled and run again. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Modules inclusion does not provide a superclass and subclass relationship.&lt;br /&gt;
| Interface extending provides a superclass and subclass relationship. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| When we use mixins we included only a module of the class. &lt;br /&gt;
| We include the entire interface class. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Mainly for code reuse and does not provide any hierarchy. &lt;br /&gt;
| It provides a hierarchy. &lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Functionality and Behavior Comparison===&lt;br /&gt;
The functionality and behavior of Mixins in Ruby is compared with Interfaces in Java&lt;br /&gt;
&lt;br /&gt;
====Comparable Functionality====&lt;br /&gt;
Sometime we are required to compare the class members of different objects of the same class and/or sort them. This is the comparable functionality that we are trying to achieve for a given class.  This functionality can be achieved by both the Ruby using mixins and Java using interfaces. There is not much difference using both.&lt;br /&gt;
&lt;br /&gt;
'''Ruby'''&lt;br /&gt;
&lt;br /&gt;
The [http://www.ruby-doc.org/core/classes/Comparable.html Comparable] mixin is used by classes whose objects may be ordered. The class must define the &amp;lt;=&amp;gt; operator, which compares the receiver against another object, returning -1, 0, or +1 depending on whether the receiver is less than, equal to, or greater than the other object. Comparable uses &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method between?.&lt;br /&gt;
&lt;br /&gt;
  class SizeMatters&lt;br /&gt;
     include Comparable&lt;br /&gt;
     attr :str&lt;br /&gt;
     def &amp;lt;=&amp;gt;(anOther)&lt;br /&gt;
       str.size &amp;lt;=&amp;gt; anOther.str.size&lt;br /&gt;
     end&lt;br /&gt;
     def initialize(str)&lt;br /&gt;
       @str = str&lt;br /&gt;
     end&lt;br /&gt;
     def inspect&lt;br /&gt;
       @str&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
''using class:'' &lt;br /&gt;
&lt;br /&gt;
   s1 = SizeMatters.new(&amp;quot;Z&amp;quot;)&lt;br /&gt;
   s2 = SizeMatters.new(&amp;quot;YY&amp;quot;)&lt;br /&gt;
   s3 = SizeMatters.new(&amp;quot;XXX&amp;quot;)&lt;br /&gt;
   s4 = SizeMatters.new(&amp;quot;WWWW&amp;quot;)&lt;br /&gt;
   s5 = SizeMatters.new(&amp;quot;VVVVV&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
''output:''&lt;br /&gt;
   s1 &amp;lt; s2                       #=&amp;gt; true&lt;br /&gt;
   s4.between?(s1, s3)           #=&amp;gt; false&lt;br /&gt;
   s4.between?(s3, s5)           #=&amp;gt; true&lt;br /&gt;
   [ s3, s2, s5, s4, s1 ].sort   #=&amp;gt; [Z, YY, XXX, WWWW, VVVVV]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Java'''&lt;br /&gt;
&lt;br /&gt;
In Java, comparable functionality is achieved by extending the “Comparable” interface. List of objects that implement this interface can be sorted automatically by sort method of the list interface. This interface has compareTo() method that is used by the sort() method of the list. Here compareTo() method can also be used to compare two objects with respect to the implementation in compareTo() implementation in the derived class. It returns ‘0’ , ‘1’ and ‘-1’ corresponding to whether one is equal to , greater than or lesser than other.&lt;br /&gt;
In this example code Employee class is implementing Comparable interface and have method compareTo().&lt;br /&gt;
  public class Employee implements Comparable {&lt;br /&gt;
    int EmpID;&lt;br /&gt;
    String Ename;&lt;br /&gt;
    double Sal;&lt;br /&gt;
    static int i;&lt;br /&gt;
    public Employee() {&lt;br /&gt;
        EmpID = i++;&lt;br /&gt;
        Ename = &amp;quot;dont know&amp;quot;;&lt;br /&gt;
        Sal = 0.0;&lt;br /&gt;
    }&lt;br /&gt;
    public Employee(String ename, double sal) {&lt;br /&gt;
        EmpID = i++;&lt;br /&gt;
        Ename = ename;&lt;br /&gt;
        Sal = sal;&lt;br /&gt;
    }&lt;br /&gt;
    public String toString() {&lt;br /&gt;
        return &amp;quot;EmpID &amp;quot; + EmpID + &amp;quot;\n&amp;quot; + &amp;quot;Ename &amp;quot; + Ename + &amp;quot;\n&amp;quot; + &amp;quot;Sal&amp;quot; + Sal;&lt;br /&gt;
    }&lt;br /&gt;
    public int compareTo(Object o1) {&lt;br /&gt;
        if (this.Sal == ((Employee) o1).Sal)&lt;br /&gt;
            return 0;&lt;br /&gt;
        else if ((this.Sal) &amp;gt; ((Employee) o1).Sal)&lt;br /&gt;
            return 1;&lt;br /&gt;
        else&lt;br /&gt;
            return -1;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
ComparableDemo.java is showing the use of this interface. This class first makes a list of objects of type Employee and call sort method of java.util.Collections, which internally uses compareTo() method of Employee class and sort the list accordingly.&lt;br /&gt;
 &lt;br /&gt;
  import java.util.*;&lt;br /&gt;
  public class ComparableDemo{&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
        List ts1 = new ArrayList();&lt;br /&gt;
        ts1.add(new Employee (&amp;quot;Tom&amp;quot;,40000.00));&lt;br /&gt;
        ts1.add(new Employee (&amp;quot;Harry&amp;quot;,20000.00));&lt;br /&gt;
        ts1.add(new Employee (&amp;quot;Maggie&amp;quot;,50000.00));&lt;br /&gt;
        ts1.add(new Employee (&amp;quot;Chris&amp;quot;,70000.00));&lt;br /&gt;
        Collections.sort(ts1);&lt;br /&gt;
        Iterator itr = ts1.iterator();&lt;br /&gt;
        while(itr.hasNext()){&lt;br /&gt;
            Object element = itr.next();&lt;br /&gt;
            System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
''Output:''&lt;br /&gt;
  &lt;br /&gt;
  EmpID 1&lt;br /&gt;
  Ename Harry&lt;br /&gt;
  Sal    20000.0&lt;br /&gt;
  EmpID 0&lt;br /&gt;
  Ename Tom&lt;br /&gt;
  Sal    40000.0&lt;br /&gt;
  EmpID 2&lt;br /&gt;
  Ename Maggie&lt;br /&gt;
  Sal    50000.0&lt;br /&gt;
  EmpID 3&lt;br /&gt;
  Ename Chris&lt;br /&gt;
  Sal    70000.0&lt;br /&gt;
&lt;br /&gt;
====Singleton Behaviour====&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Singleton_pattern Singleton] is a class guaranteed to have only a single instance, with a single global access point to that instance. It can be implemented in Ruby and Java. In Ruby we can include a module to acquire all the functionality of the singleton module. The code is already there and we are just reusing the code and adding that similar functionality into our class. Here our intention is to add the functionality of singleton to the class which is achieved using mixins. In java, implementation of interfaces means that the class which implements the interface becomes a type of that class, which is not the intention here. So, there is not interface which class can implement to get the functionality in itself. It has to implement the functionality itself without implementing any interface. &lt;br /&gt;
&lt;br /&gt;
'''Ruby'''&lt;br /&gt;
&lt;br /&gt;
When you include the singleton method inside your class it makes the new method private so that you cannot use it. Including ‘singleton’ adds a class method called instance that instantiates only one instance of the class&lt;br /&gt;
&lt;br /&gt;
  # require singleton lib&lt;br /&gt;
  require 'singleton'&lt;br /&gt;
  class AppConfig&lt;br /&gt;
    # mixin the singleton module&lt;br /&gt;
    include Singleton&lt;br /&gt;
    # do the actual app configuration&lt;br /&gt;
    def load_config(file)&lt;br /&gt;
      # do your work here&lt;br /&gt;
      puts &amp;quot;Application configuration file was loaded from file: #{file}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
''output:''&lt;br /&gt;
&lt;br /&gt;
  conf1 = AppConfig.instance&lt;br /&gt;
  conf1.load_config &amp;quot;/home/khelll/conf.yml&amp;quot;       #=&amp;gt;Application configuration file was loaded from file: /home/khelll/conf.yml&lt;br /&gt;
  conf2 = AppConfig.instance&lt;br /&gt;
  puts conf1 == conf2      #=&amp;gt;true&lt;br /&gt;
  # notice the following 2 lines won’t work&lt;br /&gt;
  AppConfig.new rescue(puts $!)   #=&amp;gt; new method is private&lt;br /&gt;
&lt;br /&gt;
====Enumerable Behaviour====&lt;br /&gt;
This [http://www.ruby-doc.org/core/classes/Enumerable.html enumerable] functionality adds support to the array members of the class. We can achieve this functionality using mixins in Ruby while it is not possible to do it in Java using interfaces.&lt;br /&gt;
&lt;br /&gt;
'''Implementation in Ruby:''' &lt;br /&gt;
&lt;br /&gt;
It adds the functionality of map, inject, select etc to the class which includes the ‘Enumerable’ module. The class which includes should represent a collection of items. Enumerable requires that your class contain an each method that serves up the items in the collection. All the other Enumerable methods rely on this. &lt;br /&gt;
Example:&lt;br /&gt;
This is a Team class that manages a group of members:&lt;br /&gt;
&lt;br /&gt;
  class Team&lt;br /&gt;
    include Enumerable&lt;br /&gt;
    attr_accessor :members&lt;br /&gt;
    def initialize&lt;br /&gt;
      @members = []&lt;br /&gt;
    end&lt;br /&gt;
    def each &amp;amp;block&lt;br /&gt;
      @members.each{|member| block.call(member)}&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Enumerable requires that your class contain an each method that serves up the items in the collection. All the other Enumerablemethods rely on this. Now we can use the map method, for instance:&lt;br /&gt;
&lt;br /&gt;
  team = Team.new&lt;br /&gt;
  team.members = ['joshua', 'gabriel', 'jacob']&lt;br /&gt;
  team.map{|member| member.capitalize}   # =&amp;gt; [&amp;quot;Joshua&amp;quot;, &amp;quot;Gabriel&amp;quot;, &amp;quot;Jacob&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
Now we can call any Enumerable methods on our team object itself, and it will assume we want to work with the members array within.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Data Mapper Behaviour====&lt;br /&gt;
&lt;br /&gt;
[http://datamapper.org/docs/ DataMapper] is an API which comes with the ability to use the same API to talk to a multitude of different datastores. There are adapters for the usual RDBMS suspects, NoSQL stores, various file formats and even some popular webservices. This is implemented in ruby using mixins. We need not use interfaces to implement.&lt;br /&gt;
&lt;br /&gt;
'''Implementation in Ruby:'''&lt;br /&gt;
  class Post&lt;br /&gt;
       include DataMapper::Resource&lt;br /&gt;
       property :id, Serial&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
The point to take away from this is not that Ruby Mixins are more powerful than Java Interfaces. The mixin feature of Ruby allows for the development of easy to use but powerful plugins that will be hard for any non-Ruby based framework to compete with. Java has many other ways of implementing the functionality mixins are capable of. It is just that it does not use Interfaces to achieve the functionality. Even though both of them are a way of achieving multiple inheritance in their respective languages, they achieve quiet different goals. Inheritance in Java is mainly to overcome the confusion which arises with multiple inheritance, while Mixins in Ruby is mainly to add an additional functionality to the existing class apart from the ‘primary functionality’ of the class. &lt;br /&gt;
&lt;br /&gt;
When a class implements Interface, it indicates that it is of the type interface class and forms a hierarchy with the interface class, while ruby mixins are mainly intended for code reuse and including an additional functionality to the class. It does not form any hierarchy as it is only using module of the class.&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* http://en.wikipedia.org/wiki/Interface_(Java)&lt;br /&gt;
* http://en.wikipedia.org/wiki/Mixin&lt;br /&gt;
* http://carboni.ca/blog/p/Modules-How-Do-They-Work&lt;br /&gt;
* http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/&lt;br /&gt;
* http://rubylearning.com/satishtalim/modules_mixins.html&lt;br /&gt;
* http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-&lt;br /&gt;
* http://datamapper.org/&lt;br /&gt;
* http://www.dsource.org/projects/tutorials/wiki/SingletonPattern &lt;br /&gt;
* http://corelib.rubyonrails.org/classes/Comparable.html&lt;br /&gt;
* http://kconrails.com/2010/11/30/ruby-enumerable-primer-part-1-the-basics/&lt;br /&gt;
* http://stackoverflow.com/questions/575920/is-a-ruby-module-equivalent-to-a-java-interface&lt;/div&gt;</summary>
		<author><name>Sthirum</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2c_bs&amp;diff=51683</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2c bs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2c_bs&amp;diff=51683"/>
		<updated>2011-10-02T01:25:31Z</updated>

		<summary type="html">&lt;p&gt;Sthirum: /* Module */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
An [http://en.wikipedia.org/wiki/Interface_(computing) interface] &amp;lt;ref&amp;gt;interface&amp;lt;/ref&amp;gt; in the [http://en.wikipedia.org/wiki/Java_(programming_language) Java] programming language is an abstract type that is used to specify an interface that classes must implement. Interface may only contain [http://en.wikipedia.org/wiki/Method_signature method signature] and constant declarations. An interface may never contain method definitions. Interfaces cannot be instantiated. A class that implements an interface must implement all of the methods described in the interface, or be an [http://en.wikipedia.org/wiki/Abstract_type abstract] class. Object references in Java may be specified to be of an interface type; in which case, they must either be null, or be bound to an object that implements the interface. &lt;br /&gt;
&lt;br /&gt;
In object-oriented programming languages, a [http://en.wikipedia.org/wiki/Mixin mixin] &amp;lt;ref&amp;gt; mixin &amp;lt;/ref&amp;gt; is a class that provides a certain functionality to be inherited or just reused by a subclass. Some of the functionality of mixins is provided by interfaces in popular languages like Java and [http://en.wikipedia.org/wiki/C_Sharp_(programming_language) C#]. However, an interface only specifies what the class must support and cannot provide an implementation. Another class, providing an implementation and dependent with the interface, is needed for [http://en.wikipedia.org/wiki/Code_refactoring refactoring] common behavior into a single place. Interfaces combined with aspect-oriented programming can produce full-fledged mixins in languages that support such features, such as C# or Java. &lt;br /&gt;
One benefit of using interfaces is that they simulate [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance]. All classes in Java other than [https://matrix.senecac.on.ca:8443/wiki/javaSE/index.php/Java.lang.Object java.lang.Object], must have exactly one base class; multiple inheritance of classes is not allowed.&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
===Mixins in Ruby=== &lt;br /&gt;
In Java you just have [http://en.wikipedia.org/wiki/Class_(computer_programming) classes] (both abstract and concrete) and interfaces. The [http://en.wikipedia.org/wiki/Ruby Ruby] language provides classes,[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html modules], and a mix of both. Inheriting from a mixin is not a form of specialization but is rather a means of collecting functionality. A class may inherit most or all of its functionality from one or more mixins through multiple inheritance. A mixin can also be viewed as an interface with implemented methods. When a class includes a mixin, the class implements the interface and includes, rather than inherits, all the mixins’ attributes (fields, properties) and methods. They become part of the class during compilation. A mixin can defer definition and binding of methods until runtime, though attributes and instantiation parameters are still defined at compile time. In the Ruby language a mixin is a class that is mixed with a module. &lt;br /&gt;
&lt;br /&gt;
====Module====&lt;br /&gt;
&lt;br /&gt;
A [http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html module]&amp;lt;ref&amp;gt;module&amp;lt;/ref&amp;gt; is a degenerate abstract class. A module can’t be instantiated and no class can directly extend it but it can fully implement methods. A class can leverage the implementation of a module by including the module’s methods. A module can define methods that can be shared in different and separate classes either at the class or instance level.We can view an example &amp;lt;ref&amp;gt; http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/&amp;lt;/ref&amp;gt; module that converts a numeric value to its English word.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # Convert a integer value to English.&lt;br /&gt;
  module Stringify&lt;br /&gt;
    # Requires an instance variable @value&lt;br /&gt;
    def stringify&lt;br /&gt;
      if @value == 1&lt;br /&gt;
        &amp;quot;One&amp;quot;&lt;br /&gt;
      elsif @value == 2&lt;br /&gt;
        &amp;quot;Two&amp;quot;&lt;br /&gt;
      elsif @value == 3&lt;br /&gt;
        &amp;quot;Three&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The Stringify module above makes use of a @value [http://en.wikipedia.org/wiki/Instance_variable instance variable]. The class that will be mixed with this module needs to define and set a @value instance variable since the Stringify module uses it but does not define it. In addition to instance variables a module could invoke methods not defined in the module but in the class that it will be mixed with.&lt;br /&gt;
&lt;br /&gt;
A [http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/ Self contained module] can also be written in Ruby where the methods defined in a module can be used as static methods independent of their implementation within a class.&lt;br /&gt;
&lt;br /&gt;
'''Self Contained Module Example: '''&lt;br /&gt;
&lt;br /&gt;
  # A Math module akin to Java Math class.&lt;br /&gt;
  module Math&lt;br /&gt;
    # Could be called as a class, static, method&lt;br /&gt;
    def add(val_one, val_two)&lt;br /&gt;
      BigInteger.new(val_one + val_two)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The methods in the Math module are intended to be invoked like class methods, also known as [http://en.wikipedia.org/wiki/Method_(computer_programming)#Static_methods static] methods. The add method in the Math module accepts two integer values and returns an [http://en.wikipedia.org/wiki/Instance_(computer_science) instance] of BigInteger.&lt;br /&gt;
&lt;br /&gt;
====Using Mixins====&lt;br /&gt;
&lt;br /&gt;
Implementation of the class and module are joined, interwined, combined, etc. A mixin is a different mechanism to the extend construct used to add concrete implementation to a class. With a mixin you can extend from a module instead of a class. &lt;br /&gt;
&lt;br /&gt;
The example of self-contained module shows how the method returns a BigInteger sum after accepting two integer parameters. The following example is the implementation of BigInteger class. The BigInteger class defines one constructor and directly inherits one method from the Number base class. To mix in the methods implemented in the Stringify and Math modules with the BigInteger class you will note the usage of the include and extend methods, respectively.&lt;br /&gt;
&lt;br /&gt;
  # Base Number class&lt;br /&gt;
  class Number&lt;br /&gt;
    def intValue&lt;br /&gt;
      @value&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  # BigInteger extends Number&lt;br /&gt;
  class BigInteger &amp;lt; Number&lt;br /&gt;
 &lt;br /&gt;
  # Add instance methods from Stringify&lt;br /&gt;
  include Stringify&lt;br /&gt;
 &lt;br /&gt;
  # Add class methods from Math&lt;br /&gt;
  extend Math&lt;br /&gt;
 &lt;br /&gt;
  # Add a constructor with one parameter&lt;br /&gt;
    def initialize(value)&lt;br /&gt;
      @value = value&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Using the class:&lt;br /&gt;
&lt;br /&gt;
  # Create a new object&lt;br /&gt;
  bigint1 = BigInteger.new(10)&lt;br /&gt;
  # Call a method inherited from the base class&lt;br /&gt;
  puts bigint1.intValue   # --&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
The extend method will mix a module’s methods at the class level. The method defined in the Math module can be used as a class/static method.&lt;br /&gt;
&lt;br /&gt;
  # Call class method extended from Math&lt;br /&gt;
  bigint2 = BigInteger.add(-2, 4)&lt;br /&gt;
  puts bigint2.intValue   # --&amp;gt; 2&lt;br /&gt;
&lt;br /&gt;
The include method will mix a module’s methods at the instance level, meaning that the methods will become instance methods. The method defined in the Stringify module can be used as an instance method.&lt;br /&gt;
&lt;br /&gt;
  # Call a method included from Stringify&lt;br /&gt;
  puts bigint2.stringify   # --&amp;gt; 'Two'&lt;br /&gt;
&lt;br /&gt;
There is another use of the extend method. You can enhance an object instance by mixing it with a module at run time. This ‘CurrencyFormatter’ is used to extend an object, changing it’s responsibilities at runtime.&lt;br /&gt;
&lt;br /&gt;
  # Format a numeric value as a currency&lt;br /&gt;
  module CurrencyFormatter&lt;br /&gt;
    def format&lt;br /&gt;
      &amp;quot;$#{@value}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
To mix an object instance with a module you can do the following:&lt;br /&gt;
&lt;br /&gt;
  # Add the module methods to&lt;br /&gt;
  # this object instance, only!&lt;br /&gt;
  bigint2.extend CurrencyFormatter&lt;br /&gt;
  puts bigint2.format   # --&amp;gt; '$2'&lt;br /&gt;
&lt;br /&gt;
Calling the extend method on an instance will only extend that one object; objects of the same class will not be extended with the new functionality.&lt;br /&gt;
&lt;br /&gt;
  puts bigint1.format   # will generate an error&lt;br /&gt;
&lt;br /&gt;
Modules that will be mixed with a class via the include or extend method could define something like a constructor or initializer method to the module. The module initializer method will be invoked at the time the module is mixed with a class. When a class extends a module the module’s self.extended method will be invoked:&lt;br /&gt;
&lt;br /&gt;
  module Math&lt;br /&gt;
    def self.extended(base)&lt;br /&gt;
      # Initialize module.&lt;br /&gt;
    end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
The self prefix indicates that the method is a static module level method. The base parameter in the static extended method will be either an instance object or class object of the class that extended the module depending whether you extend a object or class, respectively.&lt;br /&gt;
When a class includes a module the module’s self.included method will be invoked.&lt;br /&gt;
&lt;br /&gt;
  module Stringify&lt;br /&gt;
    def self.included(base)&lt;br /&gt;
      # Initialize module.&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The base parameter will be a class object for the class that includes the module.&lt;br /&gt;
It is important to note that inside the included and extended initializer methods you can include and extend other modules. &lt;br /&gt;
&lt;br /&gt;
  module Stringify&lt;br /&gt;
    def self.included(base)&lt;br /&gt;
      base.extend SomeOtherModule&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===Interfaces in Java===&lt;br /&gt;
Interfaces define a standardized set of commands that a class will obey. The commands are a set of methods that a class implements. The interface definition states the names of the methods and their return types and argument signatures. There is no executable body for any method. Example: &lt;br /&gt;
  interface Bicycle {&lt;br /&gt;
       void changeCadence(int newValue);   // wheel revolutions per minute&lt;br /&gt;
       void changeGear(int newValue);&lt;br /&gt;
       void speedUp(int increment);&lt;br /&gt;
       void applyBrakes(int decrement);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Implementation is independent to each class that implements the interface. Once a class implements an interface, the Java compiler knows that an instance of the class will contain the specified set of methods. Therefore, it will allow you to call those methods for an object referenced by a variable whose type is the interface. Implementing an interface enables a class to be &amp;quot;plugged in&amp;quot; in any situation that requires a specific behavior (manifested through the set of methods).&lt;br /&gt;
&lt;br /&gt;
  class ACMEBicycle implements Bicycle {&lt;br /&gt;
    //implementation specific to this class&lt;br /&gt;
       int cadence = 0;&lt;br /&gt;
       int speed = 0;&lt;br /&gt;
       int gear = 1;&lt;br /&gt;
       void changeCadence(int newValue) {&lt;br /&gt;
            cadence = newValue;&lt;br /&gt;
       }&lt;br /&gt;
       void changeGear(int newValue) {&lt;br /&gt;
            gear = newValue;&lt;br /&gt;
       }&lt;br /&gt;
       void speedUp(int increment) {&lt;br /&gt;
            speed = speed + increment;   &lt;br /&gt;
       }&lt;br /&gt;
       void applyBrakes(int decrement) {&lt;br /&gt;
            speed = speed - decrement;&lt;br /&gt;
       }&lt;br /&gt;
       void printStates() {&lt;br /&gt;
            System.out.println(&amp;quot;cadence:&amp;quot;+cadence+&amp;quot; speed:&amp;quot;+speed+&amp;quot; gear:&amp;quot;+gear);&lt;br /&gt;
       }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
  class MOUNTAINBicycle implements Bicycle {&lt;br /&gt;
  int cadence = 2;&lt;br /&gt;
       int speed = 3;&lt;br /&gt;
       int gear = 5;&lt;br /&gt;
       void changeCadence(int newValue) {&lt;br /&gt;
            cadence = newValue + 10;&lt;br /&gt;
       }&lt;br /&gt;
       void changeGear(int newValue) {&lt;br /&gt;
            gear = newValue + 4;&lt;br /&gt;
       }&lt;br /&gt;
       void speedUp(int increment) {&lt;br /&gt;
            speed = speed + increment + 10;   &lt;br /&gt;
       }&lt;br /&gt;
       void applyBrakes(int decrement) {&lt;br /&gt;
            speed = speed – decrement – 10;&lt;br /&gt;
       }&lt;br /&gt;
       void printStates() {&lt;br /&gt;
            System.out.println(&amp;quot;cadence:&amp;quot;+cadence+&amp;quot; speed:&amp;quot;+speed+&amp;quot; gear:&amp;quot;+gear);&lt;br /&gt;
       }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Using an interface rather than inheritance to specify a certain set of methods allows a class to inherit from some other class. In other words, if a class needs two different sets of methods, so it can behave like two different types of things, it could inherit one set from class A, and use an interface B to specify the other.&lt;br /&gt;
You could then reference one of these objects with either an A reference or a B reference. Implementing an interface allows a class to become more formal about the behavior it promises to provide. Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler. If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Mixins vs Interfaces==&lt;br /&gt;
Because of this mixin feature, a developer can add arbitrary methods and modify behavior of core classes at runtime. This is amazingly powerful if you are trying to write plugins and extensions to the [http://en.wikipedia.org/wiki/Software_framework framework].  Because you can add functionality to existing objects, users can install their [http://en.wikipedia.org/wiki/Plug-in_(computing) plugin] and start taking advantage of new functionality without having to make changes to the objects that they are instantiating in their application. &lt;br /&gt;
&lt;br /&gt;
In frameworks written in other languages, such as Java, plugging in new functionality means that you need to change how your objects are instantiated. This will require code changes and/or potentially configuration changes which makes it hard to develop, hard to maintain, and a pain for plugin developers to support.  But because of the mixin feature, Rails plugin developers can customize the base objects and the users of the plugins do not have to change any of their code or configuration logic.&lt;br /&gt;
&lt;br /&gt;
===Differences===&lt;br /&gt;
&lt;br /&gt;
These is a general comparision: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Mixins in Ruby&lt;br /&gt;
! Interfaces in Java&lt;br /&gt;
|-&lt;br /&gt;
| 'include' or 'extend' keywords used to include the module in the class.&lt;br /&gt;
| 'implement' keyword is used to implement interface&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Need not implement the module methods in the class.&lt;br /&gt;
| Should implement the interface methods. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| The methods are not copied into the class in which they are included. They are referenced.&lt;br /&gt;
| The methods’ implementation is in the class itself.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| The included method definition is decided at ‘run time’. If the definition changes while running the function points to the changed code.&lt;br /&gt;
| The definition is known compile time and will not change until the code is compiled and run again. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Modules inclusion does not provide a superclass and subclass relationship.&lt;br /&gt;
| Interface extending provides a superclass and subclass relationship. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| When we use mixins we included only a module of the class. &lt;br /&gt;
| We include the entire interface class. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Mainly for code reuse and does not provide any hierarchy. &lt;br /&gt;
| It provides a hierarchy. &lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Functionality and Behavior Comparison===&lt;br /&gt;
The functionality and behavior of Mixins in Ruby is compared with Interfaces in Java&lt;br /&gt;
&lt;br /&gt;
====Comparable Functionality====&lt;br /&gt;
Sometime we are required to compare the class members of different objects of the same class and/or sort them. This is the comparable functionality that we are trying to achieve for a given class.  This functionality can be achieved by both the Ruby using mixins and Java using interfaces. There is not much difference using both.&lt;br /&gt;
&lt;br /&gt;
'''Ruby'''&lt;br /&gt;
&lt;br /&gt;
The [http://www.ruby-doc.org/core/classes/Comparable.html Comparable] mixin is used by classes whose objects may be ordered. The class must define the &amp;lt;=&amp;gt; operator, which compares the receiver against another object, returning -1, 0, or +1 depending on whether the receiver is less than, equal to, or greater than the other object. Comparable uses &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method between?.&lt;br /&gt;
&lt;br /&gt;
  class SizeMatters&lt;br /&gt;
     include Comparable&lt;br /&gt;
     attr :str&lt;br /&gt;
     def &amp;lt;=&amp;gt;(anOther)&lt;br /&gt;
       str.size &amp;lt;=&amp;gt; anOther.str.size&lt;br /&gt;
     end&lt;br /&gt;
     def initialize(str)&lt;br /&gt;
       @str = str&lt;br /&gt;
     end&lt;br /&gt;
     def inspect&lt;br /&gt;
       @str&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
''using class:'' &lt;br /&gt;
&lt;br /&gt;
   s1 = SizeMatters.new(&amp;quot;Z&amp;quot;)&lt;br /&gt;
   s2 = SizeMatters.new(&amp;quot;YY&amp;quot;)&lt;br /&gt;
   s3 = SizeMatters.new(&amp;quot;XXX&amp;quot;)&lt;br /&gt;
   s4 = SizeMatters.new(&amp;quot;WWWW&amp;quot;)&lt;br /&gt;
   s5 = SizeMatters.new(&amp;quot;VVVVV&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
''output:''&lt;br /&gt;
   s1 &amp;lt; s2                       #=&amp;gt; true&lt;br /&gt;
   s4.between?(s1, s3)           #=&amp;gt; false&lt;br /&gt;
   s4.between?(s3, s5)           #=&amp;gt; true&lt;br /&gt;
   [ s3, s2, s5, s4, s1 ].sort   #=&amp;gt; [Z, YY, XXX, WWWW, VVVVV]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Java'''&lt;br /&gt;
&lt;br /&gt;
In Java, comparable functionality is achieved by extending the “Comparable” interface. List of objects that implement this interface can be sorted automatically by sort method of the list interface. This interface has compareTo() method that is used by the sort() method of the list. Here compareTo() method can also be used to compare two objects with respect to the implementation in compareTo() implementation in the derived class. It returns ‘0’ , ‘1’ and ‘-1’ corresponding to whether one is equal to , greater than or lesser than other.&lt;br /&gt;
In this example code Employee class is implementing Comparable interface and have method compareTo().&lt;br /&gt;
  public class Employee implements Comparable {&lt;br /&gt;
    int EmpID;&lt;br /&gt;
    String Ename;&lt;br /&gt;
    double Sal;&lt;br /&gt;
    static int i;&lt;br /&gt;
    public Employee() {&lt;br /&gt;
        EmpID = i++;&lt;br /&gt;
        Ename = &amp;quot;dont know&amp;quot;;&lt;br /&gt;
        Sal = 0.0;&lt;br /&gt;
    }&lt;br /&gt;
    public Employee(String ename, double sal) {&lt;br /&gt;
        EmpID = i++;&lt;br /&gt;
        Ename = ename;&lt;br /&gt;
        Sal = sal;&lt;br /&gt;
    }&lt;br /&gt;
    public String toString() {&lt;br /&gt;
        return &amp;quot;EmpID &amp;quot; + EmpID + &amp;quot;\n&amp;quot; + &amp;quot;Ename &amp;quot; + Ename + &amp;quot;\n&amp;quot; + &amp;quot;Sal&amp;quot; + Sal;&lt;br /&gt;
    }&lt;br /&gt;
    public int compareTo(Object o1) {&lt;br /&gt;
        if (this.Sal == ((Employee) o1).Sal)&lt;br /&gt;
            return 0;&lt;br /&gt;
        else if ((this.Sal) &amp;gt; ((Employee) o1).Sal)&lt;br /&gt;
            return 1;&lt;br /&gt;
        else&lt;br /&gt;
            return -1;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
ComparableDemo.java is showing the use of this interface. This class first makes a list of objects of type Employee and call sort method of java.util.Collections, which internally uses compareTo() method of Employee class and sort the list accordingly.&lt;br /&gt;
 &lt;br /&gt;
  import java.util.*;&lt;br /&gt;
  public class ComparableDemo{&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
        List ts1 = new ArrayList();&lt;br /&gt;
        ts1.add(new Employee (&amp;quot;Tom&amp;quot;,40000.00));&lt;br /&gt;
        ts1.add(new Employee (&amp;quot;Harry&amp;quot;,20000.00));&lt;br /&gt;
        ts1.add(new Employee (&amp;quot;Maggie&amp;quot;,50000.00));&lt;br /&gt;
        ts1.add(new Employee (&amp;quot;Chris&amp;quot;,70000.00));&lt;br /&gt;
        Collections.sort(ts1);&lt;br /&gt;
        Iterator itr = ts1.iterator();&lt;br /&gt;
        while(itr.hasNext()){&lt;br /&gt;
            Object element = itr.next();&lt;br /&gt;
            System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
''Output:''&lt;br /&gt;
  &lt;br /&gt;
  EmpID 1&lt;br /&gt;
  Ename Harry&lt;br /&gt;
  Sal    20000.0&lt;br /&gt;
  EmpID 0&lt;br /&gt;
  Ename Tom&lt;br /&gt;
  Sal    40000.0&lt;br /&gt;
  EmpID 2&lt;br /&gt;
  Ename Maggie&lt;br /&gt;
  Sal    50000.0&lt;br /&gt;
  EmpID 3&lt;br /&gt;
  Ename Chris&lt;br /&gt;
  Sal    70000.0&lt;br /&gt;
&lt;br /&gt;
====Singleton Behaviour====&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Singleton_pattern Singleton] is a class guaranteed to have only a single instance, with a single global access point to that instance. It can be implemented in Ruby and Java. In Ruby we can include a module to acquire all the functionality of the singleton module. The code is already there and we are just reusing the code and adding that similar functionality into our class. Here our intention is to add the functionality of singleton to the class which is achieved using mixins. In java, implementation of interfaces means that the class which implements the interface becomes a type of that class, which is not the intention here. So, there is not interface which class can implement to get the functionality in itself. It has to implement the functionality itself without implementing any interface. &lt;br /&gt;
&lt;br /&gt;
'''Ruby'''&lt;br /&gt;
&lt;br /&gt;
When you include the singleton method inside your class it makes the new method private so that you cannot use it. Including ‘singleton’ adds a class method called instance that instantiates only one instance of the class&lt;br /&gt;
&lt;br /&gt;
  # require singleton lib&lt;br /&gt;
  require 'singleton'&lt;br /&gt;
  class AppConfig&lt;br /&gt;
    # mixin the singleton module&lt;br /&gt;
    include Singleton&lt;br /&gt;
    # do the actual app configuration&lt;br /&gt;
    def load_config(file)&lt;br /&gt;
      # do your work here&lt;br /&gt;
      puts &amp;quot;Application configuration file was loaded from file: #{file}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
''output:''&lt;br /&gt;
&lt;br /&gt;
  conf1 = AppConfig.instance&lt;br /&gt;
  conf1.load_config &amp;quot;/home/khelll/conf.yml&amp;quot;       #=&amp;gt;Application configuration file was loaded from file: /home/khelll/conf.yml&lt;br /&gt;
  conf2 = AppConfig.instance&lt;br /&gt;
  puts conf1 == conf2      #=&amp;gt;true&lt;br /&gt;
  # notice the following 2 lines won’t work&lt;br /&gt;
  AppConfig.new rescue(puts $!)   #=&amp;gt; new method is private&lt;br /&gt;
&lt;br /&gt;
====Enumerable Behaviour====&lt;br /&gt;
This [http://www.ruby-doc.org/core/classes/Enumerable.html enumerable] functionality adds support to the array members of the class. We can achieve this functionality using mixins in Ruby while it is not possible to do it in Java using interfaces.&lt;br /&gt;
&lt;br /&gt;
'''Implementation in Ruby:''' &lt;br /&gt;
&lt;br /&gt;
It adds the functionality of map, inject, select etc to the class which includes the ‘Enumerable’ module. The class which includes should represent a collection of items. Enumerable requires that your class contain an each method that serves up the items in the collection. All the other Enumerable methods rely on this. &lt;br /&gt;
Example:&lt;br /&gt;
This is a Team class that manages a group of members:&lt;br /&gt;
&lt;br /&gt;
  class Team&lt;br /&gt;
    include Enumerable&lt;br /&gt;
    attr_accessor :members&lt;br /&gt;
    def initialize&lt;br /&gt;
      @members = []&lt;br /&gt;
    end&lt;br /&gt;
    def each &amp;amp;block&lt;br /&gt;
      @members.each{|member| block.call(member)}&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Enumerable requires that your class contain an each method that serves up the items in the collection. All the other Enumerablemethods rely on this. Now we can use the map method, for instance:&lt;br /&gt;
&lt;br /&gt;
  team = Team.new&lt;br /&gt;
  team.members = ['joshua', 'gabriel', 'jacob']&lt;br /&gt;
  team.map{|member| member.capitalize}   # =&amp;gt; [&amp;quot;Joshua&amp;quot;, &amp;quot;Gabriel&amp;quot;, &amp;quot;Jacob&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
Now we can call any Enumerable methods on our team object itself, and it will assume we want to work with the members array within.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Data Mapper Behaviour====&lt;br /&gt;
&lt;br /&gt;
[http://datamapper.org/docs/ DataMapper] is an API which comes with the ability to use the same API to talk to a multitude of different datastores. There are adapters for the usual RDBMS suspects, NoSQL stores, various file formats and even some popular webservices. This is implemented in ruby using mixins. We need not use interfaces to implement.&lt;br /&gt;
&lt;br /&gt;
'''Implementation in Ruby:'''&lt;br /&gt;
  class Post&lt;br /&gt;
       include DataMapper::Resource&lt;br /&gt;
       property :id, Serial&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
The point to take away from this is not that Ruby Mixins are more powerful than Java Interfaces. The mixin feature of Ruby allows for the development of easy to use but powerful plugins that will be hard for any non-Ruby based framework to compete with. Java has many other ways of implementing the functionality mixins are capable of. It is just that it does not use Interfaces to achieve the functionality. Even though both of them are a way of achieving multiple inheritance in their respective languages, they achieve quiet different goals. Inheritance in Java is mainly to overcome the confusion which arises with multiple inheritance, while Mixins in Ruby is mainly to add an additional functionality to the existing class apart from the ‘primary functionality’ of the class. &lt;br /&gt;
&lt;br /&gt;
When a class implements Interface, it indicates that it is of the type interface class and forms a hierarchy with the interface class, while ruby mixins are mainly intended for code reuse and including an additional functionality to the class. It does not form any hierarchy as it is only using module of the class.&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* http://en.wikipedia.org/wiki/Interface_(Java)&lt;br /&gt;
* http://en.wikipedia.org/wiki/Mixin&lt;br /&gt;
* http://carboni.ca/blog/p/Modules-How-Do-They-Work&lt;br /&gt;
* http://rubylearning.com/satishtalim/modules_mixins.html&lt;br /&gt;
* http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/&lt;br /&gt;
* http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-&lt;br /&gt;
* http://datamapper.org/&lt;br /&gt;
* http://www.dsource.org/projects/tutorials/wiki/SingletonPattern &lt;br /&gt;
* http://corelib.rubyonrails.org/classes/Comparable.html&lt;br /&gt;
* http://kconrails.com/2010/11/30/ruby-enumerable-primer-part-1-the-basics/&lt;br /&gt;
* http://stackoverflow.com/questions/575920/is-a-ruby-module-equivalent-to-a-java-interface&lt;/div&gt;</summary>
		<author><name>Sthirum</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2c_bs&amp;diff=51682</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2c bs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2c_bs&amp;diff=51682"/>
		<updated>2011-10-02T01:24:49Z</updated>

		<summary type="html">&lt;p&gt;Sthirum: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
An [http://en.wikipedia.org/wiki/Interface_(computing) interface] &amp;lt;ref&amp;gt;interface&amp;lt;/ref&amp;gt; in the [http://en.wikipedia.org/wiki/Java_(programming_language) Java] programming language is an abstract type that is used to specify an interface that classes must implement. Interface may only contain [http://en.wikipedia.org/wiki/Method_signature method signature] and constant declarations. An interface may never contain method definitions. Interfaces cannot be instantiated. A class that implements an interface must implement all of the methods described in the interface, or be an [http://en.wikipedia.org/wiki/Abstract_type abstract] class. Object references in Java may be specified to be of an interface type; in which case, they must either be null, or be bound to an object that implements the interface. &lt;br /&gt;
&lt;br /&gt;
In object-oriented programming languages, a [http://en.wikipedia.org/wiki/Mixin mixin] &amp;lt;ref&amp;gt; mixin &amp;lt;/ref&amp;gt; is a class that provides a certain functionality to be inherited or just reused by a subclass. Some of the functionality of mixins is provided by interfaces in popular languages like Java and [http://en.wikipedia.org/wiki/C_Sharp_(programming_language) C#]. However, an interface only specifies what the class must support and cannot provide an implementation. Another class, providing an implementation and dependent with the interface, is needed for [http://en.wikipedia.org/wiki/Code_refactoring refactoring] common behavior into a single place. Interfaces combined with aspect-oriented programming can produce full-fledged mixins in languages that support such features, such as C# or Java. &lt;br /&gt;
One benefit of using interfaces is that they simulate [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance]. All classes in Java other than [https://matrix.senecac.on.ca:8443/wiki/javaSE/index.php/Java.lang.Object java.lang.Object], must have exactly one base class; multiple inheritance of classes is not allowed.&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
===Mixins in Ruby=== &lt;br /&gt;
In Java you just have [http://en.wikipedia.org/wiki/Class_(computer_programming) classes] (both abstract and concrete) and interfaces. The [http://en.wikipedia.org/wiki/Ruby Ruby] language provides classes,[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html modules], and a mix of both. Inheriting from a mixin is not a form of specialization but is rather a means of collecting functionality. A class may inherit most or all of its functionality from one or more mixins through multiple inheritance. A mixin can also be viewed as an interface with implemented methods. When a class includes a mixin, the class implements the interface and includes, rather than inherits, all the mixins’ attributes (fields, properties) and methods. They become part of the class during compilation. A mixin can defer definition and binding of methods until runtime, though attributes and instantiation parameters are still defined at compile time. In the Ruby language a mixin is a class that is mixed with a module. &lt;br /&gt;
&lt;br /&gt;
====Module====&lt;br /&gt;
&lt;br /&gt;
A [http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html module]&amp;lt;ref&amp;gt;module&amp;lt;/ref&amp;gt; is a degenerate abstract class. A module can’t be instantiated and no class can directly extend it but it can fully implement methods. A class can leverage the implementation of a module by including the module’s methods. A module can define methods that can be shared in different and separate classes either at the class or instance level.We can view an example &amp;lt;ref&amp;gt; [http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/]&amp;lt;/ref&amp;gt; module that converts a numeric value to its English word.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # Convert a integer value to English.&lt;br /&gt;
  module Stringify&lt;br /&gt;
    # Requires an instance variable @value&lt;br /&gt;
    def stringify&lt;br /&gt;
      if @value == 1&lt;br /&gt;
        &amp;quot;One&amp;quot;&lt;br /&gt;
      elsif @value == 2&lt;br /&gt;
        &amp;quot;Two&amp;quot;&lt;br /&gt;
      elsif @value == 3&lt;br /&gt;
        &amp;quot;Three&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The Stringify module above makes use of a @value [http://en.wikipedia.org/wiki/Instance_variable instance variable]. The class that will be mixed with this module needs to define and set a @value instance variable since the Stringify module uses it but does not define it. In addition to instance variables a module could invoke methods not defined in the module but in the class that it will be mixed with.&lt;br /&gt;
&lt;br /&gt;
A [http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/ Self contained module] can also be written in Ruby where the methods defined in a module can be used as static methods independent of their implementation within a class.&lt;br /&gt;
&lt;br /&gt;
'''Self Contained Module Example: '''&lt;br /&gt;
&lt;br /&gt;
  # A Math module akin to Java Math class.&lt;br /&gt;
  module Math&lt;br /&gt;
    # Could be called as a class, static, method&lt;br /&gt;
    def add(val_one, val_two)&lt;br /&gt;
      BigInteger.new(val_one + val_two)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The methods in the Math module are intended to be invoked like class methods, also known as [http://en.wikipedia.org/wiki/Method_(computer_programming)#Static_methods static] methods. The add method in the Math module accepts two integer values and returns an [http://en.wikipedia.org/wiki/Instance_(computer_science) instance] of BigInteger.&lt;br /&gt;
&lt;br /&gt;
====Using Mixins====&lt;br /&gt;
&lt;br /&gt;
Implementation of the class and module are joined, interwined, combined, etc. A mixin is a different mechanism to the extend construct used to add concrete implementation to a class. With a mixin you can extend from a module instead of a class. &lt;br /&gt;
&lt;br /&gt;
The example of self-contained module shows how the method returns a BigInteger sum after accepting two integer parameters. The following example is the implementation of BigInteger class. The BigInteger class defines one constructor and directly inherits one method from the Number base class. To mix in the methods implemented in the Stringify and Math modules with the BigInteger class you will note the usage of the include and extend methods, respectively.&lt;br /&gt;
&lt;br /&gt;
  # Base Number class&lt;br /&gt;
  class Number&lt;br /&gt;
    def intValue&lt;br /&gt;
      @value&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  # BigInteger extends Number&lt;br /&gt;
  class BigInteger &amp;lt; Number&lt;br /&gt;
 &lt;br /&gt;
  # Add instance methods from Stringify&lt;br /&gt;
  include Stringify&lt;br /&gt;
 &lt;br /&gt;
  # Add class methods from Math&lt;br /&gt;
  extend Math&lt;br /&gt;
 &lt;br /&gt;
  # Add a constructor with one parameter&lt;br /&gt;
    def initialize(value)&lt;br /&gt;
      @value = value&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Using the class:&lt;br /&gt;
&lt;br /&gt;
  # Create a new object&lt;br /&gt;
  bigint1 = BigInteger.new(10)&lt;br /&gt;
  # Call a method inherited from the base class&lt;br /&gt;
  puts bigint1.intValue   # --&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
The extend method will mix a module’s methods at the class level. The method defined in the Math module can be used as a class/static method.&lt;br /&gt;
&lt;br /&gt;
  # Call class method extended from Math&lt;br /&gt;
  bigint2 = BigInteger.add(-2, 4)&lt;br /&gt;
  puts bigint2.intValue   # --&amp;gt; 2&lt;br /&gt;
&lt;br /&gt;
The include method will mix a module’s methods at the instance level, meaning that the methods will become instance methods. The method defined in the Stringify module can be used as an instance method.&lt;br /&gt;
&lt;br /&gt;
  # Call a method included from Stringify&lt;br /&gt;
  puts bigint2.stringify   # --&amp;gt; 'Two'&lt;br /&gt;
&lt;br /&gt;
There is another use of the extend method. You can enhance an object instance by mixing it with a module at run time. This ‘CurrencyFormatter’ is used to extend an object, changing it’s responsibilities at runtime.&lt;br /&gt;
&lt;br /&gt;
  # Format a numeric value as a currency&lt;br /&gt;
  module CurrencyFormatter&lt;br /&gt;
    def format&lt;br /&gt;
      &amp;quot;$#{@value}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
To mix an object instance with a module you can do the following:&lt;br /&gt;
&lt;br /&gt;
  # Add the module methods to&lt;br /&gt;
  # this object instance, only!&lt;br /&gt;
  bigint2.extend CurrencyFormatter&lt;br /&gt;
  puts bigint2.format   # --&amp;gt; '$2'&lt;br /&gt;
&lt;br /&gt;
Calling the extend method on an instance will only extend that one object; objects of the same class will not be extended with the new functionality.&lt;br /&gt;
&lt;br /&gt;
  puts bigint1.format   # will generate an error&lt;br /&gt;
&lt;br /&gt;
Modules that will be mixed with a class via the include or extend method could define something like a constructor or initializer method to the module. The module initializer method will be invoked at the time the module is mixed with a class. When a class extends a module the module’s self.extended method will be invoked:&lt;br /&gt;
&lt;br /&gt;
  module Math&lt;br /&gt;
    def self.extended(base)&lt;br /&gt;
      # Initialize module.&lt;br /&gt;
    end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
The self prefix indicates that the method is a static module level method. The base parameter in the static extended method will be either an instance object or class object of the class that extended the module depending whether you extend a object or class, respectively.&lt;br /&gt;
When a class includes a module the module’s self.included method will be invoked.&lt;br /&gt;
&lt;br /&gt;
  module Stringify&lt;br /&gt;
    def self.included(base)&lt;br /&gt;
      # Initialize module.&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The base parameter will be a class object for the class that includes the module.&lt;br /&gt;
It is important to note that inside the included and extended initializer methods you can include and extend other modules. &lt;br /&gt;
&lt;br /&gt;
  module Stringify&lt;br /&gt;
    def self.included(base)&lt;br /&gt;
      base.extend SomeOtherModule&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===Interfaces in Java===&lt;br /&gt;
Interfaces define a standardized set of commands that a class will obey. The commands are a set of methods that a class implements. The interface definition states the names of the methods and their return types and argument signatures. There is no executable body for any method. Example: &lt;br /&gt;
  interface Bicycle {&lt;br /&gt;
       void changeCadence(int newValue);   // wheel revolutions per minute&lt;br /&gt;
       void changeGear(int newValue);&lt;br /&gt;
       void speedUp(int increment);&lt;br /&gt;
       void applyBrakes(int decrement);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Implementation is independent to each class that implements the interface. Once a class implements an interface, the Java compiler knows that an instance of the class will contain the specified set of methods. Therefore, it will allow you to call those methods for an object referenced by a variable whose type is the interface. Implementing an interface enables a class to be &amp;quot;plugged in&amp;quot; in any situation that requires a specific behavior (manifested through the set of methods).&lt;br /&gt;
&lt;br /&gt;
  class ACMEBicycle implements Bicycle {&lt;br /&gt;
    //implementation specific to this class&lt;br /&gt;
       int cadence = 0;&lt;br /&gt;
       int speed = 0;&lt;br /&gt;
       int gear = 1;&lt;br /&gt;
       void changeCadence(int newValue) {&lt;br /&gt;
            cadence = newValue;&lt;br /&gt;
       }&lt;br /&gt;
       void changeGear(int newValue) {&lt;br /&gt;
            gear = newValue;&lt;br /&gt;
       }&lt;br /&gt;
       void speedUp(int increment) {&lt;br /&gt;
            speed = speed + increment;   &lt;br /&gt;
       }&lt;br /&gt;
       void applyBrakes(int decrement) {&lt;br /&gt;
            speed = speed - decrement;&lt;br /&gt;
       }&lt;br /&gt;
       void printStates() {&lt;br /&gt;
            System.out.println(&amp;quot;cadence:&amp;quot;+cadence+&amp;quot; speed:&amp;quot;+speed+&amp;quot; gear:&amp;quot;+gear);&lt;br /&gt;
       }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
  class MOUNTAINBicycle implements Bicycle {&lt;br /&gt;
  int cadence = 2;&lt;br /&gt;
       int speed = 3;&lt;br /&gt;
       int gear = 5;&lt;br /&gt;
       void changeCadence(int newValue) {&lt;br /&gt;
            cadence = newValue + 10;&lt;br /&gt;
       }&lt;br /&gt;
       void changeGear(int newValue) {&lt;br /&gt;
            gear = newValue + 4;&lt;br /&gt;
       }&lt;br /&gt;
       void speedUp(int increment) {&lt;br /&gt;
            speed = speed + increment + 10;   &lt;br /&gt;
       }&lt;br /&gt;
       void applyBrakes(int decrement) {&lt;br /&gt;
            speed = speed – decrement – 10;&lt;br /&gt;
       }&lt;br /&gt;
       void printStates() {&lt;br /&gt;
            System.out.println(&amp;quot;cadence:&amp;quot;+cadence+&amp;quot; speed:&amp;quot;+speed+&amp;quot; gear:&amp;quot;+gear);&lt;br /&gt;
       }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Using an interface rather than inheritance to specify a certain set of methods allows a class to inherit from some other class. In other words, if a class needs two different sets of methods, so it can behave like two different types of things, it could inherit one set from class A, and use an interface B to specify the other.&lt;br /&gt;
You could then reference one of these objects with either an A reference or a B reference. Implementing an interface allows a class to become more formal about the behavior it promises to provide. Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler. If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Mixins vs Interfaces==&lt;br /&gt;
Because of this mixin feature, a developer can add arbitrary methods and modify behavior of core classes at runtime. This is amazingly powerful if you are trying to write plugins and extensions to the [http://en.wikipedia.org/wiki/Software_framework framework].  Because you can add functionality to existing objects, users can install their [http://en.wikipedia.org/wiki/Plug-in_(computing) plugin] and start taking advantage of new functionality without having to make changes to the objects that they are instantiating in their application. &lt;br /&gt;
&lt;br /&gt;
In frameworks written in other languages, such as Java, plugging in new functionality means that you need to change how your objects are instantiated. This will require code changes and/or potentially configuration changes which makes it hard to develop, hard to maintain, and a pain for plugin developers to support.  But because of the mixin feature, Rails plugin developers can customize the base objects and the users of the plugins do not have to change any of their code or configuration logic.&lt;br /&gt;
&lt;br /&gt;
===Differences===&lt;br /&gt;
&lt;br /&gt;
These is a general comparision: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Mixins in Ruby&lt;br /&gt;
! Interfaces in Java&lt;br /&gt;
|-&lt;br /&gt;
| 'include' or 'extend' keywords used to include the module in the class.&lt;br /&gt;
| 'implement' keyword is used to implement interface&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Need not implement the module methods in the class.&lt;br /&gt;
| Should implement the interface methods. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| The methods are not copied into the class in which they are included. They are referenced.&lt;br /&gt;
| The methods’ implementation is in the class itself.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| The included method definition is decided at ‘run time’. If the definition changes while running the function points to the changed code.&lt;br /&gt;
| The definition is known compile time and will not change until the code is compiled and run again. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Modules inclusion does not provide a superclass and subclass relationship.&lt;br /&gt;
| Interface extending provides a superclass and subclass relationship. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| When we use mixins we included only a module of the class. &lt;br /&gt;
| We include the entire interface class. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Mainly for code reuse and does not provide any hierarchy. &lt;br /&gt;
| It provides a hierarchy. &lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Functionality and Behavior Comparison===&lt;br /&gt;
The functionality and behavior of Mixins in Ruby is compared with Interfaces in Java&lt;br /&gt;
&lt;br /&gt;
====Comparable Functionality====&lt;br /&gt;
Sometime we are required to compare the class members of different objects of the same class and/or sort them. This is the comparable functionality that we are trying to achieve for a given class.  This functionality can be achieved by both the Ruby using mixins and Java using interfaces. There is not much difference using both.&lt;br /&gt;
&lt;br /&gt;
'''Ruby'''&lt;br /&gt;
&lt;br /&gt;
The [http://www.ruby-doc.org/core/classes/Comparable.html Comparable] mixin is used by classes whose objects may be ordered. The class must define the &amp;lt;=&amp;gt; operator, which compares the receiver against another object, returning -1, 0, or +1 depending on whether the receiver is less than, equal to, or greater than the other object. Comparable uses &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method between?.&lt;br /&gt;
&lt;br /&gt;
  class SizeMatters&lt;br /&gt;
     include Comparable&lt;br /&gt;
     attr :str&lt;br /&gt;
     def &amp;lt;=&amp;gt;(anOther)&lt;br /&gt;
       str.size &amp;lt;=&amp;gt; anOther.str.size&lt;br /&gt;
     end&lt;br /&gt;
     def initialize(str)&lt;br /&gt;
       @str = str&lt;br /&gt;
     end&lt;br /&gt;
     def inspect&lt;br /&gt;
       @str&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
''using class:'' &lt;br /&gt;
&lt;br /&gt;
   s1 = SizeMatters.new(&amp;quot;Z&amp;quot;)&lt;br /&gt;
   s2 = SizeMatters.new(&amp;quot;YY&amp;quot;)&lt;br /&gt;
   s3 = SizeMatters.new(&amp;quot;XXX&amp;quot;)&lt;br /&gt;
   s4 = SizeMatters.new(&amp;quot;WWWW&amp;quot;)&lt;br /&gt;
   s5 = SizeMatters.new(&amp;quot;VVVVV&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
''output:''&lt;br /&gt;
   s1 &amp;lt; s2                       #=&amp;gt; true&lt;br /&gt;
   s4.between?(s1, s3)           #=&amp;gt; false&lt;br /&gt;
   s4.between?(s3, s5)           #=&amp;gt; true&lt;br /&gt;
   [ s3, s2, s5, s4, s1 ].sort   #=&amp;gt; [Z, YY, XXX, WWWW, VVVVV]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Java'''&lt;br /&gt;
&lt;br /&gt;
In Java, comparable functionality is achieved by extending the “Comparable” interface. List of objects that implement this interface can be sorted automatically by sort method of the list interface. This interface has compareTo() method that is used by the sort() method of the list. Here compareTo() method can also be used to compare two objects with respect to the implementation in compareTo() implementation in the derived class. It returns ‘0’ , ‘1’ and ‘-1’ corresponding to whether one is equal to , greater than or lesser than other.&lt;br /&gt;
In this example code Employee class is implementing Comparable interface and have method compareTo().&lt;br /&gt;
  public class Employee implements Comparable {&lt;br /&gt;
    int EmpID;&lt;br /&gt;
    String Ename;&lt;br /&gt;
    double Sal;&lt;br /&gt;
    static int i;&lt;br /&gt;
    public Employee() {&lt;br /&gt;
        EmpID = i++;&lt;br /&gt;
        Ename = &amp;quot;dont know&amp;quot;;&lt;br /&gt;
        Sal = 0.0;&lt;br /&gt;
    }&lt;br /&gt;
    public Employee(String ename, double sal) {&lt;br /&gt;
        EmpID = i++;&lt;br /&gt;
        Ename = ename;&lt;br /&gt;
        Sal = sal;&lt;br /&gt;
    }&lt;br /&gt;
    public String toString() {&lt;br /&gt;
        return &amp;quot;EmpID &amp;quot; + EmpID + &amp;quot;\n&amp;quot; + &amp;quot;Ename &amp;quot; + Ename + &amp;quot;\n&amp;quot; + &amp;quot;Sal&amp;quot; + Sal;&lt;br /&gt;
    }&lt;br /&gt;
    public int compareTo(Object o1) {&lt;br /&gt;
        if (this.Sal == ((Employee) o1).Sal)&lt;br /&gt;
            return 0;&lt;br /&gt;
        else if ((this.Sal) &amp;gt; ((Employee) o1).Sal)&lt;br /&gt;
            return 1;&lt;br /&gt;
        else&lt;br /&gt;
            return -1;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
ComparableDemo.java is showing the use of this interface. This class first makes a list of objects of type Employee and call sort method of java.util.Collections, which internally uses compareTo() method of Employee class and sort the list accordingly.&lt;br /&gt;
 &lt;br /&gt;
  import java.util.*;&lt;br /&gt;
  public class ComparableDemo{&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
        List ts1 = new ArrayList();&lt;br /&gt;
        ts1.add(new Employee (&amp;quot;Tom&amp;quot;,40000.00));&lt;br /&gt;
        ts1.add(new Employee (&amp;quot;Harry&amp;quot;,20000.00));&lt;br /&gt;
        ts1.add(new Employee (&amp;quot;Maggie&amp;quot;,50000.00));&lt;br /&gt;
        ts1.add(new Employee (&amp;quot;Chris&amp;quot;,70000.00));&lt;br /&gt;
        Collections.sort(ts1);&lt;br /&gt;
        Iterator itr = ts1.iterator();&lt;br /&gt;
        while(itr.hasNext()){&lt;br /&gt;
            Object element = itr.next();&lt;br /&gt;
            System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
''Output:''&lt;br /&gt;
  &lt;br /&gt;
  EmpID 1&lt;br /&gt;
  Ename Harry&lt;br /&gt;
  Sal    20000.0&lt;br /&gt;
  EmpID 0&lt;br /&gt;
  Ename Tom&lt;br /&gt;
  Sal    40000.0&lt;br /&gt;
  EmpID 2&lt;br /&gt;
  Ename Maggie&lt;br /&gt;
  Sal    50000.0&lt;br /&gt;
  EmpID 3&lt;br /&gt;
  Ename Chris&lt;br /&gt;
  Sal    70000.0&lt;br /&gt;
&lt;br /&gt;
====Singleton Behaviour====&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Singleton_pattern Singleton] is a class guaranteed to have only a single instance, with a single global access point to that instance. It can be implemented in Ruby and Java. In Ruby we can include a module to acquire all the functionality of the singleton module. The code is already there and we are just reusing the code and adding that similar functionality into our class. Here our intention is to add the functionality of singleton to the class which is achieved using mixins. In java, implementation of interfaces means that the class which implements the interface becomes a type of that class, which is not the intention here. So, there is not interface which class can implement to get the functionality in itself. It has to implement the functionality itself without implementing any interface. &lt;br /&gt;
&lt;br /&gt;
'''Ruby'''&lt;br /&gt;
&lt;br /&gt;
When you include the singleton method inside your class it makes the new method private so that you cannot use it. Including ‘singleton’ adds a class method called instance that instantiates only one instance of the class&lt;br /&gt;
&lt;br /&gt;
  # require singleton lib&lt;br /&gt;
  require 'singleton'&lt;br /&gt;
  class AppConfig&lt;br /&gt;
    # mixin the singleton module&lt;br /&gt;
    include Singleton&lt;br /&gt;
    # do the actual app configuration&lt;br /&gt;
    def load_config(file)&lt;br /&gt;
      # do your work here&lt;br /&gt;
      puts &amp;quot;Application configuration file was loaded from file: #{file}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
''output:''&lt;br /&gt;
&lt;br /&gt;
  conf1 = AppConfig.instance&lt;br /&gt;
  conf1.load_config &amp;quot;/home/khelll/conf.yml&amp;quot;       #=&amp;gt;Application configuration file was loaded from file: /home/khelll/conf.yml&lt;br /&gt;
  conf2 = AppConfig.instance&lt;br /&gt;
  puts conf1 == conf2      #=&amp;gt;true&lt;br /&gt;
  # notice the following 2 lines won’t work&lt;br /&gt;
  AppConfig.new rescue(puts $!)   #=&amp;gt; new method is private&lt;br /&gt;
&lt;br /&gt;
====Enumerable Behaviour====&lt;br /&gt;
This [http://www.ruby-doc.org/core/classes/Enumerable.html enumerable] functionality adds support to the array members of the class. We can achieve this functionality using mixins in Ruby while it is not possible to do it in Java using interfaces.&lt;br /&gt;
&lt;br /&gt;
'''Implementation in Ruby:''' &lt;br /&gt;
&lt;br /&gt;
It adds the functionality of map, inject, select etc to the class which includes the ‘Enumerable’ module. The class which includes should represent a collection of items. Enumerable requires that your class contain an each method that serves up the items in the collection. All the other Enumerable methods rely on this. &lt;br /&gt;
Example:&lt;br /&gt;
This is a Team class that manages a group of members:&lt;br /&gt;
&lt;br /&gt;
  class Team&lt;br /&gt;
    include Enumerable&lt;br /&gt;
    attr_accessor :members&lt;br /&gt;
    def initialize&lt;br /&gt;
      @members = []&lt;br /&gt;
    end&lt;br /&gt;
    def each &amp;amp;block&lt;br /&gt;
      @members.each{|member| block.call(member)}&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Enumerable requires that your class contain an each method that serves up the items in the collection. All the other Enumerablemethods rely on this. Now we can use the map method, for instance:&lt;br /&gt;
&lt;br /&gt;
  team = Team.new&lt;br /&gt;
  team.members = ['joshua', 'gabriel', 'jacob']&lt;br /&gt;
  team.map{|member| member.capitalize}   # =&amp;gt; [&amp;quot;Joshua&amp;quot;, &amp;quot;Gabriel&amp;quot;, &amp;quot;Jacob&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
Now we can call any Enumerable methods on our team object itself, and it will assume we want to work with the members array within.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Data Mapper Behaviour====&lt;br /&gt;
&lt;br /&gt;
[http://datamapper.org/docs/ DataMapper] is an API which comes with the ability to use the same API to talk to a multitude of different datastores. There are adapters for the usual RDBMS suspects, NoSQL stores, various file formats and even some popular webservices. This is implemented in ruby using mixins. We need not use interfaces to implement.&lt;br /&gt;
&lt;br /&gt;
'''Implementation in Ruby:'''&lt;br /&gt;
  class Post&lt;br /&gt;
       include DataMapper::Resource&lt;br /&gt;
       property :id, Serial&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
The point to take away from this is not that Ruby Mixins are more powerful than Java Interfaces. The mixin feature of Ruby allows for the development of easy to use but powerful plugins that will be hard for any non-Ruby based framework to compete with. Java has many other ways of implementing the functionality mixins are capable of. It is just that it does not use Interfaces to achieve the functionality. Even though both of them are a way of achieving multiple inheritance in their respective languages, they achieve quiet different goals. Inheritance in Java is mainly to overcome the confusion which arises with multiple inheritance, while Mixins in Ruby is mainly to add an additional functionality to the existing class apart from the ‘primary functionality’ of the class. &lt;br /&gt;
&lt;br /&gt;
When a class implements Interface, it indicates that it is of the type interface class and forms a hierarchy with the interface class, while ruby mixins are mainly intended for code reuse and including an additional functionality to the class. It does not form any hierarchy as it is only using module of the class.&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* http://en.wikipedia.org/wiki/Interface_(Java)&lt;br /&gt;
* http://en.wikipedia.org/wiki/Mixin&lt;br /&gt;
* http://carboni.ca/blog/p/Modules-How-Do-They-Work&lt;br /&gt;
* http://rubylearning.com/satishtalim/modules_mixins.html&lt;br /&gt;
* http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/&lt;br /&gt;
* http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-&lt;br /&gt;
* http://datamapper.org/&lt;br /&gt;
* http://www.dsource.org/projects/tutorials/wiki/SingletonPattern &lt;br /&gt;
* http://corelib.rubyonrails.org/classes/Comparable.html&lt;br /&gt;
* http://kconrails.com/2010/11/30/ruby-enumerable-primer-part-1-the-basics/&lt;br /&gt;
* http://stackoverflow.com/questions/575920/is-a-ruby-module-equivalent-to-a-java-interface&lt;/div&gt;</summary>
		<author><name>Sthirum</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2c_bs&amp;diff=51681</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2c bs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2c_bs&amp;diff=51681"/>
		<updated>2011-10-02T01:21:45Z</updated>

		<summary type="html">&lt;p&gt;Sthirum: /* Module */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
An [http://en.wikipedia.org/wiki/Interface_(computing) interface] &amp;lt;ref&amp;gt;interface&amp;lt;/ref&amp;gt; in the [http://en.wikipedia.org/wiki/Java_(programming_language) Java] programming language is an abstract type that is used to specify an interface that classes must implement. Interface may only contain [http://en.wikipedia.org/wiki/Method_signature method signature] and constant declarations. An interface may never contain method definitions. Interfaces cannot be instantiated. A class that implements an interface must implement all of the methods described in the interface, or be an [http://en.wikipedia.org/wiki/Abstract_type abstract] class. Object references in Java may be specified to be of an interface type; in which case, they must either be null, or be bound to an object that implements the interface. &lt;br /&gt;
&lt;br /&gt;
In object-oriented programming languages, a [http://en.wikipedia.org/wiki/Mixin mixin] &amp;lt;ref&amp;gt; mixin &amp;lt;/ref&amp;gt; is a class that provides a certain functionality to be inherited or just reused by a subclass. Some of the functionality of mixins is provided by interfaces in popular languages like Java and [http://en.wikipedia.org/wiki/C_Sharp_(programming_language) C#]. However, an interface only specifies what the class must support and cannot provide an implementation. Another class, providing an implementation and dependent with the interface, is needed for [http://en.wikipedia.org/wiki/Code_refactoring refactoring] common behavior into a single place. Interfaces combined with aspect-oriented programming can produce full-fledged mixins in languages that support such features, such as C# or Java. &lt;br /&gt;
One benefit of using interfaces is that they simulate [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance]. All classes in Java other than [https://matrix.senecac.on.ca:8443/wiki/javaSE/index.php/Java.lang.Object java.lang.Object], must have exactly one base class; multiple inheritance of classes is not allowed.&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
===Mixins in Ruby=== &lt;br /&gt;
In Java you just have [http://en.wikipedia.org/wiki/Class_(computer_programming) classes] (both abstract and concrete) and interfaces. The [http://en.wikipedia.org/wiki/Ruby Ruby] language provides classes,[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html modules], and a mix of both. Inheriting from a mixin is not a form of specialization but is rather a means of collecting functionality. A class may inherit most or all of its functionality from one or more mixins through multiple inheritance. A mixin can also be viewed as an interface with implemented methods. When a class includes a mixin, the class implements the interface and includes, rather than inherits, all the mixins’ attributes (fields, properties) and methods. They become part of the class during compilation. A mixin can defer definition and binding of methods until runtime, though attributes and instantiation parameters are still defined at compile time. In the Ruby language a mixin is a class that is mixed with a module. &lt;br /&gt;
&lt;br /&gt;
====Module====&lt;br /&gt;
&lt;br /&gt;
A [http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html module]&amp;lt;ref&amp;gt;module&amp;lt;/ref&amp;gt; is a degenerate abstract class. A module can’t be instantiated and no class can directly extend it but it can fully implement methods. A class can leverage the implementation of a module by including the module’s methods. A module can define methods that can be shared in different and separate classes either at the class or instance level.We can view an example &amp;lt;ref&amp;gt; [http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/]&amp;lt;/ref&amp;gt; module that converts a numeric value to its English word.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # Convert a integer value to English.&lt;br /&gt;
  module Stringify&lt;br /&gt;
    # Requires an instance variable @value&lt;br /&gt;
    def stringify&lt;br /&gt;
      if @value == 1&lt;br /&gt;
        &amp;quot;One&amp;quot;&lt;br /&gt;
      elsif @value == 2&lt;br /&gt;
        &amp;quot;Two&amp;quot;&lt;br /&gt;
      elsif @value == 3&lt;br /&gt;
        &amp;quot;Three&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The Stringify module above makes use of a @value [http://en.wikipedia.org/wiki/Instance_variable instance variable]. The class that will be mixed with this module needs to define and set a @value instance variable since the Stringify module uses it but does not define it. In addition to instance variables a module could invoke methods not defined in the module but in the class that it will be mixed with.&lt;br /&gt;
&lt;br /&gt;
A [http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/ Self contained module] can also be written in Ruby where the methods defined in a module can be used as static methods independent of their implementation within a class.&lt;br /&gt;
&lt;br /&gt;
'''Self Contained Module Example: '''&lt;br /&gt;
&lt;br /&gt;
  # A Math module akin to Java Math class.&lt;br /&gt;
  module Math&lt;br /&gt;
    # Could be called as a class, static, method&lt;br /&gt;
    def add(val_one, val_two)&lt;br /&gt;
      BigInteger.new(val_one + val_two)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The methods in the Math module are intended to be invoked like class methods, also known as [http://en.wikipedia.org/wiki/Method_(computer_programming)#Static_methods static] methods. The add method in the Math module accepts two integer values and returns an [http://en.wikipedia.org/wiki/Instance_(computer_science) instance] of BigInteger.&lt;br /&gt;
&lt;br /&gt;
====Using Mixins====&lt;br /&gt;
&lt;br /&gt;
Implementation of the class and module are joined, interwined, combined, etc. A mixin is a different mechanism to the extend construct used to add concrete implementation to a class. With a mixin you can extend from a module instead of a class. &lt;br /&gt;
&lt;br /&gt;
The example of self-contained module shows how the method returns a BigInteger sum after accepting two integer parameters. The following example is the implementation of BigInteger class. The BigInteger class defines one constructor and directly inherits one method from the Number base class. To mix in the methods implemented in the Stringify and Math modules with the BigInteger class you will note the usage of the include and extend methods, respectively.&lt;br /&gt;
&lt;br /&gt;
  # Base Number class&lt;br /&gt;
  class Number&lt;br /&gt;
    def intValue&lt;br /&gt;
      @value&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  # BigInteger extends Number&lt;br /&gt;
  class BigInteger &amp;lt; Number&lt;br /&gt;
 &lt;br /&gt;
  # Add instance methods from Stringify&lt;br /&gt;
  include Stringify&lt;br /&gt;
 &lt;br /&gt;
  # Add class methods from Math&lt;br /&gt;
  extend Math&lt;br /&gt;
 &lt;br /&gt;
  # Add a constructor with one parameter&lt;br /&gt;
    def initialize(value)&lt;br /&gt;
      @value = value&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Using the class:&lt;br /&gt;
&lt;br /&gt;
  # Create a new object&lt;br /&gt;
  bigint1 = BigInteger.new(10)&lt;br /&gt;
  # Call a method inherited from the base class&lt;br /&gt;
  puts bigint1.intValue   # --&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
The extend method will mix a module’s methods at the class level. The method defined in the Math module can be used as a class/static method.&lt;br /&gt;
&lt;br /&gt;
  # Call class method extended from Math&lt;br /&gt;
  bigint2 = BigInteger.add(-2, 4)&lt;br /&gt;
  puts bigint2.intValue   # --&amp;gt; 2&lt;br /&gt;
&lt;br /&gt;
The include method will mix a module’s methods at the instance level, meaning that the methods will become instance methods. The method defined in the Stringify module can be used as an instance method.&lt;br /&gt;
&lt;br /&gt;
  # Call a method included from Stringify&lt;br /&gt;
  puts bigint2.stringify   # --&amp;gt; 'Two'&lt;br /&gt;
&lt;br /&gt;
There is another use of the extend method. You can enhance an object instance by mixing it with a module at run time. This ‘CurrencyFormatter’ is used to extend an object, changing it’s responsibilities at runtime.&lt;br /&gt;
&lt;br /&gt;
  # Format a numeric value as a currency&lt;br /&gt;
  module CurrencyFormatter&lt;br /&gt;
    def format&lt;br /&gt;
      &amp;quot;$#{@value}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
To mix an object instance with a module you can do the following:&lt;br /&gt;
&lt;br /&gt;
  # Add the module methods to&lt;br /&gt;
  # this object instance, only!&lt;br /&gt;
  bigint2.extend CurrencyFormatter&lt;br /&gt;
  puts bigint2.format   # --&amp;gt; '$2'&lt;br /&gt;
&lt;br /&gt;
Calling the extend method on an instance will only extend that one object; objects of the same class will not be extended with the new functionality.&lt;br /&gt;
&lt;br /&gt;
  puts bigint1.format   # will generate an error&lt;br /&gt;
&lt;br /&gt;
Modules that will be mixed with a class via the include or extend method could define something like a constructor or initializer method to the module. The module initializer method will be invoked at the time the module is mixed with a class. When a class extends a module the module’s self.extended method will be invoked:&lt;br /&gt;
&lt;br /&gt;
  module Math&lt;br /&gt;
    def self.extended(base)&lt;br /&gt;
      # Initialize module.&lt;br /&gt;
    end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
The self prefix indicates that the method is a static module level method. The base parameter in the static extended method will be either an instance object or class object of the class that extended the module depending whether you extend a object or class, respectively.&lt;br /&gt;
When a class includes a module the module’s self.included method will be invoked.&lt;br /&gt;
&lt;br /&gt;
  module Stringify&lt;br /&gt;
    def self.included(base)&lt;br /&gt;
      # Initialize module.&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The base parameter will be a class object for the class that includes the module.&lt;br /&gt;
It is important to note that inside the included and extended initializer methods you can include and extend other modules. &lt;br /&gt;
&lt;br /&gt;
  module Stringify&lt;br /&gt;
    def self.included(base)&lt;br /&gt;
      base.extend SomeOtherModule&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===Interfaces in Java===&lt;br /&gt;
Interfaces define a standardized set of commands that a class will obey. The commands are a set of methods that a class implements. The interface definition states the names of the methods and their return types and argument signatures. There is no executable body for any method. Example: &lt;br /&gt;
  interface Bicycle {&lt;br /&gt;
       void changeCadence(int newValue);   // wheel revolutions per minute&lt;br /&gt;
       void changeGear(int newValue);&lt;br /&gt;
       void speedUp(int increment);&lt;br /&gt;
       void applyBrakes(int decrement);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Implementation is independent to each class that implements the interface. Once a class implements an interface, the Java compiler knows that an instance of the class will contain the specified set of methods. Therefore, it will allow you to call those methods for an object referenced by a variable whose type is the interface. Implementing an interface enables a class to be &amp;quot;plugged in&amp;quot; in any situation that requires a specific behavior (manifested through the set of methods).&lt;br /&gt;
&lt;br /&gt;
  class ACMEBicycle implements Bicycle {&lt;br /&gt;
    //implementation specific to this class&lt;br /&gt;
       int cadence = 0;&lt;br /&gt;
       int speed = 0;&lt;br /&gt;
       int gear = 1;&lt;br /&gt;
       void changeCadence(int newValue) {&lt;br /&gt;
            cadence = newValue;&lt;br /&gt;
       }&lt;br /&gt;
       void changeGear(int newValue) {&lt;br /&gt;
            gear = newValue;&lt;br /&gt;
       }&lt;br /&gt;
       void speedUp(int increment) {&lt;br /&gt;
            speed = speed + increment;   &lt;br /&gt;
       }&lt;br /&gt;
       void applyBrakes(int decrement) {&lt;br /&gt;
            speed = speed - decrement;&lt;br /&gt;
       }&lt;br /&gt;
       void printStates() {&lt;br /&gt;
            System.out.println(&amp;quot;cadence:&amp;quot;+cadence+&amp;quot; speed:&amp;quot;+speed+&amp;quot; gear:&amp;quot;+gear);&lt;br /&gt;
       }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
  class MOUNTAINBicycle implements Bicycle {&lt;br /&gt;
  int cadence = 2;&lt;br /&gt;
       int speed = 3;&lt;br /&gt;
       int gear = 5;&lt;br /&gt;
       void changeCadence(int newValue) {&lt;br /&gt;
            cadence = newValue + 10;&lt;br /&gt;
       }&lt;br /&gt;
       void changeGear(int newValue) {&lt;br /&gt;
            gear = newValue + 4;&lt;br /&gt;
       }&lt;br /&gt;
       void speedUp(int increment) {&lt;br /&gt;
            speed = speed + increment + 10;   &lt;br /&gt;
       }&lt;br /&gt;
       void applyBrakes(int decrement) {&lt;br /&gt;
            speed = speed – decrement – 10;&lt;br /&gt;
       }&lt;br /&gt;
       void printStates() {&lt;br /&gt;
            System.out.println(&amp;quot;cadence:&amp;quot;+cadence+&amp;quot; speed:&amp;quot;+speed+&amp;quot; gear:&amp;quot;+gear);&lt;br /&gt;
       }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Using an interface rather than inheritance to specify a certain set of methods allows a class to inherit from some other class. In other words, if a class needs two different sets of methods, so it can behave like two different types of things, it could inherit one set from class A, and use an interface B to specify the other.&lt;br /&gt;
You could then reference one of these objects with either an A reference or a B reference. Implementing an interface allows a class to become more formal about the behavior it promises to provide. Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler. If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Mixins vs Interfaces==&lt;br /&gt;
Because of this mixin feature, a developer can add arbitrary methods and modify behavior of core classes at runtime. This is amazingly powerful if you are trying to write plugins and extensions to the [http://en.wikipedia.org/wiki/Software_framework framework].  Because you can add functionality to existing objects, users can install their [http://en.wikipedia.org/wiki/Plug-in_(computing) plugin] and start taking advantage of new functionality without having to make changes to the objects that they are instantiating in their application. &lt;br /&gt;
&lt;br /&gt;
In frameworks written in other languages, such as Java, plugging in new functionality means that you need to change how your objects are instantiated. This will require code changes and/or potentially configuration changes which makes it hard to develop, hard to maintain, and a pain for plugin developers to support.  But because of the mixin feature, Rails plugin developers can customize the base objects and the users of the plugins do not have to change any of their code or configuration logic.&lt;br /&gt;
&lt;br /&gt;
===Differences===&lt;br /&gt;
&lt;br /&gt;
These is a general comparision: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Mixins in Ruby&lt;br /&gt;
! Interfaces in Java&lt;br /&gt;
|-&lt;br /&gt;
| 'include' or 'extend' keywords used to include the module in the class.&lt;br /&gt;
| 'implement' keyword is used to implement interface&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Need not implement the module methods in the class.&lt;br /&gt;
| Should implement the interface methods. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| The methods are not copied into the class in which they are included. They are referenced.&lt;br /&gt;
| The methods’ implementation is in the class itself.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| The included method definition is decided at ‘run time’. If the definition changes while running the function points to the changed code.&lt;br /&gt;
| The definition is known compile time and will not change until the code is compiled and run again. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Modules inclusion does not provide a superclass and subclass relationship.&lt;br /&gt;
| Interface extending provides a superclass and subclass relationship. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| When we use mixins we included only a module of the class. &lt;br /&gt;
| We include the entire interface class. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Mainly for code reuse and does not provide any hierarchy. &lt;br /&gt;
| It provides a hierarchy. &lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Functionality and Behavior Comparison===&lt;br /&gt;
The functionality and behavior of Mixins in Ruby is compared with Interfaces in Java&lt;br /&gt;
&lt;br /&gt;
====Comparable Functionality====&lt;br /&gt;
Sometime we are required to compare the class members of different objects of the same class and/or sort them. This is the comparable functionality that we are trying to achieve for a given class.  This functionality can be achieved by both the Ruby using mixins and Java using interfaces. There is not much difference using both.&lt;br /&gt;
&lt;br /&gt;
'''Ruby'''&lt;br /&gt;
&lt;br /&gt;
The [http://www.ruby-doc.org/core/classes/Comparable.html Comparable] mixin is used by classes whose objects may be ordered. The class must define the &amp;lt;=&amp;gt; operator, which compares the receiver against another object, returning -1, 0, or +1 depending on whether the receiver is less than, equal to, or greater than the other object. Comparable uses &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method between?.&lt;br /&gt;
&lt;br /&gt;
  class SizeMatters&lt;br /&gt;
     include Comparable&lt;br /&gt;
     attr :str&lt;br /&gt;
     def &amp;lt;=&amp;gt;(anOther)&lt;br /&gt;
       str.size &amp;lt;=&amp;gt; anOther.str.size&lt;br /&gt;
     end&lt;br /&gt;
     def initialize(str)&lt;br /&gt;
       @str = str&lt;br /&gt;
     end&lt;br /&gt;
     def inspect&lt;br /&gt;
       @str&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
''using class:'' &lt;br /&gt;
&lt;br /&gt;
   s1 = SizeMatters.new(&amp;quot;Z&amp;quot;)&lt;br /&gt;
   s2 = SizeMatters.new(&amp;quot;YY&amp;quot;)&lt;br /&gt;
   s3 = SizeMatters.new(&amp;quot;XXX&amp;quot;)&lt;br /&gt;
   s4 = SizeMatters.new(&amp;quot;WWWW&amp;quot;)&lt;br /&gt;
   s5 = SizeMatters.new(&amp;quot;VVVVV&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
''output:''&lt;br /&gt;
   s1 &amp;lt; s2                       #=&amp;gt; true&lt;br /&gt;
   s4.between?(s1, s3)           #=&amp;gt; false&lt;br /&gt;
   s4.between?(s3, s5)           #=&amp;gt; true&lt;br /&gt;
   [ s3, s2, s5, s4, s1 ].sort   #=&amp;gt; [Z, YY, XXX, WWWW, VVVVV]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Java'''&lt;br /&gt;
&lt;br /&gt;
In Java, comparable functionality is achieved by extending the “Comparable” interface. List of objects that implement this interface can be sorted automatically by sort method of the list interface. This interface has compareTo() method that is used by the sort() method of the list. Here compareTo() method can also be used to compare two objects with respect to the implementation in compareTo() implementation in the derived class. It returns ‘0’ , ‘1’ and ‘-1’ corresponding to whether one is equal to , greater than or lesser than other.&lt;br /&gt;
In this example code Employee class is implementing Comparable interface and have method compareTo().&lt;br /&gt;
  public class Employee implements Comparable {&lt;br /&gt;
    int EmpID;&lt;br /&gt;
    String Ename;&lt;br /&gt;
    double Sal;&lt;br /&gt;
    static int i;&lt;br /&gt;
    public Employee() {&lt;br /&gt;
        EmpID = i++;&lt;br /&gt;
        Ename = &amp;quot;dont know&amp;quot;;&lt;br /&gt;
        Sal = 0.0;&lt;br /&gt;
    }&lt;br /&gt;
    public Employee(String ename, double sal) {&lt;br /&gt;
        EmpID = i++;&lt;br /&gt;
        Ename = ename;&lt;br /&gt;
        Sal = sal;&lt;br /&gt;
    }&lt;br /&gt;
    public String toString() {&lt;br /&gt;
        return &amp;quot;EmpID &amp;quot; + EmpID + &amp;quot;\n&amp;quot; + &amp;quot;Ename &amp;quot; + Ename + &amp;quot;\n&amp;quot; + &amp;quot;Sal&amp;quot; + Sal;&lt;br /&gt;
    }&lt;br /&gt;
    public int compareTo(Object o1) {&lt;br /&gt;
        if (this.Sal == ((Employee) o1).Sal)&lt;br /&gt;
            return 0;&lt;br /&gt;
        else if ((this.Sal) &amp;gt; ((Employee) o1).Sal)&lt;br /&gt;
            return 1;&lt;br /&gt;
        else&lt;br /&gt;
            return -1;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
ComparableDemo.java is showing the use of this interface. This class first makes a list of objects of type Employee and call sort method of java.util.Collections, which internally uses compareTo() method of Employee class and sort the list accordingly.&lt;br /&gt;
 &lt;br /&gt;
  import java.util.*;&lt;br /&gt;
  public class ComparableDemo{&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
        List ts1 = new ArrayList();&lt;br /&gt;
        ts1.add(new Employee (&amp;quot;Tom&amp;quot;,40000.00));&lt;br /&gt;
        ts1.add(new Employee (&amp;quot;Harry&amp;quot;,20000.00));&lt;br /&gt;
        ts1.add(new Employee (&amp;quot;Maggie&amp;quot;,50000.00));&lt;br /&gt;
        ts1.add(new Employee (&amp;quot;Chris&amp;quot;,70000.00));&lt;br /&gt;
        Collections.sort(ts1);&lt;br /&gt;
        Iterator itr = ts1.iterator();&lt;br /&gt;
        while(itr.hasNext()){&lt;br /&gt;
            Object element = itr.next();&lt;br /&gt;
            System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
''Output:''&lt;br /&gt;
  &lt;br /&gt;
  EmpID 1&lt;br /&gt;
  Ename Harry&lt;br /&gt;
  Sal    20000.0&lt;br /&gt;
  EmpID 0&lt;br /&gt;
  Ename Tom&lt;br /&gt;
  Sal    40000.0&lt;br /&gt;
  EmpID 2&lt;br /&gt;
  Ename Maggie&lt;br /&gt;
  Sal    50000.0&lt;br /&gt;
  EmpID 3&lt;br /&gt;
  Ename Chris&lt;br /&gt;
  Sal    70000.0&lt;br /&gt;
&lt;br /&gt;
====Singleton Behaviour====&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Singleton_pattern Singleton] is a class guaranteed to have only a single instance, with a single global access point to that instance. It can be implemented in Ruby and Java. In Ruby we can include a module to acquire all the functionality of the singleton module. The code is already there and we are just reusing the code and adding that similar functionality into our class. Here our intention is to add the functionality of singleton to the class which is achieved using mixins. In java, implementation of interfaces means that the class which implements the interface becomes a type of that class, which is not the intention here. So, there is not interface which class can implement to get the functionality in itself. It has to implement the functionality itself without implementing any interface. &lt;br /&gt;
&lt;br /&gt;
'''Ruby'''&lt;br /&gt;
&lt;br /&gt;
When you include the singleton method inside your class it makes the new method private so that you cannot use it. Including ‘singleton’ adds a class method called instance that instantiates only one instance of the class&lt;br /&gt;
&lt;br /&gt;
  # require singleton lib&lt;br /&gt;
  require 'singleton'&lt;br /&gt;
  class AppConfig&lt;br /&gt;
    # mixin the singleton module&lt;br /&gt;
    include Singleton&lt;br /&gt;
    # do the actual app configuration&lt;br /&gt;
    def load_config(file)&lt;br /&gt;
      # do your work here&lt;br /&gt;
      puts &amp;quot;Application configuration file was loaded from file: #{file}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
''output:''&lt;br /&gt;
&lt;br /&gt;
  conf1 = AppConfig.instance&lt;br /&gt;
  conf1.load_config &amp;quot;/home/khelll/conf.yml&amp;quot;       #=&amp;gt;Application configuration file was loaded from file: /home/khelll/conf.yml&lt;br /&gt;
  conf2 = AppConfig.instance&lt;br /&gt;
  puts conf1 == conf2      #=&amp;gt;true&lt;br /&gt;
  # notice the following 2 lines won’t work&lt;br /&gt;
  AppConfig.new rescue(puts $!)   #=&amp;gt; new method is private&lt;br /&gt;
&lt;br /&gt;
====Enumerable Behaviour====&lt;br /&gt;
This [http://www.ruby-doc.org/core/classes/Enumerable.html enumerable] functionality adds support to the array members of the class. We can achieve this functionality using mixins in Ruby while it is not possible to do it in Java using interfaces.&lt;br /&gt;
&lt;br /&gt;
'''Implementation in Ruby:''' &lt;br /&gt;
&lt;br /&gt;
It adds the functionality of map, inject, select etc to the class which includes the ‘Enumerable’ module. The class which includes should represent a collection of items. Enumerable requires that your class contain an each method that serves up the items in the collection. All the other Enumerable methods rely on this. &lt;br /&gt;
Example:&lt;br /&gt;
This is a Team class that manages a group of members:&lt;br /&gt;
&lt;br /&gt;
  class Team&lt;br /&gt;
    include Enumerable&lt;br /&gt;
    attr_accessor :members&lt;br /&gt;
    def initialize&lt;br /&gt;
      @members = []&lt;br /&gt;
    end&lt;br /&gt;
    def each &amp;amp;block&lt;br /&gt;
      @members.each{|member| block.call(member)}&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Enumerable requires that your class contain an each method that serves up the items in the collection. All the other Enumerablemethods rely on this. Now we can use the map method, for instance:&lt;br /&gt;
&lt;br /&gt;
  team = Team.new&lt;br /&gt;
  team.members = ['joshua', 'gabriel', 'jacob']&lt;br /&gt;
  team.map{|member| member.capitalize}   # =&amp;gt; [&amp;quot;Joshua&amp;quot;, &amp;quot;Gabriel&amp;quot;, &amp;quot;Jacob&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
Now we can call any Enumerable methods on our team object itself, and it will assume we want to work with the members array within.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Data Mapper Behaviour====&lt;br /&gt;
&lt;br /&gt;
[http://datamapper.org/docs/ DataMapper] is an API which comes with the ability to use the same API to talk to a multitude of different datastores. There are adapters for the usual RDBMS suspects, NoSQL stores, various file formats and even some popular webservices. This is implemented in ruby using mixins. We need not use interfaces to implement.&lt;br /&gt;
&lt;br /&gt;
'''Implementation in Ruby:'''&lt;br /&gt;
  class Post&lt;br /&gt;
       include DataMapper::Resource&lt;br /&gt;
       property :id, Serial&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
The point to take away from this is not that Ruby Mixins are more powerful than Java Interfaces. The mixin feature of Ruby allows for the development of easy to use but powerful plugins that will be hard for any non-Ruby based framework to compete with. Java has many other ways of implementing the functionality mixins are capable of. It is just that it does not use Interfaces to achieve the functionality. Even though both of them are a way of achieving multiple inheritance in their respective languages, they achieve quiet different goals. Inheritance in Java is mainly to overcome the confusion which arises with multiple inheritance, while Mixins in Ruby is mainly to add an additional functionality to the existing class apart from the ‘primary functionality’ of the class. &lt;br /&gt;
&lt;br /&gt;
When a class implements Interface, it indicates that it is of the type interface class and forms a hierarchy with the interface class, while ruby mixins are mainly intended for code reuse and including an additional functionality to the class. It does not form any hierarchy as it is only using module of the class.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* http://en.wikipedia.org/wiki/Interface_(Java)&lt;br /&gt;
* http://en.wikipedia.org/wiki/Mixin&lt;br /&gt;
* http://carboni.ca/blog/p/Modules-How-Do-They-Work&lt;br /&gt;
* http://rubylearning.com/satishtalim/modules_mixins.html&lt;br /&gt;
* http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/&lt;br /&gt;
* http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-&lt;br /&gt;
* http://datamapper.org/&lt;br /&gt;
* http://www.dsource.org/projects/tutorials/wiki/SingletonPattern &lt;br /&gt;
* http://corelib.rubyonrails.org/classes/Comparable.html&lt;br /&gt;
* http://kconrails.com/2010/11/30/ruby-enumerable-primer-part-1-the-basics/&lt;br /&gt;
* http://stackoverflow.com/questions/575920/is-a-ruby-module-equivalent-to-a-java-interface&lt;/div&gt;</summary>
		<author><name>Sthirum</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2c_bs&amp;diff=51680</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2c bs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2c_bs&amp;diff=51680"/>
		<updated>2011-10-02T01:20:11Z</updated>

		<summary type="html">&lt;p&gt;Sthirum: /* Conclusion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
An [http://en.wikipedia.org/wiki/Interface_(computing) interface] &amp;lt;ref&amp;gt;interface&amp;lt;/ref&amp;gt; in the [http://en.wikipedia.org/wiki/Java_(programming_language) Java] programming language is an abstract type that is used to specify an interface that classes must implement. Interface may only contain [http://en.wikipedia.org/wiki/Method_signature method signature] and constant declarations. An interface may never contain method definitions. Interfaces cannot be instantiated. A class that implements an interface must implement all of the methods described in the interface, or be an [http://en.wikipedia.org/wiki/Abstract_type abstract] class. Object references in Java may be specified to be of an interface type; in which case, they must either be null, or be bound to an object that implements the interface. &lt;br /&gt;
&lt;br /&gt;
In object-oriented programming languages, a [http://en.wikipedia.org/wiki/Mixin mixin] &amp;lt;ref&amp;gt; mixin &amp;lt;/ref&amp;gt; is a class that provides a certain functionality to be inherited or just reused by a subclass. Some of the functionality of mixins is provided by interfaces in popular languages like Java and [http://en.wikipedia.org/wiki/C_Sharp_(programming_language) C#]. However, an interface only specifies what the class must support and cannot provide an implementation. Another class, providing an implementation and dependent with the interface, is needed for [http://en.wikipedia.org/wiki/Code_refactoring refactoring] common behavior into a single place. Interfaces combined with aspect-oriented programming can produce full-fledged mixins in languages that support such features, such as C# or Java. &lt;br /&gt;
One benefit of using interfaces is that they simulate [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance]. All classes in Java other than [https://matrix.senecac.on.ca:8443/wiki/javaSE/index.php/Java.lang.Object java.lang.Object], must have exactly one base class; multiple inheritance of classes is not allowed.&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
===Mixins in Ruby=== &lt;br /&gt;
In Java you just have [http://en.wikipedia.org/wiki/Class_(computer_programming) classes] (both abstract and concrete) and interfaces. The [http://en.wikipedia.org/wiki/Ruby Ruby] language provides classes,[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html modules], and a mix of both. Inheriting from a mixin is not a form of specialization but is rather a means of collecting functionality. A class may inherit most or all of its functionality from one or more mixins through multiple inheritance. A mixin can also be viewed as an interface with implemented methods. When a class includes a mixin, the class implements the interface and includes, rather than inherits, all the mixins’ attributes (fields, properties) and methods. They become part of the class during compilation. A mixin can defer definition and binding of methods until runtime, though attributes and instantiation parameters are still defined at compile time. In the Ruby language a mixin is a class that is mixed with a module. &lt;br /&gt;
&lt;br /&gt;
====Module====&lt;br /&gt;
&lt;br /&gt;
A [http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html module]&amp;lt;ref&amp;gt;module&amp;lt;/ref&amp;gt; is a degenerate abstract class. A module can’t be instantiated and no class can directly extend it but it can fully implement methods. A class can leverage the implementation of a module by including the module’s methods. A module can define methods that can be shared in different and separate classes either at the class or instance level.We can view an example &amp;lt;ref&amp;gt; http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/&amp;lt;/ref&amp;gt; module that converts a numeric value to its English word.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # Convert a integer value to English.&lt;br /&gt;
  module Stringify&lt;br /&gt;
    # Requires an instance variable @value&lt;br /&gt;
    def stringify&lt;br /&gt;
      if @value == 1&lt;br /&gt;
        &amp;quot;One&amp;quot;&lt;br /&gt;
      elsif @value == 2&lt;br /&gt;
        &amp;quot;Two&amp;quot;&lt;br /&gt;
      elsif @value == 3&lt;br /&gt;
        &amp;quot;Three&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The Stringify module above makes use of a @value [http://en.wikipedia.org/wiki/Instance_variable instance variable]. The class that will be mixed with this module needs to define and set a @value instance variable since the Stringify module uses it but does not define it. In addition to instance variables a module could invoke methods not defined in the module but in the class that it will be mixed with.&lt;br /&gt;
&lt;br /&gt;
A [http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/ Self contained module] can also be written in Ruby where the methods defined in a module can be used as static methods independent of their implementation within a class.&lt;br /&gt;
&lt;br /&gt;
'''Self Contained Module Example: '''&lt;br /&gt;
&lt;br /&gt;
  # A Math module akin to Java Math class.&lt;br /&gt;
  module Math&lt;br /&gt;
    # Could be called as a class, static, method&lt;br /&gt;
    def add(val_one, val_two)&lt;br /&gt;
      BigInteger.new(val_one + val_two)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The methods in the Math module are intended to be invoked like class methods, also known as [http://en.wikipedia.org/wiki/Method_(computer_programming)#Static_methods static] methods. The add method in the Math module accepts two integer values and returns an [http://en.wikipedia.org/wiki/Instance_(computer_science) instance] of BigInteger.&lt;br /&gt;
&lt;br /&gt;
====Using Mixins====&lt;br /&gt;
&lt;br /&gt;
Implementation of the class and module are joined, interwined, combined, etc. A mixin is a different mechanism to the extend construct used to add concrete implementation to a class. With a mixin you can extend from a module instead of a class. &lt;br /&gt;
&lt;br /&gt;
The example of self-contained module shows how the method returns a BigInteger sum after accepting two integer parameters. The following example is the implementation of BigInteger class. The BigInteger class defines one constructor and directly inherits one method from the Number base class. To mix in the methods implemented in the Stringify and Math modules with the BigInteger class you will note the usage of the include and extend methods, respectively.&lt;br /&gt;
&lt;br /&gt;
  # Base Number class&lt;br /&gt;
  class Number&lt;br /&gt;
    def intValue&lt;br /&gt;
      @value&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  # BigInteger extends Number&lt;br /&gt;
  class BigInteger &amp;lt; Number&lt;br /&gt;
 &lt;br /&gt;
  # Add instance methods from Stringify&lt;br /&gt;
  include Stringify&lt;br /&gt;
 &lt;br /&gt;
  # Add class methods from Math&lt;br /&gt;
  extend Math&lt;br /&gt;
 &lt;br /&gt;
  # Add a constructor with one parameter&lt;br /&gt;
    def initialize(value)&lt;br /&gt;
      @value = value&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Using the class:&lt;br /&gt;
&lt;br /&gt;
  # Create a new object&lt;br /&gt;
  bigint1 = BigInteger.new(10)&lt;br /&gt;
  # Call a method inherited from the base class&lt;br /&gt;
  puts bigint1.intValue   # --&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
The extend method will mix a module’s methods at the class level. The method defined in the Math module can be used as a class/static method.&lt;br /&gt;
&lt;br /&gt;
  # Call class method extended from Math&lt;br /&gt;
  bigint2 = BigInteger.add(-2, 4)&lt;br /&gt;
  puts bigint2.intValue   # --&amp;gt; 2&lt;br /&gt;
&lt;br /&gt;
The include method will mix a module’s methods at the instance level, meaning that the methods will become instance methods. The method defined in the Stringify module can be used as an instance method.&lt;br /&gt;
&lt;br /&gt;
  # Call a method included from Stringify&lt;br /&gt;
  puts bigint2.stringify   # --&amp;gt; 'Two'&lt;br /&gt;
&lt;br /&gt;
There is another use of the extend method. You can enhance an object instance by mixing it with a module at run time. This ‘CurrencyFormatter’ is used to extend an object, changing it’s responsibilities at runtime.&lt;br /&gt;
&lt;br /&gt;
  # Format a numeric value as a currency&lt;br /&gt;
  module CurrencyFormatter&lt;br /&gt;
    def format&lt;br /&gt;
      &amp;quot;$#{@value}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
To mix an object instance with a module you can do the following:&lt;br /&gt;
&lt;br /&gt;
  # Add the module methods to&lt;br /&gt;
  # this object instance, only!&lt;br /&gt;
  bigint2.extend CurrencyFormatter&lt;br /&gt;
  puts bigint2.format   # --&amp;gt; '$2'&lt;br /&gt;
&lt;br /&gt;
Calling the extend method on an instance will only extend that one object; objects of the same class will not be extended with the new functionality.&lt;br /&gt;
&lt;br /&gt;
  puts bigint1.format   # will generate an error&lt;br /&gt;
&lt;br /&gt;
Modules that will be mixed with a class via the include or extend method could define something like a constructor or initializer method to the module. The module initializer method will be invoked at the time the module is mixed with a class. When a class extends a module the module’s self.extended method will be invoked:&lt;br /&gt;
&lt;br /&gt;
  module Math&lt;br /&gt;
    def self.extended(base)&lt;br /&gt;
      # Initialize module.&lt;br /&gt;
    end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
The self prefix indicates that the method is a static module level method. The base parameter in the static extended method will be either an instance object or class object of the class that extended the module depending whether you extend a object or class, respectively.&lt;br /&gt;
When a class includes a module the module’s self.included method will be invoked.&lt;br /&gt;
&lt;br /&gt;
  module Stringify&lt;br /&gt;
    def self.included(base)&lt;br /&gt;
      # Initialize module.&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The base parameter will be a class object for the class that includes the module.&lt;br /&gt;
It is important to note that inside the included and extended initializer methods you can include and extend other modules. &lt;br /&gt;
&lt;br /&gt;
  module Stringify&lt;br /&gt;
    def self.included(base)&lt;br /&gt;
      base.extend SomeOtherModule&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===Interfaces in Java===&lt;br /&gt;
Interfaces define a standardized set of commands that a class will obey. The commands are a set of methods that a class implements. The interface definition states the names of the methods and their return types and argument signatures. There is no executable body for any method. Example: &lt;br /&gt;
  interface Bicycle {&lt;br /&gt;
       void changeCadence(int newValue);   // wheel revolutions per minute&lt;br /&gt;
       void changeGear(int newValue);&lt;br /&gt;
       void speedUp(int increment);&lt;br /&gt;
       void applyBrakes(int decrement);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Implementation is independent to each class that implements the interface. Once a class implements an interface, the Java compiler knows that an instance of the class will contain the specified set of methods. Therefore, it will allow you to call those methods for an object referenced by a variable whose type is the interface. Implementing an interface enables a class to be &amp;quot;plugged in&amp;quot; in any situation that requires a specific behavior (manifested through the set of methods).&lt;br /&gt;
&lt;br /&gt;
  class ACMEBicycle implements Bicycle {&lt;br /&gt;
    //implementation specific to this class&lt;br /&gt;
       int cadence = 0;&lt;br /&gt;
       int speed = 0;&lt;br /&gt;
       int gear = 1;&lt;br /&gt;
       void changeCadence(int newValue) {&lt;br /&gt;
            cadence = newValue;&lt;br /&gt;
       }&lt;br /&gt;
       void changeGear(int newValue) {&lt;br /&gt;
            gear = newValue;&lt;br /&gt;
       }&lt;br /&gt;
       void speedUp(int increment) {&lt;br /&gt;
            speed = speed + increment;   &lt;br /&gt;
       }&lt;br /&gt;
       void applyBrakes(int decrement) {&lt;br /&gt;
            speed = speed - decrement;&lt;br /&gt;
       }&lt;br /&gt;
       void printStates() {&lt;br /&gt;
            System.out.println(&amp;quot;cadence:&amp;quot;+cadence+&amp;quot; speed:&amp;quot;+speed+&amp;quot; gear:&amp;quot;+gear);&lt;br /&gt;
       }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
  class MOUNTAINBicycle implements Bicycle {&lt;br /&gt;
  int cadence = 2;&lt;br /&gt;
       int speed = 3;&lt;br /&gt;
       int gear = 5;&lt;br /&gt;
       void changeCadence(int newValue) {&lt;br /&gt;
            cadence = newValue + 10;&lt;br /&gt;
       }&lt;br /&gt;
       void changeGear(int newValue) {&lt;br /&gt;
            gear = newValue + 4;&lt;br /&gt;
       }&lt;br /&gt;
       void speedUp(int increment) {&lt;br /&gt;
            speed = speed + increment + 10;   &lt;br /&gt;
       }&lt;br /&gt;
       void applyBrakes(int decrement) {&lt;br /&gt;
            speed = speed – decrement – 10;&lt;br /&gt;
       }&lt;br /&gt;
       void printStates() {&lt;br /&gt;
            System.out.println(&amp;quot;cadence:&amp;quot;+cadence+&amp;quot; speed:&amp;quot;+speed+&amp;quot; gear:&amp;quot;+gear);&lt;br /&gt;
       }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Using an interface rather than inheritance to specify a certain set of methods allows a class to inherit from some other class. In other words, if a class needs two different sets of methods, so it can behave like two different types of things, it could inherit one set from class A, and use an interface B to specify the other.&lt;br /&gt;
You could then reference one of these objects with either an A reference or a B reference. Implementing an interface allows a class to become more formal about the behavior it promises to provide. Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler. If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Mixins vs Interfaces==&lt;br /&gt;
Because of this mixin feature, a developer can add arbitrary methods and modify behavior of core classes at runtime. This is amazingly powerful if you are trying to write plugins and extensions to the [http://en.wikipedia.org/wiki/Software_framework framework].  Because you can add functionality to existing objects, users can install their [http://en.wikipedia.org/wiki/Plug-in_(computing) plugin] and start taking advantage of new functionality without having to make changes to the objects that they are instantiating in their application. &lt;br /&gt;
&lt;br /&gt;
In frameworks written in other languages, such as Java, plugging in new functionality means that you need to change how your objects are instantiated. This will require code changes and/or potentially configuration changes which makes it hard to develop, hard to maintain, and a pain for plugin developers to support.  But because of the mixin feature, Rails plugin developers can customize the base objects and the users of the plugins do not have to change any of their code or configuration logic.&lt;br /&gt;
&lt;br /&gt;
===Differences===&lt;br /&gt;
&lt;br /&gt;
These is a general comparision: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Mixins in Ruby&lt;br /&gt;
! Interfaces in Java&lt;br /&gt;
|-&lt;br /&gt;
| 'include' or 'extend' keywords used to include the module in the class.&lt;br /&gt;
| 'implement' keyword is used to implement interface&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Need not implement the module methods in the class.&lt;br /&gt;
| Should implement the interface methods. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| The methods are not copied into the class in which they are included. They are referenced.&lt;br /&gt;
| The methods’ implementation is in the class itself.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| The included method definition is decided at ‘run time’. If the definition changes while running the function points to the changed code.&lt;br /&gt;
| The definition is known compile time and will not change until the code is compiled and run again. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Modules inclusion does not provide a superclass and subclass relationship.&lt;br /&gt;
| Interface extending provides a superclass and subclass relationship. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| When we use mixins we included only a module of the class. &lt;br /&gt;
| We include the entire interface class. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Mainly for code reuse and does not provide any hierarchy. &lt;br /&gt;
| It provides a hierarchy. &lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Functionality and Behavior Comparison===&lt;br /&gt;
The functionality and behavior of Mixins in Ruby is compared with Interfaces in Java&lt;br /&gt;
&lt;br /&gt;
====Comparable Functionality====&lt;br /&gt;
Sometime we are required to compare the class members of different objects of the same class and/or sort them. This is the comparable functionality that we are trying to achieve for a given class.  This functionality can be achieved by both the Ruby using mixins and Java using interfaces. There is not much difference using both.&lt;br /&gt;
&lt;br /&gt;
'''Ruby'''&lt;br /&gt;
&lt;br /&gt;
The [http://www.ruby-doc.org/core/classes/Comparable.html Comparable] mixin is used by classes whose objects may be ordered. The class must define the &amp;lt;=&amp;gt; operator, which compares the receiver against another object, returning -1, 0, or +1 depending on whether the receiver is less than, equal to, or greater than the other object. Comparable uses &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method between?.&lt;br /&gt;
&lt;br /&gt;
  class SizeMatters&lt;br /&gt;
     include Comparable&lt;br /&gt;
     attr :str&lt;br /&gt;
     def &amp;lt;=&amp;gt;(anOther)&lt;br /&gt;
       str.size &amp;lt;=&amp;gt; anOther.str.size&lt;br /&gt;
     end&lt;br /&gt;
     def initialize(str)&lt;br /&gt;
       @str = str&lt;br /&gt;
     end&lt;br /&gt;
     def inspect&lt;br /&gt;
       @str&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
''using class:'' &lt;br /&gt;
&lt;br /&gt;
   s1 = SizeMatters.new(&amp;quot;Z&amp;quot;)&lt;br /&gt;
   s2 = SizeMatters.new(&amp;quot;YY&amp;quot;)&lt;br /&gt;
   s3 = SizeMatters.new(&amp;quot;XXX&amp;quot;)&lt;br /&gt;
   s4 = SizeMatters.new(&amp;quot;WWWW&amp;quot;)&lt;br /&gt;
   s5 = SizeMatters.new(&amp;quot;VVVVV&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
''output:''&lt;br /&gt;
   s1 &amp;lt; s2                       #=&amp;gt; true&lt;br /&gt;
   s4.between?(s1, s3)           #=&amp;gt; false&lt;br /&gt;
   s4.between?(s3, s5)           #=&amp;gt; true&lt;br /&gt;
   [ s3, s2, s5, s4, s1 ].sort   #=&amp;gt; [Z, YY, XXX, WWWW, VVVVV]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Java'''&lt;br /&gt;
&lt;br /&gt;
In Java, comparable functionality is achieved by extending the “Comparable” interface. List of objects that implement this interface can be sorted automatically by sort method of the list interface. This interface has compareTo() method that is used by the sort() method of the list. Here compareTo() method can also be used to compare two objects with respect to the implementation in compareTo() implementation in the derived class. It returns ‘0’ , ‘1’ and ‘-1’ corresponding to whether one is equal to , greater than or lesser than other.&lt;br /&gt;
In this example code Employee class is implementing Comparable interface and have method compareTo().&lt;br /&gt;
  public class Employee implements Comparable {&lt;br /&gt;
    int EmpID;&lt;br /&gt;
    String Ename;&lt;br /&gt;
    double Sal;&lt;br /&gt;
    static int i;&lt;br /&gt;
    public Employee() {&lt;br /&gt;
        EmpID = i++;&lt;br /&gt;
        Ename = &amp;quot;dont know&amp;quot;;&lt;br /&gt;
        Sal = 0.0;&lt;br /&gt;
    }&lt;br /&gt;
    public Employee(String ename, double sal) {&lt;br /&gt;
        EmpID = i++;&lt;br /&gt;
        Ename = ename;&lt;br /&gt;
        Sal = sal;&lt;br /&gt;
    }&lt;br /&gt;
    public String toString() {&lt;br /&gt;
        return &amp;quot;EmpID &amp;quot; + EmpID + &amp;quot;\n&amp;quot; + &amp;quot;Ename &amp;quot; + Ename + &amp;quot;\n&amp;quot; + &amp;quot;Sal&amp;quot; + Sal;&lt;br /&gt;
    }&lt;br /&gt;
    public int compareTo(Object o1) {&lt;br /&gt;
        if (this.Sal == ((Employee) o1).Sal)&lt;br /&gt;
            return 0;&lt;br /&gt;
        else if ((this.Sal) &amp;gt; ((Employee) o1).Sal)&lt;br /&gt;
            return 1;&lt;br /&gt;
        else&lt;br /&gt;
            return -1;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
ComparableDemo.java is showing the use of this interface. This class first makes a list of objects of type Employee and call sort method of java.util.Collections, which internally uses compareTo() method of Employee class and sort the list accordingly.&lt;br /&gt;
 &lt;br /&gt;
  import java.util.*;&lt;br /&gt;
  public class ComparableDemo{&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
        List ts1 = new ArrayList();&lt;br /&gt;
        ts1.add(new Employee (&amp;quot;Tom&amp;quot;,40000.00));&lt;br /&gt;
        ts1.add(new Employee (&amp;quot;Harry&amp;quot;,20000.00));&lt;br /&gt;
        ts1.add(new Employee (&amp;quot;Maggie&amp;quot;,50000.00));&lt;br /&gt;
        ts1.add(new Employee (&amp;quot;Chris&amp;quot;,70000.00));&lt;br /&gt;
        Collections.sort(ts1);&lt;br /&gt;
        Iterator itr = ts1.iterator();&lt;br /&gt;
        while(itr.hasNext()){&lt;br /&gt;
            Object element = itr.next();&lt;br /&gt;
            System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
''Output:''&lt;br /&gt;
  &lt;br /&gt;
  EmpID 1&lt;br /&gt;
  Ename Harry&lt;br /&gt;
  Sal    20000.0&lt;br /&gt;
  EmpID 0&lt;br /&gt;
  Ename Tom&lt;br /&gt;
  Sal    40000.0&lt;br /&gt;
  EmpID 2&lt;br /&gt;
  Ename Maggie&lt;br /&gt;
  Sal    50000.0&lt;br /&gt;
  EmpID 3&lt;br /&gt;
  Ename Chris&lt;br /&gt;
  Sal    70000.0&lt;br /&gt;
&lt;br /&gt;
====Singleton Behaviour====&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Singleton_pattern Singleton] is a class guaranteed to have only a single instance, with a single global access point to that instance. It can be implemented in Ruby and Java. In Ruby we can include a module to acquire all the functionality of the singleton module. The code is already there and we are just reusing the code and adding that similar functionality into our class. Here our intention is to add the functionality of singleton to the class which is achieved using mixins. In java, implementation of interfaces means that the class which implements the interface becomes a type of that class, which is not the intention here. So, there is not interface which class can implement to get the functionality in itself. It has to implement the functionality itself without implementing any interface. &lt;br /&gt;
&lt;br /&gt;
'''Ruby'''&lt;br /&gt;
&lt;br /&gt;
When you include the singleton method inside your class it makes the new method private so that you cannot use it. Including ‘singleton’ adds a class method called instance that instantiates only one instance of the class&lt;br /&gt;
&lt;br /&gt;
  # require singleton lib&lt;br /&gt;
  require 'singleton'&lt;br /&gt;
  class AppConfig&lt;br /&gt;
    # mixin the singleton module&lt;br /&gt;
    include Singleton&lt;br /&gt;
    # do the actual app configuration&lt;br /&gt;
    def load_config(file)&lt;br /&gt;
      # do your work here&lt;br /&gt;
      puts &amp;quot;Application configuration file was loaded from file: #{file}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
''output:''&lt;br /&gt;
&lt;br /&gt;
  conf1 = AppConfig.instance&lt;br /&gt;
  conf1.load_config &amp;quot;/home/khelll/conf.yml&amp;quot;       #=&amp;gt;Application configuration file was loaded from file: /home/khelll/conf.yml&lt;br /&gt;
  conf2 = AppConfig.instance&lt;br /&gt;
  puts conf1 == conf2      #=&amp;gt;true&lt;br /&gt;
  # notice the following 2 lines won’t work&lt;br /&gt;
  AppConfig.new rescue(puts $!)   #=&amp;gt; new method is private&lt;br /&gt;
&lt;br /&gt;
====Enumerable Behaviour====&lt;br /&gt;
This [http://www.ruby-doc.org/core/classes/Enumerable.html enumerable] functionality adds support to the array members of the class. We can achieve this functionality using mixins in Ruby while it is not possible to do it in Java using interfaces.&lt;br /&gt;
&lt;br /&gt;
'''Implementation in Ruby:''' &lt;br /&gt;
&lt;br /&gt;
It adds the functionality of map, inject, select etc to the class which includes the ‘Enumerable’ module. The class which includes should represent a collection of items. Enumerable requires that your class contain an each method that serves up the items in the collection. All the other Enumerable methods rely on this. &lt;br /&gt;
Example:&lt;br /&gt;
This is a Team class that manages a group of members:&lt;br /&gt;
&lt;br /&gt;
  class Team&lt;br /&gt;
    include Enumerable&lt;br /&gt;
    attr_accessor :members&lt;br /&gt;
    def initialize&lt;br /&gt;
      @members = []&lt;br /&gt;
    end&lt;br /&gt;
    def each &amp;amp;block&lt;br /&gt;
      @members.each{|member| block.call(member)}&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Enumerable requires that your class contain an each method that serves up the items in the collection. All the other Enumerablemethods rely on this. Now we can use the map method, for instance:&lt;br /&gt;
&lt;br /&gt;
  team = Team.new&lt;br /&gt;
  team.members = ['joshua', 'gabriel', 'jacob']&lt;br /&gt;
  team.map{|member| member.capitalize}   # =&amp;gt; [&amp;quot;Joshua&amp;quot;, &amp;quot;Gabriel&amp;quot;, &amp;quot;Jacob&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
Now we can call any Enumerable methods on our team object itself, and it will assume we want to work with the members array within.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Data Mapper Behaviour====&lt;br /&gt;
&lt;br /&gt;
[http://datamapper.org/docs/ DataMapper] is an API which comes with the ability to use the same API to talk to a multitude of different datastores. There are adapters for the usual RDBMS suspects, NoSQL stores, various file formats and even some popular webservices. This is implemented in ruby using mixins. We need not use interfaces to implement.&lt;br /&gt;
&lt;br /&gt;
'''Implementation in Ruby:'''&lt;br /&gt;
  class Post&lt;br /&gt;
       include DataMapper::Resource&lt;br /&gt;
       property :id, Serial&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
The point to take away from this is not that Ruby Mixins are more powerful than Java Interfaces. The mixin feature of Ruby allows for the development of easy to use but powerful plugins that will be hard for any non-Ruby based framework to compete with. Java has many other ways of implementing the functionality mixins are capable of. It is just that it does not use Interfaces to achieve the functionality. Even though both of them are a way of achieving multiple inheritance in their respective languages, they achieve quiet different goals. Inheritance in Java is mainly to overcome the confusion which arises with multiple inheritance, while Mixins in Ruby is mainly to add an additional functionality to the existing class apart from the ‘primary functionality’ of the class. &lt;br /&gt;
&lt;br /&gt;
When a class implements Interface, it indicates that it is of the type interface class and forms a hierarchy with the interface class, while ruby mixins are mainly intended for code reuse and including an additional functionality to the class. It does not form any hierarchy as it is only using module of the class.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* http://en.wikipedia.org/wiki/Interface_(Java)&lt;br /&gt;
* http://en.wikipedia.org/wiki/Mixin&lt;br /&gt;
* http://carboni.ca/blog/p/Modules-How-Do-They-Work&lt;br /&gt;
* http://rubylearning.com/satishtalim/modules_mixins.html&lt;br /&gt;
* http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/&lt;br /&gt;
* http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-&lt;br /&gt;
* http://datamapper.org/&lt;br /&gt;
* http://www.dsource.org/projects/tutorials/wiki/SingletonPattern &lt;br /&gt;
* http://corelib.rubyonrails.org/classes/Comparable.html&lt;br /&gt;
* http://kconrails.com/2010/11/30/ruby-enumerable-primer-part-1-the-basics/&lt;br /&gt;
* http://stackoverflow.com/questions/575920/is-a-ruby-module-equivalent-to-a-java-interface&lt;/div&gt;</summary>
		<author><name>Sthirum</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2c_bs&amp;diff=51679</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2c bs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2c_bs&amp;diff=51679"/>
		<updated>2011-10-02T01:19:18Z</updated>

		<summary type="html">&lt;p&gt;Sthirum: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
An [http://en.wikipedia.org/wiki/Interface_(computing) interface] &amp;lt;ref&amp;gt;interface&amp;lt;/ref&amp;gt; in the [http://en.wikipedia.org/wiki/Java_(programming_language) Java] programming language is an abstract type that is used to specify an interface that classes must implement. Interface may only contain [http://en.wikipedia.org/wiki/Method_signature method signature] and constant declarations. An interface may never contain method definitions. Interfaces cannot be instantiated. A class that implements an interface must implement all of the methods described in the interface, or be an [http://en.wikipedia.org/wiki/Abstract_type abstract] class. Object references in Java may be specified to be of an interface type; in which case, they must either be null, or be bound to an object that implements the interface. &lt;br /&gt;
&lt;br /&gt;
In object-oriented programming languages, a [http://en.wikipedia.org/wiki/Mixin mixin] &amp;lt;ref&amp;gt; mixin &amp;lt;/ref&amp;gt; is a class that provides a certain functionality to be inherited or just reused by a subclass. Some of the functionality of mixins is provided by interfaces in popular languages like Java and [http://en.wikipedia.org/wiki/C_Sharp_(programming_language) C#]. However, an interface only specifies what the class must support and cannot provide an implementation. Another class, providing an implementation and dependent with the interface, is needed for [http://en.wikipedia.org/wiki/Code_refactoring refactoring] common behavior into a single place. Interfaces combined with aspect-oriented programming can produce full-fledged mixins in languages that support such features, such as C# or Java. &lt;br /&gt;
One benefit of using interfaces is that they simulate [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance]. All classes in Java other than [https://matrix.senecac.on.ca:8443/wiki/javaSE/index.php/Java.lang.Object java.lang.Object], must have exactly one base class; multiple inheritance of classes is not allowed.&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
===Mixins in Ruby=== &lt;br /&gt;
In Java you just have [http://en.wikipedia.org/wiki/Class_(computer_programming) classes] (both abstract and concrete) and interfaces. The [http://en.wikipedia.org/wiki/Ruby Ruby] language provides classes,[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html modules], and a mix of both. Inheriting from a mixin is not a form of specialization but is rather a means of collecting functionality. A class may inherit most or all of its functionality from one or more mixins through multiple inheritance. A mixin can also be viewed as an interface with implemented methods. When a class includes a mixin, the class implements the interface and includes, rather than inherits, all the mixins’ attributes (fields, properties) and methods. They become part of the class during compilation. A mixin can defer definition and binding of methods until runtime, though attributes and instantiation parameters are still defined at compile time. In the Ruby language a mixin is a class that is mixed with a module. &lt;br /&gt;
&lt;br /&gt;
====Module====&lt;br /&gt;
&lt;br /&gt;
A [http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html module]&amp;lt;ref&amp;gt;module&amp;lt;/ref&amp;gt; is a degenerate abstract class. A module can’t be instantiated and no class can directly extend it but it can fully implement methods. A class can leverage the implementation of a module by including the module’s methods. A module can define methods that can be shared in different and separate classes either at the class or instance level.We can view an example &amp;lt;ref&amp;gt; http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/&amp;lt;/ref&amp;gt; module that converts a numeric value to its English word.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # Convert a integer value to English.&lt;br /&gt;
  module Stringify&lt;br /&gt;
    # Requires an instance variable @value&lt;br /&gt;
    def stringify&lt;br /&gt;
      if @value == 1&lt;br /&gt;
        &amp;quot;One&amp;quot;&lt;br /&gt;
      elsif @value == 2&lt;br /&gt;
        &amp;quot;Two&amp;quot;&lt;br /&gt;
      elsif @value == 3&lt;br /&gt;
        &amp;quot;Three&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The Stringify module above makes use of a @value [http://en.wikipedia.org/wiki/Instance_variable instance variable]. The class that will be mixed with this module needs to define and set a @value instance variable since the Stringify module uses it but does not define it. In addition to instance variables a module could invoke methods not defined in the module but in the class that it will be mixed with.&lt;br /&gt;
&lt;br /&gt;
A [http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/ Self contained module] can also be written in Ruby where the methods defined in a module can be used as static methods independent of their implementation within a class.&lt;br /&gt;
&lt;br /&gt;
'''Self Contained Module Example: '''&lt;br /&gt;
&lt;br /&gt;
  # A Math module akin to Java Math class.&lt;br /&gt;
  module Math&lt;br /&gt;
    # Could be called as a class, static, method&lt;br /&gt;
    def add(val_one, val_two)&lt;br /&gt;
      BigInteger.new(val_one + val_two)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The methods in the Math module are intended to be invoked like class methods, also known as [http://en.wikipedia.org/wiki/Method_(computer_programming)#Static_methods static] methods. The add method in the Math module accepts two integer values and returns an [http://en.wikipedia.org/wiki/Instance_(computer_science) instance] of BigInteger.&lt;br /&gt;
&lt;br /&gt;
====Using Mixins====&lt;br /&gt;
&lt;br /&gt;
Implementation of the class and module are joined, interwined, combined, etc. A mixin is a different mechanism to the extend construct used to add concrete implementation to a class. With a mixin you can extend from a module instead of a class. &lt;br /&gt;
&lt;br /&gt;
The example of self-contained module shows how the method returns a BigInteger sum after accepting two integer parameters. The following example is the implementation of BigInteger class. The BigInteger class defines one constructor and directly inherits one method from the Number base class. To mix in the methods implemented in the Stringify and Math modules with the BigInteger class you will note the usage of the include and extend methods, respectively.&lt;br /&gt;
&lt;br /&gt;
  # Base Number class&lt;br /&gt;
  class Number&lt;br /&gt;
    def intValue&lt;br /&gt;
      @value&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  # BigInteger extends Number&lt;br /&gt;
  class BigInteger &amp;lt; Number&lt;br /&gt;
 &lt;br /&gt;
  # Add instance methods from Stringify&lt;br /&gt;
  include Stringify&lt;br /&gt;
 &lt;br /&gt;
  # Add class methods from Math&lt;br /&gt;
  extend Math&lt;br /&gt;
 &lt;br /&gt;
  # Add a constructor with one parameter&lt;br /&gt;
    def initialize(value)&lt;br /&gt;
      @value = value&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Using the class:&lt;br /&gt;
&lt;br /&gt;
  # Create a new object&lt;br /&gt;
  bigint1 = BigInteger.new(10)&lt;br /&gt;
  # Call a method inherited from the base class&lt;br /&gt;
  puts bigint1.intValue   # --&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
The extend method will mix a module’s methods at the class level. The method defined in the Math module can be used as a class/static method.&lt;br /&gt;
&lt;br /&gt;
  # Call class method extended from Math&lt;br /&gt;
  bigint2 = BigInteger.add(-2, 4)&lt;br /&gt;
  puts bigint2.intValue   # --&amp;gt; 2&lt;br /&gt;
&lt;br /&gt;
The include method will mix a module’s methods at the instance level, meaning that the methods will become instance methods. The method defined in the Stringify module can be used as an instance method.&lt;br /&gt;
&lt;br /&gt;
  # Call a method included from Stringify&lt;br /&gt;
  puts bigint2.stringify   # --&amp;gt; 'Two'&lt;br /&gt;
&lt;br /&gt;
There is another use of the extend method. You can enhance an object instance by mixing it with a module at run time. This ‘CurrencyFormatter’ is used to extend an object, changing it’s responsibilities at runtime.&lt;br /&gt;
&lt;br /&gt;
  # Format a numeric value as a currency&lt;br /&gt;
  module CurrencyFormatter&lt;br /&gt;
    def format&lt;br /&gt;
      &amp;quot;$#{@value}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
To mix an object instance with a module you can do the following:&lt;br /&gt;
&lt;br /&gt;
  # Add the module methods to&lt;br /&gt;
  # this object instance, only!&lt;br /&gt;
  bigint2.extend CurrencyFormatter&lt;br /&gt;
  puts bigint2.format   # --&amp;gt; '$2'&lt;br /&gt;
&lt;br /&gt;
Calling the extend method on an instance will only extend that one object; objects of the same class will not be extended with the new functionality.&lt;br /&gt;
&lt;br /&gt;
  puts bigint1.format   # will generate an error&lt;br /&gt;
&lt;br /&gt;
Modules that will be mixed with a class via the include or extend method could define something like a constructor or initializer method to the module. The module initializer method will be invoked at the time the module is mixed with a class. When a class extends a module the module’s self.extended method will be invoked:&lt;br /&gt;
&lt;br /&gt;
  module Math&lt;br /&gt;
    def self.extended(base)&lt;br /&gt;
      # Initialize module.&lt;br /&gt;
    end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
The self prefix indicates that the method is a static module level method. The base parameter in the static extended method will be either an instance object or class object of the class that extended the module depending whether you extend a object or class, respectively.&lt;br /&gt;
When a class includes a module the module’s self.included method will be invoked.&lt;br /&gt;
&lt;br /&gt;
  module Stringify&lt;br /&gt;
    def self.included(base)&lt;br /&gt;
      # Initialize module.&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The base parameter will be a class object for the class that includes the module.&lt;br /&gt;
It is important to note that inside the included and extended initializer methods you can include and extend other modules. &lt;br /&gt;
&lt;br /&gt;
  module Stringify&lt;br /&gt;
    def self.included(base)&lt;br /&gt;
      base.extend SomeOtherModule&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===Interfaces in Java===&lt;br /&gt;
Interfaces define a standardized set of commands that a class will obey. The commands are a set of methods that a class implements. The interface definition states the names of the methods and their return types and argument signatures. There is no executable body for any method. Example: &lt;br /&gt;
  interface Bicycle {&lt;br /&gt;
       void changeCadence(int newValue);   // wheel revolutions per minute&lt;br /&gt;
       void changeGear(int newValue);&lt;br /&gt;
       void speedUp(int increment);&lt;br /&gt;
       void applyBrakes(int decrement);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Implementation is independent to each class that implements the interface. Once a class implements an interface, the Java compiler knows that an instance of the class will contain the specified set of methods. Therefore, it will allow you to call those methods for an object referenced by a variable whose type is the interface. Implementing an interface enables a class to be &amp;quot;plugged in&amp;quot; in any situation that requires a specific behavior (manifested through the set of methods).&lt;br /&gt;
&lt;br /&gt;
  class ACMEBicycle implements Bicycle {&lt;br /&gt;
    //implementation specific to this class&lt;br /&gt;
       int cadence = 0;&lt;br /&gt;
       int speed = 0;&lt;br /&gt;
       int gear = 1;&lt;br /&gt;
       void changeCadence(int newValue) {&lt;br /&gt;
            cadence = newValue;&lt;br /&gt;
       }&lt;br /&gt;
       void changeGear(int newValue) {&lt;br /&gt;
            gear = newValue;&lt;br /&gt;
       }&lt;br /&gt;
       void speedUp(int increment) {&lt;br /&gt;
            speed = speed + increment;   &lt;br /&gt;
       }&lt;br /&gt;
       void applyBrakes(int decrement) {&lt;br /&gt;
            speed = speed - decrement;&lt;br /&gt;
       }&lt;br /&gt;
       void printStates() {&lt;br /&gt;
            System.out.println(&amp;quot;cadence:&amp;quot;+cadence+&amp;quot; speed:&amp;quot;+speed+&amp;quot; gear:&amp;quot;+gear);&lt;br /&gt;
       }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
  class MOUNTAINBicycle implements Bicycle {&lt;br /&gt;
  int cadence = 2;&lt;br /&gt;
       int speed = 3;&lt;br /&gt;
       int gear = 5;&lt;br /&gt;
       void changeCadence(int newValue) {&lt;br /&gt;
            cadence = newValue + 10;&lt;br /&gt;
       }&lt;br /&gt;
       void changeGear(int newValue) {&lt;br /&gt;
            gear = newValue + 4;&lt;br /&gt;
       }&lt;br /&gt;
       void speedUp(int increment) {&lt;br /&gt;
            speed = speed + increment + 10;   &lt;br /&gt;
       }&lt;br /&gt;
       void applyBrakes(int decrement) {&lt;br /&gt;
            speed = speed – decrement – 10;&lt;br /&gt;
       }&lt;br /&gt;
       void printStates() {&lt;br /&gt;
            System.out.println(&amp;quot;cadence:&amp;quot;+cadence+&amp;quot; speed:&amp;quot;+speed+&amp;quot; gear:&amp;quot;+gear);&lt;br /&gt;
       }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Using an interface rather than inheritance to specify a certain set of methods allows a class to inherit from some other class. In other words, if a class needs two different sets of methods, so it can behave like two different types of things, it could inherit one set from class A, and use an interface B to specify the other.&lt;br /&gt;
You could then reference one of these objects with either an A reference or a B reference. Implementing an interface allows a class to become more formal about the behavior it promises to provide. Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler. If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Mixins vs Interfaces==&lt;br /&gt;
Because of this mixin feature, a developer can add arbitrary methods and modify behavior of core classes at runtime. This is amazingly powerful if you are trying to write plugins and extensions to the [http://en.wikipedia.org/wiki/Software_framework framework].  Because you can add functionality to existing objects, users can install their [http://en.wikipedia.org/wiki/Plug-in_(computing) plugin] and start taking advantage of new functionality without having to make changes to the objects that they are instantiating in their application. &lt;br /&gt;
&lt;br /&gt;
In frameworks written in other languages, such as Java, plugging in new functionality means that you need to change how your objects are instantiated. This will require code changes and/or potentially configuration changes which makes it hard to develop, hard to maintain, and a pain for plugin developers to support.  But because of the mixin feature, Rails plugin developers can customize the base objects and the users of the plugins do not have to change any of their code or configuration logic.&lt;br /&gt;
&lt;br /&gt;
===Differences===&lt;br /&gt;
&lt;br /&gt;
These is a general comparision: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Mixins in Ruby&lt;br /&gt;
! Interfaces in Java&lt;br /&gt;
|-&lt;br /&gt;
| 'include' or 'extend' keywords used to include the module in the class.&lt;br /&gt;
| 'implement' keyword is used to implement interface&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Need not implement the module methods in the class.&lt;br /&gt;
| Should implement the interface methods. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| The methods are not copied into the class in which they are included. They are referenced.&lt;br /&gt;
| The methods’ implementation is in the class itself.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| The included method definition is decided at ‘run time’. If the definition changes while running the function points to the changed code.&lt;br /&gt;
| The definition is known compile time and will not change until the code is compiled and run again. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Modules inclusion does not provide a superclass and subclass relationship.&lt;br /&gt;
| Interface extending provides a superclass and subclass relationship. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| When we use mixins we included only a module of the class. &lt;br /&gt;
| We include the entire interface class. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Mainly for code reuse and does not provide any hierarchy. &lt;br /&gt;
| It provides a hierarchy. &lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Functionality and Behavior Comparison===&lt;br /&gt;
The functionality and behavior of Mixins in Ruby is compared with Interfaces in Java&lt;br /&gt;
&lt;br /&gt;
====Comparable Functionality====&lt;br /&gt;
Sometime we are required to compare the class members of different objects of the same class and/or sort them. This is the comparable functionality that we are trying to achieve for a given class.  This functionality can be achieved by both the Ruby using mixins and Java using interfaces. There is not much difference using both.&lt;br /&gt;
&lt;br /&gt;
'''Ruby'''&lt;br /&gt;
&lt;br /&gt;
The [http://www.ruby-doc.org/core/classes/Comparable.html Comparable] mixin is used by classes whose objects may be ordered. The class must define the &amp;lt;=&amp;gt; operator, which compares the receiver against another object, returning -1, 0, or +1 depending on whether the receiver is less than, equal to, or greater than the other object. Comparable uses &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method between?.&lt;br /&gt;
&lt;br /&gt;
  class SizeMatters&lt;br /&gt;
     include Comparable&lt;br /&gt;
     attr :str&lt;br /&gt;
     def &amp;lt;=&amp;gt;(anOther)&lt;br /&gt;
       str.size &amp;lt;=&amp;gt; anOther.str.size&lt;br /&gt;
     end&lt;br /&gt;
     def initialize(str)&lt;br /&gt;
       @str = str&lt;br /&gt;
     end&lt;br /&gt;
     def inspect&lt;br /&gt;
       @str&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
''using class:'' &lt;br /&gt;
&lt;br /&gt;
   s1 = SizeMatters.new(&amp;quot;Z&amp;quot;)&lt;br /&gt;
   s2 = SizeMatters.new(&amp;quot;YY&amp;quot;)&lt;br /&gt;
   s3 = SizeMatters.new(&amp;quot;XXX&amp;quot;)&lt;br /&gt;
   s4 = SizeMatters.new(&amp;quot;WWWW&amp;quot;)&lt;br /&gt;
   s5 = SizeMatters.new(&amp;quot;VVVVV&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
''output:''&lt;br /&gt;
   s1 &amp;lt; s2                       #=&amp;gt; true&lt;br /&gt;
   s4.between?(s1, s3)           #=&amp;gt; false&lt;br /&gt;
   s4.between?(s3, s5)           #=&amp;gt; true&lt;br /&gt;
   [ s3, s2, s5, s4, s1 ].sort   #=&amp;gt; [Z, YY, XXX, WWWW, VVVVV]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Java'''&lt;br /&gt;
&lt;br /&gt;
In Java, comparable functionality is achieved by extending the “Comparable” interface. List of objects that implement this interface can be sorted automatically by sort method of the list interface. This interface has compareTo() method that is used by the sort() method of the list. Here compareTo() method can also be used to compare two objects with respect to the implementation in compareTo() implementation in the derived class. It returns ‘0’ , ‘1’ and ‘-1’ corresponding to whether one is equal to , greater than or lesser than other.&lt;br /&gt;
In this example code Employee class is implementing Comparable interface and have method compareTo().&lt;br /&gt;
  public class Employee implements Comparable {&lt;br /&gt;
    int EmpID;&lt;br /&gt;
    String Ename;&lt;br /&gt;
    double Sal;&lt;br /&gt;
    static int i;&lt;br /&gt;
    public Employee() {&lt;br /&gt;
        EmpID = i++;&lt;br /&gt;
        Ename = &amp;quot;dont know&amp;quot;;&lt;br /&gt;
        Sal = 0.0;&lt;br /&gt;
    }&lt;br /&gt;
    public Employee(String ename, double sal) {&lt;br /&gt;
        EmpID = i++;&lt;br /&gt;
        Ename = ename;&lt;br /&gt;
        Sal = sal;&lt;br /&gt;
    }&lt;br /&gt;
    public String toString() {&lt;br /&gt;
        return &amp;quot;EmpID &amp;quot; + EmpID + &amp;quot;\n&amp;quot; + &amp;quot;Ename &amp;quot; + Ename + &amp;quot;\n&amp;quot; + &amp;quot;Sal&amp;quot; + Sal;&lt;br /&gt;
    }&lt;br /&gt;
    public int compareTo(Object o1) {&lt;br /&gt;
        if (this.Sal == ((Employee) o1).Sal)&lt;br /&gt;
            return 0;&lt;br /&gt;
        else if ((this.Sal) &amp;gt; ((Employee) o1).Sal)&lt;br /&gt;
            return 1;&lt;br /&gt;
        else&lt;br /&gt;
            return -1;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
ComparableDemo.java is showing the use of this interface. This class first makes a list of objects of type Employee and call sort method of java.util.Collections, which internally uses compareTo() method of Employee class and sort the list accordingly.&lt;br /&gt;
 &lt;br /&gt;
  import java.util.*;&lt;br /&gt;
  public class ComparableDemo{&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
        List ts1 = new ArrayList();&lt;br /&gt;
        ts1.add(new Employee (&amp;quot;Tom&amp;quot;,40000.00));&lt;br /&gt;
        ts1.add(new Employee (&amp;quot;Harry&amp;quot;,20000.00));&lt;br /&gt;
        ts1.add(new Employee (&amp;quot;Maggie&amp;quot;,50000.00));&lt;br /&gt;
        ts1.add(new Employee (&amp;quot;Chris&amp;quot;,70000.00));&lt;br /&gt;
        Collections.sort(ts1);&lt;br /&gt;
        Iterator itr = ts1.iterator();&lt;br /&gt;
        while(itr.hasNext()){&lt;br /&gt;
            Object element = itr.next();&lt;br /&gt;
            System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
''Output:''&lt;br /&gt;
  &lt;br /&gt;
  EmpID 1&lt;br /&gt;
  Ename Harry&lt;br /&gt;
  Sal    20000.0&lt;br /&gt;
  EmpID 0&lt;br /&gt;
  Ename Tom&lt;br /&gt;
  Sal    40000.0&lt;br /&gt;
  EmpID 2&lt;br /&gt;
  Ename Maggie&lt;br /&gt;
  Sal    50000.0&lt;br /&gt;
  EmpID 3&lt;br /&gt;
  Ename Chris&lt;br /&gt;
  Sal    70000.0&lt;br /&gt;
&lt;br /&gt;
====Singleton Behaviour====&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Singleton_pattern Singleton] is a class guaranteed to have only a single instance, with a single global access point to that instance. It can be implemented in Ruby and Java. In Ruby we can include a module to acquire all the functionality of the singleton module. The code is already there and we are just reusing the code and adding that similar functionality into our class. Here our intention is to add the functionality of singleton to the class which is achieved using mixins. In java, implementation of interfaces means that the class which implements the interface becomes a type of that class, which is not the intention here. So, there is not interface which class can implement to get the functionality in itself. It has to implement the functionality itself without implementing any interface. &lt;br /&gt;
&lt;br /&gt;
'''Ruby'''&lt;br /&gt;
&lt;br /&gt;
When you include the singleton method inside your class it makes the new method private so that you cannot use it. Including ‘singleton’ adds a class method called instance that instantiates only one instance of the class&lt;br /&gt;
&lt;br /&gt;
  # require singleton lib&lt;br /&gt;
  require 'singleton'&lt;br /&gt;
  class AppConfig&lt;br /&gt;
    # mixin the singleton module&lt;br /&gt;
    include Singleton&lt;br /&gt;
    # do the actual app configuration&lt;br /&gt;
    def load_config(file)&lt;br /&gt;
      # do your work here&lt;br /&gt;
      puts &amp;quot;Application configuration file was loaded from file: #{file}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
''output:''&lt;br /&gt;
&lt;br /&gt;
  conf1 = AppConfig.instance&lt;br /&gt;
  conf1.load_config &amp;quot;/home/khelll/conf.yml&amp;quot;       #=&amp;gt;Application configuration file was loaded from file: /home/khelll/conf.yml&lt;br /&gt;
  conf2 = AppConfig.instance&lt;br /&gt;
  puts conf1 == conf2      #=&amp;gt;true&lt;br /&gt;
  # notice the following 2 lines won’t work&lt;br /&gt;
  AppConfig.new rescue(puts $!)   #=&amp;gt; new method is private&lt;br /&gt;
&lt;br /&gt;
====Enumerable Behaviour====&lt;br /&gt;
This [http://www.ruby-doc.org/core/classes/Enumerable.html enumerable] functionality adds support to the array members of the class. We can achieve this functionality using mixins in Ruby while it is not possible to do it in Java using interfaces.&lt;br /&gt;
&lt;br /&gt;
'''Implementation in Ruby:''' &lt;br /&gt;
&lt;br /&gt;
It adds the functionality of map, inject, select etc to the class which includes the ‘Enumerable’ module. The class which includes should represent a collection of items. Enumerable requires that your class contain an each method that serves up the items in the collection. All the other Enumerable methods rely on this. &lt;br /&gt;
Example:&lt;br /&gt;
This is a Team class that manages a group of members:&lt;br /&gt;
&lt;br /&gt;
  class Team&lt;br /&gt;
    include Enumerable&lt;br /&gt;
    attr_accessor :members&lt;br /&gt;
    def initialize&lt;br /&gt;
      @members = []&lt;br /&gt;
    end&lt;br /&gt;
    def each &amp;amp;block&lt;br /&gt;
      @members.each{|member| block.call(member)}&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Enumerable requires that your class contain an each method that serves up the items in the collection. All the other Enumerablemethods rely on this. Now we can use the map method, for instance:&lt;br /&gt;
&lt;br /&gt;
  team = Team.new&lt;br /&gt;
  team.members = ['joshua', 'gabriel', 'jacob']&lt;br /&gt;
  team.map{|member| member.capitalize}   # =&amp;gt; [&amp;quot;Joshua&amp;quot;, &amp;quot;Gabriel&amp;quot;, &amp;quot;Jacob&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
Now we can call any Enumerable methods on our team object itself, and it will assume we want to work with the members array within.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Data Mapper Behaviour====&lt;br /&gt;
&lt;br /&gt;
[http://datamapper.org/docs/ DataMapper] is an API which comes with the ability to use the same API to talk to a multitude of different datastores. There are adapters for the usual RDBMS suspects, NoSQL stores, various file formats and even some popular webservices. This is implemented in ruby using mixins. We need not use interfaces to implement.&lt;br /&gt;
&lt;br /&gt;
'''Implementation in Ruby:'''&lt;br /&gt;
  class Post&lt;br /&gt;
       include DataMapper::Resource&lt;br /&gt;
       property :id, Serial&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
The point to take away from this is not that Ruby Mixins are more powerful than Java Interfaces. The mixin feature of Ruby allows for the development of easy to use but powerful plugins that will be hard for any non-Ruby based framework to compete with. Java has many other ways of implementing the functionality mixins are capable of. It is just that it does not use Interfaces to achieve the functionality. Even though both of them are a way of achieving multiple inheritance in their respective languages, they achieve quiet different goals. Inheritance in Java is mainly to overcome the confusion which arises with multiple inheritance, while Mixins in Ruby is mainly to add an additional functionality to the existing class apart from the ‘primary functionality’ of the class. &lt;br /&gt;
&lt;br /&gt;
When a class implements Interface, it indicates that it is of the type interface class and forms a hierarchy with the interface class, while ruby mixins are mainly intended for code reuse and including an additional functionality to the class. It does not form any hierarchy as it is only using module of the class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
==References==&lt;br /&gt;
* http://en.wikipedia.org/wiki/Interface_(Java)&lt;br /&gt;
* http://en.wikipedia.org/wiki/Mixin&lt;br /&gt;
* http://carboni.ca/blog/p/Modules-How-Do-They-Work&lt;br /&gt;
* http://rubylearning.com/satishtalim/modules_mixins.html&lt;br /&gt;
* http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/&lt;br /&gt;
* http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-&lt;br /&gt;
* http://datamapper.org/&lt;br /&gt;
* http://www.dsource.org/projects/tutorials/wiki/SingletonPattern &lt;br /&gt;
* http://corelib.rubyonrails.org/classes/Comparable.html&lt;br /&gt;
* http://kconrails.com/2010/11/30/ruby-enumerable-primer-part-1-the-basics/&lt;br /&gt;
* http://stackoverflow.com/questions/575920/is-a-ruby-module-equivalent-to-a-java-interface&lt;/div&gt;</summary>
		<author><name>Sthirum</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2c_bs&amp;diff=51678</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2c bs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2c_bs&amp;diff=51678"/>
		<updated>2011-10-02T01:16:37Z</updated>

		<summary type="html">&lt;p&gt;Sthirum: /* Conclusion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
An [http://en.wikipedia.org/wiki/Interface_(computing) interface] &amp;lt;ref&amp;gt;interface&amp;lt;/ref&amp;gt; in the [http://en.wikipedia.org/wiki/Java_(programming_language) Java] programming language is an abstract type that is used to specify an interface that classes must implement. Interface may only contain [http://en.wikipedia.org/wiki/Method_signature method signature] and constant declarations. An interface may never contain method definitions. Interfaces cannot be instantiated. A class that implements an interface must implement all of the methods described in the interface, or be an [http://en.wikipedia.org/wiki/Abstract_type abstract] class. Object references in Java may be specified to be of an interface type; in which case, they must either be null, or be bound to an object that implements the interface. &lt;br /&gt;
&lt;br /&gt;
In object-oriented programming languages, a [http://en.wikipedia.org/wiki/Mixin mixin] &amp;lt;ref&amp;gt; mixin &amp;lt;/ref&amp;gt; is a class that provides a certain functionality to be inherited or just reused by a subclass. Some of the functionality of mixins is provided by interfaces in popular languages like Java and [http://en.wikipedia.org/wiki/C_Sharp_(programming_language) C#]. However, an interface only specifies what the class must support and cannot provide an implementation. Another class, providing an implementation and dependent with the interface, is needed for [http://en.wikipedia.org/wiki/Code_refactoring refactoring] common behavior into a single place. Interfaces combined with aspect-oriented programming can produce full-fledged mixins in languages that support such features, such as C# or Java. &lt;br /&gt;
One benefit of using interfaces is that they simulate [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance]. All classes in Java other than [https://matrix.senecac.on.ca:8443/wiki/javaSE/index.php/Java.lang.Object java.lang.Object], must have exactly one base class; multiple inheritance of classes is not allowed.&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
===Mixins in Ruby=== &lt;br /&gt;
In Java you just have [http://en.wikipedia.org/wiki/Class_(computer_programming) classes] (both abstract and concrete) and interfaces. The [http://en.wikipedia.org/wiki/Ruby Ruby] language provides classes,[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html modules], and a mix of both. Inheriting from a mixin is not a form of specialization but is rather a means of collecting functionality. A class may inherit most or all of its functionality from one or more mixins through multiple inheritance. A mixin can also be viewed as an interface with implemented methods. When a class includes a mixin, the class implements the interface and includes, rather than inherits, all the mixins’ attributes (fields, properties) and methods. They become part of the class during compilation. A mixin can defer definition and binding of methods until runtime, though attributes and instantiation parameters are still defined at compile time. In the Ruby language a mixin is a class that is mixed with a module. &lt;br /&gt;
&lt;br /&gt;
====Module====&lt;br /&gt;
&lt;br /&gt;
A [http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html module]&amp;lt;ref&amp;gt;module&amp;lt;/ref&amp;gt; is a degenerate abstract class. A module can’t be instantiated and no class can directly extend it but it can fully implement methods. A class can leverage the implementation of a module by including the module’s methods. A module can define methods that can be shared in different and separate classes either at the class or instance level.We can view an example &amp;lt;ref&amp;gt; http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/&amp;lt;/ref&amp;gt; module that converts a numeric value to its English word.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # Convert a integer value to English.&lt;br /&gt;
  module Stringify&lt;br /&gt;
    # Requires an instance variable @value&lt;br /&gt;
    def stringify&lt;br /&gt;
      if @value == 1&lt;br /&gt;
        &amp;quot;One&amp;quot;&lt;br /&gt;
      elsif @value == 2&lt;br /&gt;
        &amp;quot;Two&amp;quot;&lt;br /&gt;
      elsif @value == 3&lt;br /&gt;
        &amp;quot;Three&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The Stringify module above makes use of a @value [http://en.wikipedia.org/wiki/Instance_variable instance variable]. The class that will be mixed with this module needs to define and set a @value instance variable since the Stringify module uses it but does not define it. In addition to instance variables a module could invoke methods not defined in the module but in the class that it will be mixed with.&lt;br /&gt;
&lt;br /&gt;
A [http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/ Self contained module] can also be written in Ruby where the methods defined in a module can be used as static methods independent of their implementation within a class.&lt;br /&gt;
&lt;br /&gt;
'''Self Contained Module Example: '''&lt;br /&gt;
&lt;br /&gt;
  # A Math module akin to Java Math class.&lt;br /&gt;
  module Math&lt;br /&gt;
    # Could be called as a class, static, method&lt;br /&gt;
    def add(val_one, val_two)&lt;br /&gt;
      BigInteger.new(val_one + val_two)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The methods in the Math module are intended to be invoked like class methods, also known as [http://en.wikipedia.org/wiki/Method_(computer_programming)#Static_methods static] methods. The add method in the Math module accepts two integer values and returns an [http://en.wikipedia.org/wiki/Instance_(computer_science) instance] of BigInteger.&lt;br /&gt;
&lt;br /&gt;
====Using Mixins====&lt;br /&gt;
&lt;br /&gt;
Implementation of the class and module are joined, interwined, combined, etc. A mixin is a different mechanism to the extend construct used to add concrete implementation to a class. With a mixin you can extend from a module instead of a class. &lt;br /&gt;
&lt;br /&gt;
The example of self-contained module shows how the method returns a BigInteger sum after accepting two integer parameters. The following example is the implementation of BigInteger class. The BigInteger class defines one constructor and directly inherits one method from the Number base class. To mix in the methods implemented in the Stringify and Math modules with the BigInteger class you will note the usage of the include and extend methods, respectively.&lt;br /&gt;
&lt;br /&gt;
  # Base Number class&lt;br /&gt;
  class Number&lt;br /&gt;
    def intValue&lt;br /&gt;
      @value&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  # BigInteger extends Number&lt;br /&gt;
  class BigInteger &amp;lt; Number&lt;br /&gt;
 &lt;br /&gt;
  # Add instance methods from Stringify&lt;br /&gt;
  include Stringify&lt;br /&gt;
 &lt;br /&gt;
  # Add class methods from Math&lt;br /&gt;
  extend Math&lt;br /&gt;
 &lt;br /&gt;
  # Add a constructor with one parameter&lt;br /&gt;
    def initialize(value)&lt;br /&gt;
      @value = value&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Using the class:&lt;br /&gt;
&lt;br /&gt;
  # Create a new object&lt;br /&gt;
  bigint1 = BigInteger.new(10)&lt;br /&gt;
  # Call a method inherited from the base class&lt;br /&gt;
  puts bigint1.intValue   # --&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
The extend method will mix a module’s methods at the class level. The method defined in the Math module can be used as a class/static method.&lt;br /&gt;
&lt;br /&gt;
  # Call class method extended from Math&lt;br /&gt;
  bigint2 = BigInteger.add(-2, 4)&lt;br /&gt;
  puts bigint2.intValue   # --&amp;gt; 2&lt;br /&gt;
&lt;br /&gt;
The include method will mix a module’s methods at the instance level, meaning that the methods will become instance methods. The method defined in the Stringify module can be used as an instance method.&lt;br /&gt;
&lt;br /&gt;
  # Call a method included from Stringify&lt;br /&gt;
  puts bigint2.stringify   # --&amp;gt; 'Two'&lt;br /&gt;
&lt;br /&gt;
There is another use of the extend method. You can enhance an object instance by mixing it with a module at run time. This ‘CurrencyFormatter’ is used to extend an object, changing it’s responsibilities at runtime.&lt;br /&gt;
&lt;br /&gt;
  # Format a numeric value as a currency&lt;br /&gt;
  module CurrencyFormatter&lt;br /&gt;
    def format&lt;br /&gt;
      &amp;quot;$#{@value}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
To mix an object instance with a module you can do the following:&lt;br /&gt;
&lt;br /&gt;
  # Add the module methods to&lt;br /&gt;
  # this object instance, only!&lt;br /&gt;
  bigint2.extend CurrencyFormatter&lt;br /&gt;
  puts bigint2.format   # --&amp;gt; '$2'&lt;br /&gt;
&lt;br /&gt;
Calling the extend method on an instance will only extend that one object; objects of the same class will not be extended with the new functionality.&lt;br /&gt;
&lt;br /&gt;
  puts bigint1.format   # will generate an error&lt;br /&gt;
&lt;br /&gt;
Modules that will be mixed with a class via the include or extend method could define something like a constructor or initializer method to the module. The module initializer method will be invoked at the time the module is mixed with a class. When a class extends a module the module’s self.extended method will be invoked:&lt;br /&gt;
&lt;br /&gt;
  module Math&lt;br /&gt;
    def self.extended(base)&lt;br /&gt;
      # Initialize module.&lt;br /&gt;
    end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
The self prefix indicates that the method is a static module level method. The base parameter in the static extended method will be either an instance object or class object of the class that extended the module depending whether you extend a object or class, respectively.&lt;br /&gt;
When a class includes a module the module’s self.included method will be invoked.&lt;br /&gt;
&lt;br /&gt;
  module Stringify&lt;br /&gt;
    def self.included(base)&lt;br /&gt;
      # Initialize module.&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The base parameter will be a class object for the class that includes the module.&lt;br /&gt;
It is important to note that inside the included and extended initializer methods you can include and extend other modules. &lt;br /&gt;
&lt;br /&gt;
  module Stringify&lt;br /&gt;
    def self.included(base)&lt;br /&gt;
      base.extend SomeOtherModule&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===Interfaces in Java===&lt;br /&gt;
Interfaces define a standardized set of commands that a class will obey. The commands are a set of methods that a class implements. The interface definition states the names of the methods and their return types and argument signatures. There is no executable body for any method. Example: &lt;br /&gt;
  interface Bicycle {&lt;br /&gt;
       void changeCadence(int newValue);   // wheel revolutions per minute&lt;br /&gt;
       void changeGear(int newValue);&lt;br /&gt;
       void speedUp(int increment);&lt;br /&gt;
       void applyBrakes(int decrement);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Implementation is independent to each class that implements the interface. Once a class implements an interface, the Java compiler knows that an instance of the class will contain the specified set of methods. Therefore, it will allow you to call those methods for an object referenced by a variable whose type is the interface. Implementing an interface enables a class to be &amp;quot;plugged in&amp;quot; in any situation that requires a specific behavior (manifested through the set of methods).&lt;br /&gt;
&lt;br /&gt;
  class ACMEBicycle implements Bicycle {&lt;br /&gt;
    //implementation specific to this class&lt;br /&gt;
       int cadence = 0;&lt;br /&gt;
       int speed = 0;&lt;br /&gt;
       int gear = 1;&lt;br /&gt;
       void changeCadence(int newValue) {&lt;br /&gt;
            cadence = newValue;&lt;br /&gt;
       }&lt;br /&gt;
       void changeGear(int newValue) {&lt;br /&gt;
            gear = newValue;&lt;br /&gt;
       }&lt;br /&gt;
       void speedUp(int increment) {&lt;br /&gt;
            speed = speed + increment;   &lt;br /&gt;
       }&lt;br /&gt;
       void applyBrakes(int decrement) {&lt;br /&gt;
            speed = speed - decrement;&lt;br /&gt;
       }&lt;br /&gt;
       void printStates() {&lt;br /&gt;
            System.out.println(&amp;quot;cadence:&amp;quot;+cadence+&amp;quot; speed:&amp;quot;+speed+&amp;quot; gear:&amp;quot;+gear);&lt;br /&gt;
       }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
  class MOUNTAINBicycle implements Bicycle {&lt;br /&gt;
  int cadence = 2;&lt;br /&gt;
       int speed = 3;&lt;br /&gt;
       int gear = 5;&lt;br /&gt;
       void changeCadence(int newValue) {&lt;br /&gt;
            cadence = newValue + 10;&lt;br /&gt;
       }&lt;br /&gt;
       void changeGear(int newValue) {&lt;br /&gt;
            gear = newValue + 4;&lt;br /&gt;
       }&lt;br /&gt;
       void speedUp(int increment) {&lt;br /&gt;
            speed = speed + increment + 10;   &lt;br /&gt;
       }&lt;br /&gt;
       void applyBrakes(int decrement) {&lt;br /&gt;
            speed = speed – decrement – 10;&lt;br /&gt;
       }&lt;br /&gt;
       void printStates() {&lt;br /&gt;
            System.out.println(&amp;quot;cadence:&amp;quot;+cadence+&amp;quot; speed:&amp;quot;+speed+&amp;quot; gear:&amp;quot;+gear);&lt;br /&gt;
       }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Using an interface rather than inheritance to specify a certain set of methods allows a class to inherit from some other class. In other words, if a class needs two different sets of methods, so it can behave like two different types of things, it could inherit one set from class A, and use an interface B to specify the other.&lt;br /&gt;
You could then reference one of these objects with either an A reference or a B reference. Implementing an interface allows a class to become more formal about the behavior it promises to provide. Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler. If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Mixins vs Interfaces==&lt;br /&gt;
Because of this mixin feature, a developer can add arbitrary methods and modify behavior of core classes at runtime. This is amazingly powerful if you are trying to write plugins and extensions to the [http://en.wikipedia.org/wiki/Software_framework framework].  Because you can add functionality to existing objects, users can install their [http://en.wikipedia.org/wiki/Plug-in_(computing) plugin] and start taking advantage of new functionality without having to make changes to the objects that they are instantiating in their application. &lt;br /&gt;
&lt;br /&gt;
In frameworks written in other languages, such as Java, plugging in new functionality means that you need to change how your objects are instantiated. This will require code changes and/or potentially configuration changes which makes it hard to develop, hard to maintain, and a pain for plugin developers to support.  But because of the mixin feature, Rails plugin developers can customize the base objects and the users of the plugins do not have to change any of their code or configuration logic.&lt;br /&gt;
&lt;br /&gt;
===Differences===&lt;br /&gt;
&lt;br /&gt;
These is a general comparision: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Mixins in Ruby&lt;br /&gt;
! Interfaces in Java&lt;br /&gt;
|-&lt;br /&gt;
| 'include' or 'extend' keywords used to include the module in the class.&lt;br /&gt;
| 'implement' keyword is used to implement interface&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Need not implement the module methods in the class.&lt;br /&gt;
| Should implement the interface methods. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| The methods are not copied into the class in which they are included. They are referenced.&lt;br /&gt;
| The methods’ implementation is in the class itself.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| The included method definition is decided at ‘run time’. If the definition changes while running the function points to the changed code.&lt;br /&gt;
| The definition is known compile time and will not change until the code is compiled and run again. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Modules inclusion does not provide a superclass and subclass relationship.&lt;br /&gt;
| Interface extending provides a superclass and subclass relationship. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| When we use mixins we included only a module of the class. &lt;br /&gt;
| We include the entire interface class. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Mainly for code reuse and does not provide any hierarchy. &lt;br /&gt;
| It provides a hierarchy. &lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Functionality and Behavior Comparison===&lt;br /&gt;
The functionality and behavior of Mixins in Ruby is compared with Interfaces in Java&lt;br /&gt;
&lt;br /&gt;
====Comparable Functionality====&lt;br /&gt;
Sometime we are required to compare the class members of different objects of the same class and/or sort them. This is the comparable functionality that we are trying to achieve for a given class.  This functionality can be achieved by both the Ruby using mixins and Java using interfaces. There is not much difference using both.&lt;br /&gt;
&lt;br /&gt;
'''Ruby'''&lt;br /&gt;
&lt;br /&gt;
The [http://www.ruby-doc.org/core/classes/Comparable.html Comparable] mixin is used by classes whose objects may be ordered. The class must define the &amp;lt;=&amp;gt; operator, which compares the receiver against another object, returning -1, 0, or +1 depending on whether the receiver is less than, equal to, or greater than the other object. Comparable uses &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method between?.&lt;br /&gt;
&lt;br /&gt;
  class SizeMatters&lt;br /&gt;
     include Comparable&lt;br /&gt;
     attr :str&lt;br /&gt;
     def &amp;lt;=&amp;gt;(anOther)&lt;br /&gt;
       str.size &amp;lt;=&amp;gt; anOther.str.size&lt;br /&gt;
     end&lt;br /&gt;
     def initialize(str)&lt;br /&gt;
       @str = str&lt;br /&gt;
     end&lt;br /&gt;
     def inspect&lt;br /&gt;
       @str&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
''using class:'' &lt;br /&gt;
&lt;br /&gt;
   s1 = SizeMatters.new(&amp;quot;Z&amp;quot;)&lt;br /&gt;
   s2 = SizeMatters.new(&amp;quot;YY&amp;quot;)&lt;br /&gt;
   s3 = SizeMatters.new(&amp;quot;XXX&amp;quot;)&lt;br /&gt;
   s4 = SizeMatters.new(&amp;quot;WWWW&amp;quot;)&lt;br /&gt;
   s5 = SizeMatters.new(&amp;quot;VVVVV&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
''output:''&lt;br /&gt;
   s1 &amp;lt; s2                       #=&amp;gt; true&lt;br /&gt;
   s4.between?(s1, s3)           #=&amp;gt; false&lt;br /&gt;
   s4.between?(s3, s5)           #=&amp;gt; true&lt;br /&gt;
   [ s3, s2, s5, s4, s1 ].sort   #=&amp;gt; [Z, YY, XXX, WWWW, VVVVV]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Java'''&lt;br /&gt;
&lt;br /&gt;
In Java, comparable functionality is achieved by extending the “Comparable” interface. List of objects that implement this interface can be sorted automatically by sort method of the list interface. This interface has compareTo() method that is used by the sort() method of the list. Here compareTo() method can also be used to compare two objects with respect to the implementation in compareTo() implementation in the derived class. It returns ‘0’ , ‘1’ and ‘-1’ corresponding to whether one is equal to , greater than or lesser than other.&lt;br /&gt;
In this example code Employee class is implementing Comparable interface and have method compareTo().&lt;br /&gt;
  public class Employee implements Comparable {&lt;br /&gt;
    int EmpID;&lt;br /&gt;
    String Ename;&lt;br /&gt;
    double Sal;&lt;br /&gt;
    static int i;&lt;br /&gt;
    public Employee() {&lt;br /&gt;
        EmpID = i++;&lt;br /&gt;
        Ename = &amp;quot;dont know&amp;quot;;&lt;br /&gt;
        Sal = 0.0;&lt;br /&gt;
    }&lt;br /&gt;
    public Employee(String ename, double sal) {&lt;br /&gt;
        EmpID = i++;&lt;br /&gt;
        Ename = ename;&lt;br /&gt;
        Sal = sal;&lt;br /&gt;
    }&lt;br /&gt;
    public String toString() {&lt;br /&gt;
        return &amp;quot;EmpID &amp;quot; + EmpID + &amp;quot;\n&amp;quot; + &amp;quot;Ename &amp;quot; + Ename + &amp;quot;\n&amp;quot; + &amp;quot;Sal&amp;quot; + Sal;&lt;br /&gt;
    }&lt;br /&gt;
    public int compareTo(Object o1) {&lt;br /&gt;
        if (this.Sal == ((Employee) o1).Sal)&lt;br /&gt;
            return 0;&lt;br /&gt;
        else if ((this.Sal) &amp;gt; ((Employee) o1).Sal)&lt;br /&gt;
            return 1;&lt;br /&gt;
        else&lt;br /&gt;
            return -1;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
ComparableDemo.java is showing the use of this interface. This class first makes a list of objects of type Employee and call sort method of java.util.Collections, which internally uses compareTo() method of Employee class and sort the list accordingly.&lt;br /&gt;
 &lt;br /&gt;
  import java.util.*;&lt;br /&gt;
  public class ComparableDemo{&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
        List ts1 = new ArrayList();&lt;br /&gt;
        ts1.add(new Employee (&amp;quot;Tom&amp;quot;,40000.00));&lt;br /&gt;
        ts1.add(new Employee (&amp;quot;Harry&amp;quot;,20000.00));&lt;br /&gt;
        ts1.add(new Employee (&amp;quot;Maggie&amp;quot;,50000.00));&lt;br /&gt;
        ts1.add(new Employee (&amp;quot;Chris&amp;quot;,70000.00));&lt;br /&gt;
        Collections.sort(ts1);&lt;br /&gt;
        Iterator itr = ts1.iterator();&lt;br /&gt;
        while(itr.hasNext()){&lt;br /&gt;
            Object element = itr.next();&lt;br /&gt;
            System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
''Output:''&lt;br /&gt;
  &lt;br /&gt;
  EmpID 1&lt;br /&gt;
  Ename Harry&lt;br /&gt;
  Sal    20000.0&lt;br /&gt;
  EmpID 0&lt;br /&gt;
  Ename Tom&lt;br /&gt;
  Sal    40000.0&lt;br /&gt;
  EmpID 2&lt;br /&gt;
  Ename Maggie&lt;br /&gt;
  Sal    50000.0&lt;br /&gt;
  EmpID 3&lt;br /&gt;
  Ename Chris&lt;br /&gt;
  Sal    70000.0&lt;br /&gt;
&lt;br /&gt;
====Singleton Behaviour====&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Singleton_pattern Singleton] is a class guaranteed to have only a single instance, with a single global access point to that instance. It can be implemented in Ruby and Java. In Ruby we can include a module to acquire all the functionality of the singleton module. The code is already there and we are just reusing the code and adding that similar functionality into our class. Here our intention is to add the functionality of singleton to the class which is achieved using mixins. In java, implementation of interfaces means that the class which implements the interface becomes a type of that class, which is not the intention here. So, there is not interface which class can implement to get the functionality in itself. It has to implement the functionality itself without implementing any interface. &lt;br /&gt;
&lt;br /&gt;
'''Ruby'''&lt;br /&gt;
&lt;br /&gt;
When you include the singleton method inside your class it makes the new method private so that you cannot use it. Including ‘singleton’ adds a class method called instance that instantiates only one instance of the class&lt;br /&gt;
&lt;br /&gt;
  # require singleton lib&lt;br /&gt;
  require 'singleton'&lt;br /&gt;
  class AppConfig&lt;br /&gt;
    # mixin the singleton module&lt;br /&gt;
    include Singleton&lt;br /&gt;
    # do the actual app configuration&lt;br /&gt;
    def load_config(file)&lt;br /&gt;
      # do your work here&lt;br /&gt;
      puts &amp;quot;Application configuration file was loaded from file: #{file}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
''output:''&lt;br /&gt;
&lt;br /&gt;
  conf1 = AppConfig.instance&lt;br /&gt;
  conf1.load_config &amp;quot;/home/khelll/conf.yml&amp;quot;       #=&amp;gt;Application configuration file was loaded from file: /home/khelll/conf.yml&lt;br /&gt;
  conf2 = AppConfig.instance&lt;br /&gt;
  puts conf1 == conf2      #=&amp;gt;true&lt;br /&gt;
  # notice the following 2 lines won’t work&lt;br /&gt;
  AppConfig.new rescue(puts $!)   #=&amp;gt; new method is private&lt;br /&gt;
&lt;br /&gt;
====Enumerable Behaviour====&lt;br /&gt;
This [http://www.ruby-doc.org/core/classes/Enumerable.html enumerable] functionality adds support to the array members of the class. We can achieve this functionality using mixins in Ruby while it is not possible to do it in Java using interfaces.&lt;br /&gt;
&lt;br /&gt;
'''Implementation in Ruby:''' &lt;br /&gt;
&lt;br /&gt;
It adds the functionality of map, inject, select etc to the class which includes the ‘Enumerable’ module. The class which includes should represent a collection of items. Enumerable requires that your class contain an each method that serves up the items in the collection. All the other Enumerable methods rely on this. &lt;br /&gt;
Example:&lt;br /&gt;
This is a Team class that manages a group of members:&lt;br /&gt;
&lt;br /&gt;
  class Team&lt;br /&gt;
    include Enumerable&lt;br /&gt;
    attr_accessor :members&lt;br /&gt;
    def initialize&lt;br /&gt;
      @members = []&lt;br /&gt;
    end&lt;br /&gt;
    def each &amp;amp;block&lt;br /&gt;
      @members.each{|member| block.call(member)}&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Enumerable requires that your class contain an each method that serves up the items in the collection. All the other Enumerablemethods rely on this. Now we can use the map method, for instance:&lt;br /&gt;
&lt;br /&gt;
  team = Team.new&lt;br /&gt;
  team.members = ['joshua', 'gabriel', 'jacob']&lt;br /&gt;
  team.map{|member| member.capitalize}   # =&amp;gt; [&amp;quot;Joshua&amp;quot;, &amp;quot;Gabriel&amp;quot;, &amp;quot;Jacob&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
Now we can call any Enumerable methods on our team object itself, and it will assume we want to work with the members array within.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Data Mapper Behaviour====&lt;br /&gt;
&lt;br /&gt;
[http://datamapper.org/docs/ DataMapper] is an API which comes with the ability to use the same API to talk to a multitude of different datastores. There are adapters for the usual RDBMS suspects, NoSQL stores, various file formats and even some popular webservices. This is implemented in ruby using mixins. We need not use interfaces to implement.&lt;br /&gt;
&lt;br /&gt;
'''Implementation in Ruby:'''&lt;br /&gt;
  class Post&lt;br /&gt;
       include DataMapper::Resource&lt;br /&gt;
       property :id, Serial&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
The point to take away from this is not that Ruby Mixins are more powerful than Java Interfaces. The mixin feature of Ruby allows for the development of easy to use but powerful plugins that will be hard for any non-Ruby based framework to compete with. Java has many other ways of implementing the functionality mixins are capable of. It is just that it does not use Interfaces to achieve the functionality. Even though both of them are a way of achieving multiple inheritance in their respective languages, they achieve quiet different goals. Inheritance in Java is mainly to overcome the confusion which arises with multiple inheritance, while Mixins in Ruby is mainly to add an additional functionality to the existing class apart from the ‘primary functionality’ of the class. &lt;br /&gt;
&lt;br /&gt;
When a class implements Interface, it indicates that it is of the type interface class and forms a hierarchy with the interface class, while ruby mixins are mainly intended for code reuse and including an additional functionality to the class. It does not form any hierarchy as it is only using module of the class.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* http://en.wikipedia.org/wiki/Interface_(Java)&lt;br /&gt;
* http://en.wikipedia.org/wiki/Mixin&lt;br /&gt;
* http://carboni.ca/blog/p/Modules-How-Do-They-Work&lt;br /&gt;
* http://rubylearning.com/satishtalim/modules_mixins.html&lt;br /&gt;
* http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/&lt;br /&gt;
* http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-&lt;br /&gt;
* http://datamapper.org/&lt;br /&gt;
* http://www.dsource.org/projects/tutorials/wiki/SingletonPattern &lt;br /&gt;
* http://corelib.rubyonrails.org/classes/Comparable.html&lt;br /&gt;
* http://kconrails.com/2010/11/30/ruby-enumerable-primer-part-1-the-basics/&lt;br /&gt;
* http://stackoverflow.com/questions/575920/is-a-ruby-module-equivalent-to-a-java-interface&lt;/div&gt;</summary>
		<author><name>Sthirum</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2c_bs&amp;diff=51677</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2c bs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2c_bs&amp;diff=51677"/>
		<updated>2011-10-02T01:14:56Z</updated>

		<summary type="html">&lt;p&gt;Sthirum: /* Using Mixins */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
An [http://en.wikipedia.org/wiki/Interface_(computing) interface] &amp;lt;ref&amp;gt;interface&amp;lt;/ref&amp;gt; in the [http://en.wikipedia.org/wiki/Java_(programming_language) Java] programming language is an abstract type that is used to specify an interface that classes must implement. Interface may only contain [http://en.wikipedia.org/wiki/Method_signature method signature] and constant declarations. An interface may never contain method definitions. Interfaces cannot be instantiated. A class that implements an interface must implement all of the methods described in the interface, or be an [http://en.wikipedia.org/wiki/Abstract_type abstract] class. Object references in Java may be specified to be of an interface type; in which case, they must either be null, or be bound to an object that implements the interface. &lt;br /&gt;
&lt;br /&gt;
In object-oriented programming languages, a [http://en.wikipedia.org/wiki/Mixin mixin] &amp;lt;ref&amp;gt; mixin &amp;lt;/ref&amp;gt; is a class that provides a certain functionality to be inherited or just reused by a subclass. Some of the functionality of mixins is provided by interfaces in popular languages like Java and [http://en.wikipedia.org/wiki/C_Sharp_(programming_language) C#]. However, an interface only specifies what the class must support and cannot provide an implementation. Another class, providing an implementation and dependent with the interface, is needed for [http://en.wikipedia.org/wiki/Code_refactoring refactoring] common behavior into a single place. Interfaces combined with aspect-oriented programming can produce full-fledged mixins in languages that support such features, such as C# or Java. &lt;br /&gt;
One benefit of using interfaces is that they simulate [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance]. All classes in Java other than [https://matrix.senecac.on.ca:8443/wiki/javaSE/index.php/Java.lang.Object java.lang.Object], must have exactly one base class; multiple inheritance of classes is not allowed.&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
===Mixins in Ruby=== &lt;br /&gt;
In Java you just have [http://en.wikipedia.org/wiki/Class_(computer_programming) classes] (both abstract and concrete) and interfaces. The [http://en.wikipedia.org/wiki/Ruby Ruby] language provides classes,[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html modules], and a mix of both. Inheriting from a mixin is not a form of specialization but is rather a means of collecting functionality. A class may inherit most or all of its functionality from one or more mixins through multiple inheritance. A mixin can also be viewed as an interface with implemented methods. When a class includes a mixin, the class implements the interface and includes, rather than inherits, all the mixins’ attributes (fields, properties) and methods. They become part of the class during compilation. A mixin can defer definition and binding of methods until runtime, though attributes and instantiation parameters are still defined at compile time. In the Ruby language a mixin is a class that is mixed with a module. &lt;br /&gt;
&lt;br /&gt;
====Module====&lt;br /&gt;
&lt;br /&gt;
A [http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html module]&amp;lt;ref&amp;gt;module&amp;lt;/ref&amp;gt; is a degenerate abstract class. A module can’t be instantiated and no class can directly extend it but it can fully implement methods. A class can leverage the implementation of a module by including the module’s methods. A module can define methods that can be shared in different and separate classes either at the class or instance level.We can view an example &amp;lt;ref&amp;gt; http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/&amp;lt;/ref&amp;gt; module that converts a numeric value to its English word.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # Convert a integer value to English.&lt;br /&gt;
  module Stringify&lt;br /&gt;
    # Requires an instance variable @value&lt;br /&gt;
    def stringify&lt;br /&gt;
      if @value == 1&lt;br /&gt;
        &amp;quot;One&amp;quot;&lt;br /&gt;
      elsif @value == 2&lt;br /&gt;
        &amp;quot;Two&amp;quot;&lt;br /&gt;
      elsif @value == 3&lt;br /&gt;
        &amp;quot;Three&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The Stringify module above makes use of a @value [http://en.wikipedia.org/wiki/Instance_variable instance variable]. The class that will be mixed with this module needs to define and set a @value instance variable since the Stringify module uses it but does not define it. In addition to instance variables a module could invoke methods not defined in the module but in the class that it will be mixed with.&lt;br /&gt;
&lt;br /&gt;
A [http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/ Self contained module] can also be written in Ruby where the methods defined in a module can be used as static methods independent of their implementation within a class.&lt;br /&gt;
&lt;br /&gt;
'''Self Contained Module Example: '''&lt;br /&gt;
&lt;br /&gt;
  # A Math module akin to Java Math class.&lt;br /&gt;
  module Math&lt;br /&gt;
    # Could be called as a class, static, method&lt;br /&gt;
    def add(val_one, val_two)&lt;br /&gt;
      BigInteger.new(val_one + val_two)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The methods in the Math module are intended to be invoked like class methods, also known as [http://en.wikipedia.org/wiki/Method_(computer_programming)#Static_methods static] methods. The add method in the Math module accepts two integer values and returns an [http://en.wikipedia.org/wiki/Instance_(computer_science) instance] of BigInteger.&lt;br /&gt;
&lt;br /&gt;
====Using Mixins====&lt;br /&gt;
&lt;br /&gt;
Implementation of the class and module are joined, interwined, combined, etc. A mixin is a different mechanism to the extend construct used to add concrete implementation to a class. With a mixin you can extend from a module instead of a class. &lt;br /&gt;
&lt;br /&gt;
The example of self-contained module shows how the method returns a BigInteger sum after accepting two integer parameters. The following example is the implementation of BigInteger class. The BigInteger class defines one constructor and directly inherits one method from the Number base class. To mix in the methods implemented in the Stringify and Math modules with the BigInteger class you will note the usage of the include and extend methods, respectively.&lt;br /&gt;
&lt;br /&gt;
  # Base Number class&lt;br /&gt;
  class Number&lt;br /&gt;
    def intValue&lt;br /&gt;
      @value&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  # BigInteger extends Number&lt;br /&gt;
  class BigInteger &amp;lt; Number&lt;br /&gt;
 &lt;br /&gt;
  # Add instance methods from Stringify&lt;br /&gt;
  include Stringify&lt;br /&gt;
 &lt;br /&gt;
  # Add class methods from Math&lt;br /&gt;
  extend Math&lt;br /&gt;
 &lt;br /&gt;
  # Add a constructor with one parameter&lt;br /&gt;
    def initialize(value)&lt;br /&gt;
      @value = value&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Using the class:&lt;br /&gt;
&lt;br /&gt;
  # Create a new object&lt;br /&gt;
  bigint1 = BigInteger.new(10)&lt;br /&gt;
  # Call a method inherited from the base class&lt;br /&gt;
  puts bigint1.intValue   # --&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
The extend method will mix a module’s methods at the class level. The method defined in the Math module can be used as a class/static method.&lt;br /&gt;
&lt;br /&gt;
  # Call class method extended from Math&lt;br /&gt;
  bigint2 = BigInteger.add(-2, 4)&lt;br /&gt;
  puts bigint2.intValue   # --&amp;gt; 2&lt;br /&gt;
&lt;br /&gt;
The include method will mix a module’s methods at the instance level, meaning that the methods will become instance methods. The method defined in the Stringify module can be used as an instance method.&lt;br /&gt;
&lt;br /&gt;
  # Call a method included from Stringify&lt;br /&gt;
  puts bigint2.stringify   # --&amp;gt; 'Two'&lt;br /&gt;
&lt;br /&gt;
There is another use of the extend method. You can enhance an object instance by mixing it with a module at run time. This ‘CurrencyFormatter’ is used to extend an object, changing it’s responsibilities at runtime.&lt;br /&gt;
&lt;br /&gt;
  # Format a numeric value as a currency&lt;br /&gt;
  module CurrencyFormatter&lt;br /&gt;
    def format&lt;br /&gt;
      &amp;quot;$#{@value}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
To mix an object instance with a module you can do the following:&lt;br /&gt;
&lt;br /&gt;
  # Add the module methods to&lt;br /&gt;
  # this object instance, only!&lt;br /&gt;
  bigint2.extend CurrencyFormatter&lt;br /&gt;
  puts bigint2.format   # --&amp;gt; '$2'&lt;br /&gt;
&lt;br /&gt;
Calling the extend method on an instance will only extend that one object; objects of the same class will not be extended with the new functionality.&lt;br /&gt;
&lt;br /&gt;
  puts bigint1.format   # will generate an error&lt;br /&gt;
&lt;br /&gt;
Modules that will be mixed with a class via the include or extend method could define something like a constructor or initializer method to the module. The module initializer method will be invoked at the time the module is mixed with a class. When a class extends a module the module’s self.extended method will be invoked:&lt;br /&gt;
&lt;br /&gt;
  module Math&lt;br /&gt;
    def self.extended(base)&lt;br /&gt;
      # Initialize module.&lt;br /&gt;
    end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
The self prefix indicates that the method is a static module level method. The base parameter in the static extended method will be either an instance object or class object of the class that extended the module depending whether you extend a object or class, respectively.&lt;br /&gt;
When a class includes a module the module’s self.included method will be invoked.&lt;br /&gt;
&lt;br /&gt;
  module Stringify&lt;br /&gt;
    def self.included(base)&lt;br /&gt;
      # Initialize module.&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The base parameter will be a class object for the class that includes the module.&lt;br /&gt;
It is important to note that inside the included and extended initializer methods you can include and extend other modules. &lt;br /&gt;
&lt;br /&gt;
  module Stringify&lt;br /&gt;
    def self.included(base)&lt;br /&gt;
      base.extend SomeOtherModule&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===Interfaces in Java===&lt;br /&gt;
Interfaces define a standardized set of commands that a class will obey. The commands are a set of methods that a class implements. The interface definition states the names of the methods and their return types and argument signatures. There is no executable body for any method. Example: &lt;br /&gt;
  interface Bicycle {&lt;br /&gt;
       void changeCadence(int newValue);   // wheel revolutions per minute&lt;br /&gt;
       void changeGear(int newValue);&lt;br /&gt;
       void speedUp(int increment);&lt;br /&gt;
       void applyBrakes(int decrement);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Implementation is independent to each class that implements the interface. Once a class implements an interface, the Java compiler knows that an instance of the class will contain the specified set of methods. Therefore, it will allow you to call those methods for an object referenced by a variable whose type is the interface. Implementing an interface enables a class to be &amp;quot;plugged in&amp;quot; in any situation that requires a specific behavior (manifested through the set of methods).&lt;br /&gt;
&lt;br /&gt;
  class ACMEBicycle implements Bicycle {&lt;br /&gt;
    //implementation specific to this class&lt;br /&gt;
       int cadence = 0;&lt;br /&gt;
       int speed = 0;&lt;br /&gt;
       int gear = 1;&lt;br /&gt;
       void changeCadence(int newValue) {&lt;br /&gt;
            cadence = newValue;&lt;br /&gt;
       }&lt;br /&gt;
       void changeGear(int newValue) {&lt;br /&gt;
            gear = newValue;&lt;br /&gt;
       }&lt;br /&gt;
       void speedUp(int increment) {&lt;br /&gt;
            speed = speed + increment;   &lt;br /&gt;
       }&lt;br /&gt;
       void applyBrakes(int decrement) {&lt;br /&gt;
            speed = speed - decrement;&lt;br /&gt;
       }&lt;br /&gt;
       void printStates() {&lt;br /&gt;
            System.out.println(&amp;quot;cadence:&amp;quot;+cadence+&amp;quot; speed:&amp;quot;+speed+&amp;quot; gear:&amp;quot;+gear);&lt;br /&gt;
       }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
  class MOUNTAINBicycle implements Bicycle {&lt;br /&gt;
  int cadence = 2;&lt;br /&gt;
       int speed = 3;&lt;br /&gt;
       int gear = 5;&lt;br /&gt;
       void changeCadence(int newValue) {&lt;br /&gt;
            cadence = newValue + 10;&lt;br /&gt;
       }&lt;br /&gt;
       void changeGear(int newValue) {&lt;br /&gt;
            gear = newValue + 4;&lt;br /&gt;
       }&lt;br /&gt;
       void speedUp(int increment) {&lt;br /&gt;
            speed = speed + increment + 10;   &lt;br /&gt;
       }&lt;br /&gt;
       void applyBrakes(int decrement) {&lt;br /&gt;
            speed = speed – decrement – 10;&lt;br /&gt;
       }&lt;br /&gt;
       void printStates() {&lt;br /&gt;
            System.out.println(&amp;quot;cadence:&amp;quot;+cadence+&amp;quot; speed:&amp;quot;+speed+&amp;quot; gear:&amp;quot;+gear);&lt;br /&gt;
       }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Using an interface rather than inheritance to specify a certain set of methods allows a class to inherit from some other class. In other words, if a class needs two different sets of methods, so it can behave like two different types of things, it could inherit one set from class A, and use an interface B to specify the other.&lt;br /&gt;
You could then reference one of these objects with either an A reference or a B reference. Implementing an interface allows a class to become more formal about the behavior it promises to provide. Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler. If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Mixins vs Interfaces==&lt;br /&gt;
Because of this mixin feature, a developer can add arbitrary methods and modify behavior of core classes at runtime. This is amazingly powerful if you are trying to write plugins and extensions to the [http://en.wikipedia.org/wiki/Software_framework framework].  Because you can add functionality to existing objects, users can install their [http://en.wikipedia.org/wiki/Plug-in_(computing) plugin] and start taking advantage of new functionality without having to make changes to the objects that they are instantiating in their application. &lt;br /&gt;
&lt;br /&gt;
In frameworks written in other languages, such as Java, plugging in new functionality means that you need to change how your objects are instantiated. This will require code changes and/or potentially configuration changes which makes it hard to develop, hard to maintain, and a pain for plugin developers to support.  But because of the mixin feature, Rails plugin developers can customize the base objects and the users of the plugins do not have to change any of their code or configuration logic.&lt;br /&gt;
&lt;br /&gt;
===Differences===&lt;br /&gt;
&lt;br /&gt;
These is a general comparision: &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Mixins in Ruby&lt;br /&gt;
! Interfaces in Java&lt;br /&gt;
|-&lt;br /&gt;
| 'include' or 'extend' keywords used to include the module in the class.&lt;br /&gt;
| 'implement' keyword is used to implement interface&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Need not implement the module methods in the class.&lt;br /&gt;
| Should implement the interface methods. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| The methods are not copied into the class in which they are included. They are referenced.&lt;br /&gt;
| The methods’ implementation is in the class itself.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| The included method definition is decided at ‘run time’. If the definition changes while running the function points to the changed code.&lt;br /&gt;
| The definition is known compile time and will not change until the code is compiled and run again. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Modules inclusion does not provide a superclass and subclass relationship.&lt;br /&gt;
| Interface extending provides a superclass and subclass relationship. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| When we use mixins we included only a module of the class. &lt;br /&gt;
| We include the entire interface class. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Mainly for code reuse and does not provide any hierarchy. &lt;br /&gt;
| It provides a hierarchy. &lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Functionality and Behavior Comparison===&lt;br /&gt;
The functionality and behavior of Mixins in Ruby is compared with Interfaces in Java&lt;br /&gt;
&lt;br /&gt;
====Comparable Functionality====&lt;br /&gt;
Sometime we are required to compare the class members of different objects of the same class and/or sort them. This is the comparable functionality that we are trying to achieve for a given class.  This functionality can be achieved by both the Ruby using mixins and Java using interfaces. There is not much difference using both.&lt;br /&gt;
&lt;br /&gt;
'''Ruby'''&lt;br /&gt;
&lt;br /&gt;
The [http://www.ruby-doc.org/core/classes/Comparable.html Comparable] mixin is used by classes whose objects may be ordered. The class must define the &amp;lt;=&amp;gt; operator, which compares the receiver against another object, returning -1, 0, or +1 depending on whether the receiver is less than, equal to, or greater than the other object. Comparable uses &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method between?.&lt;br /&gt;
&lt;br /&gt;
  class SizeMatters&lt;br /&gt;
     include Comparable&lt;br /&gt;
     attr :str&lt;br /&gt;
     def &amp;lt;=&amp;gt;(anOther)&lt;br /&gt;
       str.size &amp;lt;=&amp;gt; anOther.str.size&lt;br /&gt;
     end&lt;br /&gt;
     def initialize(str)&lt;br /&gt;
       @str = str&lt;br /&gt;
     end&lt;br /&gt;
     def inspect&lt;br /&gt;
       @str&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
''using class:'' &lt;br /&gt;
&lt;br /&gt;
   s1 = SizeMatters.new(&amp;quot;Z&amp;quot;)&lt;br /&gt;
   s2 = SizeMatters.new(&amp;quot;YY&amp;quot;)&lt;br /&gt;
   s3 = SizeMatters.new(&amp;quot;XXX&amp;quot;)&lt;br /&gt;
   s4 = SizeMatters.new(&amp;quot;WWWW&amp;quot;)&lt;br /&gt;
   s5 = SizeMatters.new(&amp;quot;VVVVV&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
''output:''&lt;br /&gt;
   s1 &amp;lt; s2                       #=&amp;gt; true&lt;br /&gt;
   s4.between?(s1, s3)           #=&amp;gt; false&lt;br /&gt;
   s4.between?(s3, s5)           #=&amp;gt; true&lt;br /&gt;
   [ s3, s2, s5, s4, s1 ].sort   #=&amp;gt; [Z, YY, XXX, WWWW, VVVVV]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Java'''&lt;br /&gt;
&lt;br /&gt;
In Java, comparable functionality is achieved by extending the “Comparable” interface. List of objects that implement this interface can be sorted automatically by sort method of the list interface. This interface has compareTo() method that is used by the sort() method of the list. Here compareTo() method can also be used to compare two objects with respect to the implementation in compareTo() implementation in the derived class. It returns ‘0’ , ‘1’ and ‘-1’ corresponding to whether one is equal to , greater than or lesser than other.&lt;br /&gt;
In this example code Employee class is implementing Comparable interface and have method compareTo().&lt;br /&gt;
  public class Employee implements Comparable {&lt;br /&gt;
    int EmpID;&lt;br /&gt;
    String Ename;&lt;br /&gt;
    double Sal;&lt;br /&gt;
    static int i;&lt;br /&gt;
    public Employee() {&lt;br /&gt;
        EmpID = i++;&lt;br /&gt;
        Ename = &amp;quot;dont know&amp;quot;;&lt;br /&gt;
        Sal = 0.0;&lt;br /&gt;
    }&lt;br /&gt;
    public Employee(String ename, double sal) {&lt;br /&gt;
        EmpID = i++;&lt;br /&gt;
        Ename = ename;&lt;br /&gt;
        Sal = sal;&lt;br /&gt;
    }&lt;br /&gt;
    public String toString() {&lt;br /&gt;
        return &amp;quot;EmpID &amp;quot; + EmpID + &amp;quot;\n&amp;quot; + &amp;quot;Ename &amp;quot; + Ename + &amp;quot;\n&amp;quot; + &amp;quot;Sal&amp;quot; + Sal;&lt;br /&gt;
    }&lt;br /&gt;
    public int compareTo(Object o1) {&lt;br /&gt;
        if (this.Sal == ((Employee) o1).Sal)&lt;br /&gt;
            return 0;&lt;br /&gt;
        else if ((this.Sal) &amp;gt; ((Employee) o1).Sal)&lt;br /&gt;
            return 1;&lt;br /&gt;
        else&lt;br /&gt;
            return -1;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
ComparableDemo.java is showing the use of this interface. This class first makes a list of objects of type Employee and call sort method of java.util.Collections, which internally uses compareTo() method of Employee class and sort the list accordingly.&lt;br /&gt;
 &lt;br /&gt;
  import java.util.*;&lt;br /&gt;
  public class ComparableDemo{&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
        List ts1 = new ArrayList();&lt;br /&gt;
        ts1.add(new Employee (&amp;quot;Tom&amp;quot;,40000.00));&lt;br /&gt;
        ts1.add(new Employee (&amp;quot;Harry&amp;quot;,20000.00));&lt;br /&gt;
        ts1.add(new Employee (&amp;quot;Maggie&amp;quot;,50000.00));&lt;br /&gt;
        ts1.add(new Employee (&amp;quot;Chris&amp;quot;,70000.00));&lt;br /&gt;
        Collections.sort(ts1);&lt;br /&gt;
        Iterator itr = ts1.iterator();&lt;br /&gt;
        while(itr.hasNext()){&lt;br /&gt;
            Object element = itr.next();&lt;br /&gt;
            System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
''Output:''&lt;br /&gt;
  &lt;br /&gt;
  EmpID 1&lt;br /&gt;
  Ename Harry&lt;br /&gt;
  Sal    20000.0&lt;br /&gt;
  EmpID 0&lt;br /&gt;
  Ename Tom&lt;br /&gt;
  Sal    40000.0&lt;br /&gt;
  EmpID 2&lt;br /&gt;
  Ename Maggie&lt;br /&gt;
  Sal    50000.0&lt;br /&gt;
  EmpID 3&lt;br /&gt;
  Ename Chris&lt;br /&gt;
  Sal    70000.0&lt;br /&gt;
&lt;br /&gt;
====Singleton Behaviour====&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Singleton_pattern Singleton] is a class guaranteed to have only a single instance, with a single global access point to that instance. It can be implemented in Ruby and Java. In Ruby we can include a module to acquire all the functionality of the singleton module. The code is already there and we are just reusing the code and adding that similar functionality into our class. Here our intention is to add the functionality of singleton to the class which is achieved using mixins. In java, implementation of interfaces means that the class which implements the interface becomes a type of that class, which is not the intention here. So, there is not interface which class can implement to get the functionality in itself. It has to implement the functionality itself without implementing any interface. &lt;br /&gt;
&lt;br /&gt;
'''Ruby'''&lt;br /&gt;
&lt;br /&gt;
When you include the singleton method inside your class it makes the new method private so that you cannot use it. Including ‘singleton’ adds a class method called instance that instantiates only one instance of the class&lt;br /&gt;
&lt;br /&gt;
  # require singleton lib&lt;br /&gt;
  require 'singleton'&lt;br /&gt;
  class AppConfig&lt;br /&gt;
    # mixin the singleton module&lt;br /&gt;
    include Singleton&lt;br /&gt;
    # do the actual app configuration&lt;br /&gt;
    def load_config(file)&lt;br /&gt;
      # do your work here&lt;br /&gt;
      puts &amp;quot;Application configuration file was loaded from file: #{file}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
''output:''&lt;br /&gt;
&lt;br /&gt;
  conf1 = AppConfig.instance&lt;br /&gt;
  conf1.load_config &amp;quot;/home/khelll/conf.yml&amp;quot;       #=&amp;gt;Application configuration file was loaded from file: /home/khelll/conf.yml&lt;br /&gt;
  conf2 = AppConfig.instance&lt;br /&gt;
  puts conf1 == conf2      #=&amp;gt;true&lt;br /&gt;
  # notice the following 2 lines won’t work&lt;br /&gt;
  AppConfig.new rescue(puts $!)   #=&amp;gt; new method is private&lt;br /&gt;
&lt;br /&gt;
====Enumerable Behaviour====&lt;br /&gt;
This [http://www.ruby-doc.org/core/classes/Enumerable.html enumerable] functionality adds support to the array members of the class. We can achieve this functionality using mixins in Ruby while it is not possible to do it in Java using interfaces.&lt;br /&gt;
&lt;br /&gt;
'''Implementation in Ruby:''' &lt;br /&gt;
&lt;br /&gt;
It adds the functionality of map, inject, select etc to the class which includes the ‘Enumerable’ module. The class which includes should represent a collection of items. Enumerable requires that your class contain an each method that serves up the items in the collection. All the other Enumerable methods rely on this. &lt;br /&gt;
Example:&lt;br /&gt;
This is a Team class that manages a group of members:&lt;br /&gt;
&lt;br /&gt;
  class Team&lt;br /&gt;
    include Enumerable&lt;br /&gt;
    attr_accessor :members&lt;br /&gt;
    def initialize&lt;br /&gt;
      @members = []&lt;br /&gt;
    end&lt;br /&gt;
    def each &amp;amp;block&lt;br /&gt;
      @members.each{|member| block.call(member)}&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Enumerable requires that your class contain an each method that serves up the items in the collection. All the other Enumerablemethods rely on this. Now we can use the map method, for instance:&lt;br /&gt;
&lt;br /&gt;
  team = Team.new&lt;br /&gt;
  team.members = ['joshua', 'gabriel', 'jacob']&lt;br /&gt;
  team.map{|member| member.capitalize}   # =&amp;gt; [&amp;quot;Joshua&amp;quot;, &amp;quot;Gabriel&amp;quot;, &amp;quot;Jacob&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
Now we can call any Enumerable methods on our team object itself, and it will assume we want to work with the members array within.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Data Mapper Behaviour====&lt;br /&gt;
&lt;br /&gt;
[http://datamapper.org/docs/ DataMapper] is an API which comes with the ability to use the same API to talk to a multitude of different datastores. There are adapters for the usual RDBMS suspects, NoSQL stores, various file formats and even some popular webservices. This is implemented in ruby using mixins. We need not use interfaces to implement.&lt;br /&gt;
&lt;br /&gt;
'''Implementation in Ruby:'''&lt;br /&gt;
  class Post&lt;br /&gt;
       include DataMapper::Resource&lt;br /&gt;
       property :id, Serial&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
The point to take away from this is not that Ruby Mixins are more powerful than Java Interfaces. The mixin feature of Ruby allows for the development of easy to use but powerful plugins that will be hard for any non-Ruby based framework to compete with. Java has many other ways of implementing the functionality mixins are capable of. It is just that it does not use Interfaces to achieve the functionality. Even though both of them are a way of achieving multiple inheritance in their respective languages, they achieve quiet different goals. Inheritance in Java is mainly to overcome the confusion which arises with multiple inheritance, while Mixins in Ruby is mainly to add an additional functionality to the existing class apart from the ‘primary functionality’ of the class. &lt;br /&gt;
&lt;br /&gt;
When a class implements Interface, it indicates that it is of the type interface class and forms a hierarchy with the interface class, while ruby mixins are mainly intended for code reuse and including an additional functionality to the class. It does not form any hierarchy as it is only using module of the class. &lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
==References==&lt;br /&gt;
* http://en.wikipedia.org/wiki/Interface_(Java)&lt;br /&gt;
* http://en.wikipedia.org/wiki/Mixin&lt;br /&gt;
* http://carboni.ca/blog/p/Modules-How-Do-They-Work&lt;br /&gt;
* http://rubylearning.com/satishtalim/modules_mixins.html&lt;br /&gt;
* http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/&lt;br /&gt;
* http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-&lt;br /&gt;
* http://datamapper.org/&lt;br /&gt;
* http://www.dsource.org/projects/tutorials/wiki/SingletonPattern &lt;br /&gt;
* http://corelib.rubyonrails.org/classes/Comparable.html&lt;br /&gt;
* http://kconrails.com/2010/11/30/ruby-enumerable-primer-part-1-the-basics/&lt;br /&gt;
* http://stackoverflow.com/questions/575920/is-a-ruby-module-equivalent-to-a-java-interface&lt;/div&gt;</summary>
		<author><name>Sthirum</name></author>
	</entry>
</feed>