Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

enums

 
Santhi Bharath
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can we override equals and hashcode methods in enums?
 
chander shivdasani
Ranch Hand
Posts: 206
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
No problem Santhi.
 
Santhi Bharath
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
 
Bert Bates
author
Sheriff
Posts: 8900
5
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 8900
5
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic