Can Enum parameterized type implement an interface?

I am trying to define a class that is parameterized with an Enum and the parameterized Enum must implement an interface. Here is some code:public interface MatchingEnum<E> {
    String match(); // This interface is not yet complete
public class EnumField<E extends Enum<E> & MatchingEnum<E>> {
    private E enumeration;
    public EnumField(E enumeration) {
        this.enumeration = enumeration;
    private static class Test {
        EnumField<TestType> c;
    public enum TestType implements MatchingEnum<TestType> { One, Two, Three;
        public String match() {
            return null;
}The problem is that the compiler complains that "classes cannot directly extend java.lang.Enum". If I delete the interface for the parameterized type(below) it compiles fine. Why can't the compiler handle this? Does specifying an Interface for an Enum bounds (E) force it to directly extend Enum? If so, why does the actual Enum compile?public class EnumField<E extends Enum<E>> {Blah, blah, blah... // This compiles
public class EnumField<E extends Enum<E> & MatchingEnum<E>> {Blah, blah blah... // This does NOT compile

A: Can Enum parameterized type implement an interface?

Compiled from "EnumField.java"
public class com.dartcontainer.mdc.picaps.textui.screen.widget.EnumField extends java.lang.Object{
public com.dartcontainer.mdc.picaps.textui.screen.widget.EnumField(java.util.List);
  Code:
   0:   aload_0
   1:   invokespecial   #2; //Method java/lang/Object."<init>":()V
   4:   aload_0
   5:   aload_1
   6:   putfield        #1; //Field enumeration:Ljava/util/List;
   9:   return
public static void main(java.lang.String[]);
  Code:
   0:   new     #3; //class com/dartcontainer/mdc/picaps/textui/screen/widget/EnumField$Test
   3:   dup
   4:   aconst_null
   5:   invokespecial   #4; //Method com/dartcontainer/mdc/picaps/textui/screen/widget/EnumField$Test."<init>":(Lcom/dartcontainer/mdc/picaps/textui/screen/widget/EnumField$1;)V
   8:   astore_1
   9:   getstatic       #5; //Field java/lang/System.out:Ljava/io/PrintStream;
   12:  new     #6; //class java/lang/StringBuilder
   15:  dup
   16:  invokespecial   #7; //Method java/lang/StringBuilder."<init>":()V
   19:  ldc     #8; //String t.m =
   21:  invokevirtual   #9; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
   24:  aload_1
   25:  getfield        #10; //Field com/dartcontainer/mdc/picaps/textui/screen/widget/EnumField$Test.m:Ljava/lang/String;
   28:  invokevirtual   #9; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
   31:  invokevirtual   #11; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
   34:  invokevirtual   #12; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
   37:  return
static java.util.List access$100(com.dartcontainer.mdc.picaps.textui.screen.widget.EnumField);
  Code:
   0:   aload_0
   1:   getfield        #1; //Field enumeration:Ljava/util/List;
   4:   areturn
}

Compiled from "EnumField.java"
public class com.dartcontainer.mdc.picaps.textui.screen.widget.EnumField extends java.lang.Object{
public com.dartcontainer.mdc.picaps.textui.screen.widget.EnumField(java.util.List);
  Code:
   0:   aload_0
   1:   invokespecial   #2; //Method java/lang/Object."<init>":()V
   4:   aload_0
   5:   aload_1
   6:   putfield        #1; //Field enumeration:Ljava/util/List;
   9:   return
public static void main(java.lang.String[]);
  Code:
   0:   new     #3; //class com/dartcontainer/mdc/picaps/textui/screen/widget/EnumField$Test
   3:   dup
   4:   aconst_null
   5:   invokespecial   #4; //Method com/dartcontainer/mdc/picaps/textui/screen/widget/EnumField$Test."<init>":(Lcom/dartcontainer/mdc/picaps/textui/screen/widget/EnumField$1;)V
   8:   astore_1
   9:   getstatic       #5; //Field java/lang/System.out:Ljava/io/PrintStream;
   12:  new     #6; //class java/lang/StringBuilder
   15:  dup
   16:  invokespecial   #7; //Method java/lang/StringBuilder."<init>":()V
   19:  ldc     #8; //String t.m =
   21:  invokevirtual   #9; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
   24:  aload_1
   25:  getfield        #10; //Field com/dartcontainer/mdc/picaps/textui/screen/widget/EnumField$Test.m:Ljava/lang/String;
   28:  invokevirtual   #9; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
   31:  invokevirtual   #11; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
   34:  invokevirtual   #12; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
   37:  return
static java.util.List access$100(com.dartcontainer.mdc.picaps.textui.screen.widget.EnumField);
  Code:
   0:   aload_0
   1:   getfield        #1; //Field enumeration:Ljava/util/List;
   4:   areturn
}

Similar Messages

  • How an array implements an interface?

    Section 5.5 of JLS (dealing with casting conversions) explains something like this:
    When an interface type S is cast to an array type T,
    "then T must implement S, or a compile-time error occurs. "
    I could not understand how an array can be made to implement an interface.
    I guessed it means that the component type of array implements the interface, and prepared the following test code. It doesn't compile.
    interface S{  }
    class C implements S{ }
    class Test {
    public static void main(String[] args) {
         C[] ac = new C[100];
         S s = new C();
         C[] ac1 = (C[])s; //doesn't compile
         System.out.println(ac1.length);
    Would someone open my eyes? Thank you

    dmbdmb Your correction compiled my code. But I was not looking for that, because the corrected code casts an array type to array type. Anyway, thanks.
    jverdYou are the man. Now I got it. Thank you.
    I think I will have to remember your simplified rule (Then S must be Serializable or Cloneable) rather than the JLS' rule. With your rule, I can write a test code like this, which compiles, and which shows that an interface type can be cast to array type.
    import java.awt.Point;
    import java.io.Serializable;
    class Test {
            public static void main(String[] args) {
               Point[] pa = new Point[100]; //any array of reference type
               Serializable s = new String("test");
               Point[] pa1 = (Point[])s; //compiles, though invalid at runtime
    }

  • Enum implementing Generic interface

    Is there any way to implement a generic interface and specify the types for each enum
    public interfact IPrimaryKeyType<C extends Object> {
    public enum PrimaryKeyType implements IPrimaryKeyType {
    SINGLE,
    COMPOSITE,
    There is no way to specify the type for each enum element. Though you can specify the type for all enums as below.
    public enum PrimaryKeyType implements IPrimaryKeyType<Object> {
    SINGLE,
    COMPOSITE,
    Any ideas?

    The best way to answer questions like this is to look at the spec: http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.9
    The answer seems to be that what you're trying to do is impossible.

  • How can I know a class which implements Runnable interface has terminated?

    Hello! I have a class which has implements Runnable interface, while I want to execute some operation when the thread has terminate in multithread enviroment.How can I know the thread has terminated?Does it give out some signal?Cant I just call my operation at the end of the run() method?

    I want to execute some operation when
    the thread has terminate in multithread enviroment....
    Cant I just call my operation at the end
    of the run() method?Sure. Before run() ends, invoke that other operation.
    How
    can I know the thread has terminated?Does it give out
    some signal?Not that I'm aware of, but you can do what you described above, or I believe a different object can call isAlive on the thread.

  • Which ssl type can i use to implement push notifications in safari

    Which ssl type can i use to implement push notifications in safari, i think there are different criteria which one can i use, what are the criteria to buy ssl keys
    are the ssl key used in a web site or in a web service or both

    Anyone?

  • How can my web service class implement an interface

    I am not able to write :
    webserviceclass implements interface
    I am using servicegen script to convert java file to the web service.But then also,if i add
    javaClassComponents="javaclass1,interface1"
    It is saying interface1 does not have any no arg constructor,so can't used in the web service.
    kindly tell how can i code my web service to implement an interface.

    This forum focuses on end-user support. You can find more web development help on the [http://forums.mozillazine.org/viewforum.php?f=25 mozillaZine Web Development board]. Separate forum, separate registration. Please note the tips in the Sticky Post at the top of the forum before posting.
    That said... Firefox honors the setting autocomplete="off" in the form tag. When this attribute is set, users should not be prompted to save the username/password, and it should not be filled automatically. (Is this what wasn't working??)
    https://developer.mozilla.org/en/How_to_Turn_Off_Form_Autocompletion
    Knowledgeable users can bypass this setting by running a script to strip this attribute. I doubt that very many users would do that, but if people have to log in very frequently, it is more likely to happen. Users also may use add-ons that manage passwords, and those add-ons might not honor the autcomplete="off" setting. I haven't used any such add-ons, so I don't know the situation there.
    I'm sure this isn't completely satisfactory but hopefully it helps to some extent.

  • Type param in interface referring to concrete class

    I want to declare a method in an interface that takes a type parameter that represents the concrete implementing subclass. I can live with something less than that, though I would like to avoid casting as much as possible...
    Here's my attempt at a simplified (though contrived) example:
    public interface Collectable {
        Collection<Collectable> collectMe();
    public class StampCollection implements Collectable<Stamp> {
        // Super-specialized collection for holding stamps
    public class Stamp implements Collectable {
        public Collection<Stamp> collectMe() {
            return new StampCollection(this);
    }This doesn't work because StampCollection is deemed to be incompatible with Collection<Collectable>. I'm assuming this is because the type parameter for the interface that StampCollection implements is Stamp, which is more specific than Collectable. Is that right? What are my options for fixing this (with minimal casting).

    Also, I see from some other reading that there seems to be some precendent for doing things this way (having a class implement an interface parameterized by the class itself). For example, on the Angelika Langer Java Generic FAQ linked to from the notice on this forum, she gives an example of
        class Pair<A extends Comparable<A> & Cloneable ,
                   B extends Comparable<B> & Cloneable >
          implements Comparable<Pair<A,B>>, Cloneable { ... } Which is a fairly complicated example for our purposes, but does seem to use the same strategy.
    Is this a common idiom?
    Michael

  • How to find out in program which all classes have implemented an interface

    Hello,
    I have created an interface and few classes are implementing the interface.
    I want to know in a program which all class have implemented the interface.
    Is it possible to find it out and how?
    Regards,
    Bikash.

    Hi Bikash,
    Read the Database view VSEOIMPLEM with where condition REFCLSNAME = Interface Name and version = 1.
    This would give you all the classes which have implemented the interface and are active...
    If you want to look at the values that the field version can have then see Type Group SEOC ans search for version....
    Hope this help...
    Regards,
    Sitakant

  • Generic classes with parameterized super class and interface

    Hello.
    I'm trying to write a generic class that looks like the following pseudo-code.
    public interface Interface {
        public Interface getSibling(...);
    public class Klass {...}
    public class MyKlass<T extends Klass, T2 extends Interface>
            extends T implements T2 {
        public T2 getSibling(...) {
            return this;
    }where Interface and Klass each have various extensions.
    I came across this problem, or challenge, while writing classes for testing my EJBs. I tried and failed various attempts.
    Is it possible to write generic classes of this nature in Java?
    If so, please tell me and others who are like me.
    Thanks in advance.

    No. That would not work.
    Beside being forbidden by the compiler, to my understanding, it cannot be done in theory either, as the parameterized types get bound at instantiation time and are not available for static reference, which both extends and implements require.

  • Class constructor that implements an interface returns an  "interface", why

    Hi,
    I am studying some code that I need to understand well. This code works, I just don't understand the following:
    A class was defined extending an interface as so:
    public class GeometricShape implements Area {
    // constructor
    public GeometricShape() {
    System.out.println('bla);
    In another file, GeometricShape class was instantiated as follows:
    public class ExampleUse {
    Area g = new GeometricShape();
    My qustion is, why does the code above expects "new GeometricShape()" constructor to return an interface of type Area?
    Can someone explain?
    thanks

    Can someone explain?When a class implements an interface, or extends another class, or when a interface extends another interface, it means that anywhere an instance of the parent class or interface is expected, the child can be used.
    Wherever a Mammal is expected, you can provide a Dog or Cat or Whale or Human or NakedMoleRat. Each of those is a mammal.
    If you say "give me some food," and you don't specify anything else, the person you're talking to can hand you a hamburger or an apple or a bowl of rice. Any of those will meet the requirements you put forth.
    This is how the OO "is-a" relationship maps to Java.

  • Can anybody explain me what is interface

    hi gurus
    can anyone explain me what is interface
    tahnk you
    regards
    kals.

    hi
    Interfaces are independent structures that allow you to enhance the class-specific public points of contact by implementing them in classes.
    Interfaces can be defined globally in the R/3 repository or locally in ABAP program
    Can define exactly same components in Interfaces as in Classes
    Unlike classes, Interfaces do not have instances, instead they are implemented by classes
    Implemented using INTERFACES statement in the declaration part of the class
    INTERFACES statement must be included in the PUBLIC SECTION of the class
    Different classes that implement the same interface can all be addressed in the same way.
    Interface references allow users to address different classes in the same manner.
    Interfaces can also be nested.
          Interfaces are the basis for polymorphism in classes, because they allow a single interface method to behave differently in different classes.
    If an interface is implemented in the class, the interface components are added in the public section of the class.
    A component comp of an interface intf, implemented in a class, becomes a fully-fledged member of that class, with the name intf~comp.
    Classes that implement interfaces must implement all of their methods.
         METHOD intf~meth.
         ENDMETHOD.
    Interfaces allow you to use different classes in a uniform way (polymorphism).
    Interface References
    Creating Interface Reference Variables
          DATA obj TYPE REF TO intf.
    Using this reference variable, you can access any of the components defined in the interface
    Nested Interfaces
    Interface can include one or more interfaces as components, which can contain interfaces themselves.
    Compound Interface : It includes other interface as its component.
    Elementary Interface : It does not include any interface as a component.
    All interface components of a compound interface have the same level
    A component interface exists only once even if it is used once more as a component of another component interface.
    Aliases : It can be used to assign alias names to the components of component interfaces, thus making them visible within the interface definition.
                  ALIASES <alias name> FOR intf~comp.
    Where, intf  = interface name and comp = Component name
    Accessing Objects using Interface References
    It is also possible to directly generate an object to which the interface reference
          variable points initially. In this case, the TYPE addition of the statement CREATE OBJECT must be used to specify a class that implements the interface.               CREATE OBJECT iref TYPE class.
    If the interface intf contains an attribute attr and an instance method meth, you can address them as follows:
    Using the class reference variable:
    Accessing an attribute attr: cref->intf~attr
    Calling a method meth: CALL METHOD cref->intf~meth
    Using the interface reference variable:
    Accessing an attribute attr: iref->attr
    Calling a method meth: CALL METHOD iref->meth
    Accessing Static Components of Interfaces
    Use the name of the interface to access constants within an interface. 
    Accessing a constant const: intf=>const.
    To access the other static components of an interface, use an object reference or the class class that is implementing the interface. 
    Accessing a static attribute attr: class=>intf~attr.
    Calling a static method meth: CALL METHOD class=>intf~meth.

  • Must implement specifik interface

    Hi,
    Im creating a game to force myshelf to code a game-engine, just for fun. In this game I have superclass for a basic unit. This class gets subclassed down to game-units. I have different type of game units, some more advanced than others (my game idea is like a manager game where choose between units similar to warcraft 3 units).
    To help the game-engine separate between the different type of units and the methods that can be used on them, I want to use differtent interface for the advanced units. This could be the start of the Wizard-class:
    public class Wizards extends BasicUnit implements SpellCaster {
    // method and variables that are specific for a wizard and dont excist in BasicUnit
    }Question is: How can I force a method in the game-enginge class to look if a used unit implements a certain interface, in this case the SpellCaster-interface?
    I know it should be possible, like the Comparable Interface. But how do i do it?
    If its a dumb question a link to the specifik documents would do, I read it myshelf. But of course I would be happy if someone have the kindness to write an example. Again, this aint homework, just a fun x-mas project for me.

    Ani_Skywalker wrote:
    Hi again Jverd,
    I google [visitor pattern|http://en.wikipedia.org/wiki/Visitor_pattern] and through that I also find [virtual function|http://en.wikipedia.org/wiki/Virtual_functions] . I can see how its usefull in this issue.
    Thought my main concern for the moment is about [composition over inheritance|http://blogs.msdn.com/steverowe/archive/2008/04/28/prefer-composition-over-inheritance.aspxI]. This is not what we learned in school when I took my first and second java courses. But I think the composition over inheritance strategy seems very sensible.
    jverd wrote:
    I don't really know your whole picture, but one option is to define a type that has those particular operations in common, and pass that type around when that's what you need. The subclasses could inherit from a base class that provides those, or implement an interface, and then add their own methods.But this is exactly what my class BasicUnit does. Maybe my english is bad and I misslead you, or maybe I should pick a better name for that class, like Unit. But what it does is to hold methods that every unit in the game use. Subclasses that need more methods adds that. The way you wrote your example is kind of how I've done it, as it seems to me. Could you tell me were you think I do wrong?It may have just been a misunderstanding on my part, as I don't know you and I don't know your requirements or the details of how you're implementing them.
    Or it may be that you're kind of close to doing it correctly, but not quite. The situation I described as a good approach can look a lot like the not-so-good way I thought you were doing it. I'm kind of sleep-deprived right now, so I don't have the energy or brainpower to go into any more detail at the moment.
    Good luck though. I expect somebody will continue helping you if you keep working at it.

  • Dummy Implementation of Interface

    Hi SAP Gurus & Mr Uwe,
    I'm interested with your discussion (Mr Uwe's commented) a while ago regarding dummy implementation of interface on Mar 25, 2008 12:52 PM.
    I saw this CL_EX_CRM_BUS20001_R3A class of mine has the same code as the topic that you commented.
    I would like to know about this dummy implementation of interface. Firstly is what is the use of this interface? I assume that this program is the main BADI so it should have all the parameters required for the next program which is in this case is the interface. Is that right?
    Thank you very much for your insightful explanation.
    With kind regards,
    Michael

    Hi Adrian,
    Did you have any success with your Trading Partner Directory?
    Our project includes ECC, CRM 5.1, SRM6.0 and Portal. We have this CRM scenario where a customer (manager) logs on and approves one of his employees, already self-registered, as a customer Portal user. UME seems to support that scenario, but it can only retrieve company information from a SRM backend system using a custom implementation of the Trading Partner Directory interface. In our case it is a CRM scenario and CRM Business Partners are not replicated on SRM.
    What is that SRM Trading Partner Directory interface and how much effort is involved in this custom implementation?
    Does the portal need to restart for new Companies to be added?
    Is there a better way to update the list of companies in UME from a list of CRM Business Partner?
    Thanks

  • Implementing several interfaces

    Hi All,
    Say I hace class A the implements a,b,c,d (a,b,c,d are interfaces).
    How can I use polymorphism in this case?
    a name1 = new A(...... or
    b name1 = new A(......
    or maybe c name1 = new A(.....)
    What is right?
    Thank u
    Eyal

    That depends on how you want to use the object.
    The point of implementing an interface is that it tells you that an instance of your class can be used an instance of the interface.
    In your case, it means your name1 could be declared as any one of the 4 types of interface your class A implements, or as just plain A.
    When declaring name1 as a, you can only use it as a, not as b, c, d or A. When declaring name1 as A, you can use it anywhere you want an A, a, b, c or d.

  • Extending a parameterized type

    Hi,
    I have a Shape class like this
    public abstract class Shape<T extends Comparable<T> & Cloneable> {
    public abstract void draw( Graphics g );
    Now I extend it like this
    public class GraphicLine<T extends Comparable<T> & Cloneable> extends Shape{
    private int x, y;
    public void draw( Graphics view ){
    //TODO
    // Default ordering
         public int compareTo( T shape ) {
    It seems this depends on the combination of two things. The actual inheritance of the class (which is according to the old rules ) and the inheritance of the parameterized type.
    I want my shapes to be cloned and compared with other shapes. The line
    public class GraphicLine<T extends Comparable<T> & Cloneable> extends Shape
    is used because it seems to serve my purpose eventhough the line
    public class GraphicLine extends Shape compiles. I don't understand the difference though. I am guessing that the second line will lose the type constraint I have established in the super type ??
    My other doubt is the compareTo method. Can anybody comment on this ? I want my lines to be compared based on the start and end points. The code compiles.
    Thanks
    Mohan

    Thanks. Yes. I did that but still have doubts
    Does the line
    public class GraphicLine<T extends Comparable<T> &
    Cloneable> extends Shape<T>
    mean that T on both sides of the 'extends' keyword
    are linked ? Is that the reason why it is Shape<T>
    and not Shape< T extends Comparable<T>?Yes, now the T on both sides of the 'extends' is known by the compiler to refer to the same type. The class being extended uses the simple declaration in extended class declarations. It's important to realize that the bounds imposed on the args to the base class are passed on to all derived classes: if Shape expects that all of its parmtypes are Comparable, then there is no way for you to declare any derived class of Shape without passing on that restriction.
    Now my method is
         public int compareTo( T shape ) {
         GraphicLine<T> line = ( GraphicLine<T> )shape;
    and it compiles.
    It'll compile, but I'd be suprised if a call to this method ever succeeds: it appears at first glance that it'll throw ClassCastException every time. In your declaration, T is Comparable but GraphicLine is not. Your method requires an object that is Comparable and Clonable, and GraphicLappears to be neither. If you're expecting to compare GraphicLines, then that's what you need to declare in the interface of the compareTo method.
    public int compareTo( GraphicLine<T> shape ) { ... }

Maybe you are looking for

  • IPod touch 5th generation deleted over 2000 of my songs

    Hi. I rarely resort to forums like this to fix a problem but I can't seem to find anyone who's had the same issue. Today I connected my iPod touch 5th generation to my Windows 8.1 PC to sync some songs onto it. I ripped some of them from a CD, others

  • Exporting to flash.... projector?

    The end result of my keynote slideshow is to have it burned on a CD and be able to be opened without needing FlashPlayer or Keynote. 1) Is there a way to export it as a flash projector file? 2) Is there a simple way that anyone of you know to convert

  • How do I change video dimensions?

    I took four SD (640 x 480) videos and, using "Scale" transformed each one by 50%. Then I put the four of them in a 2 x 2 arrangement in a new HD scaled video (1920 x 1080). It plays fine, but now I have the black vertical bars on each side. I'd like

  • Rotation of images imported and exported from Aperture

    Hi all, Recently whilst on Holiday I used my iPad as a storage for the camera via the camera kit. When I got home I synced the iPad with the Mac to get the images into iPhoto, and then imported them into Aperture. This is where the first problem occu

  • Battery iPhone 6

    i have been using my apps in the past 35 minutes, my battery has gone from 98% - 63%. I don't have other apps running or Bluetooth, wifi or location services turned on. It's all off yet my battery drains so much. Is this normal?