0

I've been using IntelliJ IDEA for my recent coding in java, especially Minecraft.

I tried extending the class AbstractSkeletonEntity which is an abstract class with one particular method that has been trouble for me recently:

abstract SoundEvent getStepSound();

in my own code, I try overriding the method:

public class SkeletonKnightEntity extends AbstractSkeletonEntity {



public boolean SkeletonCanSpawn = true;

public SkeletonKnightEntity(EntityType<? extends AbstractSkeletonEntity> entityType, World world) {
    super(entityType, world);

}

protected SoundEvent getAmbientSound() {
    return SoundEvents.ENTITY_WITHER_SKELETON_AMBIENT;
}

protected SoundEvent getHurtSound(DamageSource source) {
    return SoundEvents.ENTITY_WITHER_SKELETON_HURT;
}

protected SoundEvent getDeathSound() {
    return SoundEvents.ENTITY_WITHER_SKELETON_DEATH;
}

@Override
protected void playStepSound(BlockPos pos, BlockState state) {
    this.playSound(this.getStepSound(), 0.15F, 1.0F);
}

@Override
protected SoundEvent getStepSound() { return SoundEvents.ENTITY_WITHER_SKELETON_STEP;}
//^ here is the error

@Override
public void attack(LivingEntity target, float pullProgress) {

}
@Override
public void tickMovement() {
    super.tickMovement();
}
public static DefaultAttributeContainer.Builder createSKnightAttributes(){
    return HostileEntity.createHostileAttributes()

            .add(EntityAttributes.GENERIC_MOVEMENT_SPEED, 0.25D)
            .add(EntityAttributes.GENERIC_FOLLOW_RANGE, 30)
            .add(EntityAttributes.GENERIC_ATTACK_DAMAGE, 1)

            ;
}
@Override
protected void initGoals() {
    this.goalSelector.add(3, new FleeEntityGoal(this, WolfEntity.class, 6.0F, 1.0D, 1.2D));
    this.goalSelector.add(5, new WanderAroundFarGoal(this, 1.0D));
    this.goalSelector.add(6, new LookAtEntityGoal(this, PlayerEntity.class, 8.0F));
    this.goalSelector.add(6, new LookAroundGoal(this));
    this.targetSelector.add(1, new RevengeGoal(this, new Class[0]));
    this.targetSelector.add(2, new FollowTargetGoal(this, PlayerEntity.class, true));
    this.targetSelector.add(3, new FollowTargetGoal(this, IronGolemEntity.class, true));
    this.targetSelector.add(3, new FollowTargetGoal(this, TurtleEntity.class, 10, true, false, TurtleEntity.BABY_TURTLE_ON_LAND_FILTER));
}
protected void dropEquipment(DamageSource source, int lootingMultiplier, boolean allowDrops) {
    super.dropEquipment(source, lootingMultiplier, allowDrops);
    Entity entity = source.getAttacker();
    if (entity instanceof CreeperEntity) {
        CreeperEntity creeperEntity = (CreeperEntity)entity;
        if (creeperEntity.shouldDropHead()) {
            creeperEntity.onHeadDropped();
            this.dropItem(Items.SKELETON_SKULL);
        }
    }

}
}

the IDE tells me for the code to work, I have to declare SkeletonKnightEntity abstract or implement the abstract method getStepSound() which I already did. when I try overriding the getStepSound() method, it tells me it's not overriding anything from the superclass. does anyone know what could be causing this?

1
  • 2
    This is the problem: SoundEvent getStepSound(); (package visibility) != protected SoundEvent getStepSound().
    – paulsm4
    Commented Nov 11, 2021 at 20:37

1 Answer 1

4

When you override a method, you can increase its visibility, but you can't decrease it. You took a public method and overrode it with a protected one. That isn't allowed. The superclass promised that that method can be called from anywhere, and you gave it a method that didn't meet that promise. Remove the protected modifier and you'll be just fine.

You didn't explicit say, so I'll include this for completeness. I'm assuming the parent method is public. If it really doesn't have a modifier (like your question shows), then it gets the default visibility of "package-private". A package-private method can't be accessed at all outside of the package it was defined in, so if the writer of that class made an abstract package-private method, then they intended that no one outside of the defining package is allowed to subclass their class, so you won't be allowed to either.

See this post for a summary of the visibility options in Java.

5
  • thanks, but that didnt seem to work. still says that i'm not overriding it Commented Nov 11, 2021 at 20:36
  • I just realized an incompleteness in my answer. Please see the second paragraph I just added. Is the method you're overriding package-private? Commented Nov 11, 2021 at 20:37
  • yeah it is package private Commented Nov 11, 2021 at 21:13
  • Then unfortunately you're slightly out of luck. The author of the class intended that it only be overridden within the same package (kind of like the sealed keyword in Kotlin or Scala), and there's no way around that. Commented Nov 11, 2021 at 21:22
  • could i create my own class that copies it and use that? Commented Nov 12, 2021 at 1:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.