Difference between compile time and runtime polymorphisms
d
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
Akhil MittalRank #133 · 16.9K reputationPosted Aug 1, 2013, 8:02 AM EDT
Polymorphism means that functions assume different forms at different times. In case of compile time it is called function overloading. For example, a program can consist of two functions where one can perform integer addition and other can perform addition of floating point numbers but the name of the functions can be same such as add. The function add() is said to be overloaded. Two or more functions can have same name but their parameter list should be different either in terms of parameters or their data types. The functions which differ only in their return types cannot be overloaded. The compiler will select the right function depending on the type of parameters passed. In cases of classes constructors could be overloaded as there can be both initialized and uninitialized objects. Here is a program which illustrates the working of compile time function overloading and constructor overloading.
eg.
namespace CommonClasses { public interface IAnimal { string Name { get; } string Talk(); } } // Assembly: Animals using System; using CommonClasses; namespace Animals { public abstract class AnimalBase { public string Name { get; private set; } protected AnimalBase(string name) { Name = name; } } public class Cat : AnimalBase, IAnimal { public Cat(string name) : base(name) { } public string Talk() { return "Meowww!"; } } public class Dog : AnimalBase, IAnimal { public Dog(string name) : base(name) { } public string Talk() { return "Arf! Arf!"; } } } // Assembly: Program // References and Uses Assemblies: Common Classes, Animals using System; using System.Collections.Generic; using Animals; using CommonClasses; namespace Program { public class TestAnimals { // prints the following: // Missy: Meowww! // Mr. Bojangles: Meowww! // Lassie: Arf! Arf! // public static void Main(string[] args) { var animals = new List() { new Cat("Missy"), new Cat("Mr. Bojangles"), new Dog("Lassie") }; foreach (var animal in animals) { Console.WriteLine(animal.Name + ": " + animal.Talk()); } } } } //Taken from a blog at Wiki Answers.Found it easy to follow.Akhil MittalRank #133 · 16.9K reputationPosted Aug 1, 2013, 8:11 AM EDT
Sanjeeb LenkaRank #301 · 8.3K reputationPosted Aug 1, 2013, 9:30 AM EDT
http://www.c-sharpcorner.com/uploadfile/puranindia/polymorphism-in-C-Sharp/