user3813234
user3813234

Reputation: 1682

why can't a compiler resolve method overriding?

In the following C# snippet

public class Animal
{
    public virtual void MakeSound()
    {
        Console.WriteLine("Animal sound");
    }
}

public class Dog:Animal
{
    public override void MakeSound()
    {
        Console.WriteLine("Dog sound");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Animal an = new Dog();
        an.MakeSound();           
        Console.ReadLine();
    }
}

the method to be called is determined at runtime. Why exactly can't the compiler figure out, which method to call?

Why doesn't the compiler see that an refers to a Dog object and then choose the method from that class?

And how does the runtime determine which method to be called?

Upvotes: 0

Views: 109

Answers (4)

Polyfun
Polyfun

Reputation: 9639

Consider this change to your code:

public class Animal
{
  public virtual void MakeSound()
  {
    Console.WriteLine("Animal sound");
  }
}

public class Dog : Animal
{
  public override void MakeSound()
  {
    Console.WriteLine("Woof!");
  }
}

public class Cat : Animal
{
  public override void MakeSound()
  {
    Console.WriteLine("Purrrrrrrrrrrrrr");
  }
}

class Program
{
  static void Main(string[] args)
  {
     Animal an = GetAnimal(DateTime.Now);
     an.MakeSound();           
     Console.ReadLine();
  }

  private Animal GetAnimal(DateTime dateTime)
  {
    if (dateTime.DayOfWeek == DayOfWeek.Monday)
    {
      return new Dog();
    }
    else
    {
      return new Cat();
    }
  }
}

Now it is impossible to know what type of animal to create at compile time, as it depends on the day of the week when the code is actually run. On Mondays you will get a Dog, but at any other time you will get a Cat. This is a defining benefit of polymorphism--types are not baked in by the compiler, but are derived on the fly when the code is executing. Polymorphism allows you work with these derived types, even though you do not know exactly what they will be when you write your code (but you do know they are all types of animals).

Upvotes: 0

Mike
Mike

Reputation: 4051

Consider following code:

class Program
{
    static void Main(string[] args)
    {
        Animal dog = new Dog();
        MakeSoundAbstract(dog);

        Animal an = new Animal();
        MakeSoundAbstract(an);

        Console.ReadLine();
    }

    static void MakeSoundAbstract(Animal animal)
    {
        animal.MakeSound();
    }
}

If compiler will determine virtual calls during compilation not during runtime then MakeSoundAbstract method will always execute MakeSound method of class Animal so we are loosing here the power of abstraction.

Upvotes: 0

knittl
knittl

Reputation: 265171

This sounds an awful lot like an exam/homework question. But let me answer your question with another question. Consider the following code:

static void Main(string[] args)
{
    var random = new Random();
    Animal an = null;
    if (random.NextDouble() < 0.5) {
      an = new Dog();
    } else {
      an = new Cat();
    }

    an.MakeSound();           
    Console.ReadLine();
}

How is the compiler supposed to know at compile time which method to call? It can not, only during runtime is the concrete type known.

Upvotes: 3

Wormbo
Wormbo

Reputation: 4992

You told the compilier that the variable is of type Animal. It looks only at declarations, while you expect it to execute your code to figure out what you mean. That's not how it works.

Upvotes: 0

Related Questions