• 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

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

 
Ranch Hand
Posts: 30
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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"
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic