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
How to call a non virtual function in the derived class by using base class pointer
Why is it so that we can have virtual constructors but we cannot have virtual destructors?
What is basic concept of oop?
#include
What is byval and byref? What are differences between them?
What is difference between oop and pop?
What are classes oop?
Can a varargs method be overloaded?
• What are the desirable attributes for memory managment?
What is encapsulation in oop?
What is polymorphism in oops with example?
write a program that takes input in digits and display the result in words from 1 to 1000
What is Difference Between Inheritance and creating object and getting data? means Class A extends B{ B.getMethod();} (OR) Class A{ b obj=new B(); obj.getMethod(); }
What is encapsulation with example?
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?