9

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);
HappyCoding
  • 641
  • 16
  • 36
David Brewer
  • 1,864
  • 8
  • 25
  • 37
  • 1
    framework guidelines framework guidelines framework guidelines framework guidelines –  Feb 09 '11 at 19:05
  • 2
    When you go back and read a bit about abstract classes, you will smack your forehead and go "duh". http://msdn.microsoft.com/en-us/library/sf985hc5(v=vs.71).aspx (first bullet point) – Joel Etherton Feb 09 '11 at 19:05
  • 5
    @Will: Are you referring to the `_` in the type name? I ask because I'd consider myself pretty knowledgeable in .NET by now (and I *think* that's your meaning), but I'm not even 100% sure what that comment means. So I doubt its meaning would be very clear to someone who's fairly new at this. – Dan Tao Feb 09 '11 at 19:11

3 Answers3

18

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?

Ed S.
  • 122,712
  • 22
  • 185
  • 265
  • Of course you _can_ create an instance of a concrete class that inherits the `abstract` class, and such an instance can also be said to be an instance of the abstract class. – Jeppe Stig Nielsen Dec 23 '16 at 18:00
  • @JeppeStigNielsen: An abstract class is a class that declares abstract methods. You cannot create an instance of an abstract class (the compiler will tell you so!) of course you *can* create instances of non-abstract sub-classes, but that's not the question (which was asked nearly six years ago...) What's your point? – Ed S. Dec 28 '16 at 17:03
  • It is just a terminology comment. Many people will call an instance of the sub-class an instance the base class as well. See as an example [this answer in another thread by Eric Lippert](http://stackoverflow.com/a/5602284/1336654). Your answer is of course entirely correct, I just wanted to say there is this other terminology which is common. – Jeppe Stig Nielsen Dec 28 '16 at 21:55
14

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?)

Dan Tao
  • 125,917
  • 54
  • 300
  • 447
6

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

John Saunders
  • 160,644
  • 26
  • 247
  • 397