Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

about enum

 
vasu chowdary
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can an enum be extended?
 
Tarun Kumar
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NO ENUM CAN NOT BE EXTENDED
 
Laura Montes
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

Well, it's clear for me that an ENUM cannot extend because I've tried and it doesn't compile but why not?

I found a tutorial in java.sun.com that says the following:

Note: All enums implicitly extend java.lang.Enum. Since Java does not support multiple inheritance, an enum cannot extend anything else.


I search this class in the API and I found that its signature.

public abstract class Enum<E extends Enum<E>>extends Object implements Comparable<E>, Serializable

Can anyone explain further, pls?
Am I right with what I found ?

Thanks & regards,
Lau
 
Paul Clapham
Sheriff
Posts: 21316
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, yes, what you found is true. An enum can't be made to extend some other class because it is already (implicitly) extending something.

But my impression is that the original question was whether something could extend an enum. (I could be wrong but that's what it looks like to me.) And that's the opposite question to the question you answered.
 
Laura Montes
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, I got it, they mean subclass an enum... well and Why not?. I don't see that the enum class is final.

Regards,
Lau
 
Dumitru Postoronca
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Line 1&2 compiles, line 3 does NOT (it says: x must be a class).
So, ENUMs ca[LIST]
  • implement interfaces
  •  
    Dumitru Postoronca
    Ranch Hand
    Posts: 46
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Line 1&2 compiles, line 3 does NOT (it says: x must be a class). Line 4 also does NOT compile (extends is not expected to be there).


    So,
  • ENUMs can implement interfaces
  • ENUMs can NOT extend anything (either class or another enum)
  • ENUMs can NOT be subclassed
  •  
    Ben Souther
    Sheriff
    Posts: 13411
    Firefox Browser Redhat VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    "vasu",
    Please check your private messages regarding an important administrative matter.
    -Ben
     
    Don't get me started about those stupid light bulbs.
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic