Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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

About overriding

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it possible to override the static method.
 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No you cannot override static method. Static method in derived class will hide the static method in the parent class.



Run the above code and see the output. The output should explain how static methods hide the base implementation.
[ December 03, 2004: Message edited by: Jay Pawar ]
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can redefine the base class static method in your derived class, however this would not be overriding.

For example, assume you have a static method- static void f()- in your base class as well as the derived class, and you invoke it as shown.

Base b=new Derived();
b.f(); // this will invoke the method in the base class only

This is because static methods belong to the class and not to the object, and cannot be overridden. The method invoked is chosen according to the declared type of the reference variable.

Also, note that base class static methods cannot be redefined in the derived class as non static and vice versa. This will cause a compile time error.

Thanks,
Seema
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"narasimha" please read our JavaRanch Naming Policy and give your self two names like: <firstname><space><family name>.
Thankyou,
-Barry
 
Everybody's invited. Except this 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