views:

110

answers:

4

I have a base class for some plugin-style stuff, and there are some methods that are absolutely required to be implemented.

I currently declare those in the base class as virtual, for example

public virtual void Save
{
    throw new NotImplementedException();
}

and in the descendand I have a

public override void Save()
{
    //do stuff
}

Is it a good practice to throw a NotImplementedException there? The descendand classes could for example be the modules for handling different file formats. Thanks

+10  A: 

Generally I would expect that your base class would be abstract and simply defer the implementation to the inheriting classes.

public abstract class MyBase
{
    public abstract void Save();
    ...
}

public class MyChild : MyBase
{
     public override void Save()
     {
         ... save ...
     }

     ...
}
tvanfosson
+5  A: 

abstract seems to be what you are really after.

ScottS
+5  A: 

Wouldn't it be better to declare it as an abstract method?

That way all implementing types will have to implement this method as well.

Oded
+8  A: 

Not a good idea.

Usually, overriding methods would call the base-class method via

base.Save()

and that would puke every time.

So generally, bad idea. In your case, it looks like making it abstract would be the better choice.