2

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.

2 Answers 2

3

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.

4
  • Also, how would I go about making sure that o's elements are of the type I need, so I can call their member functions? Commented Sep 29, 2012 at 16:39
  • if (that instanceof SparseMatrix<TypeYouNeed>)? You could also check the type and do a downcast on the members after getting them from SparseMatrix.
    – Philipp
    Commented Sep 29, 2012 at 16:41
  • @Philipp When I tried that it give me a new error: Cannot perform instanceof check against parameterized type SparseMatrix<type>. Use the form SparseMatrix<?> instead since further generic type information will be erased at runtime Commented Sep 29, 2012 at 16:44
  • @AJMansfield.. If you want to check for the generic parameterized type you are using, you need to use Reflection API... Also See ParameterizedType
    – Rohit Jain
    Commented Sep 29, 2012 at 16:47
2
public boolean equals(Object that) {
    if (that !=null && that instanceof SparseMatrix<?>) { 
        SparseMatrix that = (SparshMatrix)o;
        if (this.xSize != that.xSize ||
                this.ySize != that.ySize)
            return false;
    }
    return false;
}

Added Code to check for null value.

You need to have this method overrided in the class whose instances you want to compare.. And when you actually invoke equals on instances of that class, this overrided method will be invoked..

** Moved from comment: - Using instanceof you can only assure of what type your instance is, and not the parameterized type of that instance.. (You cannot findout whether you have ArrayList<String>, what you can find out is that it is ArrayList or ArrayList<?> to be precise) .. Reason for that is, generic type information is only available at compile time, and is erased through type erasure , so they are not available at runtime..

1
  • If that is null it won't pass the instanceof test, it will return false. Test this: String s = null; if (s instanceof Objec) { System.out.println("object"); } else { System.out.println("null"); } Commented Sep 29, 2012 at 16:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.