Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4013284
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:21:08+00:00 2026-05-20T09:21:08+00:00

I have a superclass which implements the Comparable Interface and overrides the compareTo() method.

  • 0

I have a superclass which implements the Comparable Interface and overrides the compareTo() method. One subclass of this superclass has to implement it’s own compareTo() method but I’m not able to override the compareTo() method of the superclass due to the name clash error.

public abstract class Superclass<T> implements Comparable<T> {
    public int compareTo(T bo) { };
}
public class Subclass extends Superclass<Subclass> {
    public <T> int compareTo(T bo) { }; // I get the name clash error here
}

To solve this problem I tried to make the subclass generic too (see: Java Generics name clash, method not correctly overridden), but either I did it wrong or this doesn’t work:

public abstract class Superclass<T> implements Comparable<T> {
    public int compareTo(T bo) { };
}

public class Subclass<T> extends Namable<Subclass<?>> {
    public int compareTo(T bo) { }; // I get the name clash error here
}

So, how can I override the compareTo() method of Superclass?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T09:21:08+00:00Added an answer on May 20, 2026 at 9:21 am

    I think you have a more general design issue here. Namely, why do you want to implement compareTo in the base class? In practice, this almost always leads to problems down the line.

    Consider

    public class Subclass2 extends Superclass<Subclass2> {
      ...
    }
    
    Subclass sub = new Subclass();
    Subclass2 sub2 = new Subclass2(); // the superclass portions of the two objects
                                      // are identical
    
    sub2.compareTo(sub); // what should be the result?
    

    If you have two Superclass instances (whose superclass portions are identical, but the subclass portions may not be), how can you compare them using Superclass.compareTo? If you don’t take concrete subclasses into account, the result of the comparison is 0, incorrectly. If you try to directly compare objects of different subclasses, you will fail with a runtime exception. If you try to be clever and check the runtime types of the objects before trying to directly compare them, again you need to throw some sort of runtime exception instead of returning a valid-looking result.

    I think it would be better to reverse the schema and implement compareTo only in concrete subclasses where the compiler may ensure that the method is called with the right type of parameter. If you are worried about duplicating code between subclasses, you may pull it up into a protected final method in the superclass.

    Update

    Code example:

    public abstract class Superclass {
      protected final int compareBasePortionTo(Superclass other) { ... };
    }
    
    public class Subclass extends Superclass implements Comparable<Subclass> {
      public int compareTo(Subclass other) {
        int baseCmp = compareBasePortionTo(other);
    
        if (baseCmp != 0)
          return baseCmp;
    
        // compare subclass properties
      };
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table in a DB (Postgres based), which acts like a superclass
I have three classes which implement the same protocol, and have the same parent
Have just started using Visual Studio Professional's built-in unit testing features, which as I
Have you managed to get Aptana Studio debugging to work? I tried following this,
I have a database in which all of the tables have been generated by
I have to implement a logic whereby given a child class, I need to
Eclipse is adding @Override annotations when I implement methods of an interface. Eclipse seems
I've got a bunch of strongly typed DataSet that each have two tables. One
In Objective-C, I'd like to force derived classes to implement a given interface without
Is there a way in Python, to have more than one constructor or more

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.