Multiple inheritance alternative

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have several classes in my project:
varietyData
gradeData
attributeData
TreeData

Because the TreeData class encompasses each of the previous 3 classes, to me
it would make the most sense to allow TreeData to inherit from each of them.
Unfortunately, this is not possible in .NET.

OK, fine. But what is the best alternative? Should I just add an instance
of varietyData, gradeData and attributeData to the TreeData class? Is there
any other way?

Thanks
 
[...]
OK, fine. But what is the best alternative? Should I just add an
instance of varietyData, gradeData and attributeData to the TreeData
class?
Is there any other way?

I'm pretty aggregation is the only way. You can, however, include
multiple interfaces (an interface essentially being an abstract class) in
a class definition. So if you define interfaces for each of the three
base classes, then your TreeData class can inherit those interfaces. You
would still need to implement the interfaces by including an instance of
each of the three classes in TreeData (aggregation) and using those
instances for calls to the interface methods. But at least you could use
the TreeData class as if it were multiply inherited, even though it's not.

If you don't use interfaces, then you still need to aggregate the classes,
but then you would simply expose (as a property, most likely) the
instances of the three base classes and access them that way.

Pete
 
Hi Koulbassa,
Because the TreeData class encompasses each of the previous 3 classes, to
me it would make the most sense to allow TreeData to inherit from each of
them. Unfortunately, this is not possible in .NET.

Yes, you're right. Multiple inheritance is not supported in .NET. However,
we could implement multiple interfaces in one class.

Could you tell me what the relationship among the varietyData, gradeData
and attribute Data classes?

What do you mean by "the TreeData class encompasses each of the previous 3
classes"? Does the TreeData class need to use the other three classes to
implement its methods?

In my opinion, if the four classes have "is-a" relationship among them, you
should choose inheritance.

On the other hand, if the previous three classes are not relevant, and the
TreeData class has a "has-a" relationship to each of the other three
classes, you could add instances of variableData, gradeData and
attributeData classes to the TreeData class. In addition, you may add a
public property for each of the above three instances. Thus, you could use
the methods in the varietyData, or gradeData or attributeData class through
the exposed instance of varietyData, or gradeData or attributeData, without
the need to expose methods for the other three classes in the TreeData
class.

FYI, you may visit the following links for more information on when to use
inheritance and interface:

'When to Use Inheritance'
http://msdn2.microsoft.com/en-us/library/27db6csx(vs.71).aspx

"When to Use Interfaces"
http://msdn2.microsoft.com/en-us/library/3b5b8ezk(VS.71).aspx

Hope this helps.

If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Linda

Thank you for your response. You clarified my thinking by asking whether
the relationships are 'is-a' or 'has-a'. It would appear that in this case
inheritance may be ill-advised because the classes have 'has-a' relationships.

Thanks for helping!
 
Back
Top