I was asked whether,
public static void main(String[] args) {}
be overridden any no of times in same class ? can any one explain this
I was asked whether,
public static void main(String[] args) {}
be overridden any no of times in same class ? can any one explain this
You ask:
Can Main method be overridden any number of times?
No, it can't be overridden even once due to the plain fact that you can't override static methods.
No, you cannot override any method within the same class. If you try to override it in another class, that is not going to work, since you cannot override static methods (because that makes no sense if you think about it in the context of OOP).
Overloading methods can be done. You can overload a method called main, but that method will never be the entry point of your application, since it has not the required arguments to launch. The only three correct ways to define a main methods are:
public static void main(String args[])
public static void main(String[] args)
public static void main(String ...args)
Note that this is neither overloading or overriding: these signatures are equally treated.
yes, we can 'overload' main method in java even in the same class you can something like
public static void main(int arr[])
or
public static void main(float f)
but when you execute your application, JVM will search for main method with String[] args i.e.
public static void main(String[] args)
A class can only contain a single method with that name and signature.
It can have other methods with the same name and different argument types. However, those methods will have no special significance as far as starting the program is concerned.
Yes, surely you can overload main method in Java. Keep in mind, when you run/execute the program, it doesn't execute overloaded main function automatically. You still have to call the overloaded main method from the Main method.