Hey Guys, I am creating this blog to share deep knowledge in Java, JSP, Servlets, JDBC, Hibernate, Spring and Spring MVC in details.

Can we overload the static method and can we override the final method in Java?


Can we overload the static method in Java?

Yes, you can overload a static method in Java. You can declare as many static methods of the same name as you wish provided all of them have different method signatures.

public class Test {
            public static void Show(int i) {
                        System.out.println("i : " + i);
            }
            public static void Show(int i, int j) {
                        System.out.println("i : " + i + " and j : " + j);
            }
            public static void main(String args[]) {
                        Show(10);
                        Show(10, 20);
            }
}
 Output:
  i: 10
  i: 10 and j: 20

Can we override the final method in Java?
No, you cannot override a final method in Java; final keyword with the method is to prevent method overriding. You use final when you don't want subclass changing the logic of your method by overriding it due to security reason. This is why the String class is final in Java. This concept is also used in the template design pattern where the template method is made final to prevent overriding.

Share:

Related Posts:

No comments:

Post a Comment

Popular Posts