Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes About overriding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "About overriding" Watch "About overriding" New topic

About overriding

Narasimha Reddy

Joined: Oct 05, 2004
Posts: 11
Is it possible to override the static method.

Narasimha Reddy
Jay Pawar
Ranch Hand

Joined: Aug 27, 2004
Posts: 411
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 ]

Cheers,<br />Jay<br /> <br />(SCJP 1.4)<br />Heights of great men were not achieved in one day, they were toiling day and night while their companions slept.
Seema Manivannan
Ranch Hand

Joined: Sep 20, 2001
Posts: 128
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.


Seema Manivannan<br />Author and Trainer: Java Certifications<br /> <br />Whizlabs Software<br />Success, certified!<br />Global leader: J2EE certification exam preparation!<br /><a href="" target="_blank" rel="nofollow"></a><br /> <br />An ISO 9001-2000 certified company!
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
"narasimha" please read our JavaRanch Naming Policy and give your self two names like: <firstname><space><family name>.

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Consider Paul's rocket mass heater.
subject: About overriding
It's not a secret anymore!