Senior & Expert RoR Developers Discussion Forum by Nyros Technologies

HIRE Ruby on Rails Expert Developers Programmers Coders From India
Ruby on Rails PHP .Net Developers Community, Nyros Technologies, Kakinada
 
Log in  or IF not a member please REGISTER
Username:
Password:   


Keyword
Log in | Profile 

Polymorphism

 
Post new topic   Reply to topic    Senior & Expert RoR Developers Discussion Forum by Nyros Technologies Index -> A little Ruby programming (Basic programming in RoR)
View previous topic :: View next topic  
Author Message
phani.galla



Joined: 08 Jul 2010
Posts: 7

PostPosted: Thu Jul 22, 2010 10:41 am    Post subject: Polymorphism Reply with quote

Hi friends!
Here i am going to share some basics of "Polymorphism" in Ruby.

What is Polymorphism?
Polymorphism-the name itself indicates many(poly) forms(morph).
Polymorphism is the ability/character to allow a variable,a function or a object to have more than one form.

Now let us discuss more with an example.

*************************************************************

#Lets us take Person class as the super class
class Person

#initialize the class with required variables.
def initialize(name,age)
@name = name #name of the person of a class.
@age = age #age of the same person of same class.
end

#now lets take a method with name pname
def pname
"Hi I am a person with some name\
and with some age."
end

end

#This is a student class,where Person is the super class for it.
class Student < Person

#Take a method with the same name(pname) as in main class.
def pname
"Hi my Name is #{@name}.\
I am a student.\
I am #{@age} years old."
end

end

#This is an Employee class,where Person is the super class for it.
class Employee < Person

#Take a method with the same name(pname) as in main class.
def pname(salary)
"Hi my Name is #{@name}.\
I am an employee.\
I am #{@age} years old.\
My salary is {salary}"
end
end

#This is a Unemployee class,where Person is the super class for it.
class UnEmployee < Person

#Take a method with the same name(pname) as in main class.
def pname
"Hi my name is #{@name}.\
I am looking for a job.\
I am #{@age} years old."
end

end

#create an object to Student class and pass the required variables.
per = Student.new("Jhon",16)
puts per.pname
#create an object to Employee class and pass the required variables.
per = Employee.new("Harry",24)
puts per.pname(45000)
#create an object to UnEmployee class and pass the required variables.
per = UnEmployee.new("Smith",22)
puts per.name
#create an object to main class Person and pass the required variables.
per = Person.new("hi",44)
#It displays the method in super class
puts per.pname

*************************************************************

In the above example i have taken Person class as a super class and it is inherited by three different classes named Student, Employee and UnEmployee.

Now these classes are child classes of a Person class.

I have taken a method with same name pname in super class as well as in child classes.
When we create an object to child class the pname method in super class is overridden by the child class method pname.
Here the method names in both super class and child class are same but their implementation is different.

Where as in Employee class i have taken method with same name but with passing an argument. Here the method is overloaded, method names are same but with different signatures.

I have taken same name for object variables of different classes.

Thus Polymorphism allows to perform different forms of a variable,function or an object.

Thank you,
Phani Kumar
Back to top
View user's profile Send private message
umamahesh_nyros



Joined: 17 Jul 2007
Posts: 231
Location: KAKINADA

PostPosted: Tue Jul 27, 2010 3:27 pm    Post subject: Reply with quote

Hi,

Good explanation ...
Back to top
View user's profile Send private message Send e-mail Yahoo Messenger
anand



Joined: 31 Aug 2010
Posts: 3

PostPosted: Tue Aug 31, 2010 4:08 am    Post subject: Reply with quote

Thank you. Very Happy
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Senior & Expert RoR Developers Discussion Forum by Nyros Technologies Index -> A little Ruby programming (Basic programming in RoR)
Page 1 of 1

 latest topics 
 Topics   Replies   Author   Views   Last Post 
No new posts Chat utility in a ROR application
0 shobana 2 Thu Sep 09, 2010 9:45 am
shobana View latest post
No new posts How to customize Google Maps InfoWindow using ym4r_gm
0 kalyanallampalli 54 Thu Sep 02, 2010 5:34 pm
kalyanallampalli View latest post
No new posts Drag and Drop file upload
0 umamageshwari 51 Wed Sep 01, 2010 9:36 am
umamageshwari View latest post
No new posts Flex On Rails
0 Raghu 48 Wed Sep 01, 2010 3:00 am
Raghu View latest post
No new posts MongoDB
0 Raghu 45 Tue Aug 31, 2010 3:30 pm
Raghu View latest post
No new posts Problem with Dreamweaver / java error
3 Raja 37 Tue Aug 31, 2010 3:24 pm
Raghu View latest post
No new posts Generating Model
0 phani.galla 44 Tue Aug 31, 2010 2:09 pm
phani.galla View latest post
No new posts Hide Panels using JQuery
0 Anu 48 Tue Aug 31, 2010 1:50 pm
Anu View latest post
No new posts Load a text file using ajax in jquery
0 Anu 38 Tue Aug 31, 2010 1:39 pm
Anu View latest post
No new posts Generating Migrations
0 phani.galla 31 Tue Aug 31, 2010 1:38 pm
phani.galla View latest post




Hire an expert Ruby on Rails developer / coder / programmer or development team from India now!!

Other Forums : PHP   ::   .Net   |   Free unlimited HTML CSS templates download

Nyros Technologies   |   Kakinada City Portal   |   Developers Blog   |   About Ruby on Rails Experts   |   More