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

  • Home
  • SEARCH
  • 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 8424441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:59:27+00:00 2026-06-10T03:59:27+00:00

Since interfaces cannot contain implementation, that seems to me to lead to code duplication

  • 0

Since interfaces cannot contain implementation, that seems to me to lead to code duplication in the classes that inherit from the interface. In the example below, pretend that, let’s say, the first 10 or so lines that setup reading from a Stream are duplicated. Try not to focus on the wording here, but, instead focus on the concept of how easy it is to create duplicate code between each class.

For example:

public interface IDatabaseProcessor
{
   void ProcessData(Stream stream);
}
public class SqlServerProcessor : IDatabaseProcessor
{
    void ProcessData(Stream stream)
    {
      // setting up logic to read the stream is duplicated code
    }
}
public class DB2Processor : IDatabaseProcessor
{
    void ProcessData(Stream stream)
    {
      // setting up logic to read the stream is duplicated code
    }
}

I realize that using an abstract base class for ProcessData and adding non-abstract members is one solution. However, what if I really, really want to use an interface instead?

  • 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-10T03:59:28+00:00Added an answer on June 10, 2026 at 3:59 am

    The best way to share the code across interfaces is through stateless extension methods. You can build these extensions once, and use it in all classes implementing the interface, regardless of their inheritance chain. This is what .NET did with IEnumerable<T> in LINQ, for rather impressive results. This solution is not always possible, but you should prefer it whenever you can.

    Another way to share logic is by creating an internal “helper” class. This looks like the right choice in your case: implementations can call the internally shared code as helper’s methods, without the need to duplicate any code. For example:

    internal static class SqlProcessorHelper {
        public void StreamSetup(Stream toSetUp) {
            // Shared code to prepare the stream
        }
    }
    public class SqlServerProcessor : IDatabaseProcessor {
        void ProcessData(Stream stream) {
            SqlProcessorHelper.StreamSetup(stream);
        }
    }
    public class DB2Processor : IDatabaseProcessor {
        void ProcessData(Stream stream) {
            SqlProcessorHelper.StreamSetup(stream);
        }
    }
    

    The helper class does not need to be static: if your shared methods need state, you can make your helper a regular class, and put an instance of it in each implementation of your interface where you would like to share code.

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

Sidebar

Related Questions

I wrote a code that whenever the user picks a value from the drop-down
Do interfaces (polymorphic class solely with pure virtual functions) have a vtable? Since interfaces
Since I'm a C#-/.NET-guy, I'm used to explicit interface implementations - like so: public
Good news! Since version 4.8 BerkeleyDB has c# interface. BerkeleyDB is a quite interesting
Since REST is stateless, each request that comes in has no knowledge of the
Since input and raw_input() stop the program from running anymore, I want to use
I have the following classes and interfaces. I want to essentially freeze the IOld
Since I need to send email by code, I'm currently using plan text but
I am trying to create a mex file that interfaces MATLAB with an external
I have an interface IServiceInfo and an abstract class ServiceInfo. There are several classes

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.