• 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

Can Static method be overriden?

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

Need your help to understand, can we override static methods or not.
I have heard that the

static methods can never been Overriden

Is this right or wrong?

But when i execute the below code


output : Hi in 2nd static method
I have got no errors, the code has been sucessfully execute and override method has been called. please explain,
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please go through the url..
http://faq.javaranch.com/view?OverridingVsHiding
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What you just did it hidden the static method from super class, this is not called override. So static method can not override by subclass.
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static methods can't be overridden, but it can be REDIFNED..
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Friends,
This is not method overriding. You just declared two methods in the super class and sub class with the same name. The rule is STATIC METHODS CANNOT BE OVERRIDDEN.
In the given scenario the compiler considers about type of the referance, not the actual object assigned to it.
You can call static methods by using the class name.
If you want to print "Hello in 1st static method" -- use over.disp();
If you want to print "Hi in 2nd static method " -- overridedemo.disp();

Lakmal,
---------

SCJP 1.4 SCWCD 5.0
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So that means i have redefined the static method not override.
Anyways, thanks for your replies.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic