Can we override a static method in child class? || Famous Interview Question

Can we override a static method in child class? || Famous Interview Question

Can we override a static method in child class? || Famous Interview Question

Static methods can not be overridden in the exact sense of the word, but they can hide parent static methods

In practice it means that que compiler will decide which method to execute at compile time, and not in runtime, as it does with overridden instance methods.

For a neat example have a look here.

And this is java documentation explaining the difference between overriding instance methods and hiding class (static) methods.

Overriding: Overriding in Java simply means that the particular method would be called based on the run time type of the object and not on the compile time type of it (which is the case with overriden static methods)

Hiding: Parent class methods that are static are not part of a child class (although they are accessible), so there is no question of overriding it. Even if you add another static method in a subclass, identical to the one in its parent class, this subclass static method is unique and distinct from the static method in its parent class.

Free Tips Here
Get the latest tips first.
We respect your privacy.
Free Parenting Tips
Get The Latest Tips Here.
We respect your privacy.

Parenting Fails

Advertise Here

Parenting Plan

Parenting Styles

Advertise Here