Dynamic Polymorphism in C# with Programming Example

dynamic_polymorphism
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, Late Binding, 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.

Programming Example

  • Output

    Class : Icecream
    Class : Chocolate
    IceCream Type : Chocolate
    _

    GuideLine:

    1. If base class function is marked as virtual then it is compulsory to add override keyword in derived class function.
    2. Virtual method allows child class to their own implementation of derived methods.
    3. Virtual method cannot be declared as private.

    You can also learn more about virtual method and override keyword here:

    http://www.completecsharptutorial.com/basic/c-abstract-and-virtual-method-inheritance-tutorial-with-code/

    Summary

    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.


    2 thoughts on “Dynamic Polymorphism in C# with Programming Example”

    1. If you want your derived member to have the same name as a member in a base class, but you do not want it to participate in virtual invocation, you can use the new keyword. The new keyword is put before the return type of a class member that is being replaced.
      just like following code:-

    2. What will be the output of the following program, Lets try yourself to know how deep your in this concept?

      Dare challenge:-

    Leave a Reply

    Your email address will not be published. Required fields are marked *