0

Let's say I have the following classes:

public class A
{
    public string a {get; set;}
}

public class B
{
    public int b {get; set;}
}

public class C
{
    public DateTime {get; set;}
}

...and so on. If I had multiple inheritance, what I could do is:

public class AC : A, C {}
public class BD : B, D {}
public class ACD : AC, D {}

...in which the child classes would inherit the properties of the parent classes. Assume for this situation that there are only properties involved, and that there is no duplication of properties. In C#, there is no such thing as multiple inheritance, so my notation above won't work. I also know that interfaces won't buy me anything, because an interface will only require that the implementing class implement the properties (the properties in our real-world problem all have annotations, too, which can't be put in interfaces).

Currently, we are solving the problem through stringing inheritances together, but we want something more flexible. And the only other alternative we can think of is copy/paste (obviously not optimal). Any strategy ideas for solving this puzzle?

  • 4
    Have you tried composition instead of inheritance? – Jay Feb 12 '14 at 23:21
  • 2
    @Jay right he could have `public class AC {public AC(A a, C c){...}}` and then set some local public properties A, C. If need be, he could fill this all out with dependency injection. – Yuriy Faktorovich Feb 12 '14 at 23:25
  • 1
    What does stringing inheritances together mean? If you add a bit more substance closer to your real world example it might help. Also, what is the problem with your current solution; i.e., in what way do you need more flexibility? – Dmitriy Khaykin Feb 12 '14 at 23:25
  • Is the main goal of the question to have Attributes on classes/properties and have them inherited? – Erik Philips Feb 12 '14 at 23:27
  • @YuriyFaktorovich yes, that is exactly what I was thinking (+1) – Jay Feb 12 '14 at 23:28
  • @ErikPhilips is on the right track. First, imagine that all of the properties in the parent classes have annotations (or is the C# term "attributes"?). Second, for example, class ACD may have properties of its own. Third, all of the properties in the final child classes, whether inherited from parents or not, must appear to be direct members of the class (I think the term here is "first-class"). Finally, all of these classes need to be serializable. I just checked out the concept of a property bag, but it doesn't quite work. – David Stowell Feb 13 '14 at 10:54
  • Are your attributes custom or are they for validation (data annotations)? – Erik Philips Feb 13 '14 at 20:21
  • To be fair, I'm not sure - I've inherited (_heh_) this code. As it is, it looks like we're going to have to set up a better inheritance chain to get the effect we're looking for. Not optimal, but doable. – David Stowell Feb 14 '14 at 20:06

2 Answers2

1

Try composing your classes, so that your new class has a property for class A, a property for class B and so on.

You could go a step further and make the logic of the composing class can be a façade over the composed classes.

There is a great SO post about it here: https://stackoverflow.com/a/53354/685341

Community
  • 1
  • 1
Jay
  • 9,561
  • 7
  • 51
  • 72
0

You have three options depending on what you are trying to achieve with multi inheritance:

  1. use interfaces, implement the interfaces on abstract base classes. With appropriate use of normal OO patterns (i.e. proper use of base implementations and abstract properties to force the extending class to do the implementation) you can get most of the way towards multi inheritance with this option.

  2. composition. Create a class that wraps the required child (base) classes. You don't have to wrap all child classes, you can still use inheritance to achieve some of the class definition. In this case class AC could extend (inherit from) A and also wrap C.

  3. use a mixture of interfaces and composition

Neither of the first two is a really clean solution, both will get you most of the way there. The reality is that there are few cases where you truly need multiple inheritance, when you do then option 3 should accomplish it.

slugster
  • 49,403
  • 14
  • 95
  • 145