0

In comparing two objects.

class date
{
private int a,b;
public date(int a,int b)
{
    this.a=a;
    this.b=b;
}
 public boolean equals(Object compared) {
    if (this == compared) {
        return true;
    }
    if (!(compared instanceof date)) {
        return false;
    }
    if (this.a == compared.a &&this.b == compared.b) //Error - cannot find the variable a and b
    {
        return true;
    }
    return false;
}
public static void main(String args[])
{
    date abc=new date(14,20);
    date xyz=new date(14,21);
    if(abc.equals(xyz))
    {
        System.out.println("Objects are equal");
    }
    else
    {
        System.out.println("Objects are not equal");
    }
}}

In the above program i am comparing two objects. I have added a comment line(//)to show which line was giving error. Why is this error coming? I am simply giving the reference of xyz to compare. it should work. when I am adding this line the program is working.(ignore "simpledate" by that i meant "date"

What I know is we cannot change the type of object once we have created it. if we want to compare two objects we can simply do it by "if(abc.a==xyz.aa &&abc.b==xyz.b)" Why in above program do i have to change the type?

1 Answers1

0

Cast to the right class is required

Just update the line to:

if (this.a == ((date)compared.a) &&this.b == ((date)compared.b) ) 

Or else you're comparing apples to oranges (dates to Objects).

You must see that equals(Object compared) will convert (or cast) your parameter compared to Object, so you need to downcast compared again to date inside the method.

The scope at the main method is different: You have declared that both variables are of type date. No need to cast there. But there is no way for the compiler to know the type of compared inside of the method equals(Object compared) if you're specifically asking to convert the parameter to Object.

If you really want to avoid downcasting compared, you need to change equals(Object compared) to equals(date compared). But it seems that you're trying to override the Object.equals() method, so you need to downcast inside the method.

Also: try refactor your code: All classes names must start with uppercase ( Date instead date). Object instances of classes start with lowercase.

  • But why do i have to change. When i removed the whole equals method and in main method i wrote "if(abc.a==xyz.a && abc.b==xyz.b) then it is working. I even tried of keeping the equals method and wrote "date compared" as parameter and it did not require changing the type. so if two objects are of different class type we have to use type casting? but we cannot change the type of object right?? – Shubham Sinha Feb 25 '21 at 14:01
  • Your code will result in a compilation error. You should do `if (this.a == ((date) compared).a && this.b == ((date) compared).b)` –  Feb 25 '21 at 14:07
  • That is what i am asking. WHY??? why changing the type. we are giving the reference of xyz to compared. so it can access the variables of xyz right?? – Shubham Sinha Feb 25 '21 at 14:10
  • @ShubhamSinha while you are passing in a `date` right now the method `equals` can be called with any `Object`, that is why there is an `instanceof` check to return false if the types do not match. Afterwards you can and should safely cast the argument that was passed in because now you can be sure the type is correct - the compiler cannot know that, therefore the cast is needed. – luk2302 Feb 25 '21 at 14:39
  • @ShubhamSinha: Why? because a cast to the right class is required, as the title of the answer says. I have added more explanation to the answer. – Victor Polo De Gyves Montero Feb 25 '21 at 15:11
  • So if we have to compare two objects who are of two diff types l, then we have to type cast any one??? but I heard we cannot change the type of object. Also when i m sending date type object as parameter to compared. The reference of "xyz" is copied to "compared", so compare should be able to access the memory of xyz reference. – Shubham Sinha Feb 26 '21 at 13:00
  • 1
    @ShubhamSinha https://stackoverflow.com/questions/13405377/how-does-java-object-casting-work-behind-the-scene the compiler simply does not care at all what you pass in, the parameter is declared as `Object` and that is all the compilers knows and rightfully complains. – luk2302 Feb 26 '21 at 16:31