-1

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
}
4
  • It's a reference type / "Object", so yes. In fact, you might consider it akin to public class ORDER extends Enum<ORDER>, and cannot extend another class. Commented Jun 8, 2022 at 11:16
  • 1
    Yes, every java class extends Object. See the documentation of java.lang.Object: "Class Object is the root of the class hierarchy. Every class has Object as a superclass. All objects, including arrays, implement the methods of this class." Commented Jun 8, 2022 at 11:16
  • 1
    As you can see from the documentation, yes: docs.oracle.com/javase/8/docs/api/java/lang/Enum.html Commented Jun 8, 2022 at 11:16
  • 1
    Yes. enums are a predefined list of instances of (in this case) ORDER objects. As all objects in java, by default they inherit from Object. Running System.out.println(ORDER.FIRST instanceof Object); prints true. Commented Jun 8, 2022 at 11:16

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

That's wrong! Any non-null reference is still just a reference!! What you may have meant is, that anything a non-null reference is referencing to will be an instance of java.lang.Object.
To be honest I'd let that one slide on semantics, but technically yes: the reference isn't the Object, it refers to an Object. References are fairly "out of sight/out of mind" within the language specification itself, compared to a language like C++
That was wrong originally … 😉 … now its fixed!
-1

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

9 Comments

Not sure what the real reason was for downvote, but what you recommend does not work. You cannot use instances with ORDER the way you suggest because ORDER is a class literal which is not an instance Object. You could use either ORDER.FIRST instanceof Object which checks the class of the individual enum value or you could use ORDER.class instanceof Object which would only prove that the ORDER.class-Object is an Object - not ORDER itself. The purpose of this test was to print out that the second superclass of a particular enum. And it does that ;-) No assert needed or wanted.
Sorry, my fault! Will fix it!
Not sure what the real reason was for downvote, but this answer deserved it: first, a JUnit test should use an 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).
@tquadrat: More to fix: " telling you that all (instantiable) classes are derived from java.lang.Object" Well, I hate to break this to you: Enums are not instantiable. "It is a compile-time error to attempt to explicitly instantiate an enum class" [Java Language Specification, 8.9] And your check 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.
Funny discussion about the meaning of “instantiable”. Maybe it would help to distinguish between “publicly” or “freely” instantiable and “privately” or “restrictedly” instantiable. However, this discussion was obsolete right from the start, as non-instantiable classes are also derived from the class 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
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.