Can we run abstract class in Java that has main method?

Yes, you can use the main() method in abstract class. The main() method is a static method so it is associated with Class, not with object or instance. The abstract is applicable to the object so there is no problem if it contains the main method. In main method, you can not create an instance of the abstract class but you can instantiate other concrete class.

Here is a sample program that demonstrates the use of main() method in abstract class.

public abstract class TestAbstract {
	public static void main(String[] args){
		System.out.println("Inside abstract main");
	}
}
 
Output: 
Inside abstract main
 

Hope you have enjoyed reading Can we run abstract class in Java that has main method? Please do write us if you have any suggestion/comment or come across any error on this page. Thank you for reading!



Share this page on WhatsApp

Get Free Tutorials by Email

About the Author

is the founder and main contributor for cs-fundamentals.com. He is a software professional (post graduated from BITS-Pilani) and loves writing technical articles on programming and data structures.