File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Method overriding - parent instance method vs child static method

 
Kaur Manpreet
Ranch Hand
Posts: 30
Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

The following code does not compile and throw compile time error stating that overriding method cannot be static.
In the overridding method rules there is no mention about using the static keyword

Fabric class cannot have two methods with the same name and no difference in arguments (not a legal overload for sure), its not redefining as the parent Thread class run method is not static, but I am unable to comprehend the concept of illegal override, please assist.

Is it one of the rules that we cannot use static non-access modifier in the overridden method?

Thanks

 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Section 8.4.8.2 of the Java Language Specification says:
It is a compile-time error if a static method hides an instance method.

That's exactly what you're trying to do in your example: your static run() method is trying to hide the run() instance method from class Thread that you're extending.
 
Kaur Manpreet
Ranch Hand
Posts: 30
Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand now that we cannot handle checked exceptions that are not being thrown.
However, we can always declare them as part of the method signatures even if they are not being thrown at the moment, as it might be used in future.

Thankyou all
 
Mike Simmons
Ranch Hand
Posts: 3028
10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kaur Manpreet wrote:I understand now that we cannot handle checked exceptions that are not being thrown.
However, we can always declare them as part of the method signatures even if they are not being thrown at the moment, as it might be used in future.

Thankyou all

Perhaps you were responding to this thread?
 
Kaur Manpreet
Ranch Hand
Posts: 30
Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mike, yes i was responding to that thread indeed
 
Mike Simmons
Ranch Hand
Posts: 3028
10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To add a little bit to Jesper's entirely correct response:
Kaur Manpreet wrote:In the overridding method rules there is no mention about using the static keyword

If both methods had been non-static, then the term would be overriding. If both methods were static, we would be talking about hiding. Since one is static and the other is not, it's not exactly clear what to call it, but it's not allowed.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic