Can the main() method be overloaded? or overrided? and what happens when we do so?

Showing Answers 1 - 10 of 10 Answers

jsoodan

  • Aug 5th, 2006
 

yes, It cam be overloaded, I am not sure if it can be overridden. In over loading u have same method name with different parameteres. So main can have no params or it can have a String objects as arguments.

             The purpose of passing string as args can be to dynamically submit, input, output, error filenames etc.

  Was this answer useful?  Yes

Ganesh Bala Kumar

  • Aug 8th, 2006
 

Yes, we can overload and override the main method.

public class SampleProgram {
    public static void main(int k){
        System.out.println("k >>>>>>>>  "+k);
    }
    public static void main(String args[]){
        SampleProgram program = new SampleProgram();
        program.main(5);
    }
}

public class SubSampleProgram extends SampleProgram{

    public static void main(String args[]){
         SubSampleProgram sampleProgram = new SubSampleProgram();
         sampleProgram.main(7);
    } 

}

  Was this answer useful?  Yes

Alpana Desh

  • Sep 11th, 2006
 

The main method can overloaded. if you pass different parameters then it could be overloaded. when JVM loads the program,it searches for the main method which has arguments as String. so if you overload the function then it will call only the correct one.

  Was this answer useful?  Yes

Vibs

  • May 17th, 2007
 

Yes, it can be both overloaded and overridden.
When you over ride, its going to call the main of the class that you mention with java command.
In case of overload, the overloaded method will no longer be treated as main method but it will be normal like other methods.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions