Question:
Why we can not override static method?
Answer:
We can not override final method by definition, but we can override static method. Source: CoolInterview.com
We CaNNOT override a static method. See a static method has nothing to do with the instance of the class. So when u r using a static method using .(dot) it actually subsitutes the class name. Inheritance is a concept based on instances. So when overriding u r actually redefining the method. Consider the example which will made ur concept more clear: class X{ public static void do(){ SOP("I m method do inX"); } public void go(){ SOP("I m method go in X"); } }
class Y extends X{ public static do(){ SOP("I m method do inY"); } public void go(){ SOP("I m method go in Y"); } public static void main(String a[]) { X z = new Y(); z.do(); z.go(); } }
The output will be: I m method do inX //static is not overriden I m method go in Y //overridden Source: CoolInterview.com
Answered by: Nishant Jain | Date:
| Contact Nishant Jain
I think one can override static method. But the overriding method should also be static. i.e. one cannot override static method of Base class as a non static method in derived class. Source: CoolInterview.com
Answered by: milind vengurlekar | Date:
| Contact milind vengurlekar
static method means one per class not one per instance.Overridding is done based on the type of object, and static methods are attached to a class Source: CoolInterview.com
Answered by: Nilesh prakash | Date:
| Contact Nilesh prakash
Static methods can be hidden. It cannot be overriden Source: CoolInterview.com
Answered by: Edward Sudhahar | Date: 8/1/2009
| Contact Edward Sudhahar
Java does not encourage overriding the static method. Because static methods can be accessed directly from class with .(dot) operator. Hence class containing static methods does not required to create an instance to access its members.If u r using static methods just it represents the class name.Hence u cannot override static methods. Source: CoolInterview.com
Answered by: Karthick | Date: 2/21/2010
| Contact Karthick
Static methods are not appeared in sub classes.we can't inherited static methods.So Over riding is not possible with out inheritence. Source: CoolInterview.com
Answered by: ushasri | Date: 4/1/2010
| Contact ushasri
Because static method binding is done at compile time not at run time but static variable binding is done at run time not at compile time. That is the reason that static method are not participated in overriding process. Source: CoolInterview.com
Answered by: saunik | Date: 8/31/2010
| Contact saunik
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|