Saturday, April 7, 2012

Can we override a static method ?

We can not override a static method.

Static method are associated with class and not the object, hence inheritance does not apply to static methods. Even though we can write a static method in the child class with same signature as the parent class but it will not be counted as overriding as a reference of the Parent class will always invoke the static method of the Parent class.

Example:

ClassA a = new ClassB();
a.someStaticMethod();

This type of method invocation will always invoke the definition in the parent class, and not the object instance of ClassB. Hence the two methods will exist separately.

No comments:

Post a Comment