OOPS Tutorials:Polymorphism

This is another important concept of object-oriented programming languages. The word 'poly' means different and 'morphs' means forms. Thus, Polymorphism means the ability to take more than one form. It means that an operation in the object-oriented programming may exhibit different behaviours in different situations or instances. In the object-oriented programming, it is treated as the ability of objects to have many methods of the same name, but with different forms.

The different behaviour depends on the data types used in the given operation. Polymorphism is extensively used in implementing the inheritance concept of OOPS.

Polymorphism allows you to invoke derived class methods through a base class reference during run-time. This is very much useful in the case when you need to assign a group of objects to an array and then invoke each of their methods.

Examples:

using System;

public class Vehicle
{
public virtual void Vehicle()
{
Console.WriteLine("A vehicle is for transportation.");
}
}

using System;

public class Car : Vehicle
{
public override void Vehicle()
{
Console.WriteLine("This is a Car.");
}
}

public class Byke: Vehicle
{
public override void Vehicle()
{
Console.WriteLine("This is a Vehicle.");
}
}

Prepare for interviews with the best Interview Questions at bestdotnettraining.

Comments

  1. Thanks for sharing your info. I really appreciate your efforts and I will be waiting for your further write ups thanks once again.
    Vee Eee Technologies| Vee Eee Technologies

    ReplyDelete
  2. This was a nice post and I think it is pretty easy to see from the other comments as well that this post is well written and informative. Thanks a lot.
    source: www.wbupdates.com

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete

Post a Comment