1

I'm getting a compilation error on following code:

public abstract class DbHandler<T>
{
    public abstract bool Save(T obj);
    ...
}

and its implementing class:

 public class SpaghettiTableDb : DbHandler<SpaghettiTable>
 {
    public bool Save(SpaghettiTable obj)
    {
        return false;
    }
    ...
 }

The error is :

'SpaghettiTableDb' does not implement inherited abstract member 'SeatPicker.DbHandler<SeatPicker.SpaghettiTable>.Save(SeatPicker.SpaghettiTable)'

But I think it does, so I'm not sure why I'm receiving this error.

(SpaghettiTable is just a class with some properties, nothing more)

Any help?

4
  • Have you cleaned and rebuilt your solution? Commented Aug 25, 2012 at 14:47
  • Visual Studio does have intellisense - so try hovering over the base class with your mouse, you'll see a little drop down, click on that and select "Implement ...". It will define it for you. Commented Aug 25, 2012 at 14:52
  • As an aside: plugins like ReSharper are very useful in cases like this, they can generate the correct boilerplate for you. Commented Aug 25, 2012 at 15:08
  • 1
    @millimoose vanilla Visual Studio can auto-generate the boiler plate code as well. Commented Aug 25, 2012 at 16:55

2 Answers 2

5

You need to use the override keyword. Otherwise you're not implementing the abstract base class and just creating a "new" separate method on the subclass.

public override bool Save(SpaghettiTable obj)
{
    return false;
}

Think of abstract methods just like virtual methods that you override. The only difference is that you force subclasses to provide an implementation of that method whereas virtual methods provide their own implementation that subclasses can optionally override with their own implementation.

EDIT: Additionally, if you want to make your life easier in Visual Studio, you can right-click (or ctrl+.) on the inheritance declaration and choose "implement abstract class" (or something like that, I don't have VS with me right now) which will automatically create all the overridden methods for you.

public class SpaghettiTableDb : DbHandler<SpaghettiTable> //right-click on "DbHandler"

Alternatively, in the empty code space within your class, you can start typing "override", then the IntelliSense will list all overridable members from the base class and when you pick one it will automatically write a default implementation for you.

EDIT: Just to extend on what you have in your code, without the override keyword, you are creating a new method that belongs to your subclass and not overriding the base class. When you call that method but from the context of using the base class, it won't call your subclass implementation since it doesn't override the base method.

Consider the following classes. (I'm using virtual instead of abstract just so it compiles and have it simpler)

public class BaseClass
{
    public virtual void Print()
    {
        Console.WriteLine("base print");
    }

    public virtual void AnotherPrint()
    {
        Console.WriteLine("base another print");
    }
}

public class SubClass : BaseClass
{
    public override void Print()
    {
        Console.WriteLine("sub print");
    }

    public void AnotherPrint()
    {
        Console.WriteLine("sub another print");
    }
}

Note that SubClass.AnotherPrint does not override BaseClass.AnotherPrint.

And when you use code like:

SubClass mySub = new SubClass();

mySub.Print(); //sub print
mySub.AnotherPrint(); //sub another print

BaseClass myBase = mySub;

myBase.Print(); //sub print
myBase.AnotherPrint(); //base another print

Note that through the code, mySub and myBase both point to the same object, but one is typed as SubClass and the other as BaseClass. When the runtime calls myBase.Print(), it can easily check the inheritance of the classes and see that SubClass has overridden the Print method and calls the SubClass implementation. However, since SubClass.AnotherPrint wasn't explicitly marked with override, the runtime/compiler considers that to be a completely different method with no link to the BaseClass.AnotherPrint method. Thus the runtime sticks with the base class implementation. When your instance is typed as the SubClass though, the compiler does see that you're pointing to that new method and essentially not to the base implementation.

Sign up to request clarification or add additional context in comments.

1 Comment

@StevenLemmens You're welcome! I just added some more info about what's going on behind the scenes if you'd like to read up on it.
2

You need to use the override keyword when implementing abstract methods or overriding virtual methods.

public override bool Save(SpaghettiTable obj)
{
    return false;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.