Solved

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

Posted on 2006-11-28
5
233 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 250 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 250 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

759 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