Question

This is probably basic java, but I'm new to the language, so please forgive me

I would like to extend the SetCustomName method of LivingEntity so that, in addition to it's normal steps, it will also throw a custom event/exception.

My attempts to do this result in errors regarding abstract, and can't have body.

In addition, I can't tell if this is a class or an interface, which means I'm not sure if I should use extends or implements.

Finally, I'm looking to add this functionality, rather then create a new object.

Can you please tell me if this is possible / steer me in the correct direction? Thank you very much for the help.

Was it helpful?

Solution 2

LivingEntity is an interface, CraftLivingEntity is a class. You can only extend a class, you implement an interface. The interface itself has no "normal steps".

If you can control whether CraftLivingEntity or a different class is instantiated, extend CLE as follows

public class MyCraftLivingEntity extends CraftLivingEntity {

    @Override // just a marker annotation to help catch inheritance issues
    public void setCustomName(String name) {

         super.setCustomName(name);

         // do whatever else
    }
}

and instantiate your class instead.

OTHER TIPS

Looked at your file you gave from GitHub..

LivingEntity is an interface and so you would have to implement it into another class because it's methods aren't allowed to be defined.

Just follow Emerson's overridden method, as it's correct. Put your custom code under the call to super.setCustomName(name);.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top