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 8394527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:05:04+00:00 2026-06-09T20:05:04+00:00

Suppose a Superclass implements Comparable<Superclass> , such that Arrays.sort(ArrayOfSuperInstances); uses this compareTo(Superclass other) method

  • 0

Suppose a Superclass implements Comparable<Superclass>, such that Arrays.sort(ArrayOfSuperInstances); uses this compareTo(Superclass other) method to sort. Does this guarantee that an array of instances of Subclass extends Superclass will sort in the same way using Arrays.sort(ArrayOfSubInstances); ? (Assuming the compareTo is not overloaded in the subclass definition)

Or in other words, will Subclass by default inherit the compareTo method of its Superclass , so that one can blindly use Arrays.sort() knowing they will be sorted as superclasses would be?

  • 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-06-09T20:05:05+00:00Added an answer on June 9, 2026 at 8:05 pm

    Yes — that is the whole principle behind polymporphism, and specifically the Liskov substitution principle. Basically, if A is a subclass of B, then you should be able to use A anywhere you’d be able to use B, and it should essentially act the same as any other instance of B (or other subclasses of B).

    So, not only will it happen, but it’s almost always what you want to happen. It’s usually wrong to compareTo in the subclass.

    Why? Well, part of the Comparable<T> contract is that comparison is transitive. Since your superclass will presumably not know what its subclasses are doing, if a subclass overrides compareTo in such a way that it gives an answer different than its superclass, it breaks the contract.

    So for instance, let’s say you have something like a Square and a ColorSquare. Square’s compareTo compares the two squares’ sizes:

    @Override
    public int compareTo(Square other) {
        return this.len - other.len;
    }
    

    …while ColorSquare also adds a comparison for color (let’s assume colors are Comparable). Java won’t let you have ColorSquare implement Comparable<ColorSquare> (since its superclass already implements Comparable<Square>), but you can use reflection to get around this:

    @Override
    public int compareTo(Square other) {
        int cmp = super.compareTo(other);
        // don't do this!
        if (cmp == 0 && (other instanceof ColorSquare)) {
            ColorSquare otherColor = (ColorSquare) other;
            cmp = color.compareTo(otherColor.color);
        }
        return cmp;
    }
    

    This looks innocent enough at first. If both shapes are ColorSquare, they’ll compare on length and color; otherwise, they’ll only compare on length.

    But what if you have:

    Square a = ...
    ColorSquare b = ...
    ColorSquare c = ...
    
    assert a.compareTo(b) == 0;  // assume this and the other asserts succeed
    assert a.compareTo(c) == 0;
    // transitivity implies that b.compareTo(c) is also 0, but maybe
    // they have the same lengths but different color!
    assert b.compareTo(c) == 1; // contract is broken!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have MyEdge and MyEdgeModified , such that MyEdge is the superclass and
I couldn't find the answer to this in any other question. Suppose that I
Suppose I have a stringbuilder in C# that does this: StringBuilder sb = new
Suppose I have a pure virtual method in the base interface that returns to
Suppose I have a static method of my class that returns an object of
Suppose the following. There is some superclass or interface declaring some method: Base.doSmth() .
I have an object in a LinkedHashSet that implements equals , hashCode and compareTo
In a book, I saw that if a subclass is overriding a superclass's method,
Suppose I have a class Baz that inherits from classes Foo and Bar ,
Suppose I have a simple model, such as Record: @Model public class Record {

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.