Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot create an instance of the abstract class or interface

I'm not familiar on using abstract class.

I'm trying to call a abstract class and get this error Cannot create an instance of the abstract class or interface and I already research this error but I'm really confused on this.

Here's my code:

        string B1String;
        while ((B1String = OasisFile.ReadLine()) != null)
        {
          Questions_Base oQuestions_Base = new Questions_Base(); // error here
          oQuestions_Base.Import(B1String);
        }

Please advice me.. thanks!

like image 850
ghie Avatar asked Jul 01 '26 19:07

ghie


1 Answers

The purpose of an abstract class it to serve as part of a class hierarchy where more-derived classes share some common implementation.

If you have a flight simulator, you might define an abstract class ThingsThatFly that implements some properties (air speed, altitude, heading) and methods (TakeOff(), Land()) that all flying things have in common, but would be declared abstract because ThingsThatFly is an abstraction of all concrete things that fly. You could certainly have classes inbetween as well, for example Cessna172 could inherit from Airplane that inherits from ThingsThatFly. You would do that if all airplanes have some common implementation that e.g. birds don't have (for example, a Fuel Remaining property).

You would then have a number of concrete (= real life) things that fly like a Cessna 172, a Space Shuttle and a Duck. Each of those would be a concrete class that derives from ThingsThatFly

This is different than having the concrete classes implement an interface such as IThingsThatFly in that the abstract class provides not only a definition of the properties and methods that are expected, but also provides a (hopefully useful) implementation of those properties and methods.

like image 109
Eric J. Avatar answered Jul 05 '26 07:07

Eric J.