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
Are polymorphisms mutations?
Can you explain polymorphism?
What is polymorphism used for?
How do you define social class?
What is abstraction in oops?
what is graphics
What is the difference between abstraction and polymorphism?
What is destructor in oop?
Describe these concepts: Polymorphism, Inheritance and Abstraction.
Templates mean
What are the 4 pillars of oop?
What is difference between multiple inheritance and multilevel inheritance?
What are benefits of oop?
what are the different types of qualifier in java?
Why is there no multiple inheritance?