29 April 2015

Can you run main method from Abstract Class?

Can you run below code?
package dms.pastor.abstractclassrunner;

public abstract class AbstractClassRunner {
    public static void main(String[] args) {
        System.out.println("If you see this,it means you can run main method in Abstract Class");
    }
}

Answer is ...Yes ,we can run  main method from abstract class.
It was a rather shock when I heard this,but then I asked myself. How it is possible?

I think about it and then I realised that:
  • Abstract method cannot be instantiated 
  • BUT , main(String[] args) is a static method and that means, it can be accessed without instantiation of the class to which it belongs
As result you can run main(String[] args) in abstract class.
 Simple.