开发者

Suppress "The method does not override the inherited method since it is private to a different package."

开发者 https://www.devze.com 2023-04-07 17:57 出处:网络
How can I suppress the following error with the @SuppressWarning annotation? The method Foo.trololo() does not override the inherited method from Bar since it is private to a different package

How can I suppress the following error with the @SuppressWarning annotation?

The method Foo.trololo() does not override the inherited method from Bar since it is private to a different package

So far as I can tell the only way is to blanket the entire method with @SuppressWarning("all") which I would prefer not to do.


For clarification: The naming and scope of both methods is a deliberate choice and the two classes were deliberately put开发者_JAVA百科 into different packages knowing the methods would not be visible to each other. I am merely looking to declare that I acknowledge what I am doing and would not like to be warned of it.


What it means (you probably know this already)

It means that, even though your method has the same name as a (non-private) method in the super class, it doesn't override that method.

Regarding the warning message

This is an Eclipse-specific warning. Nothing in the language spec says that it should produce this warning. It's an "IDE-specific feature" if you so like. Therefore there is no "generic" way of suppressing the message.

(Note for instance that javac does not produce this warning.)

How to disable this warning (in Eclipse)

(I know you are not looking for this, but some other visitor of this page may!)

To disable this warning, you go to

       Window -> Preferences -> Java -> Compiler -> Errors / Warnings

and set "Method does not override package visible method" to "Ignore".


This warning means that you have a package-private method trololo in Bar, and a trololo method in Foo, which extends Bar, but is not in the same package.

If your goal is to override the Bar.trololo method with Foo.trololo, then the Bar.trololo method must be made protected. If your goal is to have two different methods, it would be better to name the second one tralala to make it much clearer and avoid confusion.


If you want to Suppress Warning a specific project, in the Eclipse Package Explorer, right click the project -> Properties -> Java Compiler -> Errors/Warnings then Check "Enable project specific settings" -> Name shadowing and conflicts then set "Method does not override package visible method" to "Ignore". Rebuild the project and warning will go away. Done!!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号