What is polymorphism ? Explain with examples

Answer Posted / jun jitendra

"Poly" means "many" and "morph" means "form". Polymorphism is the ability of an object (or reference) to assume (be replaced by) or become many different forms of object. Example: function overloading, function overriding, virtual functions. Another example can be a plus ‘+’ sign, used for adding two integers or for using it to concatenate two strings.
//method hiding in polymorphism
using System;
namespace Polymorphism
{
class A
{
public void Foo() { Console.WriteLine("A::Foo()"); }
}

class B : A
{
public new void Foo() { Console.WriteLine("B::Foo()"); }
}

class Test
{
static void Main(string[] args)
{
A a;
B b;

a = new A();
b = new B();
a.Foo(); // output --> "A::Foo()"
b.Foo(); // output --> "B::Foo()"

a = new B();
a.Foo(); // output --> "A::Foo()"
}
}
}

Is This Answer Correct ?    4 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Are polymorphisms mutations?

705


Can you explain polymorphism?

584


What is polymorphism used for?

577


How do you define social class?

604


What is abstraction in oops?

592






what is graphics

2013


What is the difference between abstraction and polymorphism?

619


What is destructor in oop?

627


Describe these concepts: Polymorphism, Inheritance and Abstraction.

616


Templates mean

1592


What are the 4 pillars of oop?

674


What is difference between multiple inheritance and multilevel inheritance?

604


What are benefits of oop?

641


what are the different types of qualifier in java?

1847


Why is there no multiple inheritance?

572