In this chapter you will learn:
- What is Runtime Polymorphism?
- How to implement it in program?
What is Runtime Polymorphism?
Runtime Polymorphism is also known as Dynamic Polymorphism,
Method overriding etc. Whereas in static polymorphism we overload a function; in dynamic polymorphism we override a base class function using virtual or override keyword. Method overriding means having two or more methods with the same name, same signature but with different implementation.
static void Main(string args)
Chocolate ch = new Chocolate();
Console.WriteLine("Class : Icecream");
public virtual void flavor()
Console.WriteLine("IceCream Type : Vanilla");
class Chocolate : IceCream
Console.WriteLine("Class : Chocolate");
public override void flavor()
Console.WriteLine("IceCream Type : Chocolate");
Class : Chocolate
IceCream Type : Chocolate
- If base class function is marked as virtual then it is compulsory to add override keyword in derived class function.
- Virtual method allows child class to their own implementation of derived methods.
- Virtual method cannot be declared as private.
You can also learn more about virtual method and override keyword here:
In this chapter you learned Runtime Polymorphism implementation using Virtual and Override keyword. In the next chapter some programming examples are there that will help you to understand polymorphism clearly.