简体   繁体   中英

Abstract Class vs Class

abstract class ClassStupid
{
    public ClassStupid()
    {

    }
}

ClassStupid stupid = new ClassStupid(); //This is not possible. We can not create an Instance of Abstract Class.

My question is what is the MAGIC in the C# compiler that prevents the program to create an instance of abstract class inspite of having a constructor.

From my Wiki

In object-oriented programming, a constructor (sometimes shortened to ctor) in a class is a special type of subroutine called at the creation of an object. It prepares the new object for use, often accepting parameters which the constructor uses to set any member variables required when the object is first created. It is called a constructor because it constructs the values of data members of the class.

How is the abstract class constructor different from Normal class Constructor?

The constructors of abstract and non-abstract classes are not different; the classes, however, are different, and the compiler knows about that. This is the reason the construction of abstract classes the way you show in the post is prohibited: the compiler simply checks the IsAbstract flag, and disallows the construction at compile time.

Moreover, CLR also has a runtime flag indicating that a class is abstract. That is why you wouldn't be able to instantiate an abstract class at runtime through reflection .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM