OdeToCode IC Logo

What's Wrong With This Code? (#4)

Tuesday, August 29, 2006

This program throws an exception at runtime. If you've been burned by this problem before, you'll know what's wrong before you even see the definition for class Bar...

using System;

class Program
{
  
static void Main()
  {
    
Foo foo = new Bar();
  }
}

abstract class Foo
{
  
public Foo()
  {
    Init();
  }

  
protected abstract void Init();
}

class Bar : Foo
{
  
string message;

  
public Bar()
  {
    message =
"Hello!";
  }

  
protected override void Init()
  {
    
Console.WriteLine(message.ToUpper());
  }
}