Hello Java Programmers, how are you doing? Hope you are doing well. It's been a 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 it 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.
When you compile this class using javac on the command line or just type the code in Eclipse IDE, you will see the following compile-time error:
You can see that even though you can declare a constructor inside an abstract class, as soon as you try to instantiate an abstract class, the compiler is throwing a "Cannot instantiate the type Hello.Nested" error.
This is true for both top-level and nested abstract classes 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 whether an abstract class can have a constructor or not and if it's allowed then why do you need a constructor for an abstract class that is incomplete.
The real use of a constructor in an abstract class is to initialize the fields which belong to the abstract class itself but the 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 these best Java Programming Courses to learn Java in a comprehensive and structured manner.
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 a constructor inside the abstract class.
This kind of question is very popular during a telephonic round of interviews 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.
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 the 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 a constructor inside an abstract class, as soon as you try to instantiate an abstract class, the compiler is throwing a "Cannot instantiate the type Hello.Nested" error.
This is true for both top-level and nested abstract classes 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 whether an abstract class can have a constructor or not and if it's allowed then why do you need a constructor for an abstract class that is incomplete.
The real use of a constructor in an abstract class is to initialize the fields which belong to the abstract class itself but the 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 these best Java Programming Courses to learn Java in a comprehensive and structured manner.
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 a constructor inside the abstract class.
This kind of question is very popular during a telephonic round of interviews 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
- The Java Developer RoadMap
- Can you make a class static in Java?
- Why String is Immutable in Java?
- 10 Free Courses to learn Java for Beginners
- Can you run a Java program without the 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?
- How to Crack Oracle's Java Certification?
- Java Basics: Learn to Code the Right Way
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.
when I compile above code I am getting following error
ReplyDeleteError:(6, 20) java: non-static variable this cannot be referenced from a static context
That's because Nested is not a static class, just put static keyword before Nested and it should work.
Deleteit will not allowed
ReplyDelete