Jump to content

[1.15.2] What does "@Override" do?


axilirate

Recommended Posts

When a class/object is a child of another (it extends from it), it also has its fields and methods (only public and protected fields can be accessed directly).

If you want to have a different implementation of any of the parent methods, you override them.

For example, if you have a base class which describes all GeometricShapes and in it you have a method getArea().

You can override this method in the class triangles and rectangles which extend from GeometricShapes and you can make them return the respective areas.

Edited by Cerandior
Link to comment
Share on other sites

Hi

 

The reason you should use @Override (it's optional) is so that, when Forge or minecraft updates and the signature for a method changes, your compiler tells you that there is a problem. 

 

eg

 

public class ItemVariants extends Item
{
  // what animation to use when the player holds the "use" button
  @Override
  public UseAction getUseAction(ItemStack stack) {
    return UseAction.DRINK;
  }
}

 

When minecraft is updated, and getUseAction(ItemStack stack) changes to getUseActionAnimationType(ItemStack stack), or changes to getUseAction(ItemStack stack, Hand whichHandUsing), then your compiler will give you an error.

Otherwise your code will silently stop working because your item will now use the Item base method which is

   public UseAction getUseAction(ItemStack stack) {
      return stack.getItem().isFood() ? UseAction.EAT : UseAction.NONE;
   }

 

This happens a lot and from bitter experience I can tell you that @Override can save you hours of pain and frustration.

 

-TGG

 

 

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.