aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes It is Strange about main method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "It is Strange about main method" Watch "It is Strange about main method" New topic
Author

It is Strange about main method

Krishna Latha Grandhi
Ranch Hand

Joined: Nov 08, 2005
Posts: 110


if we try to execute the above code with
main(java.lang.String[]) in Tester cannot override main(java.lang.String[]) in TestEx1;

This output is correct. ok.

But if we commented the main method in Tester class the super class method is gets executed. without inheriting the method how can that main method gets executed.

Please clarify my doubt..

Regards,
Krishna.
Naresh Gunda
Ranch Hand

Joined: Oct 15, 2005
Posts: 163
final methods can be inherited.
final methods can't be overridden.
static methods can be inherited.

When u execuate a program, JVM looks for main method. It is not defined in Tester class, but inherited from super class.

Regards
Naresh
[ December 21, 2005: Message edited by: Naresh Kumar ]
Krishna Latha Grandhi
Ranch Hand

Joined: Nov 08, 2005
Posts: 110
final methods can be inherited.
final methods can't be overridden.
static methods can be inherited.

Hey, I know the above things...
But I have a doubt that how the final method of TestEx1 class gets invoked from Tester class...
But You contradicted your assumptions...

When u execuate a program, JVM looks for main method. It is not defined in Tester class, but inherited from super class.


Plz any one Explain.....

Regards,
Krishna.
Hemant Agarwal
Ranch Hand

Joined: Nov 21, 2005
Posts: 138
The JVM will look for the main method in that class or any of it's superclass. If it found the main method inside the class it is calling then it will not look into the superclass otherwise it will look out for main method inside all of it's super classes starting from that class. If it found, then the compiler will not give errors.
Krishna Latha Grandhi
Ranch Hand

Joined: Nov 08, 2005
Posts: 110
Thank you Hemanth

Regards,
Krishna
Sachin Dimble
Ranch Hand

Joined: Dec 07, 2005
Posts: 100
Hari do u agree with these-
final methods can be inherited.
final methods can't be overridden.
static methods can be inherited.

if yes then it's simple u are inheriting static main of TestExe1 into Tester that means now ur Tester has also a main method(it's own though inherited) & same is executing!!!
Hope u understand!

With Best Regards,
Sachin.
vipul patel
Ranch Hand

Joined: Oct 16, 2005
Posts: 146
The main method in the child class is just hiding the one defined in the parent class.
 
Consider Paul's rocket mass heater.
 
subject: It is Strange about main method