Solved

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

Posted on 2006-11-28
5
227 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
  • 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

743 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

9 Experts available now in Live!

Get 1:1 Help Now