• 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

Overriding

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
We know that a static method cannot be overridden to be non-static. I have two other doubts.
a) Can a non-static method be overridden to be static?
b) Can static methods be overridden at all?
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a) Can a non-static method be overridden to be static?
Nope. Check this out: http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#228745
b) Can static methods be overridden at all?
Static methods are said to be "hidden" by other static methods defined in subclasses but not overridden.
Overriding applies to instance methods
Hiding applies to static methods.
HIH
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kumar,
1. No. From JLS section 8.4.6.2:


If a class declares a static method, then the declaration of that method is said to hide any and all methods with the same signature in the superclasses and superinterfaces of the class that would otherwise be accessible to code in the class. A compile-time error occurs if a static method hides an instance method.


2. No. From JLS section 8.4.6.1:


A compile-time error occurs if an instance method overrides a static method.


along with the first part from 8.4.6.2 we can safely say NO.
Regards,
Manfred.
 
reply
    Bookmark Topic Watch Topic
  • New Topic