Is it possible to instantiate an Abstract class in Java?

Earlier I have shared one of the frequently asked questions in Java, can we make an abstract class final in Java and my readers really liked and asked for more such questions. So, today I am going to talk about whether we can create an instance of an Abstract class in Java or not? This is another interesting core Java question you will find on telephonic interviews or Oracle certified Java programmer certification. The answer to this question is simple, No, you cannot instantiate an abstract class in Java because it is abstract, it is not complete hence it cannot be used.  When you create an instance of a class, its's constructor is called and even though abstract class can have a constructor, the compiler will not allow you to create an instance of the class. It's a compile-time error to create an instance of an abstract class in Java.

Let's see some code example to prove this point that it's illegal to create an instance of an abstract class in Java:

package tool;

 * Java Program to prove that you cannnot
 * create instance of an abstract class
 * in Java. even though you can define
 * constructor.
public class Hello {

  public static void main(String[] args) {
    Nested n = new Nested();

  abstract class Nested {

    public Nested() {
      System.out.println("No-argument default constructor");


When you compile this class using javac on the command line or just type the code in Eclipse IDE, you will see following compile time error:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
at tool.Hello.main(

You can see that even though you can declare constructor inside an abstract class, as soon as you try to instantiate an abstract class, the compiler is throwing "Cannot instantiate the type Hello.Nested" error.

This is true for both top level and nested abstract class in Java, you cannot extend any of them.

This is actually the follow-up of another interesting core Java question, can an abstract class have a constructor in Java. In the last post, I explained you about whether an abstract class can have constructor or not and if it's allowed then why do you need a constructor for an abstract class which is incomplete.

The real use of constructor in an abstract class is to initialize the fields which belong to the abstract class itself but constructor must be invoked from a concrete subclass as part of creating an instance of a concrete subclass in Java.

Btw, if you are just starting with Java then you should first check out the Complete Java Masterclass on Udemy, one of the better course to start with Java.

Is it possible to instantiate an Abstract class in Java?

That's all about whether you can instantiate an abstract class in Java or not. As I said, Java specification doesn't allow you to create an object of an abstract class, it's a compile-time error, even though you can have constructor inside the abstract class.

This kind of question is very popular during a telephonic round of interview as well as Java certification exams like OCAJP8 and OCPJP8 which often gives you code based upon this concept and various choices like what will happen when you run this program.

You will find many such questions on popular mock exam simulators like Whizlabs or David Mayer's Java 8 Exam Simulator.

Other interesting Core Java Articles and Resources you may like
Can you make a class static in Java?
Can you run a Java program without main() method?
Is Java pass by value or pass by reference?
Why Java doesn't support multiple inheritances?
Why operator overloading is not supported in Java?
Java Basics: Learn to Code the Right Way

Thanks for reading this article so far, if you like this tutorial then please share with your friends and colleagues. If you have any questions or feedback then please drop a comment.

No comments:

Post a Comment