Is it Possible to Create Object or Instance of an Abstract class in Java?

Hello guys, how are you doing? Hope you are doing well. It's been long since  I shared a core Java interview question in this blog, so let's start with that. 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 you can create an instance of an Abstract class in Java or not? This is another interesting core Java question that you will find on telephonic interviews, a written test that has multiple-choice questions and most notably Oracle certified Java programmer certification like OCAJP 8  and OCAJP 11.

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.




No, you cannot create an instance of an Abstract class in Java?

Let's see some code examples to prove this point that it's illegal to create an instance of an object of an abstract class in Java. This example throws a compile-time error to indicate that it's not possible. The error message is not very clear, but it does say that "Cannot Instantiate The Type Hello.Nested", which effectively says that you cannot 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(Hello.java:15)

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 to 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 courses 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 Core Java Articles and Resources you may like

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

P. S. - If you want to learn Java from scratch or want to prepare for Java certification, then I suggest you join a comprehensive Java course like The Complete Java Masterclass by Tim Buchalaka, one of my favorite courses to learn Java in depth.

2 comments:

  1. when I compile above code I am getting following error

    Error:(6, 20) java: non-static variable this cannot be referenced from a static context

    ReplyDelete
    Replies
    1. That's because Nested is not a static class, just put static keyword before Nested and it should work.

      Delete