Results 1 to 3 of 3
  1. #1
    pyler is offline Member
    Join Date
    Jul 2012
    Rep Power

    Default Problem with compareTo() method

    I have a generic class that contains a compareTo() method
    I'm getting an error on the method it'self that goes something like;

    Java Code:
    Name clash: the method compareTo(T) of type twos<T N> has the same erasure as compareTo(T) of type Compareable<T> but does not override it.
    How do I fix it?

    here's my class
    Java Code:
    public class twos<P,N> implements Comparable<twos<P,N>>{
    private P a;
    private N b;
    public twos(P puu, N paa ){
    public P getP(){
    return a;}
    public N getN(){
    return b;}
    public compareTo(P bee){
    int num1 = ((Comparable<twos<P, N>>) this.a).compareTo( (twos<P, N>) bee.b );
    			if (num1 != 0)
    				return num1;
    			if (this.a < bee.a) return -1;
    			if (this.a > bee.a) return 1;
    			return 0;
    }//end of class

    Any help is appreciated

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Rep Power

    Default Re: Problem with compareTo() method

    Check out the two typecasts you do on line 13; notice a difference?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Northern Virginia, United States
    Rep Power

    Default Re: Problem with compareTo() method

    What exactly are you trying to do here? The comparable interface is:

    Java Code:
    interface Comparable<T> {
        public int compareTo<T o>
    So your compareTo is expecting a type of P and not Twos<P,N> (and it needs to return an int). And I
    don't understand either of your casts for types P a or N b. Do they also have their own Comparable interface implementations?

    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. compareTo method
    By Tabula Rasa in forum New To Java
    Replies: 4
    Last Post: 04-21-2011, 04:49 PM
  2. Doubt About CompareTo method() ?
    By makpandian in forum New To Java
    Replies: 8
    Last Post: 03-08-2011, 06:19 AM
  3. using the compareTo method
    By anonymous445 in forum New To Java
    Replies: 3
    Last Post: 01-19-2011, 03:53 AM
  4. need help wit this compareTo method
    By ShinTec in forum New To Java
    Replies: 4
    Last Post: 02-02-2010, 02:24 AM
  5. ArrayLists compareTo method, equals method
    By random0munky in forum New To Java
    Replies: 2
    Last Post: 10-26-2009, 07:20 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts