Monday, November 18, 2013

Can we write and execute main method inside an Abstract class ?



- With abstract class, we can't create an instance or object. But that class can have static methods as well as instance methods.

- If we want to utilize instance methods, we must extend that class and implement all abstract methods and then create an object to that child class. Finally we can utilize instance methods.

- We also can write and execute main method from an Abstract class.

Code snippet:

AbstractMain.java
------------------------
public abstract class AbstractMain{
        public static void main(String nag[]){
                System.out.println("hello :");
        }
}//end main

compile the above code and try to run from command-line. We get the output.

So abstract only restricts from object creation for a class. but if we want to use any static methods like above, we can simply use them..:)