-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
}
  • It's a reference type / "Object", so yes. In fact, you might consider it akin to `public class ORDER extends Enum`, and cannot extend another class. – Rogue Jun 08 '22 at 11:16
  • 1
    Yes, every java class extends Object. See the documentation of [java.lang.Object](https://docs.oracle.com/javase/10/docs/api/java/lang/Object.html): "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." – OH GOD SPIDERS Jun 08 '22 at 11:16
  • 1
    As you can see from the documentation, yes: https://docs.oracle.com/javase/8/docs/api/java/lang/Enum.html – JustAnotherDeveloper Jun 08 '22 at 11:16
  • 1
    Yes. `enum`s 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`. – f1sh Jun 08 '22 at 11:16

2 Answers2

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.

Duloren
  • 2,395
  • 1
  • 25
  • 36
  • 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`. – tquadrat Jun 08 '22 at 11:43
  • 1
    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++ – Rogue Jun 08 '22 at 11:46
  • That was wrong originally … … now its fixed! – tquadrat Jun 08 '22 at 17:35
-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
fhossfel
  • 2,041
  • 16
  • 24
  • 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. – fhossfel Jun 08 '22 at 13:23
  • Sorry, my fault! Will fix it! – tquadrat Jun 08 '22 at 17:30
  • 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 Jun 08 '22 at 17:32
  • @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. – fhossfel Jun 08 '22 at 22:17
  • – `ORDER.FIRST` is an instance of the class `ORDER` that is derived from `java.lang.Object`. So `ORDER` is instantiable – proved by the fact that there are instances of that class – and that `instanceof` proves that `FIRST` is also an instance of `java.lang.Object` is what we wanted to achieve. – tquadrat Jun 09 '22 at 06:46
  • Eh, sorry, but no. The fact that there is an instance of an object does not prove that a class is instantiable. There is for this example this pattern of singletons. Your Java skills sound a little bit rusty so you may not have heard about it. But we can agree that the constants of an enum are of the type of the enum to which they belong. But this was a design decision of the Java language specification. It did not have to be that way. It is a valid case to ask that question of the TS. – fhossfel Jun 09 '22 at 10:20
  • A singleton is the one and only instance of a class – instance!! Before you come up with statements about rusty Java skills, you should learn some basics. When using buzzwords, it helps if you have understood them. A non-instantiable class is one with only a private constructor and no factory method – also called a utility class. A sample would be `java.nio.file.Files`. enum constants in Java are instances of the respective enum class – that *you* cannot create additional instances does not mean that the class is not instantiable … same for a singleton, by the way. – tquadrat Jun 09 '22 at 10:42
  • I am not sure if you are a native speaker of the English language but the word "instantiable" means "can be instantiated". And a Singleton cannot (and should not) be instantiated if done properly. But we are not arguing about the word "A non-instantiable class is one with only a private constructor and no factory method." I think this might fit your description: public class Singleton { private static final Singleton instance = new Singleton(); private Singleton() {} public static Singleton getInstance() { return instance; } } – fhossfel Jun 09 '22 at 11:03
  • 1
    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`… – Holger May 17 '23 at 14:22