Cyclic inheritance when implementing inner interface in enum

JavaInterfaceEnums

Java Problem Overview


I have the following implementation that gives a compiler error:

public enum FusionStat implements MonsterStatBuilderHelper {
    ATTACK {
        @Override
        public MonsterCard.MonsterCardBuilder safeCreateBuilder(final MonsterCard baseMonsterCard, final MonsterCard fusedMonsterCard, final FusionCard fusionCard) {
            Objects.requireNonNull(baseMonsterCard);
            Objects.requireNonNull(fusedMonsterCard);
            Objects.requireNonNull(fusionCard);
            if (baseMonsterCard.equals(fusedMonsterCard)) {
                throw new IllegalArgumentException("baseMonsterCard and fusedMonsterCard need to be different");
            }
            return new MonsterCard.MonsterCardBuilder(baseMonsterCard)
                    .attack(baseMonsterCard.getAttack() + (fusionCard.getFusionPower() * fusedMonsterCard.getAttack()));
        }
    },

    HITPOINTS {
        @Override
        public MonsterCard.MonsterCardBuilder safeCreateBuilder(final MonsterCard baseMonsterCard, final MonsterCard fusedMonsterCard, final FusionCard fusionCard) {
            Objects.requireNonNull(baseMonsterCard);
            Objects.requireNonNull(fusedMonsterCard);
            Objects.requireNonNull(fusionCard);
            if (baseMonsterCard.equals(fusedMonsterCard)) {
                throw new IllegalArgumentException("baseMonsterCard and fusedMonsterCard need to be different");
            }
            return new MonsterCard.MonsterCardBuilder(baseMonsterCard)
                    .maximumHitpoints((int)(baseMonsterCard.getMaximumHitpoints() + (fusionCard.getFusionPower() / 100d * fusedMonsterCard.getMaximumHitpoints())))
                    .hitpoints((int)(baseMonsterCard.getHitpoints() + (fusionCard.getFusionPower() / 100d * fusedMonsterCard.getHitpoints())));
        }
    };

    protected interface MonsterStatBuilderHelper extends MonsterStatBuilder {
        default MonsterCard.MonsterCardBuilder safeCreateBuilder(final MonsterCard baseMonsterCard, final MonsterCard fusedMonsterCard, final FusionCard fusionCard) {
            return createBuilder(baseMonsterCard, fusedMonsterCard, fusionCard);
        }
    }
}

@FunctionalInterface
interface MonsterStatBuilder {
    MonsterCard.MonsterCardBuilder createBuilder(final MonsterCard baseMonsterCard, final MonsterCard fusedMonsterCard, final FusionCard fusionCard);
}

It gives a cyclic inheritance error on the first line involving FusionStat.

I don't exactly see what is going on. I had first implemented an abstract class and wanted to let the enum extend that, until I realized that enums cannot extend classes. Now I try to (ab)use the default methods in Java 8.

I am interested in the thought process about why my code does not compile, I was trying to remove code duplication (still have to do so), by pulling the duplicated code inside safeCreateBuilder.

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

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.

Solution 3 - Java

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionskiwiView Question on Stackoverflow
Solution 1 - JavaQuestionMarcsView Answer on Stackoverflow
Solution 2 - JavaAnkit BhartiView Answer on Stackoverflow
Solution 3 - JavaDannyView Answer on Stackoverflow