Why does java allow to call static methods on instances? - Java
Why does Java allow me to call static methods on instances?
Your assumption is wrong. It never calls on the instance of the class. It always called on class.
Try below sample code and you will never get
NullPointerException
class ABC {
public static void hello() {
System.out.println("Hello");
}
}
ABC abc = null;
abc.hello();
What would have gone wrong if Java would have supported Runtime Polymorphism for static methods?
Polymorphism comes into picture when you override the method in subclass. since static method belong to class hence there is no meaning of overriding static methods. Hence Polymorphism always works for instance methods only that belongs to the instances of the class.
Source: Stackoverflow
Comments
Post a Comment