• 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 Doubt

 
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The output of Line1 is true??? Considering the equals method is not overridden in this class, the equlas() of the Object will be called, then why is it returning true??
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


looks overridden to me?
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, its Overloaded.


this is the signature for overriding equals().
 
Paul Stat
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abhi vijay wrote:No, its Overloaded.


this is the signature for overriding equals().



Sorry yes but even so, you're calling the overloaded method by passing it a reference to an eq object instead of a standard run of the mill object reference

Couldn't you change the overloaded equals method to do the following as a test?



Then if it prints false you'll know why
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abhi vijay wrote:No, its Overloaded.


this is the signature for overriding equals().



I think the covariant factor is coming into picture. Normally if you were to override the equals method, we would check if the object o is instance of eq or not.

so if condition will be like

However since you are passing the eq instance itself so JVM is taking the covariant factor into consideration and resolving the eq instance.
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't complicate yourselves thinking about covariant stuff, as far as I know covariant returns is a concept that applies to overriding (since Java SE 5 I believe.) This is simple overloading. Since one of the overloaded methods takes eq and the other Object, the call will resolve to the method with the most specific parameter type that is assignment-compatible with the type of the argument at the call (in this case eq.) First cast the eq reference to Object, and run your code again. See what you get then.
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ruben, I didnt understand.
 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Abhi, your program can acess two equals method.One that takes the Object as parameter and the other that takes the eq object.
And in your program you are sending eq object. Now the program has to choose one of the two. It would definetly call the method that takes eq object because thats the method which is more specific. If you send any other object then the method from the Object class will be called.
Hope i put my point clearly.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Abhi,

The class will have 2 methods:
public boolean equals(eq q) // Explicitly coded
public boolean equals(Object o) // Inherited from Object

Basically, you have 2 overloaded methods.

When equals gets called, the compiler looks at the type of the argument to decide which of the overloaded methods to call (overload resolution.)

If you call the method equals with an eq reference, there are two possible methods to call: equals(eq) and equals(Object). That is because eq is a subtype of Object. However, the most specific of the two methods is equals(eq), so that will be the one called.

On the other hand, if you call the method equals with an Object reference, the only method that can get called is equals(Object), because Object is not a subtype of eq, and that means equals(eq) can't accept that call.

Does that make sense now?
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh yes. It makes perfect sense. I had solved a question like this in Devaka's Simulator, but there it was invoked using an Object reference. So I got confused. Thanks.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No problem. Also, I pretty much repeated what James said. He must have submitted his answer while I was writing mine. Sorry there buddy!
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks James.
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ruben Soto wrote:No problem. Also, I pretty much repeated what James said. He must have submitted his answer while I was writing mine. Sorry there buddy!



 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Here size :2 that means when adding to the HASHSET the overloaded method is not considered.? Why so?
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
EDIT: NOT MUCH useful
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abhi wrote: Here size :2 that means when adding to the HASHSET the overloaded method is not considered.? Why so?




If you dive in HashSet.java, you will find that HashSet is calling



method.

 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So is there any way to call the overloaded equals method after the hashCode is executed.
(I guess Explicitily call the overloaded method from the hashCode does not make sense )
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

James Tharakan wrote: So is there any way to call the overloaded equals method after the hashCode is executed.
(I guess Explicitily call the overloaded method from the hashCode does not make sense )




No, for that you have to write a new JDK 10. You have to redesign HashMap.java code.

see this is written there in HashMap.put():



 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Punit Singh wrote:
No, for that you have to write a new JDK 10. You have to redesign HashMap.java code.



Hmmm...
 
It was the best of times. It was the worst of times. It was a tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic