-7

Please clear about run time polymorphism

Is this true?

To achieve runtime polymorphism in c# we use Interface?

I have already gone through all the posts on SO. answers are not straight forward its contradictory. I expect answer in YES / NO please help me in that

Community
  • 1
  • 1
Neo
  • 15,491
  • 59
  • 215
  • 405
  • 1
    Answered [here](http://stackoverflow.com/questions/8701345/learning-to-use-interfaces-effectively)? – PakkuDon Jan 05 '14 at 08:24
  • yes i have already gone through all the posts on SO. answers are not straight forward its contradictory. I expect answer in YES / NO please help me in that. – Neo Jan 05 '14 at 08:30
  • 1
    @user1723204 yes, there is compile-time polymorphism and runtime polymorphism – Leo Jan 05 '14 at 08:33
  • I had to vote down your question. The only thing contradictory is you. You stated that you want yes or no answers...well, that's been done already...check the answers. Then you are explicitly asking for code examples by adding comments to the answers...what is it exactly what you want? – Leo Jan 05 '14 at 09:23

2 Answers2

3

Well, it's not a false statement. You can achieve dynamic/runtime polymorphism (late binding) using interfaces but you can also use base classes or abstract classes where the underlying concrete type is determined at runtime. An example of late binding. ..

Supposed you have the following interface

interface IOrder
{
    void ProcessOrder(int orderId);
    void ProcessOrder(int orderId, int userId);
 }

Then you have an order object that implements this interface...

public StoreAOrder : IOrder {...}

And then you use dependency injection to process an order...

public class OrderProcessor
{
    private IOrder order;

    public OrderProcessor(IOrder _order)
    {
        order = _order;
     }


    public void Process()
    {
        order.ProcessOrder(id, User.Current.Id);
     }
}

Notice that at compile timw the OrderProcessor doesn't really know what is the concrete class that implements IOrder. Furthermore, it doesn't know anything about the overloads that the underlying object exposes. This is what is called runtime polymorphism.

Leo
  • 14,625
  • 2
  • 37
  • 55
1

You don't have to define seperate interfaces for this. You can also use a baseclass to achieve polymorphism.

For more info check this page:

Polymorphism - C# programming

Example of using polymorphism in C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Animals
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Animal> animals = new List<Animal>();
            animals.Add(new Dog());
            animals.Add(new Cat());

            foreach(Animal animal in animals)
            {
                animal.PrintWhoAreYou();
            }

            Console.ReadKey();
        }
    }

    abstract class Animal
    {
        public abstract void PrintWhoAreYou();

        private bool feeded = false;

        public void Feed()
        {
           feeded = true;
        }
    }

    class Dog : Animal
    {
        public override void PrintWhoAreYou()
        {
            Console.WriteLine("I am a dog!");
        }
    }
    class Cat : Animal
    {
        public override void PrintWhoAreYou()
        {
            Console.WriteLine("I am a cat!");
        }
    }
}

As you can see using a base class you can define common functionality in the base class, instead of repeating code.

You can use an interface to define a set of public methods that are offered by the classes those are realizing this interface.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Animals
{
    class Program
    {
        static void Main(string[] args)
        {
            List<IAnimal> animals = new List<IAnimal>();
            animals.Add(new Dog());
            animals.Add(new Cat());

            foreach(Animal animal in animals)
            {
                animal.PrintWhoAreYou();
            }

            Console.ReadKey();
        }
    }

    interface IAnimal
    {
        void PrintWhoAreYou();
    }

    abstract class Animal : IAnimal
    {
        public abstract void PrintWhoAreYou();

        private bool feeded = false;

        public void Feed()
        {
            feeded = true;
        }

    }

    class Dog : Animal
    {
        public override void PrintWhoAreYou()
        {
            Console.WriteLine("I am a dog!");
        }
    }
    class Cat : Animal
    {
        public override void PrintWhoAreYou()
        {
            Console.WriteLine("I am a cat!");
        }
    }
}
Attila
  • 3,206
  • 2
  • 31
  • 44