Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-4641

Defining an abstract method in enum

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.7.6
    • 2.0.8, 2.1.3, 2.2.0-beta-1
    • Compiler
    • None
    • Mac OS X 10.6.6
      IntelliJ 10.0.1

    Description

      In Java you can define abstract methods for an enum type to override it with a concrete method in a constant-specific class body (see Effective Java, 2nd Ed., page 152). When I try to do the same in Groovy I get a compilation error:

      Can't have an abstract method in a non-abstract class. The class 'Day' must be declared abstract or the method 'java.lang.String getAction()' must be implemented.

      This compilation error can be reproduced with this enum:

      enum Day {
         SUNDAY {
            String getAction() {
               'Relax'
            }
         },
         MONDAY {
             String getAction() {
                'Work'
             }
         }
      
         abstract String getAction()
      }
      

      An enum in Groovy should provide the same behavior for enums as Java.

      Attachments

        1. DayTest.groovy
          0.5 kB
          Benjamin Muschko

        Activity

          People

            paulk Paul King
            subzero66 Benjamin Muschko
            Votes:
            16 Vote for this issue
            Watchers:
            9 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: