• Post Reply Bookmark Topic Watch Topic
  • New Topic

main() method overriding  RSS feed

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can we override a main() method??
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happened when you tried it?
 
sireesha vadlamani
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can i write a program...i am confused :-O
Can you please give an example...
 
sireesha vadlamani
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sireesha vadlamani wrote:How can i write a program...i am confused :-O
Can you please give an example...

I tried it and it executes the parent's main() method only and gives the output of baseclass only. Code is...
class parent
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}
class child extends parent
{
public static void main(String[] args)
{
System.out.println("child extends parent");
}
}
class ExecuteDemo
{
public static void main(String[] args)
{
child ch=new child();

}
}
OUTPUT:
Hello world!
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a nutshell, there is nothing special about methods name "main". they work just like any other method.

So whatever you can do with a method named "fred", or "computeScore" or "thisIsABadNameForAMethod", you can do with methods named "main".


The ONLY time anything special happens with main is when you try and run your program. You tell java what class to start with:
java <some class name here>

When you type that, the JVM starts, it looks for the class' classfile, and then looks for a specific method with a specific signiature:

public static void main (String [] someVariableName)


The key parts are: it has to be public. It has to be static. It has to be void. It has to be called "main". It has to take a String array as an argument, but that array can be named whatever you want.

IF it finds that method, then that method is run.

end of story.

The presence (or lack thereof) of other methods named "main" with different signatures is irrelevant.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What did you run on the command line? I assume you typed

java parent

so the Parent class' main method ran, and that does nothing but prints "Hello World!". What else should it do?
 
Ranch Hand
Posts: 30
1
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java you can not override static methods, main is static as you know.
 
sireesha vadlamani
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:In a nutshell, there is nothing special about methods name "main". they work just like any other method.

So whatever you can do with a method named "fred", or "computeScore" or "thisIsABadNameForAMethod", you can do with methods named "main".


The ONLY time anything special happens with main is when you try and run your program. You tell java what class to start with:
java <some class name here>

When you type that, the JVM starts, it looks for the class' classfile, and then looks for a specific method with a specific signiature:

public static void main (String [] someVariableName)


The key parts are: it has to be public. It has to be static. It has to be void. It has to be called "main". It has to take a String array as an argument, but that array can be named whatever you want.

IF it finds that method, then that method is run.

end of story.
sorry, am asking about method overriding which means, same signature+same method name but the methods are present in different classes. In that situation can we ovrride the main() methods in that two different class? If so, please explain with an example.
Thanks

The presence (or lack thereof) of other methods named "main" with different signatures is irrelevant.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sireesha vadlamani wrote:am asking about method overriding which means, same signature+same method name but the methods are present in different classes. In that situation can we ovrride the main() methods in that two different class?

If you are talking about the main method that is used as an entry point to a program, see Arthur's reply above.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!