phani.galla
Joined: 08 Jul 2010 Posts: 7
|
Posted: Thu Jul 22, 2010 10:41 am Post subject: Polymorphism |
|
|
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 |
|