Reputation: 4360
So according to the java docs
All enums implicitly extend java.lang.Enum. Because a class can only extend one parent, the Java language does not support multiple inheritance of state, and therefore an enum cannot extend anything else.
But in java we also know:-
All classes in Java extend java.lang.Object class implicitly
But we can obviously extend Classes in java. Since the class we extend is itself extending from Object
(hence it does not cause multiple inheritance, Or state it as:- If you do not extend any other class extend from Object
else extend from class XYZ
that extends from Object
)
Is the explanation of the java docs incorrect for the fact that Enums are not able to extend other classes/enums etc?
Am I missing some silly point?
Upvotes: 2
Views: 147
Reputation: 5318
Run this code and you'll get the answer:
System.out.println(MyEnum.class.getSuperclass());
System.out.println(MyEnum.class.getSuperclass().getSuperclass());
public enum MyEnum {
}
Output:
class java.lang.Enum
class java.lang.Object
And on top of that consider that multiple inheritance is not allowed in Java. I.e. MyEnum
already extends Enum
while Enum
already extends Object
. There's no room for any other extension in this chain.
Upvotes: 1
Reputation: 421090
It simply means that you can not let your enums extend anything else than the implicitly extended Enum
.
When it says Java does not allow extending multiple classes, it means that you can't have
.--------. .----------.
| Enum | | YourBase |
'--------' '----------'
^ ^
\ /
\ /
.----------.
| YourEnum |
'----------'
THIS on the other hand, is entirely ok:
.--------.
| Object |
'--------'
^
|
.--------.
| Enum |
'--------'
^
|
.----------.
| YourEnum |
'----------'
When the documentation says
All classes in Java extend java.lang.Object class implicitly
It simply means that each class either extends Object directly, or indirectly through it's super class.
Upvotes: 5