I have defined my own enum, which will extend Java Enum Class. Does Java Enum Class extend Java Object Class?
enum ORDER {
FIRST,
SECOND,
THIRD,
FOURTH
}
I have defined my own enum, which will extend Java Enum Class. Does Java Enum Class extend Java Object Class?
enum ORDER {
FIRST,
SECOND,
THIRD,
FOURTH
}
Yes, in java any non-null is a reference for an instance of Object. In other words
aRef instanceof Object
is true
unless aRef
is null
regardless aRef
type is an enum
or a regular class.
Indeed, the enum
keyword "defines a class (called an enum type). The enum class body can include methods and other fields. The compiler automatically adds some special methods when it creates an enum." https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html
Thus, references for enum types have the same elementary properties as other "regular" objects.
Thus, you can make calls like:
ORDER obj = ORDER.FIRST;
System.out.println(obj.hashCode());
System.out.println(obj.equals(otherObj));
in the same way as using an object of a non-enum class.
Yes. A simple unit test can prove that the class hierarchy in your example is ORDER -> java.lang.Enum -> java.lang.Object:
This will return