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 7720947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:48:00+00:00 2026-06-01T03:48:00+00:00

I find myself wishing an interface could be composed of methods that are fulfilled

  • 0

I find myself wishing an interface could be composed of methods that are fulfilled in several classes. If I could do that, I could make an interface for a role that has one function from this class and one function from that class.

Does any language support this? Is it possible in the typical Java/C#/VB.NET style OO?

For example: I have a function that takes an value object, and saves all the parts into the correct tables. I just made an interface called StatsPersistence that I inject to a function so I can unit test its behavior, but it is missing a method. I want to be able add a method to that, but that method is on a different class from the first two, so this is obviously not possible. So, I have 2 methods in one class and 1 are in the other, but they both are needed for the “role” of “StatsPersistence”. Also, those methods are all three needed for other roles too, not just this one.

What I have:

interface StatsPersistence
    public saveSession(session)  // fulfilled in much larger StatsActiveRecord
    public saveAppUse(appUse)    // fulfilled in much larger StatsActiveRecord

What I want:

interface StatsPersistence
    public saveSession(session)  // fulfilled in much larger StatsActiveRecord
    public saveAppUse(appUse)    // fulfilled in much larger StatsActiveRecord
    public updateUnit(name)      // fulfilled in much larger UnitActiveRecord

I could just make a new class that wraps all three methods, and have that class just make calls to the two implementers, but that seems just like a lot of OOverkill.

My thoughts are leaning towards:

  • My interface role is too broad
  • Make a class that wraps these three, and have it implement my desired interface
  • 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-01T03:48:02+00:00Added an answer on June 1, 2026 at 3:48 am

    Assume you have a variable of type StatsPersistence. An interface contract forces any instance of a StatsPersistence to have the three methods. Therefore, a StatsActiveRecord cannot be stored as a StatsPersistence, and same for the UnitActiveRecord. This means that neither object can be an instance of StatsPersistence, since neither object fulfills the interface contract. Thus, it is impossible to split an interface implementation between classes.

    There are a few alternatives.

    As you mentioned, you could have a master class that implements the interface and calls the two separate classes. That could be tricky, since the two ‘subclasses’ are tied together by the master.

    You mentioned the interface being too broad. You could split the interface, if appropriate.

    The best solution (in the general case) is to combine the classes. If two classes have to work together to decide who does certain things, they may as well be one class.

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

Sidebar

Related Questions

I find myself very often creating an object that has no public methods and
I often find myself wishing I could have an object's member variable be const,
String.IsNullOrEmpty() appears to be an extremely well used method and I find myself wishing
I find myself often wanting to structure my exception classes like this: # legends.py
I find myself creating a significant number of wrapper classes, purely because I want
In large apps I find myself really wishing I had built-in AOP facilities. As
I find myself typing Ctrl+Shift+O several times to organize imports (usually imports which are
Find myself writing an extension for Chrome, as a test I want to make
I find myself currently working on several projects which include user entries in a
I find myself writing code that looks like this a lot: set<int> affected_items; while

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.