Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How do you implement the Comparable interface inside of a custom class?

Posted on 2006-11-28
5
Medium Priority
?
242 Views
Last Modified: 2010-03-31
Hello,

Thanks for taking the time to help.  Allow me to explain the situation:

I have a "dog" class.  Somehow, I need to create a subclass that extends the java's "Comparable" class so that "dog"s can be compared.  

The reason I need to do this is because I have a tree that is going to be built up of dogs.  However, the tree's "find" feature needs to be able to access the Comparable "something (i dont know what you would call this)" in order to traverse the tree.

So, can someone please explain to me how I would go about implementing the Comparable "(something, i think you cal it a subclass?)" into my "dog" class so that I can achieve my desired outcome?


Thanks for the help,

an aspiring java learner
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
  • 2
  • 2
5 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 1000 total points
ID: 18034963
What aspect(s) of dog are you comparing on. The name? If so

public int compareTo(Object other) {
    return name.compareTo((Dog)other).name);
}
0
 
LVL 23

Accepted Solution

by:
Ajay-Singh earned 1000 total points
ID: 18034968
public class Dog implements Comparable<Dog> {
    private String m_name;

    public int compareTo(Dog o) {
        return m_name.compareTo(o.m_name); // assuming that you want to compare based on name        
    }
}
0
 

Author Comment

by:alexthecodepoet
ID: 18035051
Awesome, these are perfect!!

Thanks guys, especially for your timeliness.  

I appreciate the help very very much!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 18035080
:-)
0
 

Author Comment

by:alexthecodepoet
ID: 18040573
Hi Guys,

I posted a followup question, perhaps you can help?

Thanks

http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_22076950.html
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

721 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