What is method hiding in Java

     

  

Can we override a Static methods  in Java?

    No we can't override the Static methods, it cannot be over-ridden. Though you can declare a method with the same signature in a subclass.

    Over-riding [run-time polymorphism] - A subclass provides an implementation of a method in super-class and which implementation to execute is decided at run-time according to the object [subclass OR super-class object] used for call.

    Even if we have the same type of static method in subclass as well as in super class, It won't be overridden in the exact sense, instead, that is called ‘method hiding’.

Why it is called mehtod hiding 

 Because there won’t be any run-time polymorphism. Method call will be resolved at compile time itself depending on the class used to create object.

  Note: Don’t confuse it with overloading [compile-time polymorphism]. Static methods can be overloaded with same name, but different parameters.

Post a Comment