What is polymorphism ? Explain with examples

Answer Posted / manjeet

POLYMORPHISM is derived from two latin words poly(means-
many) and morphs(means-forms).this concept of OOPS provides
one function to be carried out in several ways or on
several object types.
working:-The polymorphism is the ability of responding
different object in there own way to a particular
message.so,when message is sent requesting an object to do
particular function,the message names the function the
object should perform.beacause diffrent objects can have
different functions with same name,the meaning of the
message must be decided with respect to the particular
object that recieved the message.so,the same message sent
to two different objects can invoke two different functions.

example:-If a brazilian is commanded to speak(),he/she may
speak portuguese. However, if a indian is commanded to speak
(), he/she may speak hindi. They both inherit speak() from
human, but their Subclass methods override the methods of
the Superclass; this is Overriding Polymorphism and
Inheritance. Adding a walk class to human would give both
indian and brazilian object's the same walk method.

// Assembly: Common Classes
// Namespace: CommonClasses

public interface Ihuman
{
string Name
{
get;
}
string Talk();
}

// Assembly: human
// Namespace: human

public class humanBase
{
private string _name;
AnimalBase(string name)
{
_name = name;
}
public string Name
{
get
{
return _name;
}
}
}

// Assembly: human
// Namespace: human

public class indian : humanBase, Ihuman
{
public indian(String name) :
base(name)
{
}

public string Talk() {
return "hindi!";
}
}

// Assembly: human
// Namespace: human

public class brazil : humanBase, Ihuman
{
public brazil(string name) :
base(name)
{
}

public string Talk() {
return "portuguese";
}
}

// Assembly: Program
// Namespace: Program
// References and Uses Assemblies: Common Classes, human

public class Testhuman
{
// prints the following:
//
// ram: hindi!
// Mr. harsh: hindi!
// Lara: portuguese!
//
public static void Main(String[] args)
{
List<Ihuman> human = new List<Ihuman>();
human.Add(new indian("ram"));
human.Add(new indian("Mr. harsh"));
human.Add(new brazilian("Lara"));

foreach(Ihuman human in human)
{
Console.WriteLine(human.Name + ": " +
human.Talk());
}
}
}

Is This Answer Correct ?    8 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Why polymorphism is used in oops?

586


What is polymorphism and its types?

601


what is difference between class template and template class?

2161


Why multiple inheritance is not allowed?

586


String = "C++ is an object oriented programming language.An imp feature of oops is classes and objects".Write a pgm to count the repeated words from this scenario?

1944






Question: Write a program that prints a paycheck. Ask the program user for the name of the employee, the hourly rate, and the number of hours worked. If the number of hours exceeds 40, the employee is paid “time and a half”, that is, 150 percent of the hourly rate on the hours exceeding 40. Be sure to use stepwi se refine ment and break your solution into several functions. Use the int_name function to print the dollar amount of the check.

703


Why is polymorphism needed?

605


What is multilevel inheritance explain with example?

630


What is this pointer in oop?

557


write a program to enter a string like"sunil is a good boy and seeking for a job" not more than 10 characters including space in one line,rest characters should b in other line.if the next line starts from in between the previous word,then print whole word to next line.

1795


What does and I oop and sksksk mean?

655


What is the oops and benefits of oops programming?

558


Why do we use polymorphism?

582


Which is not an object oriented programming language?

544


What is difference between class and object with example?

566