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
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.
Where is pseudocode used?
Write A Program to find the ambiguities in Multiple Inheritance? How are they resolved.(Virtual Functions)
What is for loop and its syntax?
Is data hiding and abstraction same?
What is super in oop?
what is different between oops and c++
Why it is called runtime polymorphism?
Why is polymorphism used?
What does no cap mean?
write a program to enter a string like"sunil is a good boy and seeking for a job" not more than 10 characters including space in one line,rest characters should b in other line.if the next line starts from in between the previous word,then print whole word to next line.
Can we create object of interface?
What is abstraction example?
Following are the class specifications: class {int a}; class {int b}; Using friend funtion,calculate the max of two objects and display it.
Question: Implement a base class Appointment and derived classes Onetime, Daily, Weekly, and Monthly. An appointment has a description (for example, “see the dentist”) and a date and time. Write a virtual function occurs_on(int year, int month, int day) that checks whether the appointment occurs on that date. For example, for a monthly appointment, you must check whether the day of the month matches. Then fill a vector of Appointment* with a mixture of appointments. Have the user enter a date and print out all appointments that happen on that date.