Skip to main content

OOP Polymorphism

Definition: "Poly" stands for "many" and "morph" stands for "forms". Generally, polymorphism means one name different uses. Technically, it means being able to send the same message to different objects and get different result.

Polymorphism through Inheritance
We can achieve polymorphism through inheritance. For example

class GenericParser
 def parse
   raise NotImplementedError, 'You must implement the parse method'
 end
end

class JsonParser < GenericParser
 def parse
   puts 'An instance of the JsonParser class received the parse message'
 end
end

class XmlParser < GenericParser
 def parse
   puts 'An instance of the XmlParser class received the parse message'
 end
end

Here the GenericParser is the base class. Class JsonParser and XmlParser are inherited from the GenericParser.

Now suppose we run the below code.

puts 'Using the XmlParser'
parser = XmlParser.new
parser.parse

puts 'Using the JsonParser'
parser = JsonParser.new
parser.parse

The output of the above code will be as following.

OUTPUT:
Using the XmlParser
An instance of the XmlParser class received the parse message

Using the JsonParser
An instance of the JsonParser class received the parse message

Here we can see that the same parse method behaves differently for other classes based upon it is called form which object.

               
OOP Polymorphism with DuckTyping
In Ruby, traditional methods of polymorphism (seen above and in languages such as Java) are not widely accepted. A concept known as "duck typing" is preferred.

class GenericParser
  def parse(parser)
    parser.parse
  end
end

class XmlParser
  def parse
    puts 'An instance of the XmlParser class received the parse message'
  end
end

class JsonParser
  def parse
    puts 'An instance of the JsonParser class received the parse message'
  end
end

Here we do the same thing now without inheritance. We write the code as below.

parser = GenericParser.new
puts 'Using the XmlParser'
parser.parse(XmlParser.new)

puts 'Using the JsonParser'
parser.parse(JsonParser.new)
The output of the code is as below.

OUTPUT:

Using the XmlParser
An instance of the XmlParser class received the parse message

Using the JsonParser
An instance of the JsonParser class received the parse message

Here we can see that the same method parser.parse behaves differently on the basis of the which of object is passed as a parameter. It calls particular parse method on the basis of which type of object is passed.

Notice that the method behaves differently depending on the object that receives it’s message. This is polymorphism!

References:





Comments

Popular posts from this blog

5 Useful Android Apps

There are many apps available for Android but these are some useful apps which I use regularly.
1. Money View: Financial Planning
Description: With the Free Money View app, you get a real-time visibility into your entire Personal Finances. It works by itself without any manual data entry - their daily expense manager app organizes our financial summary by analyzing SMS sent by our bank and billers to our phone. The app auto-tracks your expense, bills and account balances across all our financial accounts to help us stay on top of our money. The app is highly secure as it never reads any sensitive data - no full bank account numbers, no OTP, and no Netbanking login/password.
As it anaylzes the money transactions via the SMS in our phone it is highly useful app for tracking our expenses, so we can know our money is going in which direction so we can control it.




More information:  https://play.google.com/store/apps/details?id=com.whizdm.moneyview&hl=en

2. Any.do To-do List | Task List
Descr…

List of Common mistakes in Ruby

Always use spaces around operators, after commas, colons and semicolons, around { and before }. White space might be (mostly) irrelevant to the Ruby interpreter, but its proper use is the key to writing easily readable code. Also it makes the design more readable and code much cleaner.
product = 1 * 2 array = [1, 2, 3, 4, 5] array.map { |a| a + 2 }

There should be no spaces after (, [ or before ], ) these brackets.
['ankur', 'vyas'] sum(a, b)
Also don't use spaces in while providing the range.
Use5..9 over 5 .. 9Use'a'..'z' over 'a' .. 'z'
When using switch case statements use the following indentation.
casewhen input = '+'puts'The operation is addition'when'-'puts'The operation is subtraction'when'*'puts'The operation is multiplication'elseputs'The operation is division'end
Use following indentation if the parameters exceeds to more than one line.
defsend_mail(source) Mailer.deliver(…