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

前端 未结 16 917
误落风尘
误落风尘 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:25

    every body is writing dat abstract class has some virtual function which has not defined. for dat reason we cant create object, but abstract class is a class with the key word 'abstract' which may or may not have abstract method. i think it is a concept, it does not take any memory for dat. so if we can create an object den a memory will be created which is not possible, for dat reason we can't create object of an abstract class bt we can create reference of it which does not occupy any memory location.

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

    If we have a class containing pure virtual function then the class is abstract. If we will create an object of the abstract class and calls the method having no body(as the method is pure virtual) it will give an error. That is why we cant create object of abstract class.

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

    Actually when we create an object of a normal class we use Constructor to allocate the memory for that object like

    myclass obj=new myclass();
    

    Here using constructorr clr identifies how much memory the object needed depending upon the instance variabless and methods. But in case of abstract classes we cant predict the amount of memory required as we dont implement the abstract methods so its not possible to create object.

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

    Simply speaking, an abstract class is like a shell of a class. Not all the methods have implementations, something like a circuit with some wire segments missing. While the majority of it may be constructed, it is up to the users to stick in the wires and resistors in those segments as they see fit.

    As to why Java won't let you create it, part of it is just a failsafe (many abstract classes will function just fine without any additions as long as you don't call unimplemented methods).

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

    Abstract classes should have at least one virtual method or property that has no implementation. This is marked with the abstract keyword. Inheriting classes must provide an implementation if they are not abstract themselves. You cannot create an instance of an abstract class because it does not have a complete implementation. If it does, it should not be marked abstract in the first place.

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

    It's intended to be used as a base class.

    http://msdn.microsoft.com/en-us/library/sf985hc5(VS.71).aspx

    The abstract modifier can be used with classes, methods, properties, indexers, and events.

    Use the abstract modifier in a class declaration to indicate that a class is intended only to be a base class of other classes.

    Abstract classes have the following features:

    • An abstract class cannot be instantiated.
    • An abstract class may contain abstract methods and accessors.
    • It is not possible to modify an abstract class with the sealed modifier, which means that the class cannot be inherited.
    • A non-abstract class derived from an abstract class must include actual implementations of all inherited abstract methods and accessors.
    0 讨论(0)
提交回复
热议问题