Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

overriding static method

 
Vaibhav Chauhan
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read comments from Static Methods CAN NOT be Overridden .
 
Gowher Naik
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Check above code You will get your answer.
 
Vaibhav Chauhan
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 643
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Yes you are correct.To support your answer check code above.
 
Vaibhav Chauhan
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 643
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gowher i got it....
now i m clear. thanks for your support.
 
Gowher Naik
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks gowher for clarification.
 
Chandrasekhar Mangipudi
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We can Override the static methods with same name but we cant overload with same name.Override static methods are not actually overridden.
 
Dave Reinhardt
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks dave....that link is really helpful
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic