• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Query with overriding static methods

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I tried the following piece of code



I get compiler error saying "doStuff() in B cannot override doStuff() in A; overridden method does not throw java.io.IOException"

Since static methods cant be overridden the rules of overriding shouldnt apply, ie the doStuff() method in B is not an overridden version of doStuff() in A.
Hence doStuff() in B should be able to throw a new checked Exception (SQLException in this case) even though doStuff() in A throws a different checked Exception, ie a checked Exception that is not a part of the inheritance tree of the Exception that doStuff() in B tries to throw.

I also tried making the doStuff() method in A protected and the one in B private. Again I got a compiler error saying "Not possible to override doStuff() in B. Attempting to assign a weaker privilege level."

In both cases the compiler seems to be treating it as an attempted override gone wrong even though both methods are static.

Can anyone point out the flaw in my reasoning coz I am truly stumped by it...

Any help is appreciated.
Thanks in advance.


 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chaitanya Lele wrote:
Since static methods cant be overridden the rules of overriding shouldnt apply, ie the doStuff() method in B is not an overridden version of doStuff() in A.


Here is where your logic is faulty. The rule is that you can not override static method. It does not mean that compiler will treat method B as not overriding A, but means that compiler will not allow declaration of static method with same signature in subclass. So you can not even attempt to write static method with same signature.
Hope this helps.
Ivan
 
Chaitanya Lele
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Ivan

Missed that one...
the example in K&B also renamed the function in subclass as dostuff()
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic