Solved

How do you implement a Comparable interface into 2 subclasses?

Posted on 2006-11-29
7
893 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

724 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