• 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

Implementing equals() Method

 
Ranch Hand
Posts: 115
Firefox Browser Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??? )
 
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moof extends Object implicitly! so Moof is an Object. object is a super class of all the classes
 
Ranch Hand
Posts: 76
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok
 
There were millions of the little blood suckers. But thanks to this tiny ad, I wasn't bitten once.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic