开发者

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;
        w开发者_开发知识库hile ((B1String = OasisFile.ReadLine()) != null)
        {
          Questions_Base oQuestions_Base = new Questions_Base(); // error here
          oQuestions_Base.Import(B1String);
        }

Please advice me.. thanks!


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.


An Abstract class can only be inherited.

public class CustomClass : Questions_Base {
}

Here's a link all about abstract classes and how to use them.


You cant create an instance of an abstract class.

You need to define a concrete class that inherits the abstract class, and create an instance of that.


  1. Abstract class is made to be overriden by Derived class. If you have to have Abstract class, first create s Derived class from it and use Derived class contructor.
  2. If it's not important, just remove abstract word from Questions_Base class declaration, so making that non abstract one. Also because in code provided I don't see any abstract member, so may this one is correct choice.

Regards.


An abstract class cannot be instantiated. You must provide an implementation for the class.

    abstract class Animal
    {
        public abstract void Speak() { }
    }

    public class Dog : Animal
    {
        public override void Speak()
        {
            Console.WriteLine("Woof");
        }
    }

See MSDN on abstract for more information


From the documentation: "The abstract keyword enables you to create classes and class members that are incomplete and must be implemented in a derived class."

The purpose of using abstract is exactly to prevent instantiation, because you only created the class to use as a base class and never want an instance created.

More here.


An abstract class is one which MUST be inherited.

It falls somewhere between an Interface, which defines only the interface that a class must implement and no implementation code and a class that you can create an instance of which defines both the interface and the implementation code. There are several abstract classes in the .NET framework such as CollectionBase. You cannot create an instance of CollectionBase, it is intended for you to create a class that inherits from it and extends it's capabilities.

You should simpley be able to remove the kwy work "abstract" from your class definition of Questions_Base or create a new class definition that inherits from it.


Abstract classes, marked by the keyword abstract in the class definition, are typically used to define a base class in the hierarchy. What's special about them, is that you can't create an instance of them - if you try, you will get a compile error. Instead, you have to subclass them, as taught in the chapter on inheritance, and create an instance of your subclass. So when do you need an abstract class? It really depends on what you do. To be honest, you can go a long way without needing an abstract class, but they are great for specific things, like frameworks, which is why you will find quite a bit of abstract classes within the .NET framework it self. A good rule of thumb is that the name actually makes really good sense - abstract classes are very often, if not always, used to describe something abstract, something that is more of a concept than a real thing.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