Can we override the main method?

Editorial / Best Answer

ashish_setia  

  • Member Since Oct-2006 | Oct 3rd, 2006


check out this example

class Checkmain{
public static void main(String args[]){
args[1]="ashish";
System.out.println("hello ");
}
}
class Checkmain1 extends Checkmain{
public static void main(String args[]){
System.out.println("how r u");
}

}
class Jo{
public static void main(String args[]){
String S[]=new String[10] ;

Checkmain.main(S);
Checkmain1.main(S);
}}

Showing Answers 1 - 75 of 82 Answers

Samkit Jain

  • Sep 22nd, 2006
 

No ,u cannot have two main methods in the same package

esrinivas

  • Sep 27th, 2006
 

  there is no two main methods in the same class,but we can have two main methods in  same package

srikanth

  • Sep 27th, 2006
 

NO, we cannot override main menthod as JVM will not know which main method to be executed first.

  Was this answer useful?  Yes

Ramesh

  • Oct 1st, 2006
 

We can override the main method .at the same time we can also overload the main method. i am sure i execute it

prashamsa

  • Oct 2nd, 2006
 

please ramesh can u elobrate that..with an example

  Was this answer useful?  Yes

Shankar

  • Oct 3rd, 2006
 

Hi ,You have rights to overload main(),but jvm can invoke only main with String[] s. not other arguments main.If u come for override both sub class and super class can hold its own main. any way we can't invoke main with help of either object ref or super. but at compile time we can't get any error suppose both super and sub has main.

Shankar

  • Oct 3rd, 2006
 

Hi,

Java allow main() to overload, but yvm invoke which is having String[]s asrguments.Ex.

  Was this answer useful?  Yes

Sindhu

  • Oct 3rd, 2006
 

cld u plz justify..

  Was this answer useful?  Yes

check out this example

class Checkmain{
public static void main(String args[]){
args[1]="ashish";
System.out.println("hello ");
}
}
class Checkmain1 extends Checkmain{
public static void main(String args[]){
System.out.println("how r u");
}

}
class Jo{
public static void main(String args[]){
String S[]=new String[10] ;

Checkmain.main(S);
Checkmain1.main(S);
}}

rahul

  • Oct 11th, 2006
 

Main is a static method It is not inherited at all so we can not override It

usha

  • Oct 12th, 2006
 

ya.v can override main method.but jvm always encode the public static void main(String[]).

JOYDEEP

  • Oct 16th, 2006
 

i have run ur given code, i does not run. it shows..

Exception in thread "main" java.lang.NoClassDefFoundError: Chaeckmain

then what to do?

  Was this answer useful?  Yes

may be u check the spelling of class name and try again...

for ur information althougth it looks like that we hav overrided the main method but as its a static method its just method shadowing thats it it never get overrided..

okay

regards

ashish

  Was this answer useful?  Yes

mohamed riaz

  • Oct 22nd, 2006
 

Hi,

If we have to 2 main methods in a programe(say in a package),which main method would be executed by the compiler first, as JVM starts execution from the main method.

Thank you.

  Was this answer useful?  Yes

its simple , The main method of that class whose name u specify as ur main class, that class main method will only be called first and so on...

regards

ashish

  Was this answer useful?  Yes

gaurav Sharma

  • Oct 25th, 2006
 

ramesh can u elobarate it with an example?

  Was this answer useful?  Yes

Gaurav Sharma

  • Oct 25th, 2006
 

hi ashish can u justify it with an example?

  Was this answer useful?  Yes

usha

  • Nov 14th, 2006
 

ya.v can override the main method.The jvm invokes the method wich has the method signature as "public static void main(String[])".

  Was this answer useful?  Yes

Shweta

  • Nov 27th, 2006
 

Hi Ashish,

The example which u have given is not of overriding, but of hiding.

A subclass cannot override methods that are declared static  in the superclass. In other words, a subclass cannot override a class method. A subclass can hide a  static  method in the superclass by declaring a static   method in the subclass with the same signature as the static  method in the superclass.

-Shweta

hi shewta,

u r absolutely right  we cannot override a  static method (as they r class level and the concept of overriding is at the object level only) just a reason to support. i already told in one of my replies that this is just a "smart way of the interveiw taking person to ask ques . like can we more than one main method in a program... sort of that but on the actual level we cannot override a static method.

