Yes, we can declare the main () method as final in Java.
public class Demo { public final static void main(String[] args) { System.out.println("Main Method"); } }
But, In inheritance we cannot declare main method as final in parent class. It throwss compile time error - Cannot override the final method from Parent.
public class Parent { public final static void main(String[] args) throws Exception { System.out.println("Parent called"); } } class Child extends Parent { public static void main(String[] args) throws Exception { System.out.println("Child called"); } }
Hope you have enjoyed reading if we can declare main()
method as final
in Java. Please do write us if you have any suggestion/comment or come across any error on this page. Thank you for reading!
Share this page on WhatsApp