Total Pageviews

Thursday, September 13, 2012


IS IT POSSIBLE TO EXTEND THE CLASS WHICH HAS MAIN FUNCTION???

Yes, it is possible to extend the class which has main function. When i tried it using notepad and command prompt, i got the expected output. In NetBeans, i got a message...
The file has more main classes.
and it asked me to chose the main class from the list. When i choose the super class which has main(), i got the correct output. Also the output is correct when i choose the subclass. But i think it is stupidous to extend a class having main(). I didn't find any answer in Google for this question and i think this kind of inheritance has no real application. The program which i tried is...

public class psvm_class 
{
    int a=5;
   
     public static void main(String args[])
    {
        System.out.println("hai in main()");                                          
        demo obj=new demo();                                   
        obj.display();                               
    }
}
class demo extends psvm_class
{
    void display()
    {
        System.out.println("hai in demo class");
        System.out.println("value of a is "+a);
    }
}

OUTPUT:

hai in main()
hai in demo class
value of a is 5

The above program has two main() functions in it, one which is in psvm_class. Since class demo inherits from psvm_class, it also has one copy of main() !!!!! But still the program runs without any error!!!!!

Discussions and ideas about this peculiar case are invited... Post your ideas and comments about this blog. Sometimes i may be wrong. Your ideas might help me in correcting this blog. Feel free to spot it out. Thanks for reading this blog... ;-) Stay tuned for my next blog...

3 comments:

  1. Genius sir neenga :) :) Super concept :) :)

    ReplyDelete
  2. cheeku @ over!!! :P public public... :D

    ReplyDelete
  3. superb da..i dont know how this program runs without error:)interesting!!

    ReplyDelete