2011-12-11

Can main method is executed from super classes ?

Yes,if subclass doesn't have main method JVM will not through exception directly instead it search in super class complete hierarchy till object class. If main method is found in any of the super class it stops searching and executes main method from that class. Otherwise it throws an exception.Check below program.

//Example.java

class Example  
{  
   public static void main(String[] args)  
   {  
      System.out.println("Example main");
   }  
}  

//Sample.java

class Sample extends Example  
{         
}  


Then two .class files are created Example.class and Sample.class.

Loading

Enter your Email here: