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.
java.lang.Object.Yes. A simple unit test can prove that the class hierarchy in your example is ORDER -> java.lang.Enum -> java.lang.Object:
public class EnumTest {
enum ORDER {
FIRST,
SECOND,
THIRD,
FOURTH
}
@Test
public void test() {
System.out.println(ORDER.class.getSuperclass().getSuperclass());
}
}
This will return
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running de.interassurance.service.EnumTest
class java.lang.Object
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.017 s - in de.interassurance.service.EnumTest
Results:
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
assertXXX() call to verify the assumption, and that ORDER.class.getSuperclass().getSuperclass() is not guaranteed to provide the desired information. What about assertTrue( ORDER.FIRST instanceof Object ); instead? Or a look into the Java Language Specification, telling you that all (instantiable) classes are derived from java.lang.Object (and also abstract classes, but not interfaces).assertTrue( ORDER.FIRST instanceof Object ); does not check whether the ORDER enum is an Object but rather whether the ORDER.FIRST enum constant is an instance of object. This is true only in this higgledy-piggledy job-lot of a world in which chance has imprisoned us.Object, even if we use the narrow sense of the word, e.g. abstract classes still are derived from Object, also hidden classes (like those implementing lambda expressions) are derived from Object too. Also array classes or java.lang.Void—everything derived from Object…
public class ORDER extends Enum<ORDER>, and cannot extend another class.enums are a predefined list of instances of (in this case)ORDERobjects. As all objects in java, by default they inherit fromObject. RunningSystem.out.println(ORDER.FIRST instanceof Object);printstrue.