Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

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

What are the benefits of oop?

1165


What is abstraction in oops with example?

1217


INSTANCE FIELDS DECLARED private ARE ACCESSIBLE BY THE METHODS ONLY.CAN WE CHANGE THE private FIELD OF AN OBJECT IN A METHOD OF SOME OTHER OBJECT OF THE SAME CLASS?

2146


IS IT NECESSARY TO INITIALIZE VARIABLE? WHAT IF THE INSTANCE VARIABLE IS DECLARED final ? IS IT NECESSARY TO INITIALIZE THE final VARIABLE AT THE TIME OF THEIR DECLARATION?

2021


what is difference between class template and template class?

2596


What is polymorphism explain?

1192


Why is encapsulation used?

947


What is methods in oop?

925


can we make game by using c

4035


hi all..i want to know oops concepts clearly can any1 explain??

2081


when to use 'mutable' keyword and when to use 'const cast' in c++

2110


What is destructor example?

989


class type to basic type conversion

2338


What is object and class in oops?

1004


Write a program to reverse a string using recursive function?

2275