Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8162965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:54:29+00:00 2026-06-06T18:54:29+00:00

I ran into a problem today trying to override an implementation of an interface

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T18:54:30+00:00Added an answer on June 6, 2026 at 6:54 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran into a problem today when trying to set a field using FieldInfo.SetValue()
today I ran into this problem which really bugs me, as almost the code
today I ran into a very interesting problem, which I was luckily able to
I ran into an interesting problem at work today. I got a request to
Today I ran into the following problem with NUnit. I have a class, that
Ran into a strange problem in PHP today and I'm wondering if someone can
I ran into a problem today at work wherein I have a BindingGroup that
I ran into a strange problem today, after renaming a couple of images in
I ran into a little problem today when I was creating a really quick
I ran into a peculiar problem today when I wanted to migrate the history

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.