InterviewSolution
| 1. |
Is it possible to override and overload a static method in Java? Is it possible to override a private method in Java? |
|
Answer» Because static methods are resolved at compile time, one cannot override them in Java. Because objects are only available at runtime, overriding REQUIRES a virtual METHOD that is resolved at runtime. In Java, a static method can be overloaded. Overloading has nothing to do with runtime, but each method's signature must be distinct. To alter the method signature in Java, EITHER the number of parameters, the type of ARGUMENTS, or the sequence of arguments must be changed. In Java, you can't override a private method because the subclass doesn't inherit the private method, which is required for overriding. In fact, no one outside the CLASS can see a private method, and a call to it is handled at compile time using Type information rather than at runtime using the actual object. |
|