• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

equals() overriding

 
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is from K&B
It is a simple program,but I cannot understand how objects are being compared.
First, the constructor is running for both the objects assigning them different values.
When the equal method is run it is passing object "two".
Does "this.moofValue" represent object "one" - if Yes why..Why not "two".
Why can't we write "one.moofValue"





[ April 21, 2008: Message edited by: Nabila Mohammad ]
[ April 21, 2008: Message edited by: Nabila Mohammad ]
 
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi nabila,

When we say this.moofValue, that means the moofValue of the current object that is running the equals() method, so in this case it's the object "one"
The "two" object is used as a parameter of the equals() method.
We can'nt write "one.moffValue", because your equals() method can be called by several others object, when you say "onemoffValue", you are hardcoding the calling object, that's why we use the "this".

Hope this will help
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Thanks..

I understood the first part..But dint get the second part..
About not calling one.moofValue()
 
Mamadou Touré
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

In you equals() method, when you say "one.moofValue()", that's mean you can't handle another object . Your equals() is NOT supposed only to hande the object "one". Suppose for instance you've created 2 others objects "four" and "five" and you wanna compare them, that would not be possible if you have coded "one.moofValue()" in your equals(), you could never compare the "four" and "five", but if you have "this.moofValue", the "this" could contain your "four" object.

Hope yhis will help
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not at all convinced it helps.

The keyword this refers to whichever object you happen to be inside at the moment. So if you call it from one.equals() then this means "one" and if you call it from two.equals() then this means "two" and if you call it from three.equals() then this means three. It means "itself."

In your equals() method, the "this" is actually superfluous; you could miss it out and there would be no difference in behaviour.

Did you write that equals method yourself, or copy it from somewhere? It won't work if you pass an object which is null, or isn't a Moof. I suggest you go to the beginners' forum and search for equals method This is one of the threads which came up. Note the links I quoted.
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mamadou Tour�:

In you equals() method, when you say "one.moofValue()", that's mean you can't handle another object .



What do you mean by you cannot handle another object.
Isn't that the regular way of calling a method.

Obj.method() -OR- obj.instanceVariable.


I agree with Campbell.
this refers to the current object ,in this case one
 
Mamadou Touré
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Nabila,

off course it's correct to write one.moofValue
Suppose you have the following two objects :

Moof three =new Moof(8);
Moof four =new Moof(4);

And your equals() methode is the following (putting one.moofValue instead of this.moofValue as you propose):


public boolean equals(Object o)
{
if(((Moof)o).getMoofValue()==one.moofValue){
return true;}
else{return false;}

}

So given the exemple below, do you think that we can compare the 2 objects with the equals() method as you suggest to write ?
According to you what happen when we write three.equals(four) ??

Don't forget when you override equals(), you do it to run for every Moof object you pass through it , ie not only the object "one" . I mean, you should not be very specific to one object.

If you have skype, feel free to add me, and call me . My skype is docteurracine, or msn with docteur@hotmail.com



Hope this will help.
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your time and patience!
But I think i ll just stick to the Forum.
It will help others as well

When i am writing one.MoofValue
I get the error "Cannot find the the variable"

Dont know about the overriding of equals but in the regular case
obj1.equals(obj2) checks for == and gives you true or false.

So is this specifically to do with equals() method or is ther some other problem ..
I mean you require and object to compare..
Does it matter whether its "this" or "one" ..as long as it is referring to the same thing.
Like you are using "((Moof)o).getMoofValue()" where o is an object being casted to Moof and that's perfectly acceptable..
[ April 21, 2008: Message edited by: Nabila Mohammad ]
 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class EqualsTest
{
public static void main(String[] args)
{
Moof one=new Moof(8);
Moof two=new Moof(4);
if(one.equals(two))
{
System.out.println("one and two are equal");
}
else
{
System.out.println("I am so sorry,they are not equal") ;
}
}
}

class Moof
{
private int moofValue;
Moof(int val)
{
moofValue=val;
System.out.println(val);//line1
}

public int getMoofValue()
{
return moofValue;
}
public boolean equals(Object o)
{
System.out.println(((Moof)o).getMoofValue()); //line2
System.out.println(this.moofValue); //line3
if(((Moof)o).getMoofValue()==this.moofValue)
{
return true;
}
else
{
return false;
}
}
}

o/p -

8
4
4
8
I am so sorry,they are not equal

Can you explain the output and alos line1, line2 and line 3
Please
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your basic problem here is that you don't see that ref one is defined in class EqualsTest. And equals method is defined in class Moof which knows nothing about ref one. That is why you get message "Cannot find...", simply put, Moof doesn't have variable named one.
Sooooooo, equals method can only operate on:
- object which contains it, referred in equals as this,
- and on object that is passed to it which is (Object o).
In EqualsTest you say:
if(one.equals(two))
so for equals defined in Moof class this means:
-that you are calling equals method on ref one, so that makes this refer to one
- and you are passing ref two to equals, so that makes (Object o) refer to two

Conclusion: on which ever ref you call equals method, it makes this refer to that ref. Which ever ref you pass to equals method it makes Object o refer to that ref.
So you have to make clear distinction between ref on which equals is called, and ref which is passed to equals.

regards,
Ivan Ivanic
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it! Thanks alot.
That was a simple...

Dinesh,

Why dont you tell me what you think is happening..
And i ll help you with it.
It's really simple....just read it carefully.
[ April 22, 2008: Message edited by: Nabila Mohammad ]
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
on which ever ref you call equals method, it makes this refer to that ref. Which ever ref you pass to equals method it makes Object o refer to that ref.
This means that one.equals() referes to one object and one.equals(two)
this referes object o rferes to two. Correct me if i am wrong.

what about this.moofvalue. it will contain both the values, so which will it print 8 or 4.
Can you explain this...
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Dinesh Tahiliani:
on which ever ref you call equals method, it makes this refer to that ref. Which ever ref you pass to equals method it makes Object o refer to that ref.
what about this.moofvalue. it will contain both the values, so which will it print 8 or 4.




I think you got it right ..partly any way.

When you write one.equals(two)-
it means one is calling the equals method.
so whenever you use "this" it will refer to one. as that is the current object.

In this case equals() is being called by one and is passing two
which is being stored in Object o.
Then you are typecasting the this o so that you can call the method of moof.

Hope that was helpful.
[ April 23, 2008: Message edited by: Nabila Mohammad ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic