InterviewSolution
| 1. |
What is the difference between compile time polymorphism and run-time polymorphism? |
|
Answer» Compile time POLYMORPHISM MEANS we will declare methods with same NAME but different signatures because of this we will perform different tasks with same method name. This compile time polymorphism also called as early binding or method overloading. What is the difference between compile time polymorphism and run-time polymorphism? Example: public class MyClass { public int Add(int a, int b) { return a + b; } public int Add(int a, int b, int c) { return a + b + c; } }Run time polymorphism also called as late binding or method overriding or DYNAMIC polymorphism. Run time polymorphism or method overriding means same method names with same signatures. In this run time polymorphism or method overriding we can override a method in base class by creating similar function in derived class this can be achieved by using inheritance principle and using “virtual & override” keywords. Example: public class Bclass { public virtual void Sample1() { Console.WriteLine("Base Class"); } } public class DClass : Bclass { public override void Sample1() { Console.WriteLine("Derived Class"); } } class Program { static void Main(string[] args) { DClass objDc = NEW DClass(); objDc.Sample1(); Bclass objBc = new DClass(); objBc.Sample1(); } } |
|