Cyclic inheritance when implementing inner interface in enum

后端 未结 3 1562
别那么骄傲
别那么骄傲 2020-12-14 14:16

I have the following implementation that gives a compiler error:

public enum FusionStat implements MonsterStatBuilderHelper {
    ATTACK {
        @Override
         


        
相关标签:
3条回答
  • 2020-12-14 15:07

    This would be because you are implementing (coding) the interface you are implementing (inheriting) inside of the class that is inheriting from that class.

    I wish I could make that sentence better...

    But here is a visual example.

    Class A implements Interface B {
    
        Interface B {
        }
    }
    

    As far as I know, this is not allowed. You need to define the interface outside of the class (in this case, an Enum).

    Like so:

    Interface B {
    }
    
    Class A implements Interface B {
    }
    

    Best practice is probably to break them up into different files.

    0 讨论(0)
  • 2020-12-14 15:15

    FusionStat is defined as implementing MonsterStatBuilderHelper, yet inside this enum you try to declare the interface MonsterStatBuilderHelper which extends MonsterStatBuilder.

    I suspect that what you want to do is simply define the method createBuilder() inside your enum.

    If you do actually want to define the MonsterStatBuilderHelper interface, this needs to be done outside the class/enum.

    0 讨论(0)
  • 2020-12-14 15:20

    You can see here what's mistake in your code ?

    public enum FusionStat implements MonsterStatBuilderHelper {
       protected interface MonsterStatBuilderHelper extends MonsterStatBuilder {
    
       }
    }
    

    first you are implementing MonsterStatBuilderHelper for FusionStat enum again inside the enum written one more time with the same name interface MonsterStatBuilderHelper which you are already implementing for top level enum so that's why you are getting cyclic inheritance error.

    You can see below some few cyclic inheritance example

    //1st example 
    class Person extends Person {}  //this is not possible in real world, a Person itself child and parent both ?
    
    //2nd example 
    class Human extends Person{}
    class Person extends Human{}
    

    Notes : cyclic inheritance is not supported in java because logically it's not possible.

    0 讨论(0)
提交回复
热议问题