Why can't an object of abstract class be created?

前端 未结 16 918
误落风尘
误落风尘 2020-12-02 21:25

Here is a scenario in my mind and I have googled, Binged it a lot but got the answer like

\"Abstract class has not implemented method so, we cant create the object\

相关标签:
16条回答
  • 2020-12-02 21:43

    we can create object for abstract class like this also...

    public class HelloWorld
    {
        public static void main(String args[])
        {
            Person p = new Person()
            { 
                void eat()
                {
                    console.writeline("sooper..");
                } 
            }; 
            p.eat();
        }
    }
    abstract class Person
    { 
        abstract void eat(); 
    } 
    
    0 讨论(0)
  • 2020-12-02 21:47

    An abstract type is defined largely as one that can't be created. You can create subtypes of it, but not of that type itself. The CLI will not let you do this.

    An abstract class has a protected constructor (by default) allowing derived types to initialize it.

    For example, the base-type Stream is abstract. Without a derived type where would the data go? What would happen when you call an abstract method? There would be no actual implementation of the method to invoke.

    0 讨论(0)
  • 2020-12-02 21:51

    When we create a pure virtual function in Abstract class, we reserve a slot for a function in the VTABLE(studied in last topic), but doesn't put any address in that slot. Hence the VTABLE will be incomplete.

    As the VTABLE for Abstract class is incomplete, hence the compiler will not let the creation of object for such class and will display an errror message whenever you try to do so.

    Source : Study Tonight

    0 讨论(0)
  • 2020-12-02 21:52

    Here is a similar StackOverflow question. In short, it is legal to have a public constructor on an abstract class. Some tools will warn you that this makes no sense.

    Whats the utility of public constructors in abstract classes in C#?

    0 讨论(0)
提交回复
热议问题