This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes overriding static method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "overriding static method" Watch "overriding static method" New topic
Author

overriding static method

Vaibhav Chauhan
Ranch Hand

Joined: Aug 16, 2006
Posts: 115
static methods can't be overridden.

can anybody explain me the reason behind this. I mean why java does not provide the facility of overriding for static methods.
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
Read comments from Static Methods CAN NOT be Overridden .
Gowher Naik
Ranch Hand

Joined: Feb 07, 2005
Posts: 643

Check above code You will get your answer.
Vaibhav Chauhan
Ranch Hand

Joined: Aug 16, 2006
Posts: 115
thanks gowher...
what i interpreted from your code is that:
as static methods are called by the class name and not by the object to which a reference refers , that is why print() from base class will be called always because c is reference of Base class and it is immaterial for JVM that to which object does the reference 'c' refers.

please correct me if i am wrong.

thanks again.
Gowher Naik
Ranch Hand

Joined: Feb 07, 2005
Posts: 643

Yes you are correct.To support your answer check code above.
Vaibhav Chauhan
Ranch Hand

Joined: Aug 16, 2006
Posts: 115
once again thanks for such a quick response ....i was not expecting it that fast......
i agree with you. but please tell me which print() method would be called in case of following code.

// code

Test t=new Test();
t.print();

as per my last reply, print() from Test class should get called,if not then why is it so and if yes, then isn't it voilation of:
"static methods can not be overridden".
Vaibhav Chauhan
Ranch Hand

Joined: Aug 16, 2006
Posts: 115
gowher, i have found that it is the print() method of Test which gets called but isn't violation of that statement?
Gowher Naik
Ranch Hand

Joined: Feb 07, 2005
Posts: 643
Test t=new Test() ;
t.print();//print method of Test class will be print
Base b=new Base() ;
b.print();//print method of Base class will be print
Base t1=new Test();
t1.print();//print method of Base class will be print
Vaibhav Chauhan
Ranch Hand

Joined: Aug 16, 2006
Posts: 115
i agree with you.
but my question is doesn't it violate the following rule:
"static methods can not be overridden".

as here we are able to override print() method in following code.
// Test t=new Test();
// t.print();// overridding print() of Base class.

please clarify me.
Vaibhav Chauhan
Ranch Hand

Joined: Aug 16, 2006
Posts: 115
gowher i got it....
now i m clear. thanks for your support.
Gowher Naik
Ranch Hand

Joined: Feb 07, 2005
Posts: 643
"static methods can not be overridden"
but they can be redefine.
When you are declare same static method in child class as of base class
it doesnot mean you are overridding but it means you are redefining base method in child class.
Vaibhav Chauhan
Ranch Hand

Joined: Aug 16, 2006
Posts: 115
thanks gowher for clarification.
Chandrasekhar Mangipudi
Ranch Hand

Joined: Jan 29, 2006
Posts: 118
We can Override the static methods with same name but we cant overload with same name.Override static methods are not actually overridden.


Thanks & Regards,
ChandraSekharMangipudi
Dave Reinhardt
Ranch Hand

Joined: Aug 07, 2006
Posts: 54
Here is a link to our own sites FAQ that explains this topic very well. The term for using the same static method name/signature in an inheritance tree is "hiding". Overloading allows run-time polymorphism and hiding does not--that's the main difference between hiding and overriding.

javaranchFAQ
Vaibhav Chauhan
Ranch Hand

Joined: Aug 16, 2006
Posts: 115
thanks dave....that link is really helpful
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: overriding static method