Tuesday, October 27, 2015

Core Java - Can you override private or static method in Java ?


Another popular Java tricky question, As I said method overriding is a good topic to ask trick questions in Java.  Anyway, you can not override private or static method in Java, if you create similar method with same return type and same method arguments that's called method hiding. 

What is the output of the below program?
public class ExceptionHandlingExample1 {
       public static void main(String[] args) {
              ExceptionHandlingExample1 e = new ExceptionHandlingExample1();
              System.out.println(e.sayHi());
       }
      
      
       public String sayHi(){
              try {
                     return "Hi";
              } catch (Exception e) {
                     e.printStackTrace();
              } finally {
                     System.out.println(" You are in final");
              }
             
              return "Hello";
       }
}

Ans :-
You are in final
Hi

No comments:

Post a Comment