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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:29:42+00:00 2026-06-12T17:29:42+00:00

I am working on the equals() method for my sparse matrix class that I’m

  • 0

I am working on the equals() method for my sparse matrix class that I’m writing (part of a school project). I am constantly runnung into the issue that it won’t let me use any methods or other members specific to my class, because that(the name I used for the parameter to equals) has to be of the generic type Object in order to override Objects‘s equals() method. Even beyond that, I need to be able to use some of the methods of my SparseMatrix’s type parameter in order to really compare the equality, unless I can also figure ou. How can I write it to get around that obstacle?

I have a few ideas how I night do it, but none of them seem to work: I’ve triedcasting the parameter, I’ve tried overlading equals(), I’ve even tried some other stuff, but none of it seems to work.

This is what I have so far, but, as I said, I just can’t get it to work.

public boolean equals(Object that) {
    if (that instanceof SparseMatrix<?>) { 
        if (this.xSize != that.xSize ||
                this.ySize != that.ySize)
            return false;
        /* make some more comparisons that depend on specific
         * members of my matrix class, etc...*/
    }
    return false;
}

I have tried searching SO for this, and while I was able to find a few that seemed to be asking the same thing, I couldn’t find any answers that actually explained how to do it.

  • 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-12T17:29:43+00:00Added an answer on June 12, 2026 at 5:29 pm

    When you have an object of a base class and you know which kind of subclass it is, you can convert it by downcasting it. Then you can call any methods specific to the subclass.

    public boolean equals(Object o) {
        if (o instanceof SparseMatrix<?>) { 
            SparseMatrix that = (SparseMatrix)o;  // <-- downcast
            if (this.xSize != that.xSize ||
                    this.ySize != that.ySize)
                return false;
        }
        return false;
    }
    

    Should o not be an instance of SparseMatrix or a class which extends/implements SparseMatrix (you already checked that, but let’s assume you didn’t) you would get a ClassCastException.

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

Sidebar

Related Questions

I have a vector class that has it's Equals(object obj) method overridden so that
I am working on a homework assignment for my into to programming class that
I am currently working on a method that takes in a text file and
I am working on a method that compares two objects using reflection. The object
I'm working on a class project and I cannot seem to figure out what
I've got this method I'm working on (I think that's the name) and it
I ran into an interesting (and very frustrating) issue with the equals() method today
In a project that I'm working on I have to work with a rather
I working on a sample problem of over-ridding hashCode and equals method but getting
In the equals() method of my class, I am using a private instance HashMap

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.