Solved

How do you implement a Comparable interface into 2 subclasses?

Posted on 2006-11-29
7
879 Views
Last Modified: 2008-01-09
Hello,

This is a continuation of:
http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_22075902.html#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,


0
Comment
Question by:alexthecodepoet
7 Comments
 
LVL 10

Accepted Solution

by:
ADSLMark earned 410 total points
ID: 18040692
How about this approach:

You have a superclass Dog and two subclasses DogByName, DogByCount, which both extend from the Dog class. You implement the Comparable interface for both the subclasses and all common functionality is implemented in the Dog class. You keep your tree structure of Dogs, but when you add objects you should add either DogByName or DogByCount instances. Dynamically Java will determine the correct class and use the appropriate compare function.

Mark
0
 
LVL 12

Assisted Solution

by:enachemc
enachemc earned 30 total points
ID: 18040700
You implement no Comparable interface, but use 2 different implementations of Comparator<Dog>
0
 

Author Comment

by:alexthecodepoet
ID: 18042373
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,

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 5

Assisted Solution

by:gkishoreji
gkishoreji earned 30 total points
ID: 18042647
while initializing ur comparator class, u pass some value to the constructor to indicate whether to sort based on count or name...
depending on the initialized value from the comparator, do the comparison.
0
 
LVL 23

Assisted Solution

by:Ajay-Singh
Ajay-Singh earned 30 total points
ID: 18042840
Use java.util.Comparator interface


public class NameComparator implements Comparator<Dog> {
    public int compare(Dog x, Dog y) {
        return x.getName().compareTo(y.getName());
    }
}


public class CountComparator implements Comparator<Dog> {
    public int compare(Dog x, Dog y) {
        return x.getCount() - y.getCount();
    }
}
0
 
LVL 12

Expert Comment

by:enachemc
ID: 18043224
I mean what Ajay-Singh explained above - use the comparators when building the trees.
0
 

Author Comment

by:alexthecodepoet
ID: 18043380
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
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now