Answered by: ashish_setia
View all answers by ashish_setia
Member Since Oct-2006 | Answered On : 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);
}}
Answered On : Sep 22nd, 2006
No ,u cannot have two main methods in the same package
Answered On : Sep 27th, 2006
there is no two main methods in the same class,but we can have two main methods in same package
Answered On : Sep 27th, 2006
NO, we cannot override main menthod as JVM will not know which main method to be executed first.
Answered On : 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
Answered On : Oct 2nd, 2006
please ramesh can u elobrate that..with an example
Answered On : 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.
Answered On : Oct 3rd, 2006
Hi,
Java allow main() to overload, but yvm invoke which is having String[]s asrguments.Ex.
Answered On : Oct 3rd, 2006
cld u plz justify..
yes , we can overload and override the main method. and i t will work u can try this.
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);
}}
Answered On : Oct 11th, 2006
Main is a static method It is not inherited at all so we can not override It
Answered On : Oct 12th, 2006
ya.v can override main method.but jvm always encode the public static void main(String[]).
Answered On : 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?
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
Answered On : 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.
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
Answered On : Oct 25th, 2006
ramesh can u elobarate it with an example?
Answered On : Oct 25th, 2006
hi ashish can u justify it with an example?
Answered On : 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[])".
Answered On : 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
Answered On : Dec 13th, 2006
public class A{public static void main(String arg[]){main(){System.out.println("how r u");}}
Answered On : Dec 23rd, 2006
hi ashish_setia......
i have run your code and its working fine.............
mean we can override two main method.......
thanks
Answered On : 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
Answered On : 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"); }}
Answered On : Mar 12th, 2007
Remesh,
As main() is static method so we never override main() methos..if you think yes can you please rectify....
Answered On : Mar 20th, 2007
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.Code
class Checkmain{ args[1]="ashish"; } } class Checkmain1 extends Checkmain{ } } public class Main{ Checkmain.main(S); Checkmain1.main(S); } public static void main(int a){ Checkmain.main(S); Checkmain1.main(S); }}
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.
Answered On : 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.
Answered On : 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 isdone at run time.
Answered On : 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.
Answered On : 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.
We can but with diff signature
It is possible to override the main method. For details look at the following code:Code
Code
public class Main { private double main= 54.42; { Main main= new Main(); main.main(); main.main(42); main("can");main.Main(" do this"); } }
Jvm should prevent to have 2 main methods...However following codes work perfectly.
Code
public class Helloworld { } public static void main(int[] args) { } } class Helloworld1 extends Helloworld { } }
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.
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
}
}
yes , we can do that..
Its create to .class file but run only one at time
sorry,
we can't do that
yes we can overrride the main method
No we can't override main method and we can only overload main method.
Reason -- because man() is static method hence we can't over-ride it.
vinny
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
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.
No, you can't override the static method.
NO, because main is a static method. A static method can't be overridden in Java.
Answered On : Aug 10th, 2008
View all questions by nikhilsaxena View all answers by nikhilsaxena
It is not overiding if you save your file name as base class than you get output "hello" because the main of checkmain will execute.
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
you will see the out put as in string main in void main int main 2
Answered On : Jun 6th, 2011
View all questions by krishnaprasad587 View all answers by krishnaprasad587
ya...main method can be overridden
example
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....
Answered On : Sep 3rd, 2011
Yes it is possible using the inheritance.
No we cannot override the main method.
Answered On : Sep 6th, 2011
No you cant. main is a static method and static methods cannot be overridden.
Answered On : 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
class Checkmain{ } } class Checkmain1 extends Checkmain{ } } class Jo{ int[] i=new int[8]; int x=30; Checkmain.main(x,s); Checkmain1.main(s); } }
Answered On : 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.
Answered On : 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?
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:
when we run Test1 plz pass the cmd orguments then you willl be know it
Answered On : Oct 8th, 2011
lets check this example we can override the main method like this ........
Code
class Checkmain{ args[1]="ashish"; } } class Checkmain1 extends Checkmain{ } } public class Main3{ Checkmain obj = new Checkmain(); obj.main(S); Checkmain obj1 = new Checkmain1(); obj1.main(S); } }
Answered On : 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
class Scon { Scon() { System.out.println("obj created"); } public static void main(String[] args) { System.out.println("Hello World A!"); } } public class B extends Scon { public static void main(String[] args) { System.out.println("Hello World B!"); } }
Answered On : Oct 11th, 2011
NO,we cannot override 2 main method in same package
yes
Answered On : Oct 23rd, 2011
no... we cannot override the main method but can be overloaded
Answered On : Oct 28th, 2011
yeah its working ,,,thanks ashis
Code
class Checkmain{ args[1]="ashish"; } } class Checkmain1 extends Checkmain{ } } class Jo{ Checkmain.main(S); Checkmain1.main(S); }}
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....
Answered On : 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..
Answered On : 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
public class MainOverride { { MainOverride1.main(args); //MainOverride2 mainOverride2 = new MainOverride2(); } } class MainOverride1 extends MainOverride2 { { MainOverride2.main(args); } } class MainOverride2 { { } }
Answered On : Nov 11th, 2011
JVM only call "public static void main(String args[])" static method to execute.
Answered On : Nov 28th, 2011
We can't override the main() method.
Answered On : Nov 28th, 2011
We can't override static method becz that method is static.
Answered On : 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
class MainEx { main(); } static void main(){ } }
Answered On : 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
class Static1 { static void show(){ } { } static void show(int i){ } } class Static2 extends Static1 { } public class Static3{ { Static1 st=new Static2(); Static3 st3=new Static3(); st.show(); st.show(4); st.main(); st3.main(); } public static void main(){ } }
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.
Answered On : 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.
Answered On : 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.
yes we can
Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.
