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
What is balance factor?
What is overloading in oop?
What is advantage of inheritance?
What is object in oop?
Why is destructor used?
What is abstract class in oops?
What is the point of polymorphism?
What is polymorphism give a real life example?
explain sub-type and sub class? atleast u have differ it into 4 points?
What is abstraction in oops with example?
Explain virtual inheritance?
Describe these concepts: Polymorphism, Inheritance and Abstraction.
Why is abstraction used?
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?
What is encapsulation and abstraction? How are they implemented in C++?