Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Cannot create an instance of the abstract class or interface" C# error message

I changed my base class to abstract for a project and now I'm receiving the following error:

Cannot create an instance of the abstract class or interface

Am I receiving the error because naming a new instance of an abstract class is not allowed?

newPlane = new Airplane_Abstract(name, position);
like image 285
David Brewer Avatar asked Feb 09 '11 19:02

David Brewer


People also ask

Can we create instance of abstract class C?

No, you cannot create an instance of an abstract class because it does not have a complete implementation. The purpose of an abstract class is to function as a base for subclasses. It acts like a template, or an empty or partially empty structure, you should extend it and build on it before you can use it.

Why we can not create an object of abstract class or interface?

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

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.

Can you create instance of abstract class?

Abstract classes cannot be instantiated, but they can be subclassed. When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class.


3 Answers

You can't create an instance of an abstract class. Think of it as an interface that may contain some implementation logic as well. What would you expect to happen if you called an abstract instance method with no definition?

like image 194
Ed S. Avatar answered Sep 29 '22 16:09

Ed S.


I saw your earlier question, in which you asked about the meaning of "pure virtual" (abstract in C#).

The reason you cannot instantiate an abstract class is that it presumably has abstract members, with no implementation. So say your class looks like this:

abstract class Airplane_Abstract
{
    public abstract int GetSomeInteger();
}

Then assuming you could instantiate one of these, you could write code like this:

var airplane = new Airplane_Abstract();

// What would this be?
int integer = airplane.GetSomeInteger();

Granted, I don't believe you actually have to have abstract members in an abstract class. But the general idea of an abstract class is that it's one that cannot exist on its own; it must be further defined in a class which inherits from it. Abstract members are the most obvious illustration of why this would be; there could be other reasons.

Think of Shape, for example. This is a pretty common example of something that would make sense as an abstract class. You can't really instantiate just a Shape, can you? ("Create a shape." "What kind of shape?" "No kind. Just an abstract shape." Doesn't really work, does it?)

like image 44
Dan Tao Avatar answered Sep 29 '22 17:09

Dan Tao


You cannot create an instance of an abstract class. That's what abstract means.

like image 41
John Saunders Avatar answered Sep 29 '22 18:09

John Saunders