Can we create an instance of an abstract class?

In this article we will discuss can we create an instance of abstract class? What if we create an object of it? Because it is the most common question for interviews. Before reading this topic you must have to know the abstract class in Java.

No, we can’t create an object of it. As know abstract class is not the complete class. because an abstract class has abstract method(Methods without body). Although an abstract class has a constructor if you will try to create an object of it, It will throw compile time exception.

abstract class ExampleOfAbstractClass  
{
	public abstract void showData();
	
}

public class MainClass
{
	public static void main(String arg[])
	{
		ExampleOfAbstractClass object = new ExampleOfAbstractClass(); 
	}
}

Exception in thread “main” java.lang.Error: Unresolved compilation problem: Cannot instantiate the type ExampleOfAbstractClass at create.MainClass.main(MainClass.java:13)

You have seen in the above example the compiler throws the exception at compile time. The exception message is not directly saying that you can’t create an object it. It just says “Cannot Instantiate The Type ExampleOfAbstractClass”, which means that you cannot create an object of it.

can we create an instance of an abstract class

You must think about the constructor of the abstract class. If we can’t create the object of it then what is the use of constructor in the abstract class. The constructor of the abstract class is to initialize the fields which belong to the abstract class. But this constructor is invoked from a concrete subclass as part of creating an instance of a concrete subclass in Java.

Why we can’t create an object abstract class?

If java allows creating an object of it, then it will lead to two problems:

1. The first problem occurs because the abstract method doesn’t provide the body of the method. They are just declared in the abstract class. If someone calls the abstract method using that object, then What would happen?

2. Abstract classes are only like a template, to create an object of any class, the class should be concrete because an object is a concrete.

Leave a Comment