thanku dear for ur concern.

bye

regards

ashish 

  Was this answer useful?  Yes

veera

  • Dec 13th, 2006
 

public class A{public static void main(String arg[]){main(){System.out.println("how r u");}}

  Was this answer useful?  Yes

vikas

  • Dec 23rd, 2006
 

hi ashish_setia......

i have run your code and its working fine.............

mean we can override two main method.......

thanks  

  Was this answer useful?  Yes

Brju

  • Jan 14th, 2007
 

public class X{

public static void main(String[] args){

 System.out.println("X");

}

}

public class X1 extends X{

public static void main(String[] args){

 System.out.println("X1");

}

}

public class X2 extends X1{

}

c:> java X2

vishal

  • Jan 24th, 2007
 

We can..................public class Demo{ public static void main(String[] args) { System.out.println("String"); } public static void main(int[] args) { System.out.println("Int"); } public static void main(char[] args) { System.out.println("Char"); }}

  Was this answer useful?  Yes

Niraj Lavankar

  • Mar 12th, 2007
 

Remesh,
As main() is static method so we never override main() methos..if you think yes can you please rectify....

  Was this answer useful?  Yes

chandra

  • Mar 20th, 2007
 

Code
  1.  

  2. class Checkmain{

  3.     public static void main(String args[]){

  4.         args[1]="ashish";

  5.         System.out.println("hello ");

  6.     }

  7. }

  8. class Checkmain1 extends Checkmain{

  9.     public static void main(String args[]){

  10.         System.out.println("how r u");

  11.     }

  12.    

  13. }

  14.  public  class  Main{

  15.     public static void main(String args[]){

  16.         String S[]=new String[10] ;

  17.        

  18.         Checkmain.main(S);

  19.         Checkmain1.main(S);

  20.        

  21.     }

  22.  public static void main(int a){

  23.         String S[]=new String[10] ;

  24.        

  25.         Checkmain.main(S);

  26.         Checkmain1.main(S);

  27.     }}

  28.  


the name of file should be Main.java
++++++++++++++++++++++++++++++++++++++++++++++++++++++


we cant override the static method's but we can have multiple main method in a class but with different parameters the Jvm always looks for the main method with string parameter.

We can have public static void main(String[] args) in the parent class and subclass. This is essentially not considered overriding but an access point for code execution either from parent class or subclass. That's all.

  Was this answer useful?  Yes

vishal kumar

  • Aug 14th, 2007
 

Yes we can have two or more main method in same package and also call main method of one class into another's main in which it is implemented.

  Was this answer useful?  Yes

vishal kumar

  • Aug 14th, 2007
 

No we can't override main method bcoz it is static method and we know that static method don't participate in overriding process. For binding point of view binding of static method is done at compile time and overriding is done at run time.

Static method binding is done at compile time but static variable binding is done at run time.

  Was this answer useful?  Yes

praveen

  • Sep 5th, 2007
 

We cannot override main method in a class, because main is static and also we cannot have two main methods in same class, but we can use in java file.

  Was this answer useful?  Yes

Ashok Agnihotri

  • Nov 16th, 2007
 

yes, we have two main() in the same class but with different signatures. i have no idea about main() overriding but it;s 101% sure that we can overload main() but honestly i don't know how to invoke the overloaded version.

  Was this answer useful?  Yes



Code
  1.  


It is possible to override the main method.
For details look at the following code:
Code
  1.  

  2. public class Main {

  3. private double main= 54.42;

  4. public Main() { }public static void main(String[] args)

  5. {

  6. Main main= new Main();

  7. main.main();

  8. main.main(42);

  9. main("can");main.Main(" do this");

  10. }

  11. public void main() { System.out.print("yes "); }

  12. public void main(int i) { System.out.print("you "); }

  13. public static void main(String s) { System.out.print(s); }

  14. public static void Main(String s) { System.out.print(s); }

  15. }

  16.  

  Was this answer useful?  Yes

Jvm should prevent to have 2 main methods...However following codes work perfectly.

Code
  1.  

  2.  

  3. public class Helloworld {

  4.  

  5.     public static void main(String[] args) {

  6.        

  7.         System.out.println("In superclass");

  8.     }

  9.     public static void main(int[] args) {

  10.        

  11.         System.out.println("Overload in superclass");

  12.     }

  13. }

