• 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

enums

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can we override equals and hashcode methods in enums?
 
Ranch Hand
Posts: 206
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nopes. You cant. The methods equal and Hashcode is defined Final. So you cant Override them. Refer Java Docs for more details.
 
Santhi Bharath
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
chandar ,

in the Object class the methods has not declared final.if they are how can we compare strings correctly.how can you use your classes as keys in
HashXXX.

ok, in precise my question is public enums are extends Object class or not?
 
Santhi Bharath
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry chandar,

now i can understand that enums are implicitly extends java.lang.Enum not java.lang.Object and in java.lang.Enum class equals and hashcode are declared final
 
chander shivdasani
Ranch Hand
Posts: 206
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The String class is final. It extends Object Class and overrides Equals and Hashcode method. The reason it is final is because no one can change the implementation details.

Similarly Enum has also given its own definition of Equals and Hashcode methods and made it final, so that no one can change their behavior.

And Enums extend java.lang.Enum
[ September 26, 2008: Message edited by: chander shivdasani ]
 
chander shivdasani
Ranch Hand
Posts: 206
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No problem Santhi.
 
Santhi Bharath
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why they have implemented the equals() method in java.lang.Enum in such a simple fashion.it contains a single statement ,the definition is:

public final boolean equals(Object other) {
return this==other;
}

here it is just using '==' operator why this much simple???
 
chander shivdasani
Ranch Hand
Posts: 206
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Santhi,

Can you tell me from where did you find the implementation of equals method in Enum. I am unable to find it
 
Santhi Bharath
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in the zipped src folder which comes with jdk1.5.0_15 .and in java.lang.Enum class.this is the fourth method of Enum class after toString() method and befors hashCode() method. hope this is fine.
 
Santhi Bharath
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
don't open with notepad.open with wordpad or it is far better to use any IDE.
i prefer NetBeans
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm curious to know why you'd want to?
 
Santhi Bharath
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
upto me its very helpful to see the source code for understanding the things well.how the generics are implemented in collections.what are the method signatures for crucial methods.what are the exceptions thrown by a particular method.what are the interfaces particular classes implementing....wow! how many i can say.finally to do R&D on java?
 
Bert Bates
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Santhi,

That's a good approach, and I'd suggest also that you study the API "as is". By that I mean study the API WITHOUT diving into the code - the guys who wrote the API have deep understanding of how to create good OO design. Part of the reason that the SCJP exam team put API topics in the exam is to give candidates an opportunity to study how the API is designed. In many cases you can view the design of the API itself as an example of good OO design principles. (Okay, I'm sure you can all find examples of bad design in the API - let's not go there :roll: ).

The reason I bring this up is because it kind of seems like the original question in this thread could be viewed as an attempt to somehow subvert the design intent of the language - in the end that will rarely be a good approach.
 
Santhi Bharath
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
bert,

i am very glad to here a suggestion from you,considering your experience and the books you wrote, i will definitely see this thing as a must to do.a warm thank you for your suggestion bert.
 
To do a great right, do a little wrong - shakepeare. twisted little ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic