In this article, I describe what is polymorphism and when we use polymorphism in asp.net. polymorphism is one of the object oriented programming. It allows you to invoke derived class methods through abase class reference during runtime. To achieve this polymorphism we have to declare virtual keyword in base class, and override same method in derived class.
The virtual keyword indicates, the method can be overridden in any derived class.
class ShiftTime
{
public virtual void DisplayShiftTime()
{
Console.WriteLine("Shifttime");
}
}
class GeneralShift : ShiftTime
{
public override void DisplayShiftTime()
{
Console.WriteLine("GeneralShift time");
}
}
class NightShift : ShiftTime
{
public override void DisplayShiftTime()
{
Console.WriteLine("NightShift time");
}
}
public static void Main()
{
ShiftTime[] mShiftTime = new ShiftTime[3];
mShiftTime[0] = new ShiftTime();
mShiftTime[1] = new GeneralShift();
mShiftTime[2] = new NightShift();
foreach(ShiftTime m in mShiftTime)
{
m.DisplayShiftTime();
}
Console.ReadLine();
}
Description
In this above demonstration, I have written types of shift time. Here I create three class such as ShiftTime, GeneralShift and NightShift. ShiftTime is a base class and others derived class. We invoke a method DisplayShiftTime from a base class using virtual keyword and use override keyword in derived class .
Post your comments / questions
Recent Article
- How to hide the ID field from the Django admin?
- It is impossible to add a non nullable field without specifying a default. Django error
- ImportError: cannot import name 'url' from 'django.conf.urls' - Django Error
- How to Enable Virtualization in BIOS Security Settings in Intel Processors For Android Studio?
- Dependency 'androidx.activity:activity:1.8.0' requires libraries and applications that depend on it.
- AttributeError: 'NoneType' object has no attribute 'get_text' - Python
- ModuleNotFoundError: No module named 'openpyxl' - Python
- How to get thumbnail from vimeo video URL in Python?
Related Article