• Post Reply Bookmark Topic Watch Topic
  • New Topic

Confused about hiding  RSS feed

 
Casandra justina
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!
I was reading the "Oracle Certified Professional Java SE 7 Programmer Exams 1Z0-804 and 1Z0-805 by Ganesh/Sharma" and I ran into this paragraph:

"The equals() method should have Object as the argument instead of the Point argument! The current
implementation of the equals() method in the Point class hides (not overrides) the equals() method of the
Object class."

So far i was following and understanding why it was a problem to declare an equals() method with the argument Point (the class we were playing with) instead of Object, but what confused me was the fact that this method would hide the object one. I am not so sure now about what hiding actually means. I read the article about overriding vs hiding, but it is still not very clear to me.

I also tried to write some code of my one and it sees both the methods:

 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

In my opinion, the author is wrong. You don't hide methods, and as your test program illustrates, Object's equals() method is still available. But I'd be interested in other opinions.

Just an aside: when you post code, use the Code tags.
 
Casandra justina
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much

Knute Snortum wrote:

Just an aside: when you post code, use the Code tags.


And about this: sorry, i will make sure to do so in the future
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No need to apologize! It's your first post.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:In my opinion, the author is wrong. You don't hide methods, and as your test program illustrates, Object's equals() method is still available. But I'd be interested in other opinions.

Spot on, AFAIC.

@Casandra: I hope he/she/they went on to say that the way to avoid this is to add the @Override annotation to ALL methods that you intend to override.
In your case:would then give you a compiler error, because the method is not overriding anything.

It's probably worth pointing out that this is a VERY common beginner's mistake - particularly with equals() (I've done it myself more than once) - so it's well worth remembering that @Override.

HIH

Winston
 
Casandra justina
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They do talk about the annotation. I've played with it a bit and I see how helpful it is.
Thank you very much for the answer, i think i understand it now .
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Casandra justina wrote:Thank you very much for the answer, i think i understand it now .

You're most welcome.

Winston
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I added the code tags as people suggested earlier. Doesn't it look better now.
 
Casandra justina
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote: I added the code tags as people suggested earlier. Doesn't it look better now.


Absolutely!

Campbell Ritchie wrote:Welcome to the Ranch


Thank you, i like it here: people are nice and helpful, already feeling at home ;)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!