  14.  class Helloworld1 extends Helloworld {

  15.  

  16.     public static void main(String[] args) {

  17.        

  18.         System.out.println("In subclass");

  19.     }

  20.    

  21. }

  22.  

  Was this answer useful?  Yes

We can not override main method, as we know that main methods signature is :
public static void main(String args[]). Static method is associated with class not with the object of the class and class level variable (static) can not be overridden. This is a prevention done by SUN son that if you RUN : c:>java MyClass , it should call the main method only within the MyClass.java.

  Was this answer useful?  Yes

public class Helloworld { public static void main(String[] args) { System.out.println("In superclass"); } public static void main(int[] args) { System.out.println("Overload in superclass"); } } class Helloworld1 extends Helloworld { public static void main(String[] args) { System.out.println("In subclass"); } }

Above written code is not overriding in ans# 35.  You can write the code which shows that statis method is overridden but it wont give you the functionality of overriding.

 For Ex:
     Just write a  code after above written two classes by Satish.

   Then
   public class CodeTest{
       psvm (String args[]) {
        Helloworld helloWorldSubObj = new Helloworld1(); //Type is HelloWorld but obj is : HelloWorld1

     helloWorldSubObj.main(new String[1]);// This will call the super class method because statis method only search for type not for object and type is HelloWorld for reference helloWorldSubObj
         

       }
   }

  Was this answer useful?  Yes

dh.agrawal

  • Jun 13th, 2008
 

main () method can't be overridden.
 because main () is a static method and
Static method can not be overridden but can be redefined that seems to be overridden.
and ya main () method can be overloaded.
if you want any more explanation send me your doubt at dh.agrawal@yahoomail.com

  Was this answer useful?  Yes

ravi_1229

  • Jun 30th, 2008
 

yes v can override, overload main method as like a normal method. main() method is also a normal java method, but only difference between normal method and main method is that main method is called by JVM and other methods r need to be called by us. on the other hand v can also call the main() expilictly in other main method.

  Was this answer useful?  Yes

Ashique

  • Aug 11th, 2008
 

First of all if I am not mistaken the question is wrong. Because the main method is static method and static method does not inherited in its sub clasess. So no question of overiding, but yes we can overload the main method.

Yes, you can have multiple main methods in one class but you cant override the String[] argument method. see the executed class file
public class mainoverload

Code
  1.  

  2. /**

  3. * @param args

  4. */

  5. public static void main(String[] args) {

  6. // TODO Auto-generated method stub

  7. System.out.println("in string main");

  8. main();

  9. }

  10. public static void main()

  11. {

  12. System.out.println("in void main");

  13. }

  14. public static long main(long[] v)

  15. {

  16. return 0;

  17. }

  18. }



you will see the out put as in string main in void main int main 2

  Was this answer useful?  Yes

You guys aresimplymis-judging the concept of override.

Here we are talking about override that simply means that a class which contains main method and is extends by another class and we all know that derived class can only have non-static functions of super class.
now it depends upon the signature of main function where it is static or non-static.

I'm damn sure that

public static void main(String[]args) can never be overridden in derived class but any overloaded version of this method which is non-static can be overridden like

public void main()
public int main()
etc....

  Was this answer useful?  Yes

pavi

  • Sep 3rd, 2011
 

Yes it is possible using the inheritance.

  Was this answer useful?  Yes

Chetan

  • Sep 6th, 2011
 

No you cant. main is a static method and static methods cannot be overridden.

  Was this answer useful?  Yes

Paul

  • Sep 12th, 2011
 

Yes. You can Overload and Override the main() method.
Check this code. (modified from one of the examples above)

Change the parameters and check the output.

Code
  1. class Checkmain{

  2.         public static void main(int xx, String s[]){

  3.  

  4.                 System.out.print("Hello! ");

  5.         }

  6. }

  7.  

  8. class Checkmain1 extends Checkmain{

  9.         public static void main(String args[]){

  10.  

  11.                 System.out.println("How are you.");

  12.         }

  13.  

  14. }

  15.  

  16. class Jo{

  17.         public static void main(String args[]){

  18.  

  19.                 String s[]=new String[10] ;

  20.                 int[] i=new int[8];

  21.                 int x=30;

  22.  

  23.                 Checkmain.main(x,s);

  24.                 Checkmain1.main(s);

  25.         }

  26. }

  27.  

  Was this answer useful?  Yes

