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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:34:50+00:00 2026-06-13T08:34:50+00:00

I followed a tutorial, but failed to make my Country class Comparable for my

  • 0

I followed a tutorial, but failed to make my Country class Comparable for my BST.

Main:

BinarySearchTree A = new BinarySearchTree();
Country a = new Country("Romania", "Bucharest", 1112);
A.insert(a);

Country class:

public int compareTo(Object anotherCountry) throws ClassCastException {
    if (!(anotherCountry instanceof Country))
        throw new ClassCastException("A Country object expected.");
    String anotherCountryName = ((Country) anotherCountry).getName();  
    int i = this.name.compareTo(anotherCountryName);
    if(i < 0){
        return -1;
    } else {
        return 0;
    }
}

error:

@Override
public int compareTo(Object anotherCountry) throws ClassCastException {
    if (!(anotherCountry instanceof Country))
      throw new ClassCastException("A Country object expected.");
    String anotherCountryName = ((Country) anotherCountry).getName();  
    return this.name.compareTo(anotherCountryName);

Description Resource    Path    Location    Type

Name clash: The method compareTo(Object) of type Country has the same erasure as compareTo(T) of type Comparable but does not override it Country.java /Lab2_prob 4/src line 17 Java Problem

Description Resource    Path    Location    Type
The method compareTo(Object) of type Country must override or implement a supertype method  Country.java    /Lab2_prob 4/src    line 17 Java Problem

and class:

public class Country implements Comparable<Country>{
    private String name;
    private String capital;
    private int area;

Description Resource    Path    Location    Type

The type Country must implement the inherited abstract method Comparable.compareTo(Country) Country.java /Lab2_prob 4/src line 2 Java Problem

  • 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-13T08:34:51+00:00Added an answer on June 13, 2026 at 8:34 am

    Your Country class should implement Comparable:

    public class Country implements Comparable<Country>
    

    Then your compareTo method should look like this:

    @Override
    public int compareTo(Country anotherCountry) {
        return this.name.compareTo(anotherCountry.getName());
    }
    

    Note the signature of compareTo. The parameter can (and must) be of type Country, not Object. This is required because of the generic type parameter on Comparable. The upside is you don’t have to check the type anymore. The downside is you can only compare Country to other Country objects (or its subtypes), but in most cases this is what you want anyway. If not you have to change the type parameter, e.g. if you use Comparable<Object> the signature of compareTo can be Object again. You can read more about generics here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am new to cent OS. followed this tutorial but still unable to get
I followed this tutorial . But whenever I try to log in with my
I followed this tutorial and got the basics of Content Providers : http://www.vogella.de/articles/AndroidSQLite/article.html But
I've followed this tutorial: http://anandhansubbiah.com/blog/writing-your-first-android-application/ , but no matter what I do, and what
i am new to Node.js. i followed the tutorial and typed the following var
I followed this tutorial but still I go to the webpage refresh, then go
I followed the tutorial here but when I create the project in Eclipse, i
I've followed this tutorial but when I try to run the application I get
I've followed this tutorial but when I try to run the application I get
I have followed all the steps for implementing pushnotification using the raywenderlich tutorial but

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.