aspose file tools*
The moose likes Beginning Java and the fly likes Method overriding - parent instance method vs child static method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Method overriding - parent instance method vs child static method" Watch "Method overriding - parent instance method vs child static method" New topic
Author

Method overriding - parent instance method vs child static method

Kaur Manpreet
Ranch Hand

Joined: Feb 14, 2012
Posts: 30

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

Joined: Aug 16, 2005
Posts: 14347
    
  22

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Kaur Manpreet
Ranch Hand

Joined: Feb 14, 2012
Posts: 30

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

Joined: Mar 05, 2008
Posts: 3018
    
  10
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

Joined: Feb 14, 2012
Posts: 30

Thanks Mike, yes i was responding to that thread indeed
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
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.
 
wood burning stoves
 
subject: Method overriding - parent instance method vs child static method