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

Where You Can Use Interface in your Project

1419


They started with the brief introduction followed by few basic C++ questions on polumorphism, inheritance and then virtual functions. What is polymorphims? How you will access polymorphic functions in C? How virtual function mechanism works?

1402


Why it is called runtime polymorphism?

573


What is this pointer in oop?

548


What is abstraction and encapsulation?

564






What is the difference between a constructor and a destructor?

604


Objective The objective of this problem is to test the understanding of Object-Oriented Programming (OOP) concepts, in particular, on encapsulation. Problem Description Create a program for managing customer’s bank accounts. A bank customer can do the following operations: 1. Create a new bank account with an initial balance. 2. Deposit money into his/her account. 3. Withdraw money from his/her account. For this operation, you need to output “Transaction successful” if the intended amount of money can be withdrawn, otherwise output “Transaction unsuccessful” and no money will be withdrawn from his/her account. Input The input contains several operations and is terminated by “0”. The operations will be “Create name amount”, “Deposit name amount”, or “Withdraw name amount”, where name is the customer’s name and amount is an integer indicating the amount of money. There will be at most 100 bank accounts and they are all created on the first month when the bank is opening. You may assume that all account holders have unique names and the names consist of only a single word. Output The output contains the transaction result of withdrawal operations and the final balance of all customers after some withdrawal and deposit operations (same order as the input). Sample Input Create Billy 2500 Create Charlie 1000 Create John 100 Withdraw Charlie 500 Deposit John 899 Withdraw Charlie 1000 0

1693


What is encapsulation in simple terms?

533


How to handle exception in c++, For example in a functions i am assigning memory to some variables and also in next instructions in am dividing one variable also. If this functions generates a error while allocating memory to those variable and also while dividing the variable if i divide by zero then catch block how it will identify that this error has cone from perticular instruction

1650


What is oops?what is its use in software engineering?

553


What is the point of oop?

644


What is the important feature of inheritance?

628


How can you overcome the diamond problem in inheritance?

761


There are two base class B1,B2 and there is one class D which is derived from both classes, Explain the flow of calling constructors and destructors when an object of derived class is instantiated.

1448


write a code for this. serial_number contained in the header of the file will be read , if this serial number is less than a previous serial number within a successfully processed file, or is the same as another serial number within a successfully processed file, or if the field contains anything other than 7 digits, then the file must error with the reason ‘Invalid SERIAL_NUMBER’.

1771