+ 11

Static method overriding

It is said that a method declared as static can not be overridden. But when I tried. I was able to override.. how is this possible?? Is there any mistake in my code? I'm just confused.. please help https://code.sololearn.com/c7S0qJgp5uAx/?ref=app

28th Nov 2018, 4:43 PM
Shreyansh
Shreyansh - avatar
4 ответов
+ 10
Hello Yamraj, When you define a static method on the derived with same signature as a static method in base class, it is known as method hiding, which differs a little bit from method overriding. That's why you are not getting a compile-time error on your code. Take a look on those 2 links that may help you to clarify those concepts: https://docs.oracle.com/javase/tutorial/java/IandI/override.html https://www.geeksforgeeks.org/overriding-in-java/ Hope that helps :)
28th Nov 2018, 5:45 PM
Jorge Beviláqua
Jorge Beviláqua - avatar
+ 2
Hey Jorge Beviláqua, It was really helpful Thankyou soo much 😊
29th Nov 2018, 2:54 AM
Shreyansh
Shreyansh - avatar
+ 2
Glad to hear! 😃 You’re Wecome! 😊
29th Nov 2018, 3:07 AM
Jorge Beviláqua
Jorge Beviláqua - avatar
0
We cannot overload two methods in Java if they differ only by static keyword (number of parameters and types of parameters is same). See following Java program for example. This behaviour is same in C++ (See point 2 of this). filter_none edit play_arrow brightness_4 // filename Test.java public class Test { public static void foo() { System.out.println("Test.foo() called "); } public void foo() { // Compiler Error: cannot redefine foo() System.out.println("Test.foo(int) called "); } public static void main(String args[]) { Test.foo(); } } Output: Compiler Error, cannot redefine foo()
30th Nov 2018, 10:04 AM
meenal deshpande