简体   繁体   中英

user defined enum with abstract methods decompiled as abstract class cannot be extended by any other class

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.

我们无法扩展这些枚举,因为它们的构造函数是私有的。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM