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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:12:58+00:00 2026-05-14T03:12:58+00:00

Coming from a C++ background, Im used to multiple inheritance. I like the feeling

  • 0

Coming from a C++ background, Im used to multiple inheritance. I like the feeling of a shotgun squarely aimed at my foot. Nowadays, I work more in C# and Java, where you can only inherit one baseclass but implement any number of interfaces (did I get the terminology right?).

For example, lets consider two classes that implement a common interface but different (yet required) baseclasses:

public class TypeA : CustomButtonUserControl, IMagician
{
    public void DoMagic()
    {
        // ...
    }
}

public class TypeB : CustomTextUserControl, IMagician
{
    public void DoMagic()
    {
        // ...
    }
}

Both classes are UserControls so I cant substitute the base class. Both needs to implement the DoMagic function. My problem now is that both implementations of the function are identical. And I hate copy-and-paste code.

The (possible) solutions:

  1. I naturally want TypeA and TypeB to share a common baseclass, where I can write that identical function definition just once. However, due to having the limit of just one baseclass, I cant find a place along the hierarchy where it fits.
  2. One could also try to implement a sort of composite pattern. Putting the DoMagic function in a separate helper class, but the function here needs (and modifies) quite a lot of internal variables/fields. Sending them all as (reference) parameters would just look bad.
  3. My gut tells me that the adapter pattern could have a place here, some class to convert between the two when necessary. But it also feels hacky.

I tagged this with language-agnostic since it applies to all languages that use this one-baseclass-many-interfaces approach.

Also, please point out if I seem to have misunderstood any of the patterns I named.

In C++ I would just make a class with the private fields, that function implementation and put it in the inheritance list. Whats the proper approach in C#/Java and the like?

  • 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-05-14T03:12:59+00:00Added an answer on May 14, 2026 at 3:12 am

    You can use the strategy pattern or something like it to use has a (composition) instead of is a (inheritance):

    public class TypeA : CustomButtonUserControl, IMagician {
        IMagician magicObj = new Magical();
        public void DoMagic() {
            magicObj.DoMagic();
        }
    }
    
    public class TypeB : CustomButtonUserControl, IMagician {
        IMagician magicObj = new Magical();
        public void DoMagic() {
            magicObj.DoMagic();
        }
    }
    
    public class Magical : IMagician {
        public void DoMagic() {
            // shared magic
        }
    }
    

    There are other ways to instantiate your private IMagician members (such as passing them as a param via constructor) but the above should get you started.

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

Sidebar

Related Questions

Coming from a Classic ASP background, I'm used to multiple forms on a page,
Coming from a C# Background I never used any pointers. I'm creating a vector
Coming from a PHP background, I'm used to writing small functions that return a
Coming from a background using Visual Studio and Notepad++ I'm used to the indent
Okay, coming from a perl background I am used to Arrays holding refs to
I'm coming from a Prototype background and looking into JQuery. I'd like to know
Coming from a java background I'm used to having a bunch of properties files
Coming from a C++ background, I'm used to sticking the const keyword into function
Coming from a Delphi background, I am used to be able to have class
Coming from a java background, one of the things I am used to is

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.