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
What is the real life example of polymorphism?
This program numbers the lines found in a text file. Write a program that reads text from a file and outputs each line preceded by a line number. Print the line number right-adjusted in a field of 3 spaces. Follow the line number with a colon, then one space, then the text of the line. You should get a character at a time and write code to ignore leading blanks on each line. You may assume that the lines are short enough to fit within a line on the screen. Otherwise, allow default printer or screen output behavior if the line is too long (i.e., wrap or truncate). A somewhat harder version determines the number of spaces needed in the field for the line numbers by counting lines before processing the lines of the file. This version of the program should insert a new line after the last complete word that will fit within a 72-character line.
What is polymorphism and example?
How do you achieve polymorphism?
any one please tell me the purpose of operator overloading
What is the diamond problem in inheritance?
write a code for this:trailer recordId contains a value other than 99, then the file must error with the reason ‘Invalid RECORD_ID’(User Defined Exception).
What is the important feature of inheritance?
What is polymorphism used for?
Explain the concepts involved in Object Oriented programming.
What does oop mean in snapchat?
What is inheritance write a program to show use of inheritance?
What is a class in oop?
What is the full form of oops?
write a program that takes input in digits and display the result in words from 1 to 1000