• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Static method hiding

 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class A { static void sMeth( int i ) { System.out.println(i); } }

class B extends A { static void sMeth( int i ) { System.out.println(i); } }

We know that static methods cannot be inherited or overridden.
Above in class B the static sMeth() hides static method sMeth() in class A.

Why then is it not permitted for an instance method sMeth() in B to hide the static sMeth() in class A ?
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I guess this is the concept. Static methods are loaded during the compilation itself. So the methods already exists. Both the parent and the child static method exists in the run time environment in this case and the runtime system, uses the method of the child. Thus the parent static method is hidden.

Instance methods cannot be used to hide static methods because, instance method overriding is done during runtime, i.e. dynamic lookup. The required method of the Parent or the child is loaded at runtime dynamically. As this is not possible with static methods, instance methods cannot hide static methods.

I guess this is the difference between the hiding and overriding concepts.

Others please let me know if my thought process is correct.

Thanks,
Priya.
 
Don't count your weasels before they've popped. And now for a mulberry bush related tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic