0

I am trying to learn Enum when I came to know that when enums are decompiled they are final implicitly so cannot be extended by any other class but when any abstract method is added it asks for implementation of that method and decompiled it as abstract class so my question is that in this case it must allow this user defined enum to be extended by any other regular class.

below is my code which i have done on research purpose.

  1. Vehicle.java

     public enum Vehicle
     {
     car("car"), Bike;
    
     private String value;
    
     Vehicle(String value) {
     this.value = value;
     }
    
     Vehicle() {
    
     }
    
     public String getValue() {
     return value;
     }
    
     public void setValue(String value) {
     this.value = value;
     }
     }
    

decompiled as:

  public final class Vehicle extends java.lang.Enum<Vehicle> {
  public static final Vehicle car;
  public static final Vehicle Bike;
  public static Vehicle[] values();
  public static Vehicle valueOf(java.lang.String);
  public java.lang.String getValue();
  public void setValue(java.lang.String);
  static {};
  }
  1. City.java

      public enum City {
      Nagpur("np") {
       @Override
       void show() {
        // TODO Auto-generated method stub
       }
     };
    
     abstract void show();
    
     private String value;
    
     City(String value) {
      this.value = value;
     }
    
     City() {
    
     }
    
     public String getValue() {
      return value;
     }
    
     public void setValue(String value) {
      this.value = value;
     }
    }
    

decompiled as:

     public abstract class City extends java.lang.Enum<City> {
     public static final City Nagpur;
     public static City[] values();
     public static City valueOf(java.lang.String);
     abstract void show();
     public java.lang.String getValue();
     public void setValue(java.lang.String);
     City(java.lang.String, int, java.lang.String, City$1);
     static {};
     }

I have tried to recreate the scenario using regular classes which is working fine. below is the code.

  1. Enum.java

     public abstract class Enum<T extends Enum<T>> {
    
     }
    

2.Vehicle.java

     public abstract class Vehicle extends Enum<Vehicle> {
     private String name;

     Vehicle vehicle=new Vehicle() {

     @Override
     public void show() {
    // TODO Auto-generated method stub

     }
    };
    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }

    public abstract void show();
    }

3.Car.java

    public class Car extends Vehicle {

    @Override
    public void show() {
     // TODO Auto-generated method stub

    }

    }

Sorry for the long code and bad indention but I am really confused why user defined enums cannot be extended even after decompiling as abstract classes. Thanks in advance for any help.

diksha agrawal
  • 83
  • 1
  • 1
  • 6
  • Possible duplicate of [Why can I anonymously subclass an enum but not a final class?](http://stackoverflow.com/questions/16970615/why-can-i-anonymously-subclass-an-enum-but-not-a-final-class) – shmosel Feb 10 '17 at 07:33
  • What is your question heading at? You already know that the decompiler produced something that doesn’t match the original source code. In more colloquial words, that decompiler sucks. Now, why should the fact, that this source code has been generated by a decompiler that sucks, has any influence on whether the java compiler accept the source code? – Holger Feb 14 '17 at 17:44

1 Answers1

0

we cannot extend these enums because their constructors are private.

diksha agrawal
  • 83
  • 1
  • 1
  • 6