Why does the below code print \"Main\"?
public class Main
{
public static void method()
{
System.out.println(\"Main\");
}
public sta
It is because static methods are not polymorphic. Moreover static method should be invoked not by object but using the class, i.e. Main.method()
or SubMain.method()
.
When you are calling m.method()
java actually calls Main.method()
because m is of type Main.
If you want to enjoy polymorphism do not use static methods.