Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't an object of abstract class be created?

Tags:

Here is a scenario in my mind and I have googled, Binged it a lot but got the answer like

"Abstract class has not implemented method so, we cant create the object" "The word 'Abstract' instruct the compiler to not create an object of the class"

But in a simple class where we have all virtual method, able to create an object???

Also, we can define different access modified to Abstract class constructor like private, protected or public.

My search terminated to this question:

Why we can't create object of an Abstract class?

like image 920
Gaurav Arora Avatar asked Apr 23 '10 16:04

Gaurav Arora


People also ask

Why can't we create an object of an abstract class?

We cannot instantiate an abstract class in Java because it is abstract, it is not complete, hence it cannot be used.

Can there be object of abstract class?

Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be inherited from another class). Abstract method: can only be used in an abstract class, and it does not have a body. The body is provided by the subclass (inherited from).

Can we create object of abstract class yes or no?

No, we can't create an object of an abstract class. But we can create a reference variable of an abstract class. The reference variable is used to refer to the objects of derived classes (subclasses of abstract class).

Why can't we instantiate an abstract class in C++?

We can't instantiate an abstract class because the motive of abstract class is to provide a common definition of base class that multiple derived classes can share.


1 Answers

An abstract type is defined largely as one that can't be created. You can create subtypes of it, but not of that type itself. The CLI will not let you do this.

An abstract class has a protected constructor (by default) allowing derived types to initialize it.

For example, the base-type Stream is abstract. Without a derived type where would the data go? What would happen when you call an abstract method? There would be no actual implementation of the method to invoke.

like image 91
Marc Gravell Avatar answered Sep 19 '22 17:09

Marc Gravell