Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: implementing generic compareTo()

  1. #1
    Junior Member pyler's Avatar
    Join Date
    Sep 2012
    Posts
    23
    My Mood
    Busy
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Default implementing generic compareTo()

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

    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
    public class twos<P,N> implements Comparable<twos<P,N>>{
    private P a;
    private N b;
    }
    public twos(P puu, N paa ){
    a=puu;
    b=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
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: implementing generic compareTo()

    Quote Originally Posted by pyler View Post
    I have a generic class that contains a compareTo() method
    I'm getting an error on the method it'self that goes something like;
    Firstly, if the Comparable is Comparable<twos<P,N>>, then the compareTo must be:

    public int compareTo(twos<P,N> other)

    Secondly, 'a' and 'b' are of type P and N (type variables). They are not numbers (by itself), so you can't do this.a < something.
    Andrea, www.andbin.net SCJP 5 (91%) SCWCD 5 (94%)

    Useful links for Java beginners My new project Java Examples on Google Code

  3. The Following User Says Thank You to andbin For This Useful Post:

    GregBrannon (December 5th, 2013)

Similar Threads

  1. compareTo() question
    By Hikaros in forum Java Theory & Questions
    Replies: 5
    Last Post: September 9th, 2013, 01:34 AM
  2. [SOLVED] comparing objects using compareTo
    By jslice3 in forum Object Oriented Programming
    Replies: 3
    Last Post: November 19th, 2012, 10:09 PM
  3. Implementing the compareTo method?
    By colerelm in forum Java Theory & Questions
    Replies: 2
    Last Post: December 3rd, 2011, 07:47 PM
  4. Help Me with my Program CompareTo!!!!
    By WantHelp in forum What's Wrong With My Code?
    Replies: 14
    Last Post: July 7th, 2011, 01:54 PM

Tags for this Thread