andrews_nz
andrews_nz

Reputation: 185

Is it possible to force an interface implementation to be virtual in C#?

I ran into a problem today trying to override an implementation of an interface method which had not been declared virtual.

In this case I'm not able to change the interface or the base implementation and have to try something else, but I was wondering if there was a way to force a class to implement an interface using virtual methods.

Example:

interface IBuilder<T>
{
    // Already implicitly virtual
    /*virtual*/ T Build();
}

// This is a class written by someone else
class SimpleBuilder: IBuilder<SomeObject>
{
    // I would like to force this to be virtual
    public SomeObject Build() { return new SomeObject(); }
}

// This is the class I am writing.
class CustomBuilder: SimpleBuilder
{
    public /*override*/ SomeObject Build()
    {
        var obj = base.Build();
        obj.ModifyInSomeWay();
        return obj;
    }
}

Edit: CustomBuilder is intended to be used with MEF so I am deriving from SimpleBuilder in order for MEF to resolve the correct type. I've tried explicitly implementing the interface and not deriving from SimpleBuilder at all but then MEF doesn't pick up the right type.

The interface and base class in question are in a shared module created by another developer so it looks like I will have to get them to change their base class anyway.

Upvotes: 4

Views: 2718

Answers (5)

Rich O&#39;Kelly
Rich O&#39;Kelly

Reputation: 41757

No there isn't, in your example you're implicitly implementing the interface. If your class is meant to be subclassed and a method overridden it is up to the developer to ensure that the method is marked as virtual.

There is no way to force implementations of interface methods to be overrideable in code, only conventions within a development team could ensure this (eg all interfaces should be explicitly implemented, all interface implementations should be marked as virtual etc).

In this specific example you could insert an abstract class into the hierarchy:

abstract class VirtualBuilder<T> : IBuilder<T>
{
  abstract T Build();
}

But this isn't suitable in the generic case, since you lose having the benefits of interfaces and 'force' all concrete classes to only implement one interface.

Upvotes: 4

Chris Gessler
Chris Gessler

Reputation: 23113

You can't force it to be virtual but you can use the new keyword; it's called Method Hiding.

// This is the class I am writing. 
class CustomBuilder: SimpleBuilder, IBuilder<T> 
{ 
    public new SomeObject Build() 
    { 
        var obj = base.Build(); 
        obj.ModifyInSomeWay(); 
        return obj; 
    } 
} 

Upvotes: 2

J&#252;rgen Steinblock
J&#252;rgen Steinblock

Reputation: 31723

You can't do this because an Inteface just describes how a class should look like and says nothing about the implementation itself.

In other words, if you use a class that implements an interface which defines a method Foo() you can be sure, the class has this method, regardless of the implementation.

What you can do:

public interface IBuilder<T>
{
    T Build();
}

public abstract class BaseBuilder<T> : IBuilder<T>
{
    public abstract T Build();
}

public class CustomBuilder : BaseBuilder<CustomBuilder>
{
    public override CustomBuilder Build()
    {
        throw new NotImplementedException();
    }
} 

Upvotes: 0

Jirka Hanika
Jirka Hanika

Reputation: 13529

It is not possible to specify how an interface method will be implemented.

However, you have two ways of reimplementing the interface methods on the subclass even if the base implementation is not virtual:

  • hide the base implementation using the new method qualifier, or
  • explicit interface implementation like this:

    T IBuilder.Build() { var obj = Build(); obj.ModifyInSomeWay(); return obj; }

Either of these approaches will ensure that your method will be called only if your class is accessed via the interface, but not if the call uses a base class reference.

So I would say that this base class is not ready for extension through inheritance.

Upvotes: 0

Jon
Jon

Reputation: 437336

Not directly, no.

You could "force" it by providing your own base class and requiring that users derive from it:

abstract class BuilderBase<T> : IBuilder<T>
{
    public abstract T Build();
}

But this has serious problems too becase

  1. it does not immediately stop anyone from implementing the interface directly (although you could e.g. require incoming parameters to be BuilderBase<T> instead of simply IBuilder<T>, or even make IBuilder<T> internal and hide it in another assembly)
  2. it forces implementers of the interface to give up their choice for the single base class, for which they might hate you

Upvotes: 2

Related Questions