(Oct 6, 2015 01:45 PM)Raijin Wrote: I am confused. What are you trying to return?
It must be a Boolean value that's returned (true/false).
Edit: == is used in conditional statements
It
does return a boolean value and the usage of '==' is correct.
OP, your problem is that you're not casting the object. Your 'equals' method takes 'Object' as an argument, hence the program does not know exactly what is being passed as an argument because in java any class implicitly extends 'Object' (and therefore any object is an instance of 'Object'). Since the program doesn't know what kind of object it is, it can't access the fields 'length', 'height' and 'width' because it could be any other object that doesn't have those fields.
To fix your 'equals' method you have to cast the object. First off, if the object is not an instance of your class you can already return false. If it is an instance of your class, then you cast the object and perform the check. This can be achieved with this code:
Code
public boolean equals(Object obj) {
if(obj instanceof ThisClass) {
//Cast object so that the program knows what kind of object it is
ThisClass castObject = (ThisClass)obj;
//Now you can perform the check
return length == castObject.length && height == castObject.height && width == castObject.width;
} else {
//Not an instance of ThisClass, can't be equal
return false;
}
}