• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Implementing equals() Method

 
sumedha rao
Ranch Hand
Posts: 115
Firefox Browser Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,

I have started with chpt 7 and came accross this code in k and b,pg546...where i dint understand the last part of it....public boolean equals(Object o){........}



please explain i really dint understand(how can o be an instance of Moof??? )
 
Swastik Dey
Rancher
Posts: 1602
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Object class is the parent of all, I hope this gives you the answer. To add more if this rule was not there, probably it won't have allowed to to call the equals method of with reference of Moof, because the method expects Object reference type. Correct me if I am wrong.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moof extends Object implicitly! so Moof is an Object. object is a super class of all the classes
 
Matthew Raw
Ranch Hand
Posts: 76
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With the help of runtime polymorphism, the Object o which is an argument to the equals method, can be ANY object - for example, one can call

etc. This would result in the condition o instanceof Moof = false and thus the method equals() would return false. This is the prefered implementation, used in the Java API - when calling equals to compare two objects of different class hierarchies, the equals method returns false.
So the method equals() can possibly return true only if the passed object o is instanceof Moof.
The instanceof test is important, because in the "if" clause, you make a down-cast and you want to avoid RuntimeException (in this case, ClassCastException).
Hope this helps.
(It may be helpful for you to review runtime polymorphism and down-casting in the K&B book.)
 
sumedha rao
Ranch Hand
Posts: 115
Firefox Browser Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok..ya right i understood about the downcasting thing...but why do we need the comparing in the 2nd expression when the if condition is satisfied with the 1st exp(downcasting) itself???

)
 
Swastik Dey
Rancher
Posts: 1602
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the first expression, you are first checking whether the passed object is Moof or not. If it is a Moof object, then in the second expression you are checking whether both the moof objects contain the same value or not.
 
sumedha rao
Ranch Hand
Posts: 115
Firefox Browser Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic