• Post Reply Bookmark Topic Watch Topic
  • New Topic

Passing Super-type object to a Sub-type Reference!!!  RSS feed

 
S.R Paul
Ranch Hand
Posts: 30
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers!
The question is about passing Super-type object to a Sub-type Reference.
Check the code below.

How is this Possible since line 8 fails to compile?
Anyway 'm' will be assigned to an "Object"
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, because you're never actually calling that method. You're calling equals(Object), which is defined in the Object class. Your method, equals(MyClass) doesn't override it, it overloads it. Put a call to println() in your equals method, and you will see that it never actually gets executed.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
S.R Paul wrote:How is this Possible since line 8 fails to compile?

What Stephan said.

Tip: Always prefix overriding methods with the "@Override" annotation, viz:
//override equals()
@Override
public boolean equals(MyClass m){...


because you would then quickly discover that, as Stephan said, your method doesn't override Object.equals().

HIH

Winston
 
S.R Paul
Ranch Hand
Posts: 30
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.
Ya, I'm actually overloading and the original equals() is untouched.
Since, mother of all is "Object" and it has the equals(Object o), it can take any object. Ok!

Thank you, Stephan and Winston.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!