Saturday, February 5, 2011

Is It Possible To Practise Object Or Instance Of An Abstract Course Of Education Inwards Java?

Hello guys, how are you lot doing? Hope you lot are doing well. It's been long since  I shared a heart Java interview query inwards this blog, so let's start alongside that. Earlier I accept shared i of the oft asked questions inwards Java, can nosotros brand an abstract course of didactics lastly inwards Java as well as my readers genuinely liked as well as asked for to a greater extent than such questions. So, today I am going to beak most whether you lot tin ship away create an instance of an Abstract course of didactics inwards Java or not? This is to a greater extent than or less other interesting heart Java query that you lot volition discovery on telephonic interviews, a written essay out that has multiple-choice questions as well as most notably Oracle certified Java programmer certification similar OCAJP 8  as well as OCAJP 11.

The respond to this query is simple, No, you lot cannot instantiate an abstract course of didactics inwards Java because it is abstract, it is non consummate thus it cannot live used. 

When you lot create an instance of a class, its's constructor is called, as well as fifty-fifty though abstract course of didactics tin ship away accept a constructor, the compiler volition non allow you lot to create an instance of the class. It's a compile-time mistake to create an instance of an abstract course of didactics inwards Java.




No, you lot cannot create an instance of an Abstract course of didactics inwards Java?

Let's come across to a greater extent than or less code examples to show this dot that it's illegal to create an instance of an object of an abstract course of didactics inwards Java. This instance throws a compile-time mistake to signal that it's non possible. The mistake message is non real clear, but it does nation that "Cannot Instantiate The Type Hello.Nested", which effectively says that you lot cannot create an instance of an abstract course of didactics inwards Java.

package tool;  /*  * Java Program to show that you lot cannnot  * create instance of an abstract course of didactics  * inwards Java. fifty-fifty though you lot tin ship away 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 lot compile this course of didactics using javac on the dominance work or only type the code inwards Eclipse IDE, you lot volition come across next compile-time error:

Exception inwards thread "main" java.lang.Error: Unresolved compilation problem: 
At tool.Hello.main(Hello.java:15)

You tin ship away come across that fifty-fifty though you lot tin ship away declare constructor within an abstract class, equally shortly equally you lot travail to instantiate an abstract class, the compiler is throwing "Cannot instantiate the type Hello.Nested" error.

This is truthful for both top-level as well as nested abstract course of didactics inwards Java, you lot cannot extend whatever of them.

This is genuinely the follow-up of to a greater extent than or less other interesting heart Java question, can an abstract course of didactics accept a constructor inwards Java. In the lastly post, I explained to you lot most whether an abstract course of didactics tin ship away accept constructor or not, as well as if it's allowed as well as so why practice you lot demand a constructor for an abstract course of didactics which is incomplete.

The existent usage of constructor inwards an abstract course of didactics is to initialize the fields which belong to the abstract course of didactics itself but constructor must live invoked from a concrete subclass equally role of creating an instance of a concrete subclass inwards Java.

Btw, if you lot are only starting alongside Java, as well as so you lot should starting fourth dimension banking enterprise gibe out the Complete Java Masterclass on Udemy, i of the improve courses to start alongside Java.

 I shared a heart Java interview query inwards this weblog Is it Possible to Create Object or Instance of an Abstract course of didactics inwards Java?


That's all most whether you lot tin ship away instantiate an abstract course of didactics inwards Java or not. As I said, Java specification doesn't allow you lot to create an object of an abstract class, it's a compile-time error, fifty-fifty though you lot tin ship away accept constructor within the abstract class.

This variety of query is real pop during a telephonic circular of interview equally good equally Java certification exams similar OCAJP8 as well as OCPJP8, which often gives you lot code based upon this concept as well as diverse choices similar what volition come about when you lot run this program.

You volition discovery many such questions on pop mock essay out simulators similar Whizlabs or David Mayer's Java 8 Exam Simulator.


Other Core Java Articles as well as Resources you lot may like

Thanks for reading this article so far; if you lot similar this tutorial, as well as so delight part it alongside your friends as well as colleagues. If you lot accept whatever questions or feedback, as well as so delight drib a comment.

P. S. - If you lot desire to larn Java from scratch or desire to ready for Java certification, as well as so I advise you lot bring together a comprehensive Java course of didactics like The Complete Java Masterclass by Tim Buchalaka, i of my favorite courses to larn Java inwards depth.

No comments:

Post a Comment