Jump to content

[1.7.10] What happens if I forget @Override?


American2050

Recommended Posts

I always wonder....

 

What happens if I forget the @Override on some method...

 

For example in a Block that extends Block, what would happen if I forget the @Override?

 

	@Override
    public MapColor getMapColor(int p_149728_1_)
    {
        return MapColor.stoneColor;
    }

 

If I just have:

 

    public MapColor getMapColor(int p_149728_1_)
    {
        return MapColor.stoneColor;
    }

 

Would it be a problem? Is @Override really important?

Link to comment
Share on other sites

Let's say you have an AwesomeBlock that extends Block. The following class prototyping is :

 

public class AwesomeBlock extends Block

 

The class Block contains a method called getBlockName(), that displays the name of the Block. By default, it displays "Block !". Because you extended Block to your AwesomeBlock, you can call the getBlockName() method from an AwesomeBlock object.

 

AwesomeBlock block = new AwesomeBlock();

block.getBlockName();

 

This will display : "Block !". Why ? Because you have not overriden the getBlockName() into your AwesomeBlock class. After all, you want the getBlockName() function to display "Awesome Block !", so you have to @Override the getBlockName() method into your AwesomeBlock to let it display "AwesomeBlock !".

 

In your AwesomeBlock class, you have :

 

@Override
public void getBlockName() {
  System.out.println("AwesomeBlock !");
}

 

and then, if you call the getBlockName() method from your AwesomeBlock, it will now display "AwesomeBlock !".  :) This is "overriding". See this link for more infos.

Squirrel ! Squirrel ! Squirrel !

Link to comment
Share on other sites

Ugh.

 

Seriously people, @Override is an IDE annotation.  It makes Eclipse (or IntelliJ) make sure that the function you have marked as override is actually overriding a function in the superclass hierarchy.

 

"Not putting the annotation" will do jack shit other than not alerting you to potential problems.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Let's say you have an AwesomeBlock that extends Block. The following class prototyping is :

 

public class AwesomeBlock extends Block

 

The class Block contains a method called getBlockName(), that displays the name of the Block. By default, it displays "Block !". Because you extended Block to your AwesomeBlock, you can call the getBlockName() method from an AwesomeBlock object.

 

AwesomeBlock block = new AwesomeBlock();

block.getBlockName();

 

This will display : "Block !". Why ? Because you have not overriden the getBlockName() into your AwesomeBlock class. After all, you want the getBlockName() function to display "Awesome Block !", so you have to @Override the getBlockName() method into your AwesomeBlock to let it display "AwesomeBlock !".

 

In your AwesomeBlock class, you have :

 

@Override
public void getBlockName() {
  System.out.println("AwesomeBlock !");
}

 

and then, if you call the getBlockName() method from your AwesomeBlock, it will now display "AwesomeBlock !".  :) This is "overriding". See this link for more infos.

 

Thanks Major Squirrel great explanation ;) What I wasn't sure about is what would happen if I forget to indicate that I'm overriding another method.

Link to comment
Share on other sites

If you leave out the annotation, then you won't be warned if you miss your target (misspell the method name, get the mixed case name wrong, use the wrong types for the parameters).

 

When you first write a program, you'll probably get it right without warnings. However, as a program ages, things can change. The parent's method might change, so you'd like warnings to pop up on supposed overrides. You might change a type somewhere, so you'd like to be warned that it no longer fits where needed.

 

So, the annotation is not *needed*, but you want to put it in wherever it fits so that you'll get a reminder whenever you (or vanilla code) might do something that would break its promise.

 

Note: There are a whole slew of vanilla methods (mostly involving block states) that changed between 1.7 and 1.8. Because I religiously annotated all of my overrides, I am now being alerted to each and every mismatch as I upgrade my mods to 1.8.

The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.

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.