Link to home
Start Free TrialLog in
Avatar of alexthecodepoet
alexthecodepoet

asked on

How do you implement a Comparable interface into 2 subclasses?

Hello,

This is a continuation of:
https://www.experts-exchange.com/questions/22075902/How-do-you-implement-the-Comparable-interface-inside-of-a-custom-class.html?anchorAnswerId=18034968#a18034968


I asked how you implement the Comparable interface into a class.  The answer that I received was correct, but now i have a new (similar) question.

You see, i am building a tree data structure containing "dogs".  So, i needed to incorporate a comparable "function" into my "dog" class.  

The problem is that, i am not building only one tree structure, i am making 2.

One of my trees is a red black tree that should be built completely off of the dog's name, and the answer in the other question addressed this.

However, my other data structure is built by using a different "Comparable" feature of "dog" -- that is, I need to be able to compare the dogCOUNT.



So, basically what I am asking is: say i have a class, called Dog.  How do i go about implementing a Comparable subclass that will compare based off names, if specified to do so, or based off of dogCOUNT, if specified to do so.  

I assume that the method signatures would have to be different, but I dont know how to do this.

Thanks,


ASKER CERTIFIED SOLUTION
Avatar of ADSLMark
ADSLMark

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of alexthecodepoet
alexthecodepoet

ASKER

Hi Mark,

I guess i should have been more clear... dogs have: name and count: variables --so creating 2 seperate classes isn't really an option.


And enachmc, what do you mean?

Thanks,

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I mean what Ajay-Singh explained above - use the comparators when building the trees.
Hi Guys (and gals?)

Thanks for all of the help.  It turns out that Mark's solution actually did the trick.  But, i appreciate all of the other feedback.  Ajay-Singh, i think that it is awesome that you even went to the extent of pasting code. Mark's solution just hit a little bit closer to the bullseye...

much thanks to everyone though