File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes enums Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "enums" Watch "enums" New topic
Author

enums

Santhi Bharath
Ranch Hand

Joined: Jun 03, 2008
Posts: 75
can we override equals and hashcode methods in enums?


thanks and regards<br />Santhi Bharath<br />SCJP 5.0, SCWCD 5.0
chander shivdasani
Ranch Hand

Joined: Oct 09, 2007
Posts: 206

Nopes. You cant. The methods equal and Hashcode is defined Final. So you cant Override them. Refer Java Docs for more details.


Enjoy, Chander
SCJP 5, Oracle Certified PL/SQL Developer
Santhi Bharath
Ranch Hand

Joined: Jun 03, 2008
Posts: 75
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

Joined: Jun 03, 2008
Posts: 75
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

Joined: Oct 09, 2007
Posts: 206

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

Joined: Oct 09, 2007
Posts: 206

No problem Santhi.
Santhi Bharath
Ranch Hand

Joined: Jun 03, 2008
Posts: 75
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

Joined: Oct 09, 2007
Posts: 206

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

Joined: Jun 03, 2008
Posts: 75
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

Joined: Jun 03, 2008
Posts: 75
don't open with notepad.open with wordpad or it is far better to use any IDE.
i prefer NetBeans
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8813
    
    5
I'm curious to know why you'd want to?


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Santhi Bharath
Ranch Hand

Joined: Jun 03, 2008
Posts: 75
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

Joined: Oct 14, 2002
Posts: 8813
    
    5
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

Joined: Jun 03, 2008
Posts: 75
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.
 
Consider Paul's rocket mass heater.
 
subject: enums