472,958 Members | 2,403 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

Interfaces & Inheritance

Let's say I have a base class that implements IComparable<baseClass>. Really
it could be any interface but I'm picking that one for the sake of
discussion. In this class, equality and comparisons are based on a string
representation of it's attributes - a subclass may add an attribute but
still uses a string representation for equality and comparison.

So if I can avoid it, I'd like to have all my tests and code associated with
the base class. Must I have the subclass also implement
IComparable<subClassand then make wrapper calls to the base class? Would I
be better off using composition than inheritance?

I may be working way too hard to save some work, but I'd like to explore the
idea.

Thanks,
Eric
Jun 27 '08 #1
1 1210
Must I have the subclass also implement
IComparable<subClassand then make wrapper calls to the base class? Would I
be better off using composition than inheritance?
Well, as long as baseClass also implements IComparable, then it mainly
depends on how you later use it. For example, if you have a
List<subClassand call Sort(), it will use Comparer<T>.Default; if
subClass doesn't implement IComparable<subClass>, then this will fall
back to using the non-generic IComparable interface, so as long as
baseClass : IComparable, things should still work. Since we are
talking about a class (not a struct) there is no boxing overhead here
- just a little casting overhead.

Of course, if you only use comparers based on baseClass (i.e.
List<baseClass>.Sort()) then it will use your IComparable<baseClass>,
and no casting is required. There is nothing to stop your subClass
implementing IComparable<subClass>, but it gets confusing - and you'd
still want the comparisons to work out the same regardless of whether
IComparable, IComparable<baseClassor IComparable<subClasswas used
- so perhaps just draw the line at IComparable and
IComparable<baseClass(both calling the same actual method to do the
work).

Marc
Jun 27 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Shawn Casey | last post by:
Consider the following code: interface IBase { virtual void BaseFunction() = 0; }; interface IDerived : public IBase { virtual void DerivedFunction() = 0;
21
by: Franco Gustavo | last post by:
Hi, Please help me to understand this, because I don't see what I'm missing. I was reading a lot of examples on Internet that explain that C# doesn't implement multiple inheritance it...
30
by: Frank Rizzo | last post by:
We are having one of those religious debates at work: Interfaces vs Classes. My take is that Classes give you more flexibility. You can enforce a contract on the descendant classes by marking...
18
by: _dee | last post by:
Question about best use of interfaces: Say there's a 'Master' class that needs to implement a few interfaces: class Master : I1, I2, I3 { } The actual code already exists in smaller...
22
by: RSH | last post by:
Hi, I have been reading on interfaces working on samples I've run across on the web. For the life of me I cannot seem to grasp them. It appears to me that interfaces are simply blueprints to...
27
by: jm | last post by:
I am having trouble understanding the purposes of an interface, even though the concept of interfaces is around me all the time (user interface, for example). I'm just not understanding software...
23
by: Dave Rahardja | last post by:
Since C++ is missing the "interface" concept present in Java, I've been using the following pattern to simulate its behavior: class Interface0 { public: virtual void fn0() = 0; };
23
by: A.Gallus | last post by:
If I declare a function pure virtual: class A { virtual void myfunc() = 0; } and I derive a class from A: class B : public A
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.