krishna

  • Sep 27th, 2011
 

we can override the main method, but we can access only one main method during runtime. the other main method is irrelevant we are not able to call it.

  Was this answer useful?  Yes

sindhuja

  • Sep 28th, 2011
 

hi,
i have executed the above program.
output of main Checkmain.main(S) &Checkmain1.main(S) is "how are you"
i expected for the output. " how are you" should be displayed twice.
but only once it has displayed. can anyone elaborate?

  Was this answer useful?  Yes

rome2all

  • Sep 29th, 2011
 

Yes We can override main method but main method is an static method,even we can override main method,based on the class name.method name there is no problem calling the main method,and even we can call main method in another class also
ex:

Code
  1.  

  2. class Test

  3. {

  4. public static void main(String...z){

  5. for(String s:z)

  6. {

  7. System.out.println(s);

  8. }

  9. }

  10. class Test1

  11. {

  12. public static void main(String...z){

  13. Test.main(z);

  14. }

  15. }


when we run Test1 plz pass the cmd orguments then you willl be know it

  Was this answer useful?  Yes

Tushar Agarwal

  • Oct 8th, 2011
 

Lets check this example we can override the main method like this ........

Code
  1. class Checkmain{

  2.     public void main(String args[]){

  3.         args[1]="ashish";

  4.         System.out.println("hello ");

  5.     }

  6. }

  7. class Checkmain1 extends Checkmain{

  8.     public void main(String args[]){

  9.         System.out.println("how r u");

  10.     }

  11.    

  12. }

  13.  public  class  Main3{

  14.     public static void main(String args[]){

  15.         String S[]=new String[10] ;

  16.         Checkmain obj = new Checkmain();

  17.          obj.main(S);

  18. Checkmain obj1 = new Checkmain1();

  19. obj1.main(S);

  20.     }

  21.  

  22.  

  23. }

  24.  

  Was this answer useful?  Yes

mangesh

  • Oct 10th, 2011
 

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.


I think it also can be overridden

check this code i am sure its working:

Code
  1. class Scon

  2.  

  3. {

  4. Scon()

  5. {

  6. System.out.println("obj created");

  7. }

  8.  

  9. public static void main(String[] args)

  10. {

  11. System.out.println("Hello World A!");

  12. }

  13. }

  14. public class B extends Scon

  15. {

  16. public static void main(String[] args)

  17. {

  18. System.out.println("Hello World B!");

  19.  

  20.  

  21. }

  22.  

  23. }

  Was this answer useful?  Yes

karthik

  • Oct 11th, 2011
 

NO,we cannot override 2 main method in same package

  Was this answer useful?  Yes

sachin

  • Oct 23rd, 2011
 

no... we cannot override the main method but can be overloaded

  Was this answer useful?  Yes

jagadesh

  • Oct 28th, 2011
 

Yeah its working ,,,thanks ashis

Code
  1. class Checkmain{

  2. public static void main(String args[]){

  3. args[1]="ashish";

  4. System.out.println("hello ");

  5. }

  6. }

  7. class Checkmain1 extends Checkmain{

  8. public static void main(String args[]){

  9. System.out.println("how r u");

  10. }

  11.  

  12. }

  13. class Jo{

  14. public static void main(String args[]){

  15. String S[]=new String[10] ;

  16.  

  17. Checkmain.main(S);

  18. Checkmain1.main(S);

  19. }}

  20.  

  Was this answer useful?  Yes

suraj20p

  • Oct 28th, 2011
 

Any static Method including Main() cannot be overrided.


Rules:
1>If base class contains static method,and we want to have the method with same name in the child class also,then in the child class that method should also be declared as static

2>If in the child class you have static method,then you cannot have the same method in the parent class without static modifier.

3>Some how we are getting feelings that it is overriding,but static methods are class level methods,which can be called directly without using object,and hence we cannot suppress it....

  Was this answer useful?  Yes

Nizam

  • Nov 1st, 2011
 

A method which has Static non-Access modifier cannot be Override because static method can be loaded at the time of class loading(After Creation of .class file).therefor it does not support Dynamic Dispatch..

  Was this answer useful?  Yes

kiran jujare

  • Nov 3rd, 2011
 

Yes we can override the main method. But when we execute the class then JVM executes the current class main Method

javac MainOverride
java MainOverride

Then MainOverride method will get executes....

Code
  1. public class  MainOverride

  2. {

  3.         public static void main(String[] args)

  4.         {

  5.                 MainOverride1.main(args);

  6.                 //MainOverride2 mainOverride2 = new  MainOverride2();

  7.                 System.out.println("Hello World!");

  8.         }

  9. }

  10. class  MainOverride1 extends MainOverride2

  11. {

  12.         public static void main(String[] args)

  13.         {

  14.                 MainOverride2.main(args);

  15.                 System.out.println("Hello World! main one");

  16.         }

  17. }

  18. class  MainOverride2

  19. {

  20.         public static void main(String[] args)

  21.         {

  22.                 System.out.println("Hello World! main Two");

  23.         }

  24. }

  25.  

  Was this answer useful?  Yes

alok

  • Nov 11th, 2011
 

JVM only call "public static void main(String args[])" static method to execute.

Code
  1. public class abc{

  2.  

  3. public static void main(String args[]){

  4.    System.out.println(" heelo0");

  5.    abc a =new abc();

  6.    

  7.         a.main();

  8.  

  9.  

  10. }

  11.    public static void main(){

  12.      System.out.println(" this is second main");

  13.  

  14.    }

  15.  

  16. }

  Was this answer useful?  Yes

Srikanth

  • Nov 28th, 2011
 

We can't override the main() method.

  Was this answer useful?  Yes

Srikanth

  • Nov 28th, 2011
 

We can't override static method becz that method is static.

  Was this answer useful?  Yes

Akhileshwar Babu

  • Dec 6th, 2011
 

We can overload main method and we can also override it
JVM only calls on the class name

public static void main(Strina[] array)

Code
  1. class MainEx

  2. {

  3.  public static void main(String []array){

  4. main();

  5. }

  6. static void main(){

  7. }

  8. }

  Was this answer useful?  Yes

Gopal

  • Dec 11th, 2011
 

Definitely we can Override Main method and as well as Overload main method.

I proved it if you want to test Please test the following code snippet.

Code
  1. class Static1

  2. {

  3.          static void show(){

  4. System.out.println("show empty");

  5. }

  6.         static void main(String... s)

  7. {

  8.         System.out.println("before override");

  9. }

  10.  

  11.          static void show(int i){

  12. System.out.println("show:"+i);

  13. }

  14. }

  15. class Static2 extends Static1

  16. {

  17.         static void main(String... s){System.out.println("over ridden main");}

  18.         static void show()      {System.out.println("show empty in subclass");}

  19. }

  20. public class Static3{

  21.  

  22. public static void main(String... s)

  23. {

  24.         Static1 st=new Static2();

  25.         Static3 st3=new Static3();

  26.         st.show();

  27.         st.show(4);

  28.         st.main();

  29.         st3.main();

  30. }

  31. public static void main(){

  32.         System.out.println("overloaded main");

  33. }

  34.  

  35. }

  Was this answer useful?  Yes

we can override the main method. but usually we declared main method as static. So it belonging to class. So we need not to discuss about the override for main in the inheritance.

  Was this answer useful?  Yes

suresh

  • Dec 17th, 2011
 

It is not possible to override main(), for static methods overriding is not possible but if you try for static method overriding it wont give any error it is working why because it is Method-hiding not overriding.

  Was this answer useful?  Yes

vishal

  • Dec 18th, 2011
 

Overloading is always in inheritances ie this happens always within class and there in no chance to specifies main method two time in class because execution of class is started from main method.

  Was this answer useful?  Yes

sampra

  • Mar 6th, 2012
 

Yes we can

Code
  1. package com.san;

  2.  

  3. public class Test {

  4.         public static void main(String[] args) {

  5.                 System.out.println("main");

  6.  

  7.         }

  8. }

  9.  

  10. class Hello extends Test {

  11.  

  12.         public static void main(String[] args) {

  13.                 System.out.println("main---");

  14.  

  15.         }

  16.  

  17. }

  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