• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using Enumerations

 
Puneet Mittal
Ranch Hand
Posts: 45
Eclipse IDE Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


hi in the above code i have defined a enum Test. in b and c i have defined a method as argument. now in the last of enum i have defined a test() method without a body. now if i remove this test() from enum Test, the compiler gives error at 1 and 2 in main class. so i am not getting why is it so???
[ August 18, 2008: Message edited by: PUNEET MITTAL ]
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It only accepts methods that are part of the interface of the entire enumeration. You can compare it to anonymous classes - you can define your own public methods, but nobody will know they are there.

Consider the following:
 
Puneet Mittal
Ranch Hand
Posts: 45
Eclipse IDE Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Rob, but what i mean to say is that, why do we require that test() method in the last, like is it necessary to give that test() method?.
 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What we do here is something like overiding the method defined in the Enum, inside the Enum constants. Correct me if I'm wrong.
 
Puneet Mittal
Ranch Hand
Posts: 45
Eclipse IDE Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes we are overriding the methods here, but what is the need to override these methods. why cant we read those methods directly in other class by just creating a object to it. like if i dont give test() in the last of enums then can i use the test() methods defined in enum constants b and c. if so then how and if we cannot use them then why???
please tell me this concept, i am totally confused in this.
 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. It's like a,b.. are act like instances of the Enum (actually when we say "Enum.Constant" it creates a new object of that type), what we do is overiding those methods defined in the Enum in it's constants. So those methods should be defined in the Enum.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic