Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between inner class and inner enum

 
Andry Dub
Ranch Hand
Posts: 43
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class MyClass {

abstract class A{} <- legal

abstract enum MyEn{MY,YOUR} <- illegal

final class B{} <- legal

final enum MyEn2{OUR,THEIR} <- illegal
}

Why inner class can be marked as abstract or final, but innec enum can't?
 
Kelvin Chenhao Lim
Ranch Hand
Posts: 513
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't make sense to declare an enum as abstract, since there will always be at least one concrete instance of the enum. For example, "abstract enum MyEn { MY, YOUR }" says that there exist two MyEn instances (MY and YOUR), whereas the abstract modifier claims that there cannot be any MyEn instances. This is a logical contradiction, and so Java defines this as a compile-time error.

Also, you cannot extend an enum type, so all enums are in some sense already final. However, they're not really final in the usual sense, because each enum constant can have a class body that overrides the members of the enum type. Because enums have this special "final yet not quite final" quality, Java deems it a compile-time error to use the "final" modifier with enums.

Hope this helps!
 
Andry Dub
Ranch Hand
Posts: 43
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Kelvin!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic