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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:07:26+00:00 2026-05-15T12:07:26+00:00

In C#, I have a class hierarchy with a couple of abstract base classes

  • 0

In C#, I have a class hierarchy with a couple of abstract base classes near the top and a fair number of derived classes. A few these concrete classes have some common properties and methods that are implemented identically. It strikes me as wasteful and so one solution might be to implement this common behaviour in another abstract base class.

abstract class Control;

abstract class SquareControl: Control
{
    public int SquarishProperty;
    public void SquarishMethod();
};

class Window: SquareControl;
class Button: SquareControl;

However, what if several other classes in the hierarchy shared some other behaviour but also share something in common with one of the controls from another base class? Perhaps there are lots of areas of commonality. It would become impractical to model this with abstract base class implementation wouldn’t it?

abstract class FlashableControl: Control
{
    public int FlashyProperty;
    public void FlashMethod();
};

class StatusBar: FlashableControl;  // but it's also a bit square too, hmm...

So how do you go about sharing such implementations across classes without using base classes?

I imagine I want to delegate the implementaion of an interface to another class and have that class implement those properties and methods on behalf of the desired classes, so that to the user, the StatusBar and Window appear to support a standard interface, but under the covers it’s something else that implements it.

I can visualise aggregating classes that implement this behaviour, but is this appropriate and are there any pitfalls? What are the alternatives?

Thanks

  • 1 1 Answer
  • 2 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-15T12:07:27+00:00Added an answer on May 15, 2026 at 12:07 pm

    You can use a pattern like this:

    public interface ICommonServices
    {
        string SomeProperty { get; set; }
    
        void SomeMethod(string param);
    }
    
    public static class CommonServiceMethods
    {
        public static void DoSomething(this ICommonServices services, string param)
        {
            services.SomeMethod(services.SomeProperty + ": " + param + " something extra!");
        }
    }
    

    All classes that implement ICommonServices now also get some free behavior via the extension method, which depends solely on those features exposed by all ICommonServices implementers. If you need access to base class functionality, you can put that in its own interface and have ICommonServices implement that interface as well. Now you can create ‘default’ extension functionality for interfaces without having to use multiple base classes.


    EDIT

    If you want some of these methods to be internal, you can modify the pattern like this:

    public class MyObject : IServices
    {
        public string PublicProperty { get; private set; }
    
        string IServices.SomeProperty { get; set; }
    
        void IServices.SomeMethod(string param)
        {
            //Do something...
        }
    }
    
    public interface IPublicServices
    {
        string PublicProperty { get; }
    }
    
    internal interface IServices : IPublicServices
    {
        string SomeProperty { get; set; }
    
        void SomeMethod(string param);
    }
    
    internal static class ServiceMethods
    {
        public static void DoSomething(this IServices services, string param)
        {
            services.SomeMethod(services.SomeProperty + ": " + param + " something extra!");
        }
    }
    

    Basically we’re exposing both public and internal interfaces. Note that we implement the internal interface methods explicitly, so that the methods are not available for public consumption (since the public client can’t get access to the interface type.) In this case, the helper extension methods are internal, relying on the internal interface, though you could also create public helper methods that rely on the public interface.

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

Sidebar

Related Questions

Let's say I have these class hierarchy : public abstract class Parent { }
Suppose you have a Java class hierarchy of about 30 classes, with a base
I sometimes end up with a class hierarchy where I have an abstract base
Let's say I have the following class hierarchy in C++: class Base; class Derived1
I have the following hierarchy of classes class classOne { virtual void abstractMethod() =
I have a class hierarchy: abstract DomainObject { ... @Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator=SEQ)
I have a class hierarchy that looks like this: class Base<TElement> { public TElement
I have a class hierarchy with the following three classes: template<int pdim > class
Imagine I have such class hierarchy: Base A : Base B : Base C
I have a class hierarchy like this public abstract class CalendarEventBase{} public class TrainingEvent

